自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

我們聊聊如何構(gòu)建通用LLM Agent 精華

發(fā)布于 2025-1-2 12:20
瀏覽
0收藏

我們聊聊如何構(gòu)建通用LLM Agent-AI.x社區(qū)

1. LLM Agent是什么

大語言模型智能體是一種程序,其執(zhí)行邏輯由其底層模型控制。

我們聊聊如何構(gòu)建通用LLM Agent-AI.x社區(qū)

大語言模型智能體與少樣本提示或固定工作流程等方法的區(qū)別在于,它能夠定義和調(diào)整執(zhí)行用戶查詢所需的步驟。通過使用一組工具(如代碼執(zhí)行或網(wǎng)絡(luò)搜索),智能體可以決定使用哪個工具、如何使用它,并根據(jù)輸出對結(jié)果進(jìn)行迭代。這種適應(yīng)性使系統(tǒng)能夠以最小的配置處理各種用例。

我們聊聊如何構(gòu)建通用LLM Agent-AI.x社區(qū)

智能體架構(gòu)存在于一個范圍內(nèi),從固定工作流程的可靠性到自主智能體的靈活性。例如,像檢索增強(qiáng)生成(RAG)這樣的固定流程可以通過自我反思循環(huán)進(jìn)行增強(qiáng),使程序在初始響應(yīng)不足時能夠進(jìn)行迭代?;蛘?,一個反應(yīng)式(ReAct)智能體可以配備固定流程作為工具,提供一種靈活而結(jié)構(gòu)化的方法。架構(gòu)的選擇最終取決于用例以及在可靠性和靈活性之間的期望權(quán)衡。

2. 構(gòu)建通用型大語言模型智能體的步驟

2.1 選擇合適的大語言模型

選擇合適的模型對于實(shí)現(xiàn)期望的性能至關(guān)重要。需要考慮多個因素,如許可證、成本和語言支持。構(gòu)建大語言模型智能體時,最重要的考慮因素是模型在關(guān)鍵任務(wù)(如編碼、工具調(diào)用和推理)上的性能。評估基準(zhǔn)包括:

  • 大規(guī)模多任務(wù)語言理解(MMLU)(推理)
  • 伯克利函數(shù)調(diào)用排行榜(工具選擇與工具調(diào)用)
  • HumanEval 和 BigCodeBench(編碼)
  • 模型的上下文窗口也是一個關(guān)鍵因素。智能體工作流程可能會消耗大量token,有時甚至超過 10 萬個,較大的上下文窗口非常有幫助。
  • 可供考慮的模型:

前沿模型(GPT4 - o、Claude 3.5);

開源模型(Llama3.2、Qwen2.5)。

一般來說,較大的模型往往性能更好,但能夠在本地運(yùn)行的較小模型仍然是一個不錯的選擇。使用較小的模型時,你將局限于更簡單的用例,并且可能只能將智能體連接到一兩個基本工具。

2.2 定義智能體的控制邏輯(即通信結(jié)構(gòu))

我們聊聊如何構(gòu)建通用LLM Agent-AI.x社區(qū)

LLM和智能體之間的主要區(qū)別在于系統(tǒng)提示。在LLM的背景下,系統(tǒng)提示是在模型處理用戶查詢之前提供給它的一組指令和上下文信息。大語言模型的智能體行為可以在系統(tǒng)提示中進(jìn)行編碼。以下是一些常見的智能體模式,可以根據(jù)需要進(jìn)行定制:

  • 工具使用:智能體決定何時將查詢路由到適當(dāng)?shù)墓ぞ呋蛞蕾囎陨碇R。
  • 反思:智能體在回答用戶之前審查并糾正自己的答案。大多數(shù)大語言模型系統(tǒng)也可以添加反思步驟。
  • 推理 - 行動(ReAct):智能體迭代地推理如何解決查詢,執(zhí)行一個動作,觀察結(jié)果,并決定是否采取另一個動作或提供響應(yīng)。
  • 計(jì)劃 - 執(zhí)行:智能體預(yù)先計(jì)劃,將任務(wù)分解為子步驟(如果需要),然后執(zhí)行每個步驟。

最后兩種模式(ReAct 和計(jì)劃 - 執(zhí)行)通常是構(gòu)建通用型單智能體的最佳起點(diǎn)。

我們聊聊如何構(gòu)建通用LLM Agent-AI.x社區(qū)

為了有效地實(shí)現(xiàn)這些行為,你需要進(jìn)行一些提示詞工程。你可能還想使用結(jié)構(gòu)化生成技術(shù)。這基本上意味著塑造大語言模型的輸出以匹配特定的格式或模式,以便智能體的響應(yīng)與你所期望的通信風(fēng)格保持一致。

示例:以下是來自 Bee Agent 框架的反應(yīng)式(ReAct)風(fēng)格智能體的提示(??https://github.com/i-am-bee/bee-agent-framework/blob/main/src/agents/bee/prompts.ts??)。

# Communication structure
You communicate only in instruction lines. The format is: "Instruction: expected output\n". You must only use these instruction lines and must not enter empty lines between them. Each instruction must start on a new line.
{{#tools.length}}
You must skip the instruction lines Function Name, Function Input and Function Output if no function calling is required.
{{/tools.length}}


Message: User's message. You never use this instruction line.
{{^tools.length}}
Thought: A single-line plan of how to answer the user's message, including an explanation of the reasoning behind it. It must be immediately followed by Final Answer.
{{/tools.length}}
{{#tools.length}}
Thought: A single-line step-by-step plan of how to answer the user's message, including an explanation of the reasoning behind it. You can use the available functions defined above. This instruction line must be immediately followed by Function Name if one of the available functions defined above needs to be called, or by Final Answer. Do not provide the answer here.
Function Name: Name of the function. This instruction line must be immediately followed by Function Input.
Function Input: Function parameters. Empty object is a valid parameter.
Function Output: Output of the function in JSON format.
Thought: Continue your thinking process.
{{/tools.length}}
Final Answer: Answer the user or ask for more information or clarification. It must always be preceded by Thought.


## Examples
Message: Can you translate "How are you" into French?
Thought: The user wants to translate a text into French. I can do that.
Final Answer: Comment vas-tu?

2.3 定義智能體的核心指令

我們往往認(rèn)為大語言模型開箱即用就帶有許多功能。其中一些功能很棒,但其他功能可能不完全符合你的需求。為了獲得你期望的性能,在系統(tǒng)提示中明確列出你想要和不想要的所有功能非常重要。這可能包括以下指令:

  • 智能體名稱和角色:智能體的名稱以及它的用途。
  • 語氣和簡潔性:它應(yīng)該聽起來多么正式或隨意,以及應(yīng)該多么簡短。
  • 何時使用工具:決定何時依賴外部工具而不是模型自身的知識。
  • 處理錯誤:當(dāng)工具或過程出現(xiàn)問題時,智能體應(yīng)該怎么做。

示例:以下是 Bee Agent 框架中指令部分的一個片段。

# Instructions
User can only see the Final Answer, all answers must be provided there.
{{^tools.length}}
You must always use the communication structure and instructions defined above. Do not forget that Thought must be a single-line immediately followed by Final Answer.
{{/tools.length}}
{{#tools.length}}
You must always use the communication structure and instructions defined above. Do not forget that Thought must be a single-line immediately followed by either Function Name or Final Answer.
You must use Functions to retrieve factual or historical information to answer the message.
{{/tools.length}}
If the user suggests using a function that is not available, answer that the function is not available. You can suggest alternatives if appropriate.
When the message is unclear or you need more information from the user, ask in Final Answer.


# Your capabilities
Prefer to use these capabilities over functions.
- You understand these languages: English, Spanish, French.
- You can translate, analyze and summarize, even long documents.


# Notes
- If you don't know the answer, say that you don't know.
- The current time and date in ISO format can be found in the last message.
- When answering the user, use friendly formats for time and date.
- Use markdown syntax for formatting code snippets, links, JSON, tables, images, files.
- Sometimes, things don't go as planned. Functions may not provide useful information on the first few tries. You should always try a few different approaches before declaring the problem unsolvable.
- When the function doesn't give you what you were asking for, you must either use another function or a different function input.
  - When using search engines, you try different formulations of the query, possibly even in a different language.
- You cannot do complex calculations, computations, or data manipulations without using functions.

2.4 定義和優(yōu)化核心工具

工具賦予智能體超能力。通過一組定義明確的有限工具,你可以實(shí)現(xiàn)廣泛的功能。關(guān)鍵工具包括代碼執(zhí)行、網(wǎng)絡(luò)搜索、文件讀取和數(shù)據(jù)分析。對于每個工具,你需要定義以下內(nèi)容并將其作為系統(tǒng)提示的一部分:

  • 工具名稱:功能的唯一、描述性名稱。
  • 工具描述:清晰解釋工具的作用以及何時使用它。這有助于智能體確定何時選擇正確的工具。
  • 工具輸入模式:一個模式,概述必需和可選參數(shù)、它們的類型以及任何約束。智能體根據(jù)用戶查詢使用此模式填寫所需的輸入。
  • 指向運(yùn)行工具的位置 / 方式

示例:以下是來自 Langchain (??https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/tools/arxiv/tool.py???)社區(qū)的 Arxiv 工具實(shí)現(xiàn)的摘錄。此實(shí)現(xiàn)需要一個 ArxivAPIWrapper 實(shí)現(xiàn)(???https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/utilities/arxiv.py???)。

"""Tool for the Arxiv API."""


from typing import Optional, Type


from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field


from langchain_community.utilities.arxiv import ArxivAPIWrapper




class ArxivInput(BaseModel):
    """Input for the Arxiv tool."""


    query: str = Field(descriptinotallow="search query to look up")




class ArxivQueryRun(BaseTool):  # type: ignore[override, override]
    """Tool that searches the Arxiv API."""


    name: str = "arxiv"
    description: str = (
        "A wrapper around Arxiv.org "
        "Useful for when you need to answer questions about Physics, Mathematics, "
        "Computer Science, Quantitative Biology, Quantitative Finance, Statistics, "
        "Electrical Engineering, and Economics "
        "from scientific articles on arxiv.org. "
        "Input should be a search query."
    )
    api_wrapper: ArxivAPIWrapper = Field(default_factory=ArxivAPIWrapper)  # type: ignore[arg-type]
    args_schema: Type[BaseModel] = ArxivInput


    def _run(
        self,
        query: str,
        run_manager: Optional[CallbackManagerForToolRun] = None,
    ) -> str:
        """Use the Arxiv tool."""
        return self.api_wrapper.run(query)

在某些情況下,仍需要優(yōu)化工具以獲得期望的性能。這可能涉及通過一些提示工程調(diào)整工具名稱或描述,設(shè)置高級配置來處理常見錯誤,或者過濾工具的輸出。

2.5 決定內(nèi)存處理策略

大語言模型受其上下文窗口的限制,即它們一次可以 “記住” 的令牌數(shù)量。在多輪對話中的過去交互、冗長的工具輸出或智能體所基于的額外上下文等情況下,內(nèi)存可能會很快填滿。這就是為什么擁有可靠的內(nèi)存處理策略至關(guān)重要。

在智能體中,內(nèi)存是指系統(tǒng)存儲、記憶和利用過去交互信息的能力。這使智能體能夠隨著時間的推移保持上下文,根據(jù)先前的交流改進(jìn)其響應(yīng),并提供更個性化的體驗(yàn)。

常見內(nèi)存處理策略:

  • 滑動內(nèi)存:在內(nèi)存中保留最后 k 輪對話,并丟棄更早的對話。
  • token內(nèi)存:保留最后 n 個token,丟棄其余的。
  • 總結(jié)式內(nèi)存:使用大語言模型在每一輪對話時總結(jié)對話內(nèi)容,并丟棄單個消息

此外,你還可以讓大語言模型檢測關(guān)鍵時刻并存儲在長期記憶中。這使得智能體能夠 “記住” 關(guān)于用戶的重要事實(shí),從而使體驗(yàn)更加個性化。

到目前為止我們所涵蓋的五個步驟為設(shè)置智能體奠定了基礎(chǔ)。但是,如果在這個階段我們通過大語言模型運(yùn)行用戶查詢會發(fā)生什么呢?

我們聊聊如何構(gòu)建通用LLM Agent-AI.x社區(qū)

以下是可能出現(xiàn)的情況的一個示例:

User Message: Extract key insighs from this dataset
Files: bill-of-materials.csv
Thought: First, I need to inspect the columns of the dataset and provide basic data statistics.
Function Name: Python
Function Input: {"language":"python","code":"import pandas as pd\n\ndataset = pd.read_csv('bill-of-materials.csv')\n\nprint(dataset.columns)\nprint(dataset.describe())","inputFiles":["bill-of-materials.csv"]}
Function Output:

在這一點(diǎn)上,智能體會產(chǎn)生原始文本輸出。那么我們?nèi)绾巫屗鼘?shí)際執(zhí)行下一步呢?這就是解析和編排發(fā)揮作用的地方。

2.6 解析智能體的原始輸出

解析器是一種將原始數(shù)據(jù)轉(zhuǎn)換為應(yīng)用程序可以理解和處理的格式(如具有屬性的對象)的函數(shù)。

對于我們正在構(gòu)建的智能體,解析器需要識別我們在 2.2 中定義的通信結(jié)構(gòu),并返回結(jié)構(gòu)化輸出,如 JSON。這使應(yīng)用程序更容易處理和執(zhí)行智能體的下一步驟。

注意:一些模型提供商(如 OpenAI)默認(rèn)可以返回可解析的輸出。對于其他模型,尤其是開源模型,這需要進(jìn)行配置。

2.7 編排Agent的下一步驟

最后一步是設(shè)置編排邏輯。這決定了大語言模型輸出結(jié)果后會發(fā)生什么。根據(jù)輸出,你將:

  • 執(zhí)行工具調(diào)用,或者
  • 返回答案,即對用戶查詢的最終響應(yīng)或要求更多信息的后續(xù)請求。

我們聊聊如何構(gòu)建通用LLM Agent-AI.x社區(qū)

如果觸發(fā)了工具調(diào)用,工具的輸出將被發(fā)送回大語言模型(作為其工作記憶的一部分)。然后,大語言模型將決定如何處理這個新信息:要么執(zhí)行另一個工具調(diào)用,要么向用戶返回答案。

以下是這種編排邏輯在代碼中可能呈現(xiàn)的示例:

def orchestrator(llm_agent, llm_output, tools, user_query):
    """
    Orchestrates the response based on LLM output and iterates if necessary.


    Parameters:
    - llm_agent (callable): The LLM agent function for processing tool outputs.
    - llm_output (dict): Initial output from the LLM, specifying the next action.
    - tools (dict): Dictionary of available tools with their execution methods.
    - user_query (str): The original user query.


    Returns:
    - str: The final response to the user.
    """
    while True:
        action = llm_output.get("action")


        if action == "tool_call":
            # Extract tool name and parameters
            tool_name = llm_output.get("tool_name")
            tool_params = llm_output.get("tool_params", {})


            if tool_name in tools:
                try:
                    # Execute the tool
                    tool_result = tools[tool_name](**tool_params)
                    # Send tool output back to the LLM agent for further processing
                    llm_output = llm_agent({"tool_output": tool_result})
                except Exception as e:
                    return f"Error executing tool '{tool_name}': {str(e)}"
            else:
                return f"Error: Tool '{tool_name}' not found."


        elif action == "return_answer":
            # Return the final answer to the user
            return llm_output.get("answer", "No answer provided.")


        else:
            return "Error: Unrecognized action type from LLM output."

現(xiàn)在你擁有了一個能夠處理各種各樣場景的系統(tǒng),從競爭分析和高級研究到自動化復(fù)雜工作流程。

2.8 多智能體系統(tǒng)在什么情況下適用

雖然這一代大語言模型非常強(qiáng)大,但它們有一個關(guān)鍵限制:它們難以處理信息過載。過多的上下文或工具可能會使模型不堪重負(fù),導(dǎo)致性能問題。通用型單智能體最終會遇到這個瓶頸,尤其是因?yàn)锳gent通常消耗大量token。

對于某些應(yīng)用場景而言,采用多智能體的設(shè)置可能更合理。通過將職責(zé)分配給多個智能體,你能夠避免單個大語言模型智能體的上下文負(fù)擔(dān)過重,并提高整體效率。

話雖如此,通用的單智能體設(shè)置對于制作原型來說是一個很好的開始。它可以幫助你快速測試你的使用案例,并確定在哪些地方開始出現(xiàn)問題。通過這個過程,你可以:

  • 了解任務(wù)的哪些部分確實(shí)能從智能體方法中獲益。
  • 確定在更大的工作流程中可作為獨(dú)立流程拆分出來的組件。

從單個智能體入手能讓你獲得寶貴的見解,以便在擴(kuò)展到更復(fù)雜的系統(tǒng)時改進(jìn)你的方法。

3. 入門建議

使用框架是快速測試和迭代智能體配置的好方法。

原文鏈接:https://towardsdatascience.com/build-a-general-purpose-ai-agent-c40be49e74

本文轉(zhuǎn)載自鴻煊的學(xué)習(xí)筆記,作者: 鴻煊 ????

標(biāo)簽
收藏
回復(fù)
舉報
回復(fù)
相關(guān)推薦