接口
为了尽可能简化创建自定义链的过程,我们实现了一个名为"Runnable"的协议,大多数组件都实现了该协议。这是一个标准接口,包含了一些不同的方法,可以方便地定义自定义链,并以标准方式调用它们。暴露的标准接口包括:
stream
:流式返回响应的块invoke
:调用输入上的链batch
:调用输入列表上的链
这些方法也有对应的异步方法:
astream
:异步流式返回响应的块ainvoke
:异步调用输入上的链abatch
:异步调用输入列表上的链
输入类型因组件而异:
组件 | 输入类型 |
---|---|
Prompt | 字典 |
Retriever | 单个字符串 |
Model | 单个字符串、聊天消息列表或 PromptValue |
输出类型也因组件而异:
组件 | 输出类型 |
---|---|
LLM | 字符串 |
ChatModel | ChatMessage |
Prompt | PromptValue |
Retriever | 文档列表 |
让我们来看看这些方法!为此,我们将创建一个非常简单的PromptTemplate + ChatModel链。
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
#### API 参考:
* [ChatPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain.prompts.chat.ChatPromptTemplate.html) 来自 `langchain.prompts`
* [ChatOpenAI](https://api.python.langchain.com/en/latest/chat_models/langchain.chat_models.openai.ChatOpenAI.html) 来自 `langchain.chat_models`
model = ChatOpenAI()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
chain = prompt | model
Stream
for s in chain.stream({"topic": "bears"}):
print(s.content, end="", flush=True)
Sure, here's a bear-themed joke for you:
Why don't bears wear shoes?
Because they have bear feet!
Invoke
chain.invoke({"topic": "bears"})
AIMessage(content="Why don't bears wear shoes?\n\nBecause they already have bear feet!", additional_kwargs={}, example=False)
Batch
chain.batch([{"topic": "bears"}, {"topic": "cats"}])
[AIMessage(content="Why don't bears ever wear shoes?\n\nBecause they have bear feet!", additional_kwargs={}, example=False),
AIMessage(content="Why don't cats play poker in the wild?\n\nToo many cheetahs!", additional_kwargs={}, example=False)]
Async Stream
async for s in chain.astream({"topic": "bears"}):
print(s.content, end="", flush=True)
Why don't bears wear shoes?
Because they have bear feet!
Async Invoke
await chain.ainvoke({"topic": "bears"})
AIMessage(content="Sure, here you go:\n\nWhy don't bears wear shoes?\n\nBecause they have bear feet!", additional_kwargs={}, example=False)
Async Batch
await chain.abatch([{"topic": "bears"}])
[AIMessage(content="Why don't bears wear shoes?\n\nBecause they have bear feet!", additional_kwargs={}, example=False)]