
如何快速實現REST API集成以優化業務流程
當我們在下一章中深入研究這個框架及其代碼時,我們會發現確保任務由相關代理并按定義的順序處理非常容易。你肯定不會在CrewAI中看到智能體之間的任何生動互動,比如一個智能體糾正另一個智能體,一個智能體的多次講話。這些交互有利于實驗或演示,但對需要高效、確定性和成本效益高的任務完成的真實LLM產品用處不大。因此,CrewAI優先考慮精簡和可靠的方法,在一個強大的群聊中,每個人工智能代理都準確地知道該做什么以及他們的目標。
? ? ? ?在我看來,另一個也是最關鍵的優勢是它蓬勃發展的工具和支持豐富的資源,可以用來構建代理和任務,這源于它是基于LangChain設計的智能體。LangChain是一個成熟的LLM框架,已經為LLM應用程序開發人員提供了豐富的工具和外圍設備來增強語言模型的功能。
CrewAI被證明適合熟悉LangChain的LLM應用程序開發人員,或者已經在其上構建應用程序的開發人員。對他們來說,將現有的單獨代理集成到CrewAI框架中可以相對容易地實現。相比之下,AutoGen的學習曲線可能更陡峭,需要更多的時間來了解其用法并有效地集成代理。
現在讓我們深入了解如何在代碼中實踐這些優勢。
? ? ? 為了證明Agent順序任務的易用性,我將使用之前AutoGen演示相同的任務,可以參考之前的博文LLM之Agent(九)| ?通過API集成賦能Autogen Multi-Agent系統,該演示要求人工智能代理根據天氣條件和所附的保險項目列表,生成一個包含適當活動的旅行計劃。
構建一套群聊人工智能代理,需要以下角色:
? ? ? ?為了詳細說明,溝通應按順序操作,如天氣記者->活動代理人->旅行顧問->保險代理人。
下面看一下如何實現:
a)步驟1-首先安裝CrewAI軟件包
pip install crewai
b)步驟2-導入包,并進行相關設置
? ? ? 由于底層實現依賴于LangChain庫,因此除了CrewAI之外,我們還必須導入相關的LangChain包
import os
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4-1106-preview")
c)步驟3-構建代理
? ? ? ?實施過程主要是為每個代理商設置系統提示。CrewAI將系統提示(可能還有代理描述)分為多個部分。看看天氣預報員的代碼:
Weather_reporter = Agent(
role='Weather_reporter',
goal="""provide historical weather
overall status based on the dates and location user provided.""",
backstory="""You are a weather reporter who provides weather
overall status based on the dates and location user provided.
You are using historical data from your own experience. Make your response short.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
通常情況下,你應該填寫role、goal和backstory來構建一個代理。這三個部分的名稱很容易理解,其中role指的是代理的名稱,goal指的是創建該代理的原因,以及代理能力的backstory。allow_delegation是為將任務傳遞給下一個代理(如果該代理無法處理)時的情況定義的。
? ? ? ?按照相同的方法,讓我們構造其余三個代理。
from langchain.agents import load_tools
human_tools = load_tools(["human"])
activity_agent = Agent(
role='activity_agent',
goal="""responsible for activities
recommendation considering the weather situation from weather_reporter.""",
backstory="""You are an activity agent who recommends
activities considering the weather situation from weather_reporter.
Don't ask questions. Make your response short.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
travel_advisor = Agent(
role='travel_advisor',
goal="""responsible for making a travel plan by consolidating
the activities and require human input for approval.""",
backstory="""After activities recommendation generated
by activity_agent, You generate a concise travel plan
by consolidating the activities.""",
verbose=True,
allow_delegation=False,
tools=human_tools,
llm=llm,
)
Insure_agent = Agent(
role='Insure_agent',
goal="""responsible for listing the travel plan from advisor and giving the short
insurance items based on the travel plan""",
backstory="""You are an Insure agent who gives
the short insurance items based on the travel plan.
Don't ask questions. Make your response short.""",
verbose=True,
allow_delegation=False,
llm=llm,
)
人機交互是多智能體應用程序治理的基本組成部分,以確保人工智能代理在適當的監督下發言。與需要開發人員構建用戶代理以結合人類交互的AutoGen框架不同,集成LangChain的CrewAI提供了一種簡化的方法,通過將名為“human”的工具加載到tools參數中,然后在代理travel_advisor的定義中加入tools=human_tools即可無縫地集成人工輸入。我們接下來應該做的是將這個人工提示寫入我們將在下一步中介紹的Task對象的描述中。
d)步驟4-構建任務
? ? ? ?在CrewAI中,沒有針對整個組的“整體”任務,而是應該通過Task()方法為每個代理分配單獨的任務。
task_weather = Task(
description="""Provide weather
overall status in Bohol Island in Sept.""",
agent=Weather_reporter
)
task_activity = Task(
description="""Make an activity list
recommendation considering the weather situation""",
agent=activity_agent
)
task_insure = Task(
description="""1. Copy and list the travel plan from task_advisor. 2. giving the short
insurance items based on the travel plan considering its activities type and intensity.""",
agent=Insure_agent
)
task_advisor = Task(
description="""Make a travel plan which includes all the recommended activities, and weather,
Make sure to check with the human if the draft is good
before returning your Final Answer.
.""",
agent=travel_advisor
)
必須使用agent=…為每個任務顯式分配一個代理…。
如果你更喜歡評論旅行計劃人際互動,你可以試著附加這樣的文字“Make sure to check with the human if the draft is good before returning your final answer”。
e)步驟5-組建團隊并開始
? ? ? ?現在,是時候用編排策略將他們組成一支有能力的團隊了。
crew = Crew(
agents=[Weather_reporter, activity_agent, travel_advisor, Insure_agent, ],
tasks=[task_weather, task_activity, task_advisor, task_insure, ],
verbose=2
)
result = crew.kickoff()
在目前唯一的選擇順序策略中,會嚴格按照agents列表和tasks列表中的順序執行。根據我的測試,你必須確保兩個順序都是一致的,但我認為設計需要改進,以保持tasks列表是任務執行順序的唯一參考。將verbose設置為2將使系統打印[Info]和[Debug]信息。
當一切就緒時,只需調用kickoff()函數就可以開始群聊生成。
? ? ? ?從正在進行的打印中,您將看到LangChain的ReAct流程為每個任務提供的熟悉輸出。
最后,最后的答案顯示了預期的旅行計劃:
? ? ? ?當我們通過AutoGen框架開發AI群聊時,使用OpenAI的函數調用功能來調用外部API或自定義函數以擴展代理的知識是非常方便的。不幸的是,函數調用僅適用于GPT模型,很少有經過微調的開源模型。通過使用LangChain框架,該工具界面自然支持在現實世界中與CrewAI代理交互,并可用于所有兼容的模型。盡管工具的可靠性低于函數調用,但當工具的函數不需要復雜的輸入參數時,它非常適用于開源模型。
讓我們看看如何在我們的旅行計劃應用程序中使用它。
a.預置工具
? ? ? ?首先,我們希望Activity_agent提供來自互聯網搜索的旅行活動,而不是自行生產。像大多數LangChain示例一樣,我們使用DuckDuckGo作為搜索工具,該工具已內置到LangChain庫中了。
安裝DuckDuckGo軟件包:
pip install duckduckgo_search
? ? ? ?定義搜索工具:
from langchain_community.tools import DuckDuckGoSearchRun
search_tool = DuckDuckGoSearchRun()
?將search_tool插入activity_agent。
activity_agent = Agent(
role='activity_agent',
goal="""responsible for actitivies
recommendation considering the weather situation from weather_reporter.""",
backstory="""You are an activity agent who recommends
activities considering the weather situation from weather_reporter.
Don't ask questions. Make your response short.""",
verbose=True,
allow_delegation=False,
tools=[search_tool],
llm=llm,
)
? ?在最后一步中,不要忘記通知代理使用Task定義中的最新數據。
task2 = Task(
description="""Make a research for suitable and up-to-date activities
recommendation considering the weather situation""",
agent=activity_agent
)
現有的各種工具可以從LangChain集成中選擇。
b.自定義工具
? ? ?有時用戶會調用自定義函數或API,也可以通過LangChain的裝飾器@tool或StructuredTool方法創建自定義工具。
? ? ? 假設我們希望weather_reporter能夠通過自定義API搜索在線天氣數據。讓我們快速模擬一個。
from langchain.tools import BaseTool, StructuredTool, tool
from langchain.pydantic_v1 import BaseModel, Field
class WeatherInput(BaseModel):
search_string: str = Field(description="the search string for the weather status")
def get_weather(search_string:str) -> str:
"""Look up the weather status"""
return "It's raining season with typhoons."
weather_search = StructuredTool.from_function(
func=get_weather,
name="weather_search",
description="search for the weather status",
args_schema=WeatherInput,
return_direct=True,
)
??現在創建了新工具weather_search,用于接受查詢字符串以返回虛擬天氣狀態“It’s raining season with typhoons”。然后,我們更新代理以配備此工具:
Weather_reporter = Agent(
role='Weather_reporter',
goal="""providing weather
overall status based on the dates and location the user provided.""",
backstory="""You are a weather reporter who provides weather
overall status based on the dates and location the user provided.
Make your response short.""",
verbose=True,
allow_delegation=False,
tools=[weather_search],
llm=llm,
)
并更新任務:
task1 = Task(
description="""providing weather
overall status in Bohol Island in September.""",
agent=Weather_reporter
)
?在應用程序重新運行后,根據結果,weather_reporter和activity_agent都將開始使用該工具來支持它們的響應生成(藍色句子)。
? ? ? ?由于該框架與OpenAI API的推理結構沒有緊密綁定,因此在CrewAI中使用開源模型的局限性比AutoGen小得多。一個快速的方法是通過安裝Ollama來部署一個本地模型。
步驟1-安裝Ollama
按照Ollama官方[2]頁面上的說明將軟件包安裝到您的本地機器上,并確保您有足夠的本地計算資源來運行模型。
步驟2——創建LLM實例
? ? ? ?要為代理創建Ollama模型的推理,只需要使用LangChain中的Ollama()方法定義一個新的llm。
from langchain.llms import Ollama
llm_ollama = Ollama(model="YOUR_MODEL_NAME")
支持的型號列表可以參考:[3]
? ? ? ?然后,將llm_ollama提供給代理,例如:
Insure_agent = Agent(
role='Insure_agent',
goal="""responsible for listing the travel plan from advisor and giving the short
insurance items based on the travel plan""",
backstory="""You are an Insure agent who gives
the short insurance items based on the travel plan.
Don't ask questions. Make your response short.""",
verbose=True,
allow_delegation=False,
llm=llm_ollama,
)
現在,Insure_agent將通過本地語言模型生成文本。
? ? ? ?CrewAI在提供LangChain引入的可擴展功能方面具有明顯優勢,包括工具集成和對開源大型語言模型的支持。它的順序編排能力有利于多智能體應用程序的生產。盡管有這些優勢,但缺乏某些功能可能會阻礙其廣泛采用——與OpenAI的助手和更復雜的編排相關的功能明顯缺乏。CrewAI團隊必須盡快解決這些差距并部署增強功能,以滿足LLM應用程序開發的需求。
[1] https://levelup.gitconnected.com/for-a-multi-agent-framework-crewai-has-its-advantages-compared-to-autogen-a1df3ff66ed3
[2] https://ollama.ai/
[3]?https://ollama.ai/library
文章轉自微信公眾號@ArronAI