Robocorp 工具包
此笔记本介绍了如何开始使用 Robocorp Action Server作工具包和 LangChain。
Robocorp 是通过自定义作扩展 AI 代理、助手和副驾驶功能的最简单方法。
安装
首先,请参阅 Robocorp 快速入门,了解如何设置Action Server并创建您的 Action。
在 LangChain 应用程序中,安装langchain-robocorp包:
# Install package
%pip install --upgrade --quiet langchain-robocorp
当您创建新的Action Server遵循上述快速入门。
它将创建一个包含文件的目录,包括action.py.
我们可以将 python 函数添加为 actions,如下所示。
让我们添加一个虚拟函数action.py.
@action
def get_weather_forecast(city: str, days: int, scale: str = "celsius") -> str:
"""
Returns weather conditions forecast for a given city.
Args:
city (str): Target city to get the weather conditions for
days: How many day forecast to return
scale (str): Temperature scale to use, should be one of "celsius" or "fahrenheit"
Returns:
str: The requested weather conditions forecast
"""
return "75F and sunny :)"
然后我们启动服务器:
action-server start
我们可以看到:
Found new action: get_weather_forecast
通过转到运行在http://localhost:8080并使用 UI 运行函数。
环境设置
(可选)您可以设置以下环境变量:
LANGSMITH_TRACING=true:启用 LangSmith 日志运行跟踪,该跟踪也可以绑定到相应的 Action Server作运行日志。有关更多信息,请参阅 LangSmith 文档。
用法
我们启动了本地 action server(如上所示),运行在http://localhost:8080.
from langchain.agents import AgentExecutor, OpenAIFunctionsAgent
from langchain_core.messages import SystemMessage
from langchain_openai import ChatOpenAI
from langchain_robocorp import ActionServerToolkit
# Initialize LLM chat model
llm = ChatOpenAI(model="gpt-4", temperature=0)
# Initialize Action Server Toolkit
toolkit = ActionServerToolkit(url="http://localhost:8080", report_trace=True)
tools = toolkit.get_tools()
# Initialize Agent
system_message = SystemMessage(content="You are a helpful assistant")
prompt = OpenAIFunctionsAgent.create_prompt(system_message)
agent = OpenAIFunctionsAgent(llm=llm, prompt=prompt, tools=tools)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
executor.invoke("What is the current weather today in San Francisco in fahrenheit?")
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m
Invoking: `robocorp_action_server_get_weather_forecast` with `{'city': 'San Francisco', 'days': 1, 'scale': 'fahrenheit'}`
[0m[33;1m[1;3m"75F and sunny :)"[0m[32;1m[1;3mThe current weather today in San Francisco is 75F and sunny.[0m
[1m> Finished chain.[0m
{'input': 'What is the current weather today in San Francisco in fahrenheit?',
'output': 'The current weather today in San Francisco is 75F and sunny.'}
单一输入工具
默认情况下toolkit.get_tools()将作作为结构化工具返回。
要返回单个输入工具,请传递用于处理输入的 Chat 模型。
# Initialize single input Action Server Toolkit
toolkit = ActionServerToolkit(url="http://localhost:8080")
tools = toolkit.get_tools(llm=llm)