跳到主要内容

ChatWriter

本笔记本提供了一个快速概述,帮助您开始使用 Writer 聊天模型

Writer 有几个聊天模型。您可以在 Writer 文档中找到关于其最新模型及其成本、上下文窗口和支持的输入类型的信息。

:::

概述

集成细节

本地可序列化JS 支持包下载包最新版本
ChatWriterlangchain-community

模型功能

工具调用结构化输出JSON 模式图像输入音频输入视频输入令牌级流式传输原生异步令牌使用Logprobs

设置

要访问 Writer 模型,您需要创建一个 Writer 帐户,获取 API 密钥,并安装 writer-sdklangchain-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

[notice] A new release of pip is available: 24.2 -> 24.3.1
[notice] To update, run: pip install --upgrade pip
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 参考: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 参考: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 参考


此页是否对您有帮助?