企業(yè)如何做網(wǎng)站推廣公司百度官網(wǎng)優(yōu)化
AI - 淺聊一下基于LangChain的AI Agent
大家好,今天我們來聊聊一個很有意思的主題: AI Agent ,就是目前非常流行的所謂的AI智能體。AI的發(fā)展日新月異,都2024年末了,如果此時小伙伴們對這個非?;鸬母拍钸€不清楚的話,似乎有點(diǎn)落伍于時代了,希望今天通過這個短文來普及一下。
什么是AI Agent?
簡單來說,AI Agent 就是一個能夠自主進(jìn)行任務(wù)的人工智能系統(tǒng)。它可以理解輸入、處理數(shù)據(jù)并做出決策。比如自動客服機(jī)器人就在很多情況下充當(dāng)AI Agent。AI Agent通過接收用戶的指令和信息,利用內(nèi)置的AI模型或算法進(jìn)行處理,并提供相應(yīng)的輸出或采取行動。
AI Agent與RAG
RAG(Retrieval-Augmented Generation) 是一種結(jié)合檢索和生成模型的方法,用來回答復(fù)雜問題或者生成內(nèi)容。它的主要思路是先從一個大型數(shù)據(jù)庫中檢索出相關(guān)的信息,再用生成模型(比如GPT-3)來生成最終的回答。
Agent與RAG的比較
-
處理方式:
- AI Agent:更多是獨(dú)立執(zhí)行任務(wù),可能會包括決策過程。它處理的任務(wù)可以是廣泛的,比如對話、數(shù)據(jù)處理、業(yè)務(wù)操作等。
- RAG:主要用于回答問題或者生成內(nèi)容,通過檢索相關(guān)信息然后生成回答。
-
優(yōu)勢:
- AI Agent:更通用,可以用于多種任務(wù)。適用于自動化操作、實(shí)時交互等。
- RAG:結(jié)合了檢索和生成的優(yōu)勢,能夠回答非常復(fù)雜的問題,并且回答質(zhì)量通常比較高。
-
實(shí)現(xiàn)難度:
- AI Agent:需要實(shí)現(xiàn)框架、決策邏輯、模型集成等,復(fù)雜度較高。
- RAG:實(shí)現(xiàn)相對簡單一些,主要是將檢索和生成模型有效結(jié)合。
Agent與RAG的關(guān)系
理解AI Agent與RAG(Retrieval-Augmented Generation)的關(guān)系以及它們?nèi)绾我黄鸸ぷ魇且粋€很重要的課題。AI Agent 可以看作是一個更高層次的系統(tǒng),它能夠自主進(jìn)行任務(wù)并做出決策。而 RAG 是一個技術(shù)方法,主要用于增強(qiáng)生成模型的能力,使其可以利用從大數(shù)據(jù)集或知識庫中檢索到的信息來生成更準(zhǔn)確和相關(guān)的回答。簡而言之,AI Agent可以構(gòu)建在RAG之上,利用RAG的方法來提升問答和生成任務(wù)的效果。
那么為什么要在RAG上構(gòu)建AI Agent?
-
提高優(yōu)質(zhì)回答的能力:RAG方法結(jié)合了檢索和生成的優(yōu)點(diǎn),能夠從龐大的知識庫中檢索出相關(guān)的信息,再通過生成模型來生成準(zhǔn)確的回答。
-
保持上下文和連續(xù)性:RAG方法可以通過檢索到的相關(guān)文檔來保持對話的上下文和連續(xù)性,這對于長對話和復(fù)雜問題尤為重要。
-
增強(qiáng)多任務(wù)處理:AI Agent可以利用RAG來處理多個任務(wù),比如詢問問題、提供建議或者執(zhí)行操作等。
AI Agent的優(yōu)點(diǎn)和缺點(diǎn)
優(yōu)點(diǎn)
- 自動化:AI Agent可以自動處理很多重復(fù)性任務(wù),比如客服、數(shù)據(jù)分析等,減少人力成本。
- 24/7服務(wù):與人類不同,AI Agent可以全天候工作,不受時間限制。
- 速度快:處理信息和響應(yīng)速度非???#xff0c;能夠即時提供服務(wù)。
- 數(shù)據(jù)驅(qū)動:能夠處理和分析大量數(shù)據(jù),從中提取有價值的見解。
缺點(diǎn)
-
依賴數(shù)據(jù)和模型:AI Agent的性能高度依賴于訓(xùn)練數(shù)據(jù)和模型的質(zhì)量。如果數(shù)據(jù)有偏見,結(jié)果可能也會有偏見。
-
缺乏創(chuàng)造性:AI Agent在處理突發(fā)情況和非結(jié)構(gòu)化問題時,往往不如人類靈活。
-
隱私問題:處理敏感數(shù)據(jù)時,存在隱私和安全風(fēng)險。
用LangChain實(shí)現(xiàn)一個基于RAG的AI Agent
通過以下步驟,我們可以基于LangChain和OpenAI實(shí)現(xiàn)一個最小化的基于RAG的AI Agent。代碼的主要目的是創(chuàng)建一個基于LangChain的AI Agent,該AI Agent能夠檢索和生成回答,并保持對話的上下文。以下是逐步解析每個部分的內(nèi)容:
導(dǎo)入所需模塊
首先,我們需要導(dǎo)入各種庫和模塊:
import os
from langchain_openai import ChatOpenAI # 導(dǎo)入OpenAI聊天模型類
from langchain_openai import OpenAIEmbeddings # 導(dǎo)入OpenAI嵌入模型類
from langchain_core.vectorstores import InMemoryVectorStore # 導(dǎo)入內(nèi)存向量存儲類
import bs4 # 導(dǎo)入BeautifulSoup庫用于網(wǎng)頁解析
from langchain_core.tools import tool # 導(dǎo)入工具裝飾器
from langchain_community.document_loaders import WebBaseLoader # 導(dǎo)入Web頁面加載器
from langchain_text_splitters import RecursiveCharacterTextSplitter # 導(dǎo)入遞歸字符文本切分器
from langgraph.checkpoint.memory import MemorySaver # 導(dǎo)入內(nèi)存保存器
from langgraph.prebuilt import create_react_agent # 導(dǎo)入create_react_agent函數(shù)
設(shè)置API密鑰
設(shè)置OpenAI的API密鑰,這是為了能夠使用OpenAI提供的模型和服務(wù):
os.environ["OPENAI_API_KEY"] = 'your-api-key'
初始化嵌入模型和向量存儲
初始化OpenAI嵌入模型和內(nèi)存向量存儲,用于后續(xù)的文檔處理和搜索:
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
vector_store = InMemoryVectorStore(embeddings)
初始化聊天模型
初始化一個OpenAI的聊天模型,這里使用的是gpt-4o-mini
:
llm = ChatOpenAI(model="gpt-4o-mini")
加載并切分文檔內(nèi)容
使用WebBaseLoader從指定的網(wǎng)址加載文檔內(nèi)容,并使用BeautifulSoup僅解析指定的部分(如文章內(nèi)容、標(biāo)題和頭部),然后使用文本切分器來將文檔切分成小塊:
loader = WebBaseLoader(web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),bs_kwargs=dict(parse_only=bs4.SoupStrainer(class_=("post-content", "post-title", "post-header"))),
)
docs = loader.load() # 加載文檔內(nèi)容text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
all_splits = text_splitter.split_documents(docs) # 切分文檔# 將切分后的文檔添加到向量存儲
_ = vector_store.add_documents(documents=all_splits)
定義檢索工具函數(shù)
定義一個用@tool裝飾器修飾的檢索工具函數(shù),用于根據(jù)用戶的查詢檢索相關(guān)內(nèi)容:
@tool(response_format="content_and_artifact")
def retrieve(query: str):"""Retrieve information related to a query."""retrieved_docs = vector_store.similarity_search(query, k=2) # 從向量存儲中檢索最相似的2個文檔serialized = "\n\n".join((f"Source: {doc.metadata}\n" f"Content: {doc.page_content}")for doc in retrieved_docs)return serialized, retrieved_docs # 返回序列化內(nèi)容和檢索到的文檔
初始化內(nèi)存保存器和創(chuàng)建反應(yīng)代理
使用MemorySaver來保存對話狀態(tài),并使用create_react_agent
函數(shù)創(chuàng)建AI Agent:
memory = MemorySaver() # 初始化內(nèi)存保存器
agent_executor = create_react_agent(llm, [retrieve], checkpointer=memory) # 創(chuàng)建AI Agent
指定對話線程ID
指定一個對話的線程ID,用于管理多輪對話:
config = {"configurable": {"thread_id": "def234"}}
示例對話
定義一個輸入消息,然后運(yùn)行AI Agent來處理這個消息,并打印最后的響應(yīng):
input_message = ("What is the standard method for Task Decomposition?\n\n""Once you get the answer, look up common extensions of that method."
)for event in agent_executor.stream({"messages": [{"role": "user", "content": input_message}]},stream_mode="values",config=config,
):event["messages"][-1].pretty_print() # 打印最后一條消息內(nèi)容
通過這種方式,可以實(shí)現(xiàn)一個基于LangChain的最小化AI Agent,它能夠智能地響應(yīng)用戶查詢,并在對話中保持上下文的一致性。
代碼輸出
================================ Human Message =================================What is the standard method for Task Decomposition?Once you get the answer, look up common extensions of that method.
================================== Ai Message ==================================
Tool Calls:retrieve (call_HmlIDGfLMemXCh88ut0Fqzxz)Call ID: call_HmlIDGfLMemXCh88ut0FqzxzArgs:query: standard method for Task Decomposition
================================= Tool Message =================================
Name: retrieveSource: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}
Content: Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.
Task decomposition can be done (1) by LLM with simple prompting like "Steps for XYZ.\n1.", "What are the subgoals for achieving XYZ?", (2) by using task-specific instructions; e.g. "Write a story outline." for writing a novel, or (3) with human inputs.Source: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}
Content: Fig. 1. Overview of a LLM-powered autonomous agent system.
Component One: Planning#
A complicated task usually involves many steps. An agent needs to know what they are and plan ahead.
Task Decomposition#
Chain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.
================================== Ai Message ==================================
Tool Calls:retrieve (call_gk1TfyTqR131FuctblLrjkz4)Call ID: call_gk1TfyTqR131FuctblLrjkz4Args:query: common extensions of Task Decomposition methods
================================= Tool Message =================================
Name: retrieveSource: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}
Content: Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.
Task decomposition can be done (1) by LLM with simple prompting like "Steps for XYZ.\n1.", "What are the subgoals for achieving XYZ?", (2) by using task-specific instructions; e.g. "Write a story outline." for writing a novel, or (3) with human inputs.Source: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}
Content: Fig. 1. Overview of a LLM-powered autonomous agent system.
Component One: Planning#
A complicated task usually involves many steps. An agent needs to know what they are and plan ahead.
Task Decomposition#
Chain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.
================================== Ai Message ==================================The standard method for Task Decomposition involves a technique known as the Chain of Thought (CoT), which enhances model performance on complex tasks by instructing it to "think step by step." This allows the model to break down large tasks into smaller, more manageable steps. Task decomposition can be achieved through various means, such as:1. **Simple Prompting**: Using prompts like "Steps for XYZ." or "What are the subgoals for achieving XYZ?"
2. **Task-Specific Instructions**: Providing specific instructions tailored to the task, e.g., "Write a story outline." for writing a novel.
3. **Human Inputs**: Involving human guidance in the decomposition process.### Common Extensions of Task Decomposition Methods
One notable extension of the standard method is the **Tree of Thoughts** (Yao et al. 2023). This method builds upon CoT by exploring multiple reasoning possibilities at each step. It decomposes the problem into various thought steps and generates multiple thoughts for each step, forming a tree structure. The search process can utilize either breadth-first search (BFS) or depth-first search (DFS), with each state being evaluated by a classifier or majority vote.Thus, while the Chain of Thought is the foundational method, the Tree of Thoughts represents an advanced extension that allows for a more nuanced exploration of tasks.
重點(diǎn)解釋
讓我們詳細(xì)解釋一下create_react_agent
和agent_executor
在上面代碼中的作用和實(shí)現(xiàn)。
create_react_agent
create_react_agent
是一個預(yù)構(gòu)建的方法,用于創(chuàng)建一個反應(yīng)式(reactive)的AI Agent。這個Agent能夠根據(jù)特定的查詢和工具來生成響應(yīng)。在這個方法中,我們會整合一個語言模型(LLM)和一組工具,并添加一個檢查點(diǎn)(Checkpointer)來維護(hù)對話的狀態(tài)。
以下是這個方法的詳細(xì)解釋:
from langgraph.prebuilt import create_react_agent# 使用已定義的工具和語言模型創(chuàng)建一個反應(yīng)式的AI Agent
agent_executor = create_react_agent(llm, [retrieve], checkpointer=memory)
參數(shù)解釋:
- llm: 這個參數(shù)傳遞了一個大語言模型,這里是
ChatOpenAI
,它是用于生成自然語言回答的核心組件。 - tools: 一個工具列表,這里傳遞了
retrieve
工具。這個工具是用來進(jìn)行文檔檢索的,可以基于用戶的查詢,從存儲的文檔中找到相關(guān)內(nèi)容。 - checkpointer: 傳遞了一個
MemorySaver
實(shí)例,用于保存和加載對話的狀態(tài)。這個組件確保了即使在對話中斷后,也可以恢復(fù)之前的對話狀態(tài)。
agent_executor
agent_executor
是通過 create_react_agent
創(chuàng)建的AI Agent執(zhí)行器,它能夠接收用戶的輸入,利用預(yù)定義的工具和語言模型生成對應(yīng)的回答,并管理對話狀態(tài)。
以下是這個執(zhí)行器的詳細(xì)解釋和使用示例:
# Specify an ID for the thread
config = {"configurable": {"thread_id": "def234"}}input_message = ("What is the standard method for Task Decomposition?\n\n""Once you get the answer, look up common extensions of that method."
)for event in agent_executor.stream({"messages": [{"role": "user", "content": input_message}]},stream_mode="values",config=config,
):event["messages"][-1].pretty_print()
流程解釋:
-
配置線程ID:
config = {"configurable": {"thread_id": "def234"}}
:這里指定了一個線程ID,用于管理特定對話線程的狀態(tài)。
-
定義輸入消息:
input_message
定義了用戶的輸入,包括兩個部分:請求標(biāo)準(zhǔn)的任務(wù)分解方法,然后查詢這個方法的常見擴(kuò)展。
-
執(zhí)行對話流:
for event in agent_executor.stream(...)
:啟動AI Agent的對話流。messages
: 包含了用戶輸入信息的字典列表,每個字典表示一條消息。stream_mode="values"
: 配置流式輸出模式。config
: 傳遞配置選項(xiàng),這里主要傳遞了線程ID。
-
處理和輸出每個事件:
event["messages"][-1].pretty_print()
:對于每個事件,輸出對話流末尾的消息內(nèi)容(即AI生成的回復(fù))。
代碼總結(jié)
通過創(chuàng)建一個反應(yīng)式的AI Agent (agent_executor
),并結(jié)合了文檔檢索工具 (retrieve
) 和大語言模型 (llm
), 使得整個AI系統(tǒng)能夠:
- 接收用戶的查詢。
- 通過工具從存儲的文檔中檢索相關(guān)信息。
- 使用語言模型生成自然語言回答。
- 管理對話狀態(tài),使得多輪對話能夠連貫進(jìn)行。
這種設(shè)計實(shí)現(xiàn)了檢索增強(qiáng)生成(RAG)架構(gòu)的功能,既利用了檢索模塊帶來的豐富上下文信息,又結(jié)合了生成模型的靈活應(yīng)答能力,為用戶提供更精準(zhǔn)和個性化的服務(wù)。
LLM消息抓取
交互1
請求
{"messages": [[{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","HumanMessage"],"kwargs": {"content": "What is the standard method for Task Decomposition?\n\nOnce you get the answer, look up common extensions of that method.","type": "human","id": "bb39ed14-f51d-48cc-ac4c-e4eeefffaf55"}}]]
}
回復(fù)
{"generations": [[{"text": "","generation_info": {"finish_reason": "tool_calls","logprobs": null},"type": "ChatGeneration","message": {"lc": 1,"type": "constructor","id": ["langchain","schema","messages","AIMessage"],"kwargs": {"content": "","additional_kwargs": {"tool_calls": [{"id": "call_HmlIDGfLMemXCh88ut0Fqzxz","function": {"arguments": "{\"query\":\"standard method for Task Decomposition\"}","name": "retrieve"},"type": "function"}],"refusal": null},"response_metadata": {"token_usage": {"completion_tokens": 18,"prompt_tokens": 67,"total_tokens": 85,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b","finish_reason": "tool_calls","logprobs": null},"type": "ai","id": "run-1992178b-d402-49b0-9c2b-6087ab5c2535-0","tool_calls": [{"name": "retrieve","args": {"query": "standard method for Task Decomposition"},"id": "call_HmlIDGfLMemXCh88ut0Fqzxz","type": "tool_call"}],"usage_metadata": {"input_tokens": 67,"output_tokens": 18,"total_tokens": 85,"input_token_details": {"audio": 0,"cache_read": 0},"output_token_details": {"audio": 0,"reasoning": 0}},"invalid_tool_calls": []}}}]],"llm_output": {"token_usage": {"completion_tokens": 18,"prompt_tokens": 67,"total_tokens": 85,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b"},"run": null,"type": "LLMResult"
}
交互2
請求
{"messages": [[{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","HumanMessage"],"kwargs": {"content": "What is the standard method for Task Decomposition?\n\nOnce you get the answer, look up common extensions of that method.","type": "human","id": "bb39ed14-f51d-48cc-ac4c-e4eeefffaf55"}},{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","AIMessage"],"kwargs": {"content": "","additional_kwargs": {"tool_calls": [{"id": "call_HmlIDGfLMemXCh88ut0Fqzxz","function": {"arguments": "{\"query\":\"standard method for Task Decomposition\"}","name": "retrieve"},"type": "function"}],"refusal": null},"response_metadata": {"token_usage": {"completion_tokens": 18,"prompt_tokens": 67,"total_tokens": 85,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b","finish_reason": "tool_calls","logprobs": null},"type": "ai","id": "run-1992178b-d402-49b0-9c2b-6087ab5c2535-0","tool_calls": [{"name": "retrieve","args": {"query": "standard method for Task Decomposition"},"id": "call_HmlIDGfLMemXCh88ut0Fqzxz","type": "tool_call"}],"usage_metadata": {"input_tokens": 67,"output_tokens": 18,"total_tokens": 85,"input_token_details": {"audio": 0,"cache_read": 0},"output_token_details": {"audio": 0,"reasoning": 0}},"invalid_tool_calls": []}},{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","ToolMessage"],"kwargs": {"content": "Source: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}\nContent: Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.\n\nSource: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}\nContent: Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.","type": "tool","name": "retrieve","id": "290c729c-60c5-4624-a41f-514c0b67208d","tool_call_id": "call_HmlIDGfLMemXCh88ut0Fqzxz","artifact": [{"lc": 1,"type": "constructor","id": ["langchain","schema","document","Document"],"kwargs": {"id": "e42c541d-e1c7-4023-84a2-bded42cef315","metadata": {"source": "https://lilianweng.github.io/posts/2023-06-23-agent/"},"page_content": "Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.","type": "Document"}},{"lc": 1,"type": "constructor","id": ["langchain","schema","document","Document"],"kwargs": {"id": "2511dbdc-2c14-46d7-831f-53182e28d607","metadata": {"source": "https://lilianweng.github.io/posts/2023-06-23-agent/"},"page_content": "Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.","type": "Document"}}],"status": "success"}}]]
}
回復(fù)
{"generations": [[{"text": "","generation_info": {"finish_reason": "tool_calls","logprobs": null},"type": "ChatGeneration","message": {"lc": 1,"type": "constructor","id": ["langchain","schema","messages","AIMessage"],"kwargs": {"content": "","additional_kwargs": {"tool_calls": [{"id": "call_gk1TfyTqR131FuctblLrjkz4","function": {"arguments": "{\"query\":\"common extensions of Task Decomposition methods\"}","name": "retrieve"},"type": "function"}],"refusal": null},"response_metadata": {"token_usage": {"completion_tokens": 19,"prompt_tokens": 414,"total_tokens": 433,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b","finish_reason": "tool_calls","logprobs": null},"type": "ai","id": "run-28ca8d6e-c417-4784-a97f-0664b909dcaa-0","tool_calls": [{"name": "retrieve","args": {"query": "common extensions of Task Decomposition methods"},"id": "call_gk1TfyTqR131FuctblLrjkz4","type": "tool_call"}],"usage_metadata": {"input_tokens": 414,"output_tokens": 19,"total_tokens": 433,"input_token_details": {"audio": 0,"cache_read": 0},"output_token_details": {"audio": 0,"reasoning": 0}},"invalid_tool_calls": []}}}]],"llm_output": {"token_usage": {"completion_tokens": 19,"prompt_tokens": 414,"total_tokens": 433,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b"},"run": null,"type": "LLMResult"
}
交互3
請求
{"messages": [[{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","HumanMessage"],"kwargs": {"content": "What is the standard method for Task Decomposition?\n\nOnce you get the answer, look up common extensions of that method.","type": "human","id": "bb39ed14-f51d-48cc-ac4c-e4eeefffaf55"}},{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","AIMessage"],"kwargs": {"content": "","additional_kwargs": {"tool_calls": [{"id": "call_HmlIDGfLMemXCh88ut0Fqzxz","function": {"arguments": "{\"query\":\"standard method for Task Decomposition\"}","name": "retrieve"},"type": "function"}],"refusal": null},"response_metadata": {"token_usage": {"completion_tokens": 18,"prompt_tokens": 67,"total_tokens": 85,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b","finish_reason": "tool_calls","logprobs": null},"type": "ai","id": "run-1992178b-d402-49b0-9c2b-6087ab5c2535-0","tool_calls": [{"name": "retrieve","args": {"query": "standard method for Task Decomposition"},"id": "call_HmlIDGfLMemXCh88ut0Fqzxz","type": "tool_call"}],"usage_metadata": {"input_tokens": 67,"output_tokens": 18,"total_tokens": 85,"input_token_details": {"audio": 0,"cache_read": 0},"output_token_details": {"audio": 0,"reasoning": 0}},"invalid_tool_calls": []}},{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","ToolMessage"],"kwargs": {"content": "Source: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}\nContent: Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.\n\nSource: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}\nContent: Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.","type": "tool","name": "retrieve","id": "290c729c-60c5-4624-a41f-514c0b67208d","tool_call_id": "call_HmlIDGfLMemXCh88ut0Fqzxz","artifact": [{"lc": 1,"type": "constructor","id": ["langchain","schema","document","Document"],"kwargs": {"id": "e42c541d-e1c7-4023-84a2-bded42cef315","metadata": {"source": "https://lilianweng.github.io/posts/2023-06-23-agent/"},"page_content": "Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.","type": "Document"}},{"lc": 1,"type": "constructor","id": ["langchain","schema","document","Document"],"kwargs": {"id": "2511dbdc-2c14-46d7-831f-53182e28d607","metadata": {"source": "https://lilianweng.github.io/posts/2023-06-23-agent/"},"page_content": "Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.","type": "Document"}}],"status": "success"}},{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","AIMessage"],"kwargs": {"content": "","additional_kwargs": {"tool_calls": [{"id": "call_gk1TfyTqR131FuctblLrjkz4","function": {"arguments": "{\"query\":\"common extensions of Task Decomposition methods\"}","name": "retrieve"},"type": "function"}],"refusal": null},"response_metadata": {"token_usage": {"completion_tokens": 19,"prompt_tokens": 414,"total_tokens": 433,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b","finish_reason": "tool_calls","logprobs": null},"type": "ai","id": "run-28ca8d6e-c417-4784-a97f-0664b909dcaa-0","tool_calls": [{"name": "retrieve","args": {"query": "common extensions of Task Decomposition methods"},"id": "call_gk1TfyTqR131FuctblLrjkz4","type": "tool_call"}],"usage_metadata": {"input_tokens": 414,"output_tokens": 19,"total_tokens": 433,"input_token_details": {"audio": 0,"cache_read": 0},"output_token_details": {"audio": 0,"reasoning": 0}},"invalid_tool_calls": []}},{"lc": 1,"type": "constructor","id": ["langchain","schema","messages","ToolMessage"],"kwargs": {"content": "Source: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}\nContent: Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.\n\nSource: {'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}\nContent: Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.","type": "tool","name": "retrieve","id": "c7583058-518c-437a-90a0-07c20c11703a","tool_call_id": "call_gk1TfyTqR131FuctblLrjkz4","artifact": [{"lc": 1,"type": "constructor","id": ["langchain","schema","document","Document"],"kwargs": {"id": "e42c541d-e1c7-4023-84a2-bded42cef315","metadata": {"source": "https://lilianweng.github.io/posts/2023-06-23-agent/"},"page_content": "Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.","type": "Document"}},{"lc": 1,"type": "constructor","id": ["langchain","schema","document","Document"],"kwargs": {"id": "2511dbdc-2c14-46d7-831f-53182e28d607","metadata": {"source": "https://lilianweng.github.io/posts/2023-06-23-agent/"},"page_content": "Fig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.","type": "Document"}}],"status": "success"}}]]
}
回復(fù)
{"generations": [[{"text": "The standard method for Task Decomposition involves a technique known as the Chain of Thought (CoT), which enhances model performance on complex tasks by instructing it to \"think step by step.\" This allows the model to break down large tasks into smaller, more manageable steps. Task decomposition can be achieved through various means, such as:\n\n1. **Simple Prompting**: Using prompts like \"Steps for XYZ.\" or \"What are the subgoals for achieving XYZ?\"\n2. **Task-Specific Instructions**: Providing specific instructions tailored to the task, e.g., \"Write a story outline.\" for writing a novel.\n3. **Human Inputs**: Involving human guidance in the decomposition process.\n\n### Common Extensions of Task Decomposition Methods\nOne notable extension of the standard method is the **Tree of Thoughts** (Yao et al. 2023). This method builds upon CoT by exploring multiple reasoning possibilities at each step. It decomposes the problem into various thought steps and generates multiple thoughts for each step, forming a tree structure. The search process can utilize either breadth-first search (BFS) or depth-first search (DFS), with each state being evaluated by a classifier or majority vote.\n\nThus, while the Chain of Thought is the foundational method, the Tree of Thoughts represents an advanced extension that allows for a more nuanced exploration of tasks.","generation_info": {"finish_reason": "stop","logprobs": null},"type": "ChatGeneration","message": {"lc": 1,"type": "constructor","id": ["langchain","schema","messages","AIMessage"],"kwargs": {"content": "The standard method for Task Decomposition involves a technique known as the Chain of Thought (CoT), which enhances model performance on complex tasks by instructing it to \"think step by step.\" This allows the model to break down large tasks into smaller, more manageable steps. Task decomposition can be achieved through various means, such as:\n\n1. **Simple Prompting**: Using prompts like \"Steps for XYZ.\" or \"What are the subgoals for achieving XYZ?\"\n2. **Task-Specific Instructions**: Providing specific instructions tailored to the task, e.g., \"Write a story outline.\" for writing a novel.\n3. **Human Inputs**: Involving human guidance in the decomposition process.\n\n### Common Extensions of Task Decomposition Methods\nOne notable extension of the standard method is the **Tree of Thoughts** (Yao et al. 2023). This method builds upon CoT by exploring multiple reasoning possibilities at each step. It decomposes the problem into various thought steps and generates multiple thoughts for each step, forming a tree structure. The search process can utilize either breadth-first search (BFS) or depth-first search (DFS), with each state being evaluated by a classifier or majority vote.\n\nThus, while the Chain of Thought is the foundational method, the Tree of Thoughts represents an advanced extension that allows for a more nuanced exploration of tasks.","additional_kwargs": {"refusal": null},"response_metadata": {"token_usage": {"completion_tokens": 274,"prompt_tokens": 762,"total_tokens": 1036,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b","finish_reason": "stop","logprobs": null},"type": "ai","id": "run-4f607e3c-ad2c-41ca-8259-6a6ce3a07c7f-0","usage_metadata": {"input_tokens": 762,"output_tokens": 274,"total_tokens": 1036,"input_token_details": {"audio": 0,"cache_read": 0},"output_token_details": {"audio": 0,"reasoning": 0}},"tool_calls": [],"invalid_tool_calls": []}}}]],"llm_output": {"token_usage": {"completion_tokens": 274,"prompt_tokens": 762,"total_tokens": 1036,"completion_tokens_details": {"accepted_prediction_tokens": 0,"audio_tokens": 0,"reasoning_tokens": 0,"rejected_prediction_tokens": 0},"prompt_tokens_details": {"audio_tokens": 0,"cached_tokens": 0}},"model_name": "gpt-4o-mini-2024-07-18","system_fingerprint": "fp_bba3c8e70b"},"run": null,"type": "LLMResult"
}
小結(jié)
AI Agent可以被設(shè)計為獨(dú)立完成任務(wù)的系統(tǒng),而RAG是一種用來增強(qiáng)生成模型回答能力的方法。在實(shí)際應(yīng)用中,可以將AI Agent構(gòu)建在RAG方法之上,從而利用RAG的優(yōu)點(diǎn),提升對復(fù)雜問題的回答質(zhì)量和上下文連貫性。通過LangChain,我們可以輕松實(shí)現(xiàn)一個最小化的基于RAG的AI Agent,并利用它進(jìn)行智能問答。希望這篇技術(shù)博客對大家有所幫助,如果有更多問題或者討論,歡迎隨時交流!