本笔记本介绍如何开始使用 Robocorp Action Server 操作工具包和 LangChain。
Robocorp 是通过自定义操作扩展 AI 代理、助手和副驾驶功能的最简单方式。
安装
首先,参阅 Robocorp 快速入门 了解如何设置 Action Server 和创建您的操作。
在您的 LangChain 应用程序中,安装 langchain-robocorp package:
# Install package
pip install -qU langchain-robocorp
当您创建新的 Action Server 按照上述快速入门操作后。
它将创建一个包含文件的目录,包括 action.py.
我们可以将 Python 函数添加为操作,如下所示 Robocorp 操作文档.
让我们添加一个虚拟函数到 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 并使用界面来运行函数。
环境设置
您可以选择设置以下环境变量:
- -
LANGSMITH_TRACING=true:要启用 LangSmith 日志运行追踪,该追踪也可以绑定到相应的操作服务器操作运行日志。参见 LangSmith 文档 了解更多。
使用方法
我们启动了上面的本地操作服务器,运行在 http://localhost:8080.
from langchain.agents import AgentExecutor, OpenAIFunctionsAgent
from langchain.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?")
> Entering new AgentExecutor chain...
Invoking: `robocorp_action_server_get_weather_forecast` with `{'city': 'San Francisco', 'days': 1, 'scale': 'fahrenheit'}`
"75F and sunny :)"The current weather today in San Francisco is 75F and sunny.
> Finished chain.
{'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() 会将操作作为结构化工具返回。
要返回单输入工具,请传递一个用于处理输入的聊天模型。
# Initialize single input Action Server Toolkit
toolkit = ActionServerToolkit(url="http://localhost:8080")
tools = toolkit.get_tools(llm=llm)