聊天大纲
这将帮助您开始使用 Outline 聊天模型。有关所有 ChatOutline 功能和配置的详细文档,请前往 API 参考。
Outline 是一个用于生成约束语言的库。它允许您将大型语言模型 (LLM) 与各种后端一起使用,同时对生成的输出应用约束。
概述
集成详细信息
| 类 | 包 | 本地化 | 序列 化 | JS 支持 | 软件包下载 | 最新包装 |
|---|---|---|---|---|---|---|
| ChatOutlines | langchain-community | ✅ | ❌ | ❌ |
模型特点
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | 令牌级流式处理 | 本机异步 | Token 使用情况 | 日志 |
|---|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
设置
要访问 Outline 模型,您需要有互联网连接才能从 huggingface 下载模型权重。根据后端,您需要安装所需的依赖项(参见 概述 docs)
凭据
Outlines 没有内置的身份验证机制。
安装
LangChain Outline 集成位于langchain-community包中,需要outlines库:
%pip install -qU langchain-community outlines
实例
现在我们可以实例化我们的 Model 对象并生成聊天补全:
from langchain_community.chat_models.outlines import ChatOutlines
# For llamacpp backend
model = ChatOutlines(model="TheBloke/phi-2-GGUF/phi-2.Q4_K_M.gguf", backend="llamacpp")
# For vllm backend (not available on Mac)
model = ChatOutlines(model="meta-llama/Llama-3.2-1B", backend="vllm")
# For mlxlm backend (only available on Mac)
model = ChatOutlines(model="mistralai/Ministral-8B-Instruct-2410", backend="mlxlm")
# For huggingface transformers backend
model = ChatOutlines(model="microsoft/phi-2") # defaults to transformers backend
API 参考:ChatOutline
调用
from langchain_core.messages import HumanMessage
messages = [HumanMessage(content="What will the capital of mars be called?")]
response = model.invoke(messages)
response.content
API 参考:HumanMessage
流
ChatOutline 支持令牌流:
messages = [HumanMessage(content="Count to 10 in French:")]
for chunk in model.stream(messages):
print(chunk.content, end="", flush=True)
链接
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)
chain = prompt | model
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
API 参考:ChatPromptTemplate
约束生成
ChatOutlines 允许您对生成的输出应用各种约束:
Regex 约束
model.regex = r"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)"
response = model.invoke("What is the IP address of Google's DNS server?")
response.content
类型约束
model.type_constraints = int
response = model.invoke("What is the answer to life, the universe, and everything?")
response.content
Pydantic 和 JSON 架构
from pydantic import BaseModel
class Person(BaseModel):
name: str
model.json_schema = Person
response = model.invoke("Who are the main contributors to LangChain?")
person = Person.model_validate_json(response.content)
person
上下文自由语法
model.grammar = """
?start: expression
?expression: term (("+" | "-") term)*
?term: factor (("*" | "/") factor)*
?factor: NUMBER | "-" factor | "(" expression ")"
%import common.NUMBER
%import common.WS
%ignore WS
"""
response = model.invoke("Give me a complex arithmetic expression:")
response.content
LangChain 的结构化输出
你也可以将 LangChain 的结构化输出与 ChatOutlines 一起使用:
from pydantic import BaseModel
class AnswerWithJustification(BaseModel):
answer: str
justification: str
_model = model.with_structured_output(AnswerWithJustification)
result = _model.invoke("What weighs more, a pound of bricks or a pound of feathers?")
result
API 参考
有关所有 ChatOutline 功能和配置的详细文档,请访问 API 参考:https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.outlines.ChatOutlines.html
完整大纲文档:
https://dottxt-ai.github.io/outlines/latest/