如何将运行时配置添加到您的图中¶
有时,您希望在调用代理时能够对其进行配置。例如,配置要使用的LLM。 下面我们将通过一个示例来说明如何做到这一点。
设置¶
首先,让我们安装所需的包并设置我们的API密钥
import getpass
import os
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("ANTHROPIC_API_KEY")
为LangGraph开发设置LangSmith
注册LangSmith,可以快速发现并解决您的LangGraph项目中的问题,提高性能。LangSmith允许您使用跟踪数据来调试、测试和监控使用LangGraph构建的LLM应用程序——更多关于如何开始的信息,请参阅这里。
定义图¶
首先,让我们创建一个非常简单的图
import operator
from typing import Annotated, Sequence
from typing_extensions import TypedDict
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.graph import END, StateGraph, START
model = ChatAnthropic(model_name="claude-2.1")
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], operator.add]
def _call_model(state):
state["messages"]
response = model.invoke(state["messages"])
return {"messages": [response]}
# Define a new graph
builder = StateGraph(AgentState)
builder.add_node("model", _call_model)
builder.add_edge(START, "model")
builder.add_edge("model", END)
graph = builder.compile()
API Reference: ChatAnthropic | BaseMessage | HumanMessage | END | StateGraph | START
配置图表¶
很好!现在假设我们希望扩展这个示例,让用户可以从多个LLM中进行选择。
我们可以通过传递配置来轻松实现这一点。任何配置信息都需要在configurable
键中传递,如下所示。
此配置应包含不属于输入(因此我们不希望将其作为状态的一部分进行跟踪)的内容。
from langchain_openai import ChatOpenAI
from typing import Optional
from langchain_core.runnables.config import RunnableConfig
openai_model = ChatOpenAI()
models = {
"anthropic": model,
"openai": openai_model,
}
def _call_model(state: AgentState, config: RunnableConfig):
# Access the config through the configurable key
model_name = config["configurable"].get("model", "anthropic")
model = models[model_name]
response = model.invoke(state["messages"])
return {"messages": [response]}
# Define a new graph
builder = StateGraph(AgentState)
builder.add_node("model", _call_model)
builder.add_edge(START, "model")
builder.add_edge("model", END)
graph = builder.compile()
API Reference: ChatOpenAI | RunnableConfig
如果我们不进行任何配置就调用它,它将使用我们定义的默认设置(Anthropic)。
{'messages': [HumanMessage(content='hi', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello!', additional_kwargs={}, response_metadata={'id': 'msg_01WFXkfgK8AvSckLvYYrHshi', 'model': 'claude-2.1', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 10, 'output_tokens': 6}}, id='run-ece54b16-f8fc-4201-8405-b97122edf8d8-0', usage_metadata={'input_tokens': 10, 'output_tokens': 6, 'total_tokens': 16})]}
我们也可以通过配置来调用它,使其使用不同的模型。
config = {"configurable": {"model": "openai"}}
graph.invoke({"messages": [HumanMessage(content="hi")]}, config=config)
{'messages': [HumanMessage(content='hi', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 8, 'total_tokens': 17, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-f8331964-d811-4b44-afb8-56c30ade7c15-0', usage_metadata={'input_tokens': 8, 'output_tokens': 9, 'total_tokens': 17})]}
我们也可以调整图形,以接受更多的配置!例如系统消息。
from langchain_core.messages import SystemMessage
# We can define a config schema to specify the configuration options for the graph
# A config schema is useful for indicating which fields are available in the configurable dict inside the config
class ConfigSchema(TypedDict):
model: Optional[str]
system_message: Optional[str]
def _call_model(state: AgentState, config: RunnableConfig):
# Access the config through the configurable key
model_name = config["configurable"].get("model", "anthropic")
model = models[model_name]
messages = state["messages"]
if "system_message" in config["configurable"]:
messages = [
SystemMessage(content=config["configurable"]["system_message"])
] + messages
response = model.invoke(messages)
return {"messages": [response]}
# Define a new graph - note that we pass in the configuration schema here, but it is not necessary
workflow = StateGraph(AgentState, ConfigSchema)
workflow.add_node("model", _call_model)
workflow.add_edge(START, "model")
workflow.add_edge("model", END)
graph = workflow.compile()
API Reference: SystemMessage
{'messages': [HumanMessage(content='hi', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello!', additional_kwargs={}, response_metadata={'id': 'msg_01VgCANVHr14PsHJSXyKkLVh', 'model': 'claude-2.1', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 10, 'output_tokens': 6}}, id='run-f8c5f18c-be58-4e44-9a4e-d43692d7eed1-0', usage_metadata={'input_tokens': 10, 'output_tokens': 6, 'total_tokens': 16})]}
config = {"configurable": {"system_message": "respond in italian"}}
graph.invoke({"messages": [HumanMessage(content="hi")]}, config=config)
{'messages': [HumanMessage(content='hi', additional_kwargs={}, response_metadata={}),
AIMessage(content='Ciao!', additional_kwargs={}, response_metadata={'id': 'msg_011YuCYQk1Rzc8PEhVCpQGr6', 'model': 'claude-2.1', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 14, 'output_tokens': 7}}, id='run-a583341e-5868-4e8c-a536-881338f21252-0', usage_metadata={'input_tokens': 14, 'output_tokens': 7, 'total_tokens': 21})]}