作家工具
这个笔记本提供了快速入门 Writer 工具的概览。要详细了解所有 Writer 功能和配置,请参阅 Writer 文档。
概览
集成细节
| Class | 包 | 本地 | 序列化 | JS支持 | Package downloads | Package 最新版本 |
|---|---|---|---|---|---|---|
| GraphTool | langchain-writer | ❌ | ❌ | ❌ |
特性
我们为与ChatWriter结合使用提供了两种类型的工具:function和graph。
Function
函数是最常见的工具类型,它允许LLM调用外部API、从数据库中获取数据,并执行您希望的任何外部操作。请访问我们的工具调用文档以获得更多信息。
图形
The Graph 工具是一种基于图的检索增强生成(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 Tools
要绑定图工具,首先使用您想要作为源的 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])
Instantiation
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
Invocation
该模型在调用时会自动选择工具,所有模式(流式/非流式、同步/异步)均适用。
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)
在函数工具的情况下,您将会收到一个包含工具调用请求的助手消息。
print(response.tool_calls)
然后您可以手动处理工具调用请求,发送给模型并接收最终响应:<br>
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"])
The content 属性包含最终响应:
print(response.content)
链式调用
由于Writer Graph工具的特定性(你无需手动调用它,Writer服务器会自行调用并返回基于RAG的生成结果),因此无法单独调用该工具,所以GraphTool不能作为链式的一部分使用
API 参考
对于所有GraphTool功能和配置的详细文档,请参阅API参考。