Connery 工具套件與工具
使用 Connery 工具套件與工具,您可以將 Connery 動作整合到您的 LangChain 代理程式中。
什麼是 Connery?
Connery 是適用於 AI 的開放原始碼外掛程式基礎架構。
透過 Connery,您可以輕鬆建立具有一組動作的自訂外掛程式,並將其無縫整合到您的 LangChain 代理程式中。Connery 將處理諸如執行階段、授權、密碼管理、存取管理、稽核日誌和其他重要功能等關鍵方面。
此外,在我們社群的支援下,Connery 提供了各種現成的開放原始碼外掛程式集合,以增加便利性。
深入瞭解 Connery
設定
安裝
您需要安裝 langchain_community
套件才能使用 Connery 工具。
%pip install -qU langchain-community
憑證
若要在您的 LangChain 代理程式中使用 Connery 動作,您需要進行一些準備
- 依照快速入門指南設定 Connery 執行器。
- 安裝所有具有您想要在代理程式中使用的動作的外掛程式。
- 設定環境變數
CONNERY_RUNNER_URL
和CONNERY_RUNNER_API_KEY
,以便工具套件可以與 Connery 執行器通訊。
import getpass
import os
for key in ["CONNERY_RUNNER_URL", "CONNERY_RUNNER_API_KEY"]:
if key not in os.environ:
os.environ[key] = getpass.getpass(f"Please enter the value for {key}: ")
工具套件
在以下範例中,我們建立一個代理程式,該代理程式使用兩個 Connery 動作來摘要公共網頁,並透過電子郵件傳送摘要
您可以在此處查看此範例的 LangSmith 追蹤。
import os
from langchain.agents import AgentType, initialize_agent
from langchain_community.agent_toolkits.connery import ConneryToolkit
from langchain_community.tools.connery import ConneryService
from langchain_openai import ChatOpenAI
# Specify your Connery Runner credentials.
os.environ["CONNERY_RUNNER_URL"] = ""
os.environ["CONNERY_RUNNER_API_KEY"] = ""
# Specify OpenAI API key.
os.environ["OPENAI_API_KEY"] = ""
# Specify your email address to receive the email with the summary from example below.
recepient_email = "test@example.com"
# Create a Connery Toolkit with all the available actions from the Connery Runner.
connery_service = ConneryService()
connery_toolkit = ConneryToolkit.create_instance(connery_service)
# Use OpenAI Functions agent to execute the prompt using actions from the Connery Toolkit.
llm = ChatOpenAI(temperature=0)
agent = initialize_agent(
connery_toolkit.get_tools(), llm, AgentType.OPENAI_FUNCTIONS, verbose=True
)
result = agent.run(
f"""Make a short summary of the webpage http://www.paulgraham.com/vb.html in three sentences
and send it to {recepient_email}. Include the link to the webpage into the body of the email."""
)
print(result)
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m
Invoking: `CA72DFB0AB4DF6C830B43E14B0782F70` with `{'publicWebpageUrl': 'http://www.paulgraham.com/vb.html'}`
[0m[33;1m[1;3m{'summary': 'The author reflects on the concept of life being short and how having children made them realize the true brevity of life. They discuss how time can be converted into discrete quantities and how limited certain experiences are. The author emphasizes the importance of prioritizing and eliminating unnecessary things in life, as well as actively pursuing meaningful experiences. They also discuss the negative impact of getting caught up in online arguments and the need to be aware of how time is being spent. The author suggests pruning unnecessary activities, not waiting to do things that matter, and savoring the time one has.'}[0m[32;1m[1;3m
Invoking: `CABC80BB79C15067CA983495324AE709` with `{'recipient': 'test@example.com', 'subject': 'Summary of the webpage', 'body': 'Here is a short summary of the webpage http://www.paulgraham.com/vb.html:\n\nThe author reflects on the concept of life being short and how having children made them realize the true brevity of life. They discuss how time can be converted into discrete quantities and how limited certain experiences are. The author emphasizes the importance of prioritizing and eliminating unnecessary things in life, as well as actively pursuing meaningful experiences. They also discuss the negative impact of getting caught up in online arguments and the need to be aware of how time is being spent. The author suggests pruning unnecessary activities, not waiting to do things that matter, and savoring the time one has.\n\nYou can find the full webpage [here](http://www.paulgraham.com/vb.html).'}`
[0m[33;1m[1;3m{'messageId': '<2f04b00e-122d-c7de-c91e-e78e0c3276d6@gmail.com>'}[0m[32;1m[1;3mI have sent the email with the summary of the webpage to test@example.com. Please check your inbox.[0m
[1m> Finished chain.[0m
I have sent the email with the summary of the webpage to test@example.com. Please check your inbox.
注意:Connery 動作是結構化工具,因此您只能在支援結構化工具的代理程式中使用它。
工具
import os
from langchain.agents import AgentType, initialize_agent
from langchain_community.tools.connery import ConneryService
from langchain_openai import ChatOpenAI
# Specify your Connery Runner credentials.
os.environ["CONNERY_RUNNER_URL"] = ""
os.environ["CONNERY_RUNNER_API_KEY"] = ""
# Specify OpenAI API key.
os.environ["OPENAI_API_KEY"] = ""
# Specify your email address to receive the emails from examples below.
recepient_email = "test@example.com"
# Get the SendEmail action from the Connery Runner by ID.
connery_service = ConneryService()
send_email_action = connery_service.get_action("CABC80BB79C15067CA983495324AE709")
手動執行動作。
manual_run_result = send_email_action.run(
{
"recipient": recepient_email,
"subject": "Test email",
"body": "This is a test email sent from Connery.",
}
)
print(manual_run_result)
使用 OpenAI Functions 代理程式執行動作。
您可以在此處查看此範例的 LangSmith 追蹤。
llm = ChatOpenAI(temperature=0)
agent = initialize_agent(
[send_email_action], llm, AgentType.OPENAI_FUNCTIONS, verbose=True
)
agent_run_result = agent.run(
f"Send an email to the {recepient_email} and say that I will be late for the meeting."
)
print(agent_run_result)
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m
Invoking: `CABC80BB79C15067CA983495324AE709` with `{'recipient': 'test@example.com', 'subject': 'Late for Meeting', 'body': 'Dear Team,\n\nI wanted to inform you that I will be late for the meeting today. I apologize for any inconvenience caused. Please proceed with the meeting without me and I will join as soon as I can.\n\nBest regards,\n[Your Name]'}`
[0m[36;1m[1;3m{'messageId': '<d34a694d-50e0-3988-25da-e86b4c51d7a7@gmail.com>'}[0m[32;1m[1;3mI have sent an email to test@example.com informing them that you will be late for the meeting.[0m
[1m> Finished chain.[0m
I have sent an email to test@example.com informing them that you will be late for the meeting.
注意:Connery 動作是結構化工具,因此您只能在支援結構化工具的代理程式中使用它。
API 參考
如需所有 Connery 功能和組態的詳細文件,請前往 API 參考
- 工具套件:https://langchain-python.dev.org.tw/api_reference/community/agent_toolkits/langchain_community.agent_toolkits.connery.toolkit.ConneryToolkit.html
- 工具:https://langchain-python.dev.org.tw/api_reference/community/tools/langchain_community.tools.connery.service.ConneryService.html