Outlines
這將幫助您開始使用 Outlines LLM。 有關所有 Outlines 功能和配置的詳細文檔,請前往API 參考資料。
Outlines 是一個用於約束語言生成的程式庫。 它允許您使用具有各種後端的 大型語言模型 (LLM),同時對生成的輸出應用約束。
概述
整合詳細資訊
類別 | 套件 | 本機 | 可序列化 | JS 支援 | 套件下載 | 套件最新版本 |
---|---|---|---|---|---|---|
Outlines | langchain-community | ✅ | beta | ❌ |
設定
要存取 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 支援權杖串流
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/