ChatWriter
本笔记本提供了快速入门使用Writer 聊天模型的概述。
Writer 有几种聊天模型。您可以在 Writer 文档 中找到有关其最新模型及其成本、上下文窗口和支持的输入类型的信息。
:::
概述
集成详情
类 | 包 | 本地 | 可序列化 | JS支持 | 包下载 | 包最新 |
---|---|---|---|---|---|---|
ChatWriter | langchain-community | ❌ | ❌ | ❌ | ❌ | ❌ |
模型特性
工具调用 | 结构化输出 | JSON模式 | 图像输入 | 音频输入 | 视频输入 | 令牌级流式处理 | 原生异步 | 令牌使用情况 | Logprobs |
---|---|---|---|---|---|---|---|---|---|
✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |
设置
要访问Writer模型,您需要创建一个Writer账户,获取一个API密钥,并安装writer-sdk
和langchain-community
包。
凭证
前往Writer AI Studio注册OpenAI并生成API密钥。完成此操作后,设置WRITER_API_KEY环境变量:
import getpass
import os
if not os.environ.get("WRITER_API_KEY"):
os.environ["WRITER_API_KEY"] = getpass.getpass("Enter your Writer API key:")
安装
LangChain Writer 集成位于 langchain-community
包中:
%pip install -qU langchain-community writer-sdk
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m24.2[0m[39;49m -> [0m[32;49m24.3.1[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
Note: you may need to restart the kernel to use updated packages.
实例化
现在我们可以实例化我们的模型对象并生成聊天完成:
from langchain_community.chat_models.writer import ChatWriter
llm = ChatWriter(
model="palmyra-x-004",
temperature=0.7,
max_tokens=1000,
# other params...
)
API Reference:ChatWriter
调用
messages = [
(
"system",
"You are a helpful assistant that writes poems about the Python programming language.",
),
("human", "Write a poem about Python."),
]
ai_msg = llm.invoke(messages)
print(ai_msg.content)
In realms of code, where logic weaves and flows,
A language rises, Python by its name,
With syntax clear, where elegance it shows,
A serpent, wise, that time and space can tame.
Born from the mind of Guido, pure and bright,
Its beauty lies in simplicity and grace,
A tool of power, yet gentle in its might,
In every programmer's heart, a cherished place.
It dances through the data, vast and deep,
With libraries that span the digital realm,
From machine learning's secrets to keep,
To web development, it wields the helm.
In the hands of the novice and the sage,
Python spins the threads of digital dreams,
A language that can turn the age,
With a gentle learning curve, its appeal gleams.
It's more than code, a community it builds,
Where knowledge freely flows, and all are heard,
In Python's world, the future unfolds,
A language of the people, for the world.
So here's to Python, in its gentle might,
A master of the modern coding art,
May it continue to light our path each night,
In the vast, evolving world of code, its heart.
流处理
ai_stream = llm.stream(messages)
for chunk in ai_stream:
print(chunk.content, end="")
In realms of code where logic weaves,
A language rises, Python, it breezes,
With syntax clear and simple to read,
Through its elegance, our spirits are fed.
Like rivers flowing, smooth and serene,
Its structure harmonious, a coder's dream,
Indentations guide the flow of control,
In Python's world, confusion takes no toll.
A vast library, a treasure trove so bright,
For web and data, it offers its might,
With modules and packages, a rich array,
Python empowers us to code in play.
From AI to scripts, in flexibility it thrives,
A language of the future, as many now derive,
Its community, a beacon of support and cheer,
With Python, the possibilities are vast, far and near.
So here's to Python, in its gentle grace,
A tool that enhances, a language that embraces,
The art of coding, with a fluent, flowing pen,
In the Python world, we code, and we begin.
链式调用
我们可以链式我们的模型与一个提示模板,如下所示:
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that writes poems about the {input_language} programming language.",
),
("human", "{input}"),
]
)
chain = prompt | llm
chain.invoke(
{
"input_language": "Java",
"input": "Write a poem about Java.",
}
)
API Reference:ChatPromptTemplate
AIMessageChunk(content='In the realm of code, where logic weaves and flows, \nA language rises, like a phoenix from the code\'s throes. \nJava, the name, a cup of coffee\'s steam, \nBrewed in the minds, where digital dreams gleam.\n\nWith syntax clear, like morning\'s misty hue, \nIn classes and objects, it spins a tale so true. \nA platform agnostic, with a byte to spare, \nAcross the devices, it journeys everywhere.\n\nInheritance and polymorphism, its power\'s core, \nLike ancient runes, in every line they bore. \nEncapsulation, a shield, with data it does hide, \nIn the vast jungle of code, it stands as a guide.\n\nFrom applets small, to vast, server-side apps, \nIts threads run swift, through the computing traps. \nA language of the people, by the people, for the people’s use, \nBuilt on the principle, "write once, run anywhere, with no excuse."\n\nIn the heart of Android, it beats, a steady drum, \nCrafting experiences, in every smartphone\'s hum. \nIn the cloud, in the enterprise, its presence is vast, \nA cornerstone of computing, built to last.\n\nOh Java, thy elegance, thy robust design, \nA language that stands, in any computing line. \nWith every update, with every new release, \nThy community grows, with a vibrant, diverse peace.\n\nSo here\'s to Java, the versatile, the grand, \nA language that shapes the digital land. \nMay it continue to evolve, to grow, to inspire, \nIn the endless quest of turning thoughts into digital fire.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 345, 'prompt_tokens': 33, 'total_tokens': 378, 'completion_tokens_details': None, 'prompt_token_details': None}, 'model_name': 'palmyra-x-004', 'system_fingerprint': 'v1', 'finish_reason': 'stop'}, id='run-a5b4be59-0eb0-41bd-80f7-72477861b0bd-0')
工具调用
Writer 支持 工具调用,这允许您描述工具及其参数,并让模型返回一个包含要调用的工具及其输入的 JSON 对象。
ChatWriter.bind_tools()
使用ChatWriter.bind_tools
,我们可以轻松地将Pydantic类、字典模式、LangChain工具甚至函数作为工具传递给模型。在底层,这些被转换为工具模式,看起来像:
{
"name": "...",
"description": "...",
"parameters": {...} # JSONSchema
}
并在每次模型调用中传递。
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_with_tools = llm.bind_tools([GetWeather])
ai_msg = llm_with_tools.invoke(
"what is the weather like in New York City",
)
AIMessage.tool_calls
请注意,AIMessage 有一个 tool_calls
属性。它以标准化的 ToolCall 格式包含内容,该格式与模型提供者无关。
print(ai_msg.tool_calls)
[{'name': 'GetWeather',
'args': {'location': 'New York City, NY'},
'id': 'chatcmpl-tool-fe70912c800d40fc8700d604d4823001',
'type': 'tool_call'}]
有关绑定工具和工具调用输出的更多信息,请前往工具调用文档。
API参考
有关所有Writer功能的详细文档,请访问我们的API参考。