跳至主要內容

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 執行該函數。

環境設定

您可以選擇設定以下環境變數

  • LANGCHAIN_TRACING_V2=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?")


> 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="https://127.0.0.1:8080")
tools = toolkit.get_tools(llm=llm)

此頁面是否有幫助?