跳到主要內容
Open In ColabOpen on GitHub

Outlines

這將幫助您開始使用 Outlines LLM。如需所有 Outlines 功能和組態的詳細文件,請前往 API 參考

Outlines 是一個用於約束語言生成的函式庫。它允許您將大型語言模型 (LLM) 與各種後端搭配使用,同時將約束應用於產生的輸出。

概觀

整合詳細資訊

類別套件本地可序列化JS 支援套件下載套件最新版
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 參考: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 參考:PromptTemplate

串流

Outlines 支援 token 串流

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

約束生成

Outlines 允許您將各種約束應用於產生的輸出

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

類型約束

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

JSON Schema

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://langchain-python.dev.org.tw/api_reference/community/chat_models/langchain_community.chat_models.outlines.ChatOutlines.html

Outlines 文件:

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


此頁面是否有幫助?