Robocorp 工具組
本筆記本涵蓋如何開始使用 Robocorp Action Server 行動工具組和 LangChain。
Robocorp 是擴展 AI 代理、助理和副駕駛功能的最佳方式,透過自訂行動。
安裝
首先,請參閱 Robocorp 快速入門,了解如何設定 Action Server
並建立您的行動。
在您的 LangChain 應用程式中,安裝 langchain-robocorp
套件
# Install package
%pip install --upgrade --quiet langchain-robocorp
當您按照上述快速入門建立新的 Action Server
時。
它將建立一個包含檔案的目錄,包括 action.py
。
我們可以新增 python 函數作為行動,如 此處 所示。
讓我們新增一個虛擬函數到 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
透過前往在 https://127.0.0.1:8080
執行的伺服器進行本地測試,並使用 UI 執行函數。
環境設定
您可以選擇性地設定以下環境變數
LANGSMITH_TRACING=true
:啟用 LangSmith 日誌執行追蹤,該追蹤也可以綁定到各自的 Action Server 行動執行日誌。請參閱 LangSmith 文件 以了解更多資訊。
使用方式
我們啟動了本地行動伺服器,如上所示,在 https://127.0.0.1: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="https://127.0.0.1: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()
將會以結構化工具的形式傳回行動。
若要傳回單一輸入工具,請傳遞一個聊天模型,用於處理輸入。
# Initialize single input Action Server Toolkit
toolkit = ActionServerToolkit(url="https://127.0.0.1:8080")
tools = toolkit.get_tools(llm=llm)