Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

Writer Tools

This notebook provides a quick overview for getting started with Writertools. For detailed documentation of all Writer features and configurations head to theWriter docs.

Overview

Integration details

ClassPackageLocalSerializableJS supportPackage downloadsPackage latest
GraphToollangchain-writerPyPI - DownloadsPyPI - Version

Features

We provide usage of two types of tools for use withChatWriter:function andgraph.

Function

Functions are the most common type of tool, which allows the LLM to call external APIs, fetch data from databases, and generally perform any external action you want to do. Visit ourtool calling docs for additional information.

Graph

TheGraph tool is Writer's graph-based retrieval-augmented generation (RAG) called Knowledge Graph. This tool enables developers to simply pass the graph ID to the model and it will return the answer to the question in the prompt. To learn more, see ourKnowledge Graph API docs.

Setup

Sign up forWriter AI Studio to generate an API key (you can follow thisQuickstart). Then, set the WRITER_API_KEY environment variable:

import getpass
import os

ifnot os.getenv("WRITER_API_KEY"):
os.environ["WRITER_API_KEY"]= getpass.getpass("Enter your Writer API key: ")

Usage

You can bind graph or function tools toChatWriter.

Graph Tools

To bind graph tools, first create and initialize aGraphTool instance with thegraph_ids you want to use as sources:

from langchain_writer.chat_modelsimport ChatWriter
from langchain_writer.toolsimport GraphTool

chat= ChatWriter()

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

Instantiation

from typingimport Optional

from langchain_core.toolsimport tool
from pydanticimport BaseModel, Field


@tool
defget_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":
return15
elif club_name=="Real Madrid":
return13
elif club_name=="Atletico Madrid":
return2
else:
returnNone


classGetWeather(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 Reference:tool

Binding tools

Then, you can simply bind all tools to theChatWriter instance:

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

All tools are stored in thetools attribute of theChatWriter instance:

chat.tools

The tool choice mode is stored at thetool_choice attribute, which isauto by default:

chat.tool_choice

Invocation

The model will automatically choose the tool during invocation with all modes (streaming/non-streaming, sync/async).

from langchain_core.messagesimport 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 Reference:HumanMessage

In the case of function tools, you will receive an assistant message with the tool call request.

print(response.tool_calls)

Then you can manually handle tool call request, send to model and receive final response:

for tool_callin 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)

With aGraphTool, the model will call it remotely and return usage info in theadditional_kwargs under thegraph_data key:

print(response.additional_kwargs["graph_data"])

Thecontent attribute contains the final response:

print(response.content)

Chaining

Due to specificity of Writer Graph tool (you don't need to call it manually, Writer server will call it by himself and return RAG based generation) it's impossible to invoke it separately, so GraphTool can't be used as part of chain

API reference

For detailed documentation of allGraphTool features and configurations, head to theAPI reference.

Related


[8]ページ先頭

©2009-2025 Movatter.jp