Writer 工具
此笔记本提供了 Writer 工具入门的快速概述。有关所有 Writer 功能和配置的详细文档,请前往 Writer 文档。
概述
集成详细信息
| 类 | 包 | 本地化 | 序列 化 | JS 支持 | 软件包下载 | 最新包装 |
|---|---|---|---|---|---|---|
| GraphTool | langchain-writer | ❌ | ❌ | ❌ |
特征
我们提供两种类型的工具,用于ChatWriter:function和graph.
功能
函数是最常见的工具类型,它允许 LLM 调用外部 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.
图形工具
要绑定图形工具,首先创建并初始化一个GraphTool实例替换为graph_ids你想用作源:
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"],
},
},
}
装订工具
然后,您只需将所有工具绑定到ChatWriter实例:
chat.bind_tools(
[graph_tool, get_supercopa_trophies_count, GetWeather, get_product_info]
)
所有工具都存储在tools属性的ChatWriter实例:
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)
对于函数工具,您将收到一条带有工具调用请求的 assistant 消息。
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,模型将远程调用它,并在additional_kwargs在graph_data钥匙:
print(response.additional_kwargs["graph_data"])
这contentattribute 包含最终响应:
print(response.content)
链接
由于 Writer Graph 工具的特殊性(不需要手动调用,Writer 服务器会自己调用并返回基于 RAG 的生成),因此无法单独调用,因此 GraphTool 不能作为链的一部分使用
API 参考
有关所有GraphTool功能和配置,请前往 API 参考。