太長不看版
開源 LLM 現(xiàn)已達(dá)到一定的性能水平,可堪作為智能體工作流的推理引擎。在我們的測試基準(zhǔn)上,Mixtral 甚至已超越 GPT-3.5,而且我們還可以通過微調(diào)輕松地進(jìn)一步提高其性能米者。
引言
經(jīng)由因果語言建模任務(wù)訓(xùn)練出的大語言模型(LLM)可以處理很多任務(wù),但在邏輯宇智、計(jì)算及搜索等類型的任務(wù)上表現(xiàn)不盡人意塘雳。最糟糕的是陆盘,它們在數(shù)學(xué)等領(lǐng)域表現(xiàn)不佳而不自知,仍不自量力地想僅憑一己之力完成所有計(jì)算败明。
為了克服這一弱點(diǎn),方法之一就是將 LLM 集成到一個含有若干可調(diào)用工具的系統(tǒng)中太防,我們稱這樣的系統(tǒng)為 LLM 智能體(agent)妻顶。
本文,我們首先解釋了 ReAct 智能體的內(nèi)在工作原理蜒车,然后展示了如何使用最近集成到 LangChain 中的 ChatHuggingFace
接口來構(gòu)建自己的智能體讳嘱。最后,我們把幾個開源 LLM 與 GPT-3.5 和 GPT-4 一起在同一基準(zhǔn)測試上進(jìn)行了比較酿愧。
目錄
什么是智能體嬉挡?
LLM 智能體的定義相當(dāng)寬泛:LLM 智能體是所有使用 LLM 作為引擎并基于觀察對環(huán)境采取相應(yīng)行動的系統(tǒng)钝鸽。其使用 感知?反思?行動
的多輪迭代來完成任務(wù),也經(jīng)常通過規(guī)劃或知識管理系統(tǒng)來增強(qiáng)性能庞钢。如對該領(lǐng)域的全景感興趣拔恰,可參考 Xi et al., 2023 這篇論文。
本文重點(diǎn)關(guān)注 ReAct 智能體基括。ReAct 用“推理”和“行動”這兩個詞串聯(lián)起智能體的工作流颜懊。我們通過提示告訴模型可以使用哪些工具,并要求它“一步一步”(即思維鏈)思考并行動风皿,直至獲得最終答案河爹。
ReAct 智能體內(nèi)在機(jī)制示例
上圖看上去很高端,但實(shí)現(xiàn)起來其實(shí)非常簡單桐款。
可以參考一下這個 notebook咸这,這里,我們用 transformers
庫實(shí)現(xiàn)了一個簡單的工具調(diào)用示例鲁僚。
我們用下述提示模板循環(huán)調(diào)用 LLM:
Here is a question: "{question}"
You have access to these tools: {tools_descriptions}.
You should first reflect with ‘Thought: {your_thoughts}’, then you either:
- call a tool with the proper JSON formatting,
- or your print your final answer starting with the prefix ‘Final Answer:’
等 LLM 輸出回答后炊苫,就用如下方式解析其回答:
- 如果回答中包含字符串
‘Final Answer:’
,則結(jié)束循環(huán)并打印答案冰沙。 - 否則侨艾,LLM 會輸出一個工具調(diào)用。你可以解析此輸出以獲取工具名及參數(shù)拓挥,并使用所述參數(shù)調(diào)用所述工具唠梨。然后,將此次工具調(diào)用的輸出附加到提示中侥啤,并把擴(kuò)展后的提示輸入給 LLM当叭,直到它有足夠的信息生成最終答案茬故。
舉個例子,當(dāng)回答問題 How many seconds are in 1:23:45?
時蚁鳖,LLM 的輸出可能如下所示:
Thought: I need to convert the time string into seconds.
Action:
{
"action": "convert_time",
"action_input": {
"time": "1:23:45"
}
}
由于此回答中不含字符串 ‘Final Answer:’
磺芭,所以其輸出的應(yīng)該是一個工具調(diào)用。此時醉箕,我們解析此輸出并獲取工具調(diào)用參數(shù):使用參數(shù) {"time": "1:23:45"}
調(diào)用工具 convert_time
钾腺。
可以看到,工具返回了 {'seconds': '5025'}
讥裤。
此時放棒,我們將整個過程及結(jié)果添加到提示中,新提示就變成了如下這樣(比之前稍微復(fù)雜一些了):
Here is a question: "How many seconds are in 1:23:45?"
You have access to these tools:
- convert_time: converts a time given in hours:minutes:seconds into seconds.
You should first reflect with ‘Thought: {your_thoughts}’, then you either:
- call a tool with the proper JSON formatting,
- or your print your final answer starting with the prefix ‘Final Answer:’
Thought: I need to convert the time string into seconds.
Action:
{
"action": "convert_time",
"action_input": {
"time": "1:23:45"
}
}
Observation: {'seconds': '5025'}
?? 我們再次調(diào)用 LLM己英,并將這個新提示輸入給它间螟。鑒于它在 Observation
字段中得到了工具返回的結(jié)果,這輪 LLM 很有可能輸出如下:
Thought: I now have the information needed to answer the question.
Final Answer: There are 5025 seconds in 1:23:45.
至此损肛,任務(wù)解決厢破!
智能體系統(tǒng)面臨的挑戰(zhàn)
智能體系統(tǒng)中的 LLM 引擎需要克服以下幾個難點(diǎn):
從候選工具集中選出能實(shí)現(xiàn)預(yù)期目標(biāo)的工具:例如當(dāng)被問到
“大于 30,000 的最小素?cái)?shù)是多少?”
時荧关,智能體可以調(diào)用Search
工具溉奕,并問它`“K2 的高度是多少”,但這么做無濟(jì)于事忍啤。-
以規(guī)定的參數(shù)格式調(diào)用工具:例如加勤,當(dāng)嘗試計(jì)算 10 分鐘內(nèi)行駛了 3 公里的汽車的速度時,必須調(diào)用
Calculator
以讓其執(zhí)行“距離”除以“時間”的操作同波,假設(shè)Calculator
工具能接受 JSON 格式的調(diào)用:{"tool": "Calculator", "args": "3km/10min"}
鳄梅,看上去很簡單,但其實(shí)會有很多小陷阱未檩,一步不慎就前功盡棄戴尸,例如:- 工具名稱拼寫錯誤:
“calculator”
或“Compute”
是無效的 - 僅給出參數(shù)名而未給出參數(shù)值:
“args”: “distance/time”
- 參數(shù)格式未標(biāo)準(zhǔn)化:
“args”:"3km in 10minutes”
- 工具名稱拼寫錯誤:
有效吸收并使用歷史信息,無論是原始上下文信息還是前面若干輪工具調(diào)用所返回的觀察冤狡。
那么孙蒙,在真實(shí)場景中如何設(shè)置并使用智能體呢?
使用 LangChain 運(yùn)行智能體
我們最近封裝了一個 ChatHuggingFace
接口悲雳,你可以利用它在 ????LangChain 中使用開源模型創(chuàng)建智能體挎峦。
要創(chuàng)建 ChatModel 并為其提供工具,代碼非常簡單合瓢,你可在 Langchain 文檔 中查閱所有內(nèi)容坦胶。
from langchain_community.llms import HuggingFaceHub
from langchain_community.chat_models.huggingface import ChatHuggingFace
llm = HuggingFaceHub(
repo_id="HuggingFaceH4/zephyr-7b-beta",
task="text-generation",
)
chat_model = ChatHuggingFace(llm=llm)
你可以通過給 chat_model
提供 ReAct 風(fēng)格的提示和工具,將其變成智能體:
from langchain import hub
from langchain.agents import AgentExecutor, load_tools
from langchain.agents.format_scratchpad import format_log_to_str
from langchain.agents.output_parsers import (
ReActJsonSingleInputOutputParser,
)
from langchain.tools.render import render_text_description
from langchain_community.utilities import SerpAPIWrapper
# setup tools
tools = load_tools(["serpapi", "llm-math"], llm=llm)
# setup ReAct style prompt
prompt = hub.pull("hwchase17/react-json")
prompt = prompt.partial(
tools=render_text_description(tools),
tool_names=", ".join([t.name for t in tools]),
)
# define the agent
chat_model_with_stop = chat_model.bind(stop=["\nObservation"])
agent = (
{
"input": lambda x: x["input"],
"agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),
}
| prompt
| chat_model_with_stop
| ReActJsonSingleInputOutputParser()
)
# instantiate AgentExecutor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke(
{
"input": "Who is the current holder of the speed skating world record on 500 meters? What is her current age raised to the 0.43 power?"
}
)
智能體第一輪輸出如下:
Thought: To answer this question, I need to find age of the current speedskating world record holder. I will use the search tool to find this information.
Action:
{
"action": "search",
"action_input": "speed skating world record holder 500m age"
}
Observation: ...
智能體對決:開源 LLM 充當(dāng)通用推理智能體的表現(xiàn)如何?
你可在此處找到我們使用的基準(zhǔn)測試代碼顿苇。
評估
我們想要度量開源 LLM 作為通用推理智能體時的表現(xiàn)峭咒。因此,我們選用的問題都是需要依賴邏輯推演以及一些基本工具的使用才能回答出來的纪岁。這里凑队,我們將所需工具限制為計(jì)算器和互聯(lián)網(wǎng)搜索。
最終數(shù)據(jù)集 結(jié)合了以下 3 個數(shù)據(jù)集的樣本:
為了測試互聯(lián)網(wǎng)搜索能力蜂科,我們從HotpotQA中選擇了一些問題顽决,該數(shù)據(jù)集原本是一個檢索數(shù)據(jù)集,但在可以訪問互聯(lián)網(wǎng)時导匣,其可用于通用問答場景。有些問題原先需要結(jié)合多個不同來源的信息茸时,對這類問題贡定,我們可以執(zhí)行多次互聯(lián)網(wǎng)搜索來綜合出最終結(jié)果。
為了用上計(jì)算器可都,我們添加了來自 GSM8K 的一些問題缓待,該數(shù)據(jù)集用于測試小學(xué)數(shù)學(xué)四則運(yùn)算(加、減渠牲、乘旋炒、除)的能力。
我們還從 GAIA 中挑選了一些問題签杈,該數(shù)據(jù)集是面向通用人工智能助手的一個非常困難的基準(zhǔn)測試集瘫镇。原始數(shù)據(jù)集中的問題會需要用到很多不同的工具,如代碼解釋器或 pdf 閱讀器答姥,我們精心挑選了一些只需使用搜索和計(jì)算器的問題铣除。
評估時,我們選用 Prometheus 格式作為提示格式鹦付,并請 GPT-4 對結(jié)果以 5 分制李克特量表(Likert scale)形式進(jìn)行評分尚粘。具體使用的提示模板可參閱此處。
模型
我們對下列先進(jìn)開源模型進(jìn)行了評估:
- Llama2-70b-chat
- Mixtral-8x7B-Instruct-v0.1
- OpenHermes-2.5-Mistral-7B
- Zephyr-7b-beta
- SOLAR-10.7B-Instruct-v1.0
上述模型的評估是基于 LangChain 的 ReAct 實(shí)現(xiàn)的敲长。因此郎嫁,其函數(shù)調(diào)用模板為:
{
"action": $TOOL_NAME,
"action_input": $INPUT
}
為了進(jìn)行比較,我們還使用 LangChain 的 OpenAI 智能體在相同的數(shù)據(jù)集上對 GPT-3.5 和 GPT-4 進(jìn)行了評估祈噪。此時泽铛,我們使用 OpenAI 自己的函數(shù)調(diào)用模板,模型已經(jīng)針對該模板進(jìn)行了調(diào)優(yōu)钳降,因此我們認(rèn)為此時 OpenAI 模型性能處于最佳水平厚宰。
結(jié)果
由于并沒有專門針對給定調(diào)用函數(shù)格式進(jìn)行微調(diào),從這個角度講,與 OpenAI 智能體相比铲觉,開源模型智能體稍有劣勢澈蝙。
盡管如此,還是有一些模型表現(xiàn)讓人眼前一亮撵幽! ??
下面給出了 Mixtral-8x7B 回答問題 “Which city has a larger population, Guiyang or Tacheng?”
的一個例子灯荧。
Thought: To answer this question, I need to find the current populations of both Guiyang and Tacheng. I will use the search tool to find this information.
Action:
{
"action": "search",
"action_input": "current population of Guiyang"
}
4.697 million (2016)
Thought: I have obtained the population of Guiyang, which is 4.697 million (as of 2016). Now, I will use the search tool again to find the population of Tacheng.
Action:
{
"action": "search",
"action_input": "current population of Tacheng"
}
Population: Xinjiang: Tacheng data was reported at 1,110.000 Persons in 2021. This records a decrease from the previous number of 1,138.638 Persons for 2020.
Population: Xinjiang: Tacheng data is updated yearly, averaging 1,023.000 Persons from Dec 2005 to 2021, with 17 observations.
I have obtained the population of Tacheng, which is approximately 1.11 million (as of 2021). Comparing the two populations, Guiyang has a larger population than Tacheng.
Thought: I now know the final answer
Final Answer: Guiyang has a larger population, which is approximately 4.697 million (as of 2016), compared to Tacheng's population of approximately 1.11 million (as of 2021).
以下給出了我們測得的各模型在各數(shù)據(jù)集上的表現(xiàn)(為了便于閱讀,我們把 1-5 分制轉(zhuǎn)換成了 0 - 100%):
如你所見盐杂,部分開源模型在智能體工作流中表現(xiàn)欠佳:雖然我們能預(yù)料到較小的 Zephyr-7b 可能不盡如人意逗载,但 Llama2-70b 的表現(xiàn)卻讓我們大跌眼鏡。
?? 但 Mixtral-8x7B 表現(xiàn)得非常好链烈,甚至超越了 GPT-3.5厉斟! ??
而且這僅僅是開箱性能:與 GPT-3.5 不同,Mixtral 并沒有針對智能體工作流場景微調(diào)過(據(jù)我們所知)强衡,這在一定程度上說明其性能還有進(jìn)步空間擦秽。例如,在 GAIA 上漩勤,10% 的失敗案例是因?yàn)?Mixtral 嘗試使用錯誤的參數(shù)格式調(diào)用工具感挥。通過針對函數(shù)調(diào)用和任務(wù)規(guī)劃技能進(jìn)行適當(dāng)?shù)奈⒄{(diào),Mixtral 的分?jǐn)?shù)有可能會進(jìn)一步提高越败。
??我們強(qiáng)烈建議開源社區(qū)針對智能體場景微調(diào) Mixtral触幼,以超越 GPT-4! ??
最后的話:
雖然本文僅使用了 GAIA 基準(zhǔn)的一小部分問題和工具究飞,但該基準(zhǔn)似乎有潛力對智能體工作流整體性能進(jìn)行可靠度量置谦,因?yàn)槠錅y例通常需要多步推理以及嚴(yán)格的邏輯。
智能體工作流可以提高 LLM 的性能噪猾。例如霉祸,在 GSM8K 數(shù)據(jù)集上,GPT-4 的技術(shù)報(bào)告 表明 5-樣本 CoT 提示的成功率為 92%袱蜡;但一旦使用計(jì)算器工具丝蹭,零樣本的成功率就能提升到 95%。而對 Mixtral-8x7B坪蚁,LLM 排行榜 測得其 5-樣本成功率僅為 57.6%奔穿,但我們在智能體工作流中測得的零樣本成功率為 73%。 (注意敏晤,我們只測試了 GSM8K 數(shù)據(jù)集中的 20 個問題贱田。)
英文原文: <url> https://huggingface.co/blog/open-source-llms-as-agents </url>
原文作者:Aymeric Roucher,Joffrey Thomas嘴脾,Andrew Reed
譯者: Matrix Yao (姚偉峰)男摧,英特爾深度學(xué)習(xí)工程師蔬墩,工作方向?yàn)?transformer-family 模型在各模態(tài)數(shù)據(jù)上的應(yīng)用及大規(guī)模模型的訓(xùn)練推理。