React Agent 自定義實現(xiàn)代碼
背景
之前使用過 langchian 中的 agent 去實現(xiàn)過一些案例,angchian 的 React Agent 是有問題的,且內(nèi)部代碼有點難看懂,所以自己來根據(jù) React 思想,靈活來實現(xiàn)試一下。
可以先看看我自定義實現(xiàn)的邏輯圖,后面詳細說明:
langchin 中的 agent
langchian 中的幾種 agent 怎么用,我都看過了,也整理了一下了,那些能用,那些有問題的可以看注釋,代碼鏈接:https://github.com/5zjk5/prompt-engineering
langchin 中 agent 的問題
先來說說我用過的發(fā)現(xiàn)的問題,就是它的 React agent 有點問題,只調(diào)用一個工具就結(jié)束了,詳細實驗的文章:langchain 的 agent + tool 使用_langchain agent tool-CSDN博客
想去看看代碼到底怎么運行的,發(fā)現(xiàn)太難看懂了。
后面在我自己實現(xiàn) React agent 的時候,突然發(fā)現(xiàn),跟 prompt 關(guān)系挺大的,langchian 那個 prompt 應(yīng)該是根據(jù) openai 的去寫的,這是我目前想到只能調(diào)用一個工具的原因。
langchain 的 agent 案例
GitHub - 5zjk5/prompt-engineering: prompt 工程項目案例
自定義 React Agent
大模型
用的智譜 glm-4-air,如果換了模型,效果還不太穩(wěn)定,需要調(diào) prompt。
工具定義
定義兩個工具,一個是 tavily 的搜索,去官網(wǎng)開通賬號就可以獲得一個 api,免費調(diào)用 1000 次;
一個工具是根據(jù)名字查詢身高的自定義函數(shù)
from tavily import TavilyClient from llm.llm_api_key import TAVILY_API_KEY import time def tavily_search(query): try: # Step 1. Instantiating your TavilyClient tavily_client = TavilyClient(api_key=TAVILY_API_KEY) # Step 2. Executing a Q&A search query answer = tavily_client.qna_search(query=query) # Step 3. That's it! Your question has been answered! return answer except: time.sleep(1) # Step 1. Instantiating your TavilyClient tavily_client = TavilyClient(api_key=TAVILY_API_KEY) # Step 2. Executing a Q&A search query answer = tavily_client.qna_search(query=query) # Step 3. That's it! Your question has been answered! return answer def height_search(name): height_dic = { "張三": 180, "李四": 175, "王五": 170, "趙六": 165, "錢七": 160, "孫八": 175, "周九": 170, "吳十": 165, "鄭十一": 180, "王十二": 175, "李十三": 170, "趙十四": 165, "錢十五": 180, "孫十六": 175, } return height_dic.get(name)
工具描述,要讓大模型理解工具,需要定義描述,這里參考的智譜官方的工具的描述寫法:
tavily_search_tool = { "type": "function", "function": { "name": 'tavily_search', "description": "根據(jù)用戶查詢,去搜索引擎,返回搜索結(jié)果", "parameters": { "type": "object", "properties": { "query": { "description": "用戶搜索內(nèi)容 query", "type": "string" }, }, "required": ["query"] } } } height_search_tool = { "type": "function", "function": { "name": 'height_search', "description": "只要是有姓名,身高關(guān)鍵字,都需要使用此工具根據(jù)姓名,查詢對應(yīng)身高,每次只能查詢一個人的身高", "parameters": { "type": "object", "properties": { "name": { "description": "指具體的姓名或名字", "type": "string" }, }, "required": ["name"] } } }
問題設(shè)定
設(shè)定一個問題:
這個問題潛在意圖是查詢錢七,李四身高,并且搜索大模型定義,是想調(diào)用身高查詢工具 2 次,搜索工具 1 次。
問題改寫,挖掘潛在意圖
為什么加這一步呢?因為把問題傳給大模型后發(fā)現(xiàn)一個問題,它可能發(fā)現(xiàn)不了潛在意圖,例如這里潛在意圖要查詢身高,問題中沒有明顯提出,大模型思考結(jié)果:
這樣的話就只使用搜索工具就結(jié)束了,所以加了一步問題改寫,去發(fā)現(xiàn)潛在意圖,是利用大模型能力去做的,用 prompt,改寫結(jié)果成功識別出潛在意圖,并思考出要調(diào)用哪個工具:
盡你所能改寫以下問題,可以有多個答案,可以參照以下工具進行改寫,識別用戶潛在意圖:
```{tools}```
Question:`{query}`
Answer 按照以下格式,每一點代表一個意圖,如果需要用到工具的需要列出工具名字,不需要具體參數(shù):
```
1.
2.
...
```
React Prompt
React agent 核心的 prompt 怎么讓模型自動規(guī)劃,先來看 langchain 中的寫法:
Answer the following questions as best you can. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Question: {input} Thought:{agent_scratchpad}
傳入變量 tool 為所有工具,tool_names 為所有工具名稱列表,input 問題輸入,agent_scratchpad 思考要做什么,怎么做。
參照進行改編:
盡你所能回答以下問題。您可以使用以下工具: ```{tools}``` 嚴格使用以下 JSON 格式: ``` {{ Question: 根據(jù) thought 當前需要回答的問題,此字段必須存在 Thought: 對于 Question 要做什么,此字段必須存在 Action: {{'tool': 要采取的動作,應(yīng)該是[{tool_names}]之一,如果不需要工具可以空著}} Action Input: 動作的輸入,是一個 JSON 格式,此字段必須存在,如果不需要輸入可以空著 Observation: 行動的結(jié)果,此字段必須存在,默認為空 }} ``` (Question/Thought/Action/Action Input/Observation 五個字段必須存在,以上步驟只能重復(fù) 1 次) 開始吧! Question:`{query}` thought:`{agent_scratchpad}`
根據(jù) agent_scratchpad 每次運行得到 json 的 action,接著提取工具名及參數(shù),去進行工具調(diào)用,這里因為是 json,格式控制好了提取就方便了。
使用完工具后,把結(jié)果賦值給 Observation。
下一步規(guī)劃
agent_scratchpad 就是下一步規(guī)劃的思考,用 prompt 去進行規(guī)劃,傳給已經(jīng)執(zhí)行的 action,問題及思考,讓自動規(guī)劃下一步應(yīng)該做什么:
# 背景 有一個問題 Question,已經(jīng)有了對這個問題的思考 Thought,已執(zhí)行的思考 Action,需要根據(jù)這些信息去規(guī)劃出下一步應(yīng)該做什么。 # 輸入 ## Question:`{query}` ## Thought:`{thought}` ## Action:`{all_action_res}` # 思考推理: - 1、參考 Question 仔細理解 Thought,思考 Action 還有哪些沒有行動。 - 2、判斷你下一步做什么行動,不能過于發(fā)散過多的行動,必須根據(jù)步驟 1 的思考。 - 3、確保你的回答在語義上與 Action 中的內(nèi)容不重復(fù)是一個全新的步驟。 - 4、若 Thought 已經(jīng)全部執(zhí)行了,直接回答`no`。 # 輸出要求(嚴格按照以下要求輸出) - 回答需要用一句話清晰的總結(jié)下一步需要做什么,不需要其他任何信息。 - 如果沒有需要做的了,直接輸出`no`,不需要其他任何信息,不需要解釋任何理由。
這里遇到一個問題,就是可能會一直重復(fù)規(guī)劃,導(dǎo)致死循環(huán),在代碼中加了判斷,理論上開始重復(fù)規(guī)劃了,說明已經(jīng)沒有可以給出新的規(guī)劃了,那就結(jié)束吧。
問題總結(jié)
所有 action 的結(jié)果,用了一個列表保存的,最后用大模型自己去總結(jié)去回答問題就可以了。
D:\programming\dev_env\anaconda\anaconda3\python.exe "D:\Python_project\NLP\大模型學(xué)習(xí)\prompt-engineering\自定義 React Agant\run_agent.py" D:\programming\dev_env\anaconda\anaconda3\Lib\site-packages\langchain\callbacks\__init__.py:37: LangChainDeprecationWarning: Importing this callback from langchain is deprecated. Importing it from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead: `from langchain_community.callbacks import get_openai_callback`. To install langchain-community run `pip install -U langchain-community`. warnings.warn( 輸入 token:103/輸出 token:268/總共 token:371/ 問題改寫,識別潛在意圖: 1. 識別用戶提到的“身高比較高的小伙子”和“長得像錢七”,可能需要查詢錢七的身高信息(使用工具:height_search)。 2. 識別用戶提到的“還有他跟他身高差不多的兄弟李四”,可能需要查詢李四的身高信息(使用工具:height_search)。 3. 用戶對“大模型”表示不清楚,需要解釋或搜索“大模型”的定義和相關(guān)信息(使用工具:tavily_search)。 ===================================== 輸入 token:53/輸出 token:376/總共 token:429/ 解決此問題的思考 Thought: 根據(jù)用戶的問題,我們需要查詢錢七和李四的身高信息,并獲取關(guān)于“大模型”的解釋和相關(guān)信息。因此,我們需要使用height_search工具來查詢身高信息,以及使用tavily_search工具來搜索大模型的相關(guān)內(nèi)容。 ===================================== 輸入 token:89/輸出 token:426/總共 token:515/ {'Action': {'tool': 'height_search'}, 'Action Input': {'name': '錢七'}, 'Observation': 160, 'Question': '1. 識別用戶提到的“身高比較高的小伙子”和“長得像錢七”,可能需要查詢錢七的身高信息(使用工具:height_search)。', 'Thought': '需要使用工具查詢錢七的身高信息。'} ===================================== 輸入 token:12/輸出 token:289/總共 token:301/ 下一步需要做什么: 需要使用工具查詢李四的身高信息。 ===================================== 輸入 token:60/輸出 token:435/總共 token:495/ {'Action': {'tool': 'height_search'}, 'Action Input': {'name': '李四'}, 'Observation': 175, 'Question': '查詢李四的身高信息。', 'Thought': '使用height_search工具查詢李四的身高。'} ===================================== 輸入 token:14/輸出 token:301/總共 token:315/ 下一步需要做什么: 使用tavily_search工具搜索大模型的相關(guān)內(nèi)容。 ===================================== 輸入 token:61/輸出 token:437/總共 token:498/ {'Action': {'tool': 'tavily_search'}, 'Action Input': {'query': '大模型是什么意思'}, 'Observation': 'Based on the data provided, the term "大模型" (Big Model) refers ' 'to a method or technology used in the fields of machine ' 'learning and artificial intelligence to handle large-scale ' 'data and complex models. These models are typically ' 'constructed using deep neural networks with a large number of ' 'parameters, ranging from billions to even trillions. The ' 'purpose of big models is to improve model expressive power ' 'and predictive performance, enabling them to handle more ' 'complex tasks and datasets effectively. Big models play a ' 'crucial role in addressing challenges posed by increasing ' 'data volumes and model complexities in the field of AI and ' 'machine learning.', 'Question': '大模型是什么意思?', 'Thought': '使用搜索引擎查詢大模型的相關(guān)信息。'} ===================================== 輸入 token:10/輸出 token:311/總共 token:321/ 開始生成重復(fù)步驟,或已執(zhí)行 action 過多,判斷結(jié)束了!重復(fù)步驟:使用搜索引擎查詢大模型的相關(guān)信息。 下一步需要做什么: no ===================================== 輸入 token:109/輸出 token:332/總共 token:441/ 最終答案: 根據(jù)您的描述,錢七的身高是160厘米,而李四的身高是175厘米。至于您提到的“大模型”,這是一種在機器學(xué)習(xí)和人工智能領(lǐng)域中使用的方法或技術(shù)。大模型通常指的是具有大量參數(shù)(從數(shù)十億到數(shù)萬億不等)的深度神經(jīng)網(wǎng)絡(luò)模型。這些模型的目的是提高表達能力和預(yù)測性能,使它們能夠更有效地處理大規(guī)模數(shù)據(jù)和復(fù)雜任務(wù)。 簡而言之,大模型是為了應(yīng)對人工智能和機器學(xué)習(xí)領(lǐng)域中數(shù)據(jù)量增加和模型復(fù)雜性提升的挑戰(zhàn)而發(fā)展起來的技術(shù)。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Process finished with exit code 0
代碼
prompt-engineering/自定義 React Agant at master · 5zjk5/prompt-engineering · GitHub
到此這篇關(guān)于React Agent 自定義實現(xiàn)的文章就介紹到這了,更多相關(guān)React Agent 自定義內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react?component?function組件使用詳解
這篇文章主要為大家介紹了react?component?function組件的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11React 進入頁面后自動 focus 到某個輸入框的解決方案
React.js 當中提供了 ref 屬性來幫助我們獲取已經(jīng)掛載的元素的 DOM 節(jié)點,你可以給某個 JSX 元素加上 ref屬性,這篇文章主要介紹了React 進入頁面以后自動 focus 到某個輸入框,需要的朋友可以參考下2024-02-02react-native-fs實現(xiàn)文件下載、文本存儲的示例代碼
本篇文章主要介紹了react-native-fs實現(xiàn)文件下載、文本存儲的示例代碼,具有一定的參考價值,有興趣的可以了解下2017-09-09基于React.js實現(xiàn)兔兔牌九宮格翻牌抽獎組件
這篇文章主要為大家詳細介紹了如何基于React.js實現(xiàn)兔兔牌九宮格翻牌抽獎組件,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-01-01react?native?reanimated實現(xiàn)動畫示例詳解
這篇文章主要為大家介紹了react?native?reanimated實現(xiàn)動畫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03淺談React的React.FC與React.Component的使用
本文主要介紹了React的React.FC與React.Component的使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09react router 4.0以上的路由應(yīng)用詳解
本篇文章主要介紹了react router 4.0以上的路由應(yīng)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09