亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React Agent 自定義實(shí)現(xiàn)代碼

 更新時(shí)間:2024年10月21日 10:34:13   作者:木下瞳  
在使用langchain的ReactAgent遇到問(wèn)題后,作者嘗試自定義ReactAgent實(shí)現(xiàn),通過(guò)詳細(xì)分析langchain中的agent功能和問(wèn)題,結(jié)合React思想,作者設(shè)計(jì)了新的agent邏輯并在GitHub上分享了代碼,新的ReactAgent通過(guò)改進(jìn)prompt和工具調(diào)用邏輯,提升了任務(wù)執(zhí)行的效果和穩(wěn)定性

背景

之前使用過(guò) langchian 中的 agent 去實(shí)現(xiàn)過(guò)一些案例,angchian 的 React Agent 是有問(wèn)題的,且內(nèi)部代碼有點(diǎn)難看懂,所以自己來(lái)根據(jù) React 思想,靈活來(lái)實(shí)現(xiàn)試一下。

可以先看看我自定義實(shí)現(xiàn)的邏輯圖,后面詳細(xì)說(shuō)明:

langchin 中的 agent

langchian 中的幾種 agent 怎么用,我都看過(guò)了,也整理了一下了,那些能用,那些有問(wèn)題的可以看注釋,代碼鏈接:https://github.com/5zjk5/prompt-engineering

langchin 中 agent 的問(wèn)題

先來(lái)說(shuō)說(shuō)我用過(guò)的發(fā)現(xiàn)的問(wèn)題,就是它的 React agent 有點(diǎn)問(wèn)題,只調(diào)用一個(gè)工具就結(jié)束了,詳細(xì)實(shí)驗(yàn)的文章:langchain 的 agent + tool 使用_langchain agent tool-CSDN博客

想去看看代碼到底怎么運(yùn)行的,發(fā)現(xiàn)太難看懂了。

后面在我自己實(shí)現(xiàn) React agent 的時(shí)候,突然發(fā)現(xiàn),跟 prompt 關(guān)系挺大的,langchian 那個(gè) prompt 應(yīng)該是根據(jù) openai 的去寫的,這是我目前想到只能調(diào)用一個(gè)工具的原因。

langchain 的 agent 案例

GitHub - 5zjk5/prompt-engineering: prompt 工程項(xiàng)目案例

自定義 React Agent

大模型

用的智譜 glm-4-air,如果換了模型,效果還不太穩(wěn)定,需要調(diào) prompt。

工具定義

定義兩個(gè)工具,一個(gè)是 tavily 的搜索,去官網(wǎng)開(kāi)通賬號(hào)就可以獲得一個(gè) api,免費(fèi)調(diào)用 1000 次;

一個(gè)工具是根據(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ù)姓名,查詢對(duì)應(yīng)身高,每次只能查詢一個(gè)人的身高",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "description": "指具體的姓名或名字",
                        "type": "string"
                    },
                },
                "required": ["name"]
            }
        }
      }

問(wèn)題設(shè)定

設(shè)定一個(gè)問(wèn)題:

這個(gè)問(wèn)題潛在意圖是查詢錢七,李四身高,并且搜索大模型定義,是想調(diào)用身高查詢工具 2 次,搜索工具 1 次。

問(wèn)題改寫,挖掘潛在意圖

為什么加這一步呢?因?yàn)榘褑?wèn)題傳給大模型后發(fā)現(xiàn)一個(gè)問(wèn)題,它可能發(fā)現(xiàn)不了潛在意圖,例如這里潛在意圖要查詢身高,問(wèn)題中沒(méi)有明顯提出,大模型思考結(jié)果:

這樣的話就只使用搜索工具就結(jié)束了,所以加了一步問(wèn)題改寫,去發(fā)現(xiàn)潛在意圖,是利用大模型能力去做的,用 prompt,改寫結(jié)果成功識(shí)別出潛在意圖,并思考出要調(diào)用哪個(gè)工具:

盡你所能改寫以下問(wèn)題,可以有多個(gè)答案,可以參照以下工具進(jìn)行改寫,識(shí)別用戶潛在意圖:
```{tools}```
Question:`{query}`
Answer 按照以下格式,每一點(diǎn)代表一個(gè)意圖,如果需要用到工具的需要列出工具名字,不需要具體參數(shù):
```
1. 
2. 
...
```

React Prompt

React agent 核心的 prompt 怎么讓模型自動(dòng)規(guī)劃,先來(lái)看 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 問(wèn)題輸入,agent_scratchpad 思考要做什么,怎么做。

參照進(jìn)行改編:

盡你所能回答以下問(wèn)題。您可以使用以下工具:
```{tools}```
嚴(yán)格使用以下 JSON 格式:
```
{{
    Question: 根據(jù) thought 當(dāng)前需要回答的問(wèn)題,此字段必須存在
    Thought: 對(duì)于 Question 要做什么,此字段必須存在
    Action: {{'tool': 要采取的動(dòng)作,應(yīng)該是[{tool_names}]之一,如果不需要工具可以空著}}
    Action Input: 動(dòng)作的輸入,是一個(gè) JSON 格式,此字段必須存在,如果不需要輸入可以空著
    Observation: 行動(dòng)的結(jié)果,此字段必須存在,默認(rèn)為空
}}
```
(Question/Thought/Action/Action Input/Observation 五個(gè)字段必須存在,以上步驟只能重復(fù) 1 次)
開(kāi)始吧!
Question:`{query}`
thought:`{agent_scratchpad}`

根據(jù) agent_scratchpad 每次運(yùn)行得到 json 的 action,接著提取工具名及參數(shù),去進(jìn)行工具調(diào)用,這里因?yàn)槭?json,格式控制好了提取就方便了。

使用完工具后,把結(jié)果賦值給 Observation。

下一步規(guī)劃

agent_scratchpad 就是下一步規(guī)劃的思考,用 prompt 去進(jìn)行規(guī)劃,傳給已經(jīng)執(zhí)行的 action,問(wèn)題及思考,讓自動(dòng)規(guī)劃下一步應(yīng)該做什么:

# 背景
有一個(gè)問(wèn)題 Question,已經(jīng)有了對(duì)這個(gè)問(wèn)題的思考 Thought,已執(zhí)行的思考 Action,需要根據(jù)這些信息去規(guī)劃出下一步應(yīng)該做什么。
# 輸入
## Question:`{query}`
## Thought:`{thought}`
## Action:`{all_action_res}`
# 思考推理:
- 1、參考 Question 仔細(xì)理解 Thought,思考 Action 還有哪些沒(méi)有行動(dòng)。
- 2、判斷你下一步做什么行動(dòng),不能過(guò)于發(fā)散過(guò)多的行動(dòng),必須根據(jù)步驟 1 的思考。
- 3、確保你的回答在語(yǔ)義上與 Action 中的內(nèi)容不重復(fù)是一個(gè)全新的步驟。
- 4、若 Thought 已經(jīng)全部執(zhí)行了,直接回答`no`。
# 輸出要求(嚴(yán)格按照以下要求輸出)
- 回答需要用一句話清晰的總結(jié)下一步需要做什么,不需要其他任何信息。
- 如果沒(méi)有需要做的了,直接輸出`no`,不需要其他任何信息,不需要解釋任何理由。

這里遇到一個(gè)問(wèn)題,就是可能會(huì)一直重復(fù)規(guī)劃,導(dǎo)致死循環(huán),在代碼中加了判斷,理論上開(kāi)始重復(fù)規(guī)劃了,說(shuō)明已經(jīng)沒(méi)有可以給出新的規(guī)劃了,那就結(jié)束吧。

問(wèn)題總結(jié)

所有 action 的結(jié)果,用了一個(gè)列表保存的,最后用大模型自己去總結(jié)去回答問(wèn)題就可以了。

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/
問(wèn)題改寫,識(shí)別潛在意圖:
1. 識(shí)別用戶提到的“身高比較高的小伙子”和“長(zhǎng)得像錢七”,可能需要查詢錢七的身高信息(使用工具:height_search)。
2. 識(shí)別用戶提到的“還有他跟他身高差不多的兄弟李四”,可能需要查詢李四的身高信息(使用工具:height_search)。
3. 用戶對(duì)“大模型”表示不清楚,需要解釋或搜索“大模型”的定義和相關(guān)信息(使用工具:tavily_search)。
=====================================
輸入 token:53/輸出 token:376/總共 token:429/
解決此問(wèn)題的思考 Thought:
根據(jù)用戶的問(wèn)題,我們需要查詢錢七和李四的身高信息,并獲取關(guān)于“大模型”的解釋和相關(guān)信息。因此,我們需要使用height_search工具來(lái)查詢身高信息,以及使用tavily_search工具來(lái)搜索大模型的相關(guān)內(nèi)容。
=====================================
輸入 token:89/輸出 token:426/總共 token:515/
{'Action': {'tool': 'height_search'},
 'Action Input': {'name': '錢七'},
 'Observation': 160,
 'Question': '1. 識(shí)別用戶提到的“身高比較高的小伙子”和“長(zhǎng)得像錢七”,可能需要查詢錢七的身高信息(使用工具: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/
開(kāi)始生成重復(fù)步驟,或已執(zhí)行 action 過(guò)多,判斷結(jié)束了!重復(fù)步驟:使用搜索引擎查詢大模型的相關(guān)信息。
下一步需要做什么:
no
=====================================
輸入 token:109/輸出 token:332/總共 token:441/
最終答案:
根據(jù)您的描述,錢七的身高是160厘米,而李四的身高是175厘米。至于您提到的“大模型”,這是一種在機(jī)器學(xué)習(xí)和人工智能領(lǐng)域中使用的方法或技術(shù)。大模型通常指的是具有大量參數(shù)(從數(shù)十億到數(shù)萬(wàn)億不等)的深度神經(jīng)網(wǎng)絡(luò)模型。這些模型的目的是提高表達(dá)能力和預(yù)測(cè)性能,使它們能夠更有效地處理大規(guī)模數(shù)據(jù)和復(fù)雜任務(wù)。
簡(jiǎn)而言之,大模型是為了應(yīng)對(duì)人工智能和機(jī)器學(xué)習(xí)領(lǐng)域中數(shù)據(jù)量增加和模型復(fù)雜性提升的挑戰(zhàn)而發(fā)展起來(lái)的技術(shù)。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Process finished with exit code 0

代碼

prompt-engineering/自定義 React Agant at master · 5zjk5/prompt-engineering · GitHub

到此這篇關(guān)于React Agent 自定義實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)React Agent 自定義內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論