跳到主要內容

ChatWriter

本筆記本提供快速入門 Writer 聊天模型的概觀。

Writer 有多種聊天模型。您可以在 Writer 文件中找到有關其最新模型以及其成本、上下文窗口和支援的輸入類型等資訊。

:::

概觀

整合詳細資訊

類別套件本地可序列化JS 支援套件下載套件最新版本
ChatWriterlangchain-community

模型功能

工具呼叫結構化輸出JSON 模式圖片輸入音訊輸入視訊輸入Token 層級串流原生非同步Token 使用量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 類別、dict 模式、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 參考


此頁面是否有幫助?