Outlines
Outlines 是一個用於約束語言生成的 Python 函式庫。它為各種語言模型提供統一的介面,並允許使用正規表示式匹配、類型約束、JSON 結構描述和上下文無關文法等技術進行結構化生成。
Outlines 支援多個後端,包括
- Hugging Face Transformers
- llama.cpp
- vLLM
- MLX
此整合讓您能夠將 Outlines 模型與 LangChain 搭配使用,同時提供 LLM 和聊天模型介面。
安裝與設定
若要將 Outlines 與 LangChain 搭配使用,您需要安裝 Outlines 函式庫
pip install outlines
根據您選擇的後端,您可能需要安裝額外的依賴項
- 對於 Transformers:
pip install transformers torch datasets
- 對於 llama.cpp:
pip install llama-cpp-python
- 對於 vLLM:
pip install vllm
- 對於 MLX:
pip install mlx
LLM
若要將 Outlines 作為 LangChain 中的 LLM 使用,您可以使用 Outlines
類別
from langchain_community.llms import Outlines
API 參考:Outlines
聊天模型
若要將 Outlines 作為 LangChain 中的聊天模型使用,您可以使用 ChatOutlines
類別
from langchain_community.chat_models import ChatOutlines
API 參考:ChatOutlines
模型配置
Outlines
和 ChatOutlines
類別都共用類似的配置選項
model = Outlines(
model="meta-llama/Llama-2-7b-chat-hf", # Model identifier
backend="transformers", # Backend to use (transformers, llamacpp, vllm, or mlxlm)
max_tokens=256, # Maximum number of tokens to generate
stop=["\n"], # Optional list of stop strings
streaming=True, # Whether to stream the output
# Additional parameters for structured generation:
regex=None,
type_constraints=None,
json_schema=None,
grammar=None,
# Additional model parameters:
model_kwargs={"temperature": 0.7}
)
模型識別符
model
參數可以是
- Hugging Face 模型名稱 (例如,"meta-llama/Llama-2-7b-chat-hf")
- 模型的本機路徑
- 對於 GGUF 模型,格式為 "repo_id/file_name" (例如,"TheBloke/Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_K_M.gguf")
後端選項
backend
參數指定要使用的後端
"transformers"
:用於 Hugging Face Transformers 模型 (預設)"llamacpp"
:用於使用 llama.cpp 的 GGUF 模型"transformers_vision"
:用於視覺語言模型 (例如,LLaVA)"vllm"
:用於使用 vLLM 函式庫的模型"mlxlm"
:用於使用 MLX 架構的模型
結構化生成
Outlines 提供多種結構化生成方法
-
正規表示式匹配:
model = Outlines(
model="meta-llama/Llama-2-7b-chat-hf",
regex=r"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)"
)這將確保生成的文字與指定的正規表示式模式 (在此案例中為有效的 IP 位址) 相符。
-
類型約束:
model = Outlines(
model="meta-llama/Llama-2-7b-chat-hf",
type_constraints=int
)這將輸出限制為有效的 Python 類型 (int、float、bool、datetime.date、datetime.time、datetime.datetime)。
-
JSON 結構描述:
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
model = Outlines(
model="meta-llama/Llama-2-7b-chat-hf",
json_schema=Person
)這確保生成的輸出符合指定的 JSON 結構描述或 Pydantic 模型。
-
上下文無關文法:
model = Outlines(
model="meta-llama/Llama-2-7b-chat-hf",
grammar="""
?start: expression
?expression: term (("+" | "-") term)*
?term: factor (("*" | "/") factor)*
?factor: NUMBER | "-" factor | "(" expression ")"
%import common.NUMBER
"""
)這會生成符合 EBNF 格式的指定上下文無關文法的文字。
使用範例
LLM 範例
from langchain_community.llms import Outlines
llm = Outlines(model="meta-llama/Llama-2-7b-chat-hf", max_tokens=100)
result = llm.invoke("Tell me a short story about a robot.")
print(result)
API 參考:Outlines
聊天模型範例
from langchain_community.chat_models import ChatOutlines
from langchain_core.messages import HumanMessage, SystemMessage
chat = ChatOutlines(model="meta-llama/Llama-2-7b-chat-hf", max_tokens=100)
messages = [
SystemMessage(content="You are a helpful AI assistant."),
HumanMessage(content="What's the capital of France?")
]
result = chat.invoke(messages)
print(result.content)
串流範例
from langchain_community.chat_models import ChatOutlines
from langchain_core.messages import HumanMessage
chat = ChatOutlines(model="meta-llama/Llama-2-7b-chat-hf", streaming=True)
for chunk in chat.stream("Tell me a joke about programming."):
print(chunk.content, end="", flush=True)
print()
API 參考:ChatOutlines | HumanMessage
結構化輸出範例
from langchain_community.llms import Outlines
from pydantic import BaseModel
class MovieReview(BaseModel):
title: str
rating: int
summary: str
llm = Outlines(
model="meta-llama/Llama-2-7b-chat-hf",
json_schema=MovieReview
)
result = llm.invoke("Write a short review for the movie 'Inception'.")
print(result)
API 參考:Outlines
其他功能
Tokenizer 存取
您可以存取模型底層的 tokenizer
tokenizer = llm.tokenizer
encoded = tokenizer.encode("Hello, world!")
decoded = tokenizer.decode(encoded)