聊天作家
这个笔记本提供了快速入门 Writer 聊天 的概览。
Writer 有多种聊天模型。您可以在Writer 文档中找到它们最新的模型及其成本、上下文窗口和支持的输入类型。
概览
集成细节
| Class | 包 | 本地 | 序列化 | JS支持 | Package downloads | Package 最新版本 |
|---|---|---|---|---|---|---|
| ChatWriter | langchain-writer | ❌ | ❌ | ❌ |
模型特性
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | Token级流式传输 | 原生异步 | Token 使用 | 对数概率 |
|---|---|---|---|---|---|---|---|---|---|
| ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |
Credentials
注册 Writer AI Studio 并按照 快速入门指南 获取 API 密钥。然后设置 WRITER_API_KEY 环境变量:
import getpass
import os
if not os.getenv("WRITER_API_KEY"):
os.environ["WRITER_API_KEY"] = getpass.getpass("Enter your Writer API key: ")
如果您想要实现模型调用的自动化跟踪,您也可以通过取消注释下方代码设置您的
LangSmith API密钥:
# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
安装
ChatWriter 是从 langchain-writer 包中可用的。使用以下命令安装它:
%pip install -qU langchain-writer
Instantiation
现在我们可以通过实例化模型对象来生成聊天完成内容:
from langchain_writer import ChatWriter
llm = ChatWriter(
model="palmyra-x-004",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
)
用法
要使用该模型,请传递一个消息列表并调用invoke方法:
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
然后,您可以访问消息的内容:<br>
print(ai_msg.content)
流式传输
您可以流式传输响应。首先,创建一个流:<br>
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming. Sing a song about it"),
]
ai_stream = llm.stream(messages)
ai_stream
然后,遍历流以获取片段:
for chunk in ai_stream:
print(chunk.content, end="")
工具调用
Writer模型如Palmyra X 004支持工具调用,这让你能够描述工具及其参数。模型将会返回一个包含要调用的工具和该工具输入的JSON对象。
绑定工具
使用 ChatWriter.bind_tools,您可以轻松地将 Pydantic 类、字典模式、LangChain 工具或甚至函数作为工具传递给模型。在底层,这些内容会转换为工具模式,其外观如下所示:
{
"name": "...",
"description": "...",
"parameters": {...} # JSONSchema
}
这些在每次模型调用时都会传递。
例如,要使用一个获取给定位置天气的工具,你可以定义一个 Pydantic 类并将它传递给 ChatWriter.bind_tools:
from pydantic import BaseModel, Field
class GetWeather(BaseModel):
"""Get the current weather in a given location"""
location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
llm.bind_tools([GetWeather])
然后,您可以调用该工具来使用模型:<br>
ai_msg = llm.invoke(
"what is the weather like in New York City",
)
ai_msg
终于,您可以访问工具调用并继续执行您的函数:
print(ai_msg.tool_calls)
ChatWriter.bind_tools() 方法不会创建带有绑定工具的新实例,而是将接收到的 tools 和 tool_choice 存储在初始类实例属性中,以便在使用 ChatWriter 调用时作为参数传递给 Palmyra 大语言模型(LLM)调用。这种方法支持不同类型的工具,例如 function 和 graph。Graph 是远程调用的 Writer Palmyra 工具之一。欲了解更多信息,请访问我们的 文档。
有关 LangChain 工具使用的信息,请访问 LangChain 工具调用文档。
Batching
您可以批量发送请求并设置max_concurrency:
ai_batch = llm.batch(
[
"How to cook pancakes?",
"How to compose poem?",
"How to run faster?",
],
config={"max_concurrency": 3},
)
ai_batch
然后,遍历批次以获取结果:
for batch in ai_batch:
print(batch.content)
print("-" * 100)
异步使用
以上所有功能(调用、流式处理、批量处理、工具调用)也支持异步使用。
提示模板
提示模板帮助将用户输入和参数转换为对语言模型的指令。您可以使用ChatWriter与提示模板一起使用,例如:
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)
chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
API 参考
详细描述所有ChatWriter功能和配置的文档请参阅API参考。
附加资源
您可以查找有关Writer模型(包括成本、上下文窗口和支持的输入类型)以及工具的信息,在Writer文档中。