Skip to main content
Open In ColabOpen on GitHub

写作者工具

本笔记本提供了快速入门 Writer 工具 的概览。如需了解所有 Writer 功能和配置的详细文档,请访问 Writer 文档

概览

集成详情

本地可序列化的JS 支持软件包下载最新包裹
GraphToollangchain-writerPyPI - DownloadsPyPI - Version

特性

我们提供两种工具供ChatWriter使用:functiongraph

函数

函数是最常见的工具类型,它允许大语言模型调用外部 API、从数据库获取数据,并通常执行您想要的任何外部操作。请访问我们的工具调用文档以获取更多信息。

图形

Graph工具是 Writer 基于图的检索增强生成(RAG)功能,称为知识图谱。该工具使开发人员只需将图 ID 传递给模型,它便会返回提示中问题的答案。要了解更多信息,请参阅我们的知识图谱 API 文档

设置

注册 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: ")

使用

您可以将图或函数工具绑定到 ChatWriter

图工具

要绑定图工具,首先使用您想用作源的 graph_ids 创建并初始化一个 GraphTool 实例:

from langchain_writer.chat_models import ChatWriter
from langchain_writer.tools import GraphTool

chat = ChatWriter()

graph_id = getpass.getpass("Enter Writer Knowledge Graph ID: ")
graph_tool = GraphTool(graph_ids=[graph_id])

实例化

from typing import Optional

from langchain_core.tools import tool
from pydantic import BaseModel, Field


@tool
def get_supercopa_trophies_count(club_name: str) -> Optional[int]:
"""Returns information about supercopa trophies count.

Args:
club_name: Club you want to investigate info of supercopa trophies about

Returns:
Number of supercopa trophies or None if there is no info about requested club
"""

if club_name == "Barcelona":
return 15
elif club_name == "Real Madrid":
return 13
elif club_name == "Atletico Madrid":
return 2
else:
return None


class GetWeather(BaseModel):
"""Get the current weather in a given location"""

location: str = Field(..., description="The city and state, e.g. San Francisco, CA")


get_product_info = {
"type": "function",
"function": {
"name": "get_product_info",
"description": "Get information about a product by its id",
"parameters": {
"type": "object",
"properties": {
"product_id": {
"type": "number",
"description": "The unique identifier of the product to retrieve information for",
}
},
"required": ["product_id"],
},
},
}
API 参考:工具

绑定工具

然后,您可以简单地将所有工具绑定到 ChatWriter 实例:

chat.bind_tools(
[graph_tool, get_supercopa_trophies_count, GetWeather, get_product_info]
)

所有工具都存储在 toolsChatWriter 实例属性中:

chat.tools

工具选择模式存储在 tool_choice 属性中,默认为 auto

chat.tool_choice

调用

该模型在调用期间将自动选择工具,适用于所有模式(流式/非流式、同步/异步)。

from langchain_core.messages import HumanMessage

messages = [
HumanMessage(
"Use knowledge graph tool to compose this answer. Tell me what th first line of documents stored in your KG. Also I want to know: how many SuperCopa trophies have Barcelona won?"
)
]

response = chat.invoke(messages)
messages.append(response)
API 参考:HumanMessage

在使用函数工具时,您将收到一条包含工具调用请求的助手消息。

print(response.tool_calls)

然后,您可以手动处理工具调用请求,将其发送给模型并接收最终响应:

for tool_call in response.tool_calls:
selected_tool = {
"get_supercopa_trophies_count": get_supercopa_trophies_count,
}[tool_call["name"].lower()]
tool_msg = selected_tool.invoke(tool_call)
messages.append(tool_msg)

response = chat.invoke(messages)
print(response.content)

使用 GraphTool 时,模型将远程调用它,并在 graph_data 键下的 additional_kwargs 中返回使用情况信息:

print(response.additional_kwargs["graph_data"])

content 属性包含最终响应:

print(response.content)

链式调用

由于 Writer Graph 工具的特殊性(您无需手动调用它,Writer 服务器将自行调用并返回基于 RAG 的生成结果),因此无法单独调用该工具,所以 GraphTool 不能作为链的一部分使用。

API 参考

有关所有 GraphTool 功能和配置的详细文档,请参阅 API 参考