Skip to main content
Open In ColabOpen on GitHub

Outlines

这将帮助您开始使用 Outlines LLM。有关所有 Outlines 功能和配置的详细文档,请前往 API 参考

Outlines 是一个用于受限语言生成的库。它允许您使用各种后端的语言模型 (LLMs),同时对生成的输出应用约束。

概述

集成详情

ClassPackageLocalSerializableJS supportPackage downloadsPackage latest
Outlineslangchain-communitybetaPyPI - DownloadsPyPI - Version

设置

要访问 Outlines 模型,您需要有互联网连接以下载模型权重(从 huggingface 下载)。根据您需要的后端安装相应的依赖项(请参阅 Outlines 文档

凭据

Outlines 没有内置的认证机制。

安装

LangChain 的 Outlines 集成位于 langchain-community 包中,需要 outlines 库:

%pip install -qU langchain-community outlines

实例化

现在我们可以实例化我们的模型对象并生成聊天补全:

from langchain_community.llms import Outlines

# For use with llamacpp backend
model = Outlines(model="microsoft/Phi-3-mini-4k-instruct", backend="llamacpp")

# For use with vllm backend (not available on Mac)
model = Outlines(model="microsoft/Phi-3-mini-4k-instruct", backend="vllm")

# For use with mlxlm backend (only available on Mac)
model = Outlines(model="microsoft/Phi-3-mini-4k-instruct", backend="mlxlm")

# For use with huggingface transformers backend
model = Outlines(
model="microsoft/Phi-3-mini-4k-instruct"
) # defaults to backend="transformers"
API Reference:Outlines

调用

model.invoke("Hello how are you?")

链式调用

from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("How to say {input} in {output_language}:\n")

chain = prompt | model
chain.invoke(
{
"output_language": "German",
"input": "I love programming.",
}
)
API Reference:PromptTemplate

流式传输

Outlines 支持令牌的流式传输:

for chunk in model.stream("Count to 10 in French:"):
print(chunk, end="", flush=True)

受限生成

Outlines 允许您对生成的输出应用各种约束:

正则表达式约束

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

类型约束

model.type_constraints = int
response = model.invoke("What is the answer to life, the universe, and everything?")

JSON 模式

from pydantic import BaseModel


class Person(BaseModel):
name: str


model.json_schema = Person
response = model.invoke("Who is the author of LangChain?")
person = Person.model_validate_json(response)

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

API 参考

如需了解 ChatOutlines 所有功能和配置的详细文档,请前往 API 参考:https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.outlines.ChatOutlines.html

Outlines 文档:

https://dottxt-ai.github.io/outlines/latest/