前言
該系列教程的代碼: https://github.com/shar-pen/Langchain-MiniTutorial
我主要參考 langchain 官方教程, 有選擇性的記錄了一下學習內容
這是教程清單
- 1.初試langchain
- 2.prompt
- 3.OutputParser/輸出解析
- 4.model/vllm模型部署和langchain調用
- 5.DocumentLoader/多種文檔加載器
- 6.TextSplitter/文檔切分
- 7.Embedding/文本向量化
- 8.VectorStore/向量數據庫存儲和檢索
- 9.Retriever/檢索器
- 10.Reranker/文檔重排序
- 11.RAG管道/多輪對話RAG
- 12.Agent/工具定義/Agent調用工具/Agentic RAG
Tool
工具是一個接口,允許代理、鏈條或大語言模型與外部世界互動。
LangChain 提供了易于使用的內置工具,并且還允許用戶輕松構建自定義工具。
你可以在下面的鏈接中找到集成到 LangChain 中的工具列表。
- 集成到 LangChain 中的工具列表
內置工具 Built-in tools
你可以使用 LangChain 提供的預定義工具和工具包。
工具指的是單一的實用工具,而工具包將多個工具組合成一個單元供使用。
你可以在下面的鏈接中找到相關的工具。
注意
- LangChain 工具/工具包
web 檢索工具
from langchain_community.utilities import GoogleSerperAPIWrappersearch = GoogleSerperAPIWrapper()
search.run("Obama's first name?")
圖片生成工具
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI# Initialize the ChatOpenAI model
llm = ChatOpenAI(base_url='http://localhost:5551/v1',api_key='EMPTY',model_name='Qwen2.5-7B-Instruct',temperature=0.2,
)# Define a prompt template for DALL-E image generation
prompt = PromptTemplate.from_template("Generate a detailed IMAGE GENERATION prompt for DALL-E based on the following description. ""Return only the prompt, no intro, no explanation, no chatty, no markdown, no code block, no nothing. Just the prompt""Output should be less than 1000 characters. Write in English only.""Image Description: \n{image_desc}",
)# Create a chain connecting the prompt, LLM, and output parser
chain = prompt | llm | StrOutputParser()# Execute the chain
image_prompt = chain.invoke({"image_desc": "A Neo-Classicism painting satirizing people looking at their smartphones."}
)# Output the image prompt
print(image_prompt)
幾乎所有 tool 都是需要 api key 的
Python REPL 工具
此工具提供一個類,用于在 REPL (Read-Eval-Print Loop) 環境中執行 Python 代碼。
- PythonREPLTool
描述
- 提供一個 Python shell 環境。
- 執行有效的 Python 命令作為輸入。
- 使用
print(...)
函數查看結果。
主要特點
- sanitize_input:可選項,用于清理輸入(默認:True)
- python_repl:PythonREPL 的實例(默認:在全局作用域中執行)
使用方法
- 創建
PythonREPLTool
的實例。 - 使用
run
、arun
或invoke
方法執行 Python 代碼。
輸入清理
- 從輸入字符串中移除不必要的空格、反引號、關鍵字 “python” 和其他多余的元素。
from langchain_experimental.tools import PythonREPLTool# Creates a tool for executing Python code.
python_tool = PythonREPLTool()# Executes Python code and returns the results.
print(python_tool.invoke("print(100 + 200)"))
下面是請求大語言模型編寫 Python 代碼并返回結果的示例。
工作流程概述
- 請求大語言模型為特定任務編寫 Python 代碼。
- 執行生成的代碼以獲取結果。
- 輸出結果。
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableLambdapython_tool = PythonREPLTool()
# A function that executes Python code, outputs intermediate steps, and returns the tool execution results.
def print_and_execute(code, debug=True):if debug:print("CODE:")print(code)return python_tool.invoke(code)# A prompt requesting Python code to be written.
prompt = ChatPromptTemplate.from_messages([("system","You are Raymond Hetting, an expert python programmer, well versed in meta-programming and elegant, concise and short but well documented code. You follow the PEP8 style guide. ""Return only the code, no intro, no explanation, no chatty, no markdown, no code block, no nothing. Just the code.",),("human", "{input}"),]
)
# Create LLM model.
llm = ChatOpenAI(base_url='http://localhost:5551/v1',api_key='EMPTY',model_name='Qwen2.5-7B-Instruct',temperature=0.2,
)# Create a chain using the prompt and the LLM model.
chain = prompt | llm | StrOutputParser() | RunnableLambda(print_and_execute)# Outputting the results.
print(chain.invoke("Write code to generate Powerball numbers."))
自定義工具
除了 LangChain 提供的內置工具外,你還可以定義和使用自己的自定義工具。
為此,可以使用 langchain.tools
模塊提供的 @tool
裝飾器將一個函數轉換為工具。
@tool 裝飾器: 這個裝飾器允許你將一個函數轉換為工具。它提供了各種選項來定制工具的行為。
使用方法
- 在函數上方應用
@tool
裝飾器。 - 根據需要設置裝飾器參數。
使用這個裝飾器,你可以輕松地將常規 Python 函數轉換為強大的工具,從而實現自動化文檔生成和靈活的接口創建。
from langchain.tools import tool# Convert a function into a tool using a decorator.
@tool
def add_numbers(a: int, b: int) -> int:"""Add two numbers"""return a + b@tool
def multiply_numbers(a: int, b: int) -> int:"""Multiply two numbers"""return a * b# Execute tool.
print(add_numbers.invoke({"a": 3, "b": 4}))
print(multiply_numbers.invoke({"a": 3, "b": 4}))
創建一個用于 Google 新聞文章搜索的自定義工具
定義 GoogleNews
類,該類將作為一個工具,用于搜索 Google 新聞文章。
注意
- 不需要 API 密鑰(因為它使用 RSS 源)。
此工具用于搜索由 news.google.com 提供的新聞文章。
描述
- 使用 Google 新聞搜索 API 來檢索最新的新聞。
- 允許基于關鍵詞搜索新聞。
主要參數
k
(int):返回的最大搜索結果數(默認:5)。
# hl: 語言, gl: 區域, ceid: 區域和語言代碼
url = f"{self.base_url}?hl=en&gl=US&ceid=US:en"
在代碼中,你可以通過修改語言 (hl)、區域 (gl) 和區域與語言代碼 (ceid) 來調整搜索結果的語言和區域。
注意
將提供的代碼保存為 google_news.py
,然后你可以在其他文件中使用 from google_news import GoogleNews
進行導入。
import feedparser
from urllib.parse import quote
from typing import List, Dict, Optionalclass GoogleNews:"""This is a class for searching Google News and returning the results."""def __init__(self):"""Initializes the GoogleNews class.Sets the base_url attribute."""self.base_url = "https://news.google.com/rss"def _fetch_news(self, url: str, k: int = 3) -> List[Dict[str, str]]:"""Fetches news from the given URL.Args:url (str): The URL to fetch the news from.k (int): The maximum number of news articles to fetch (default: 3).Returns:List[Dict[str, str]]: A list of dictionaries containing news titles and links."""news_data = feedparser.parse(url)return [{"title": entry.title, "link": entry.link}for entry in news_data.entries[:k]]def _collect_news(self, news_list: List[Dict[str, str]]) -> List[Dict[str, str]]:"""Formats and returns the list of news articles.Args:news_list (List[Dict[str, str]]): A list of dictionaries containing news information.Returns:List[Dict[str, str]]: A list of dictionaries containing URLs and content."""if not news_list:print("No news available for the given keyword.")return []result = []for news in news_list:result.append({"url": news["link"], "content": news["title"]})return resultdef search_latest(self, k: int = 3) -> List[Dict[str, str]]:"""Searches for the latest news.Args:k (int): The maximum number of news articles to search for (default: 3).Returns:List[Dict[str, str]]: A list of dictionaries containing URLs and content."""#url = f"{self.base_url}?hl=ko&gl=KR&ceid=KR:ko"url = f"{self.base_url}?hl=en&gl=US&ceid=US:en" # hl: ??, gl: ??, ceid: ?? ? ?? ??news_list = self._fetch_news(url, k)return self._collect_news(news_list)def search_by_keyword(self, keyword: Optional[str] = None, k: int = 3) -> List[Dict[str, str]]:"""Searches for news using a keyword. Args:keyword (Optional[str]): The keyword to search for (default: None).k (int): The maximum number of news articles to search for (default: 3).Returns:List[Dict[str, str]]: A list of dictionaries containing URLs and content."""if keyword:encoded_keyword = quote(keyword)url = f"{self.base_url}/search?q={encoded_keyword}"else:url = f"{self.base_url}?hl=en&gl=US&ceid=US:en"news_list = self._fetch_news(url, k)return self._collect_news(news_list)google_tool = GoogleNews()
google_tool.search_by_keyword("AI Investment")
地址是 google 的, 得翻墻, 以下是示例結果
[{'url': 'https://news.google.com/rss/articles/CBMimAFBVV95cUxPNkFrLURMdEZWOV9zdmRrTUhNbVFkdWswZWx2Qmh4cTJlMmFIdmpsQ3doaVluenA3TEJaT0U3RWVmanl3TTQ5V3RfS3kyYVpydEloNWZXbjBmSF85MGR5cjNFSFI5eFhtTGdIVlNXX3UxNmxwMnVIb2NkTXA5WFVZR2hKLUw5RU9iT3k1Zno2UG10N2h1b2g5Sw?oc=5','content': 'Nvidia Calls China\'s DeepSeek an "Excellent AI Advancement": Should Investors Press the Buy Button? - The Motley Fool'},{'url': 'https://news.google.com/rss/articles/CBMikwFBVV95cUxPd2ZnMnNwSWo2ZGhVSUJuNHd5S1Y3WUNWSkM4a0h5aHZQWU8tdzdlaW9pb25RUnI2cEwyZGtTemo5VUgwTDNHLVppNkw2MXdsbTRnb0UteHhtaHgxV043ZE9ZeG5aLUlCTzBGSHc1TFJzaHJsZENObzMxdTlvaEcyaG9vSjlRSTFWYXJEelF6RkRETnc?oc=5','content': 'How DeepSeek is upending AI innovation and investment after sending tech leaders reeling - New York Post'},{'url': 'https://news.google.com/rss/articles/CBMivwFBVV95cUxNUGdjLVE5dFpLaVZOcFY1djBRQXBLeTNZalptNmstNXlWRkpvX1U2aTJ5cDNiS3RNT2JzeGI1SnlzTXIyS2dWcEdieDB4R1kxSEo2eXUydlRkVWlzOGdUTnVCQ2NwNjNjaFpCdVpxQkphZXYxLU9BaXhBWmdVYWVjQnY1N3Q1aUtqaER5LV9WVlNWZ3BXMk5WR0gwWnlIU3RIazJZelZJQUM1ek12ZDFodEg1eDFaRm56eTR5UEh3VQ?oc=5','content': 'DeepSeek Marks The End Of The First Phase Of The AI Investment Boom - Forbes'}]
再用 tool 裝飾器
from langchain.tools import tool
from typing import List, Dict# Create a tool for searching news by keyword
@tool
def search_keyword(query: str) -> List[Dict[str, str]]:"""Look up news by keyword"""print(query)news_tool = GoogleNews()return news_tool.search_by_keyword(query, k=5)# Execution Results
search_keyword.invoke({"query": "LangChain AI"})
bind_tools
bind_tools
是 LangChain 中的一個強大功能,用于將自定義工具與大語言模型 (LLMs) 集成,從而實現增強的 AI 工作流。
接下來展示如何創建、綁定工具、解析并執行輸出,并將它們集成到 AgentExecutor
中。
import requests
from bs4 import BeautifulSoup
from langchain_core.tools import tool# Define the tools
@tool
def get_word_length(word: str) -> int:"""Return the length of the given text"""return len(word)@tool
def add_function(a: float, b: float) -> float:"""Add two numbers together"""return a + btools = [get_word_length, add_function]
現在,讓我們使用 bind_tools
函數將定義的工具與特定的大語言模型 (LLM) 關聯起來。
from langchain_openai import ChatOpenAI# Create a model
llm = ChatOpenAI(base_url='http://localhost:5551/v1',api_key='EMPTY',model_name='Qwen2.5-3B-Instruct',temperature=0.2,
)# Tool binding
llm_with_tools = llm.bind_tools(tools)
這是綁定函數的說明
import jsonprint(json.dumps(llm_with_tools.kwargs, indent=2))
llm_with_tools.invoke("What is the length of the given text 'LangChain OpenTutorial'?"
)
結果存儲在 tool_calls
中。讓我們打印 tool_calls
。
[注意]
name
表示工具的名稱。args
包含傳遞給工具的參數。
from pprint import pprint# Execution result
ret = llm_with_tools.invoke("What is the length of the given text 'LangChain OpenTutorial'?"
)pprint(ret.__dict__)
print(20*'-')
pprint(ret.tool_calls)
tool 輸出解析 JsonOutputToolsParser
接下來,我們將把 llm_with_tools
與 JsonOutputToolsParser
連接起來,以解析 tool_calls
并查看結果。
type
表示工具的類型。args
包含傳遞給工具的參數。
from langchain_core.output_parsers.openai_tools import JsonOutputToolsParser# Tool Binding + Tool Parser
chain = llm_with_tools | JsonOutputToolsParser(tools=tools)# Execution Result
tool_call_results = chain.invoke("What is the length of the given text 'LangChain OpenTutorial'?"
)
print(tool_call_results)
execute_tool_calls
函數識別合適的工具,傳遞相應的 args
,然后執行該工具。
def execute_tool_calls(tool_call_results):"""Function to execute the tool call results.:param tool_call_results: List of the tool call results:param tools: List of available tools"""# Iterate over the list of the tool call resultsfor tool_call_result in tool_call_results:# Tool name (function name)tool_name = tool_call_result["type"]# Tool argumentstool_args = tool_call_result["args"]# Find the tool that matches the name and execute it# Use the next() function to find the first matching toolmatching_tool = next((tool for tool in tools if tool.name == tool_name), None)if matching_tool:# Execute the toolresult = matching_tool.invoke(tool_args)print(f"[Executed Tool] {tool_name} [Args] {tool_args}\n[Execution Result] {result}")else:print(f"Warning: Unable to find the tool corresponding to {tool_name}.")# Execute the tool calls
execute_tool_calls(tool_call_results)
將工具與解析器綁定以執行
這次,我們將工具綁定、解析結果和執行工具調用的整個過程合并為一個步驟。
llm_with_tools
:綁定工具的LLM模型。JsonOutputToolsParser
:處理工具調用結果的解析器。execute_tool_calls
:執行工具調用結果的函數。
[流程摘要]
- 將工具綁定到模型。
- 解析工具調用的結果。
- 執行工具調用的結果。
from langchain_core.output_parsers.openai_tools import JsonOutputToolsParser# bind_tools + Parser + Execution
chain = llm_with_tools | JsonOutputToolsParser(tools=tools) | execute_tool_calls
chain.invoke("What is the length of the given text 'LangChain OpenTutorial'?")
# Execution Result 2
chain.invoke("114.5 + 121.2")# Double check
print(114.5 + 121.2)
將工具與Agent和AgentExecutor
綁定
bind_tools
提供可以由模型使用的工具(schemas)。
AgentExecutor
創建一個執行循環,用于執行諸如調用LLM、路由到合適的工具、執行工具以及重新調用模型等任務。
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI# Create an Agent prompt
prompt = ChatPromptTemplate.from_messages([("system","You are very powerful assistant, but don't know current events",),("user", "{input}"),MessagesPlaceholder(variable_name="agent_scratchpad"),]
)# Create a model
llm = ChatOpenAI(base_url='http://localhost:5551/v1',api_key='EMPTY',model_name='Qwen2.5-3B-Instruct',temperature=0.2,
)
from langchain.agents import AgentExecutor, create_tool_calling_agent# Use the tools defined previously
tools = [get_word_length, add_function]# Create an Agent
agent = create_tool_calling_agent(llm, tools, prompt)# Create an AgentExecutor
agent_executor = AgentExecutor(agent=agent,tools=tools,verbose=True,handle_parsing_errors=True,
)
# Execute the Agent
result = agent_executor.invoke({"input": "What is the length of the given text 'LangChain OpenTutorial'?"}
)# Execution Result
print(result["output"])
# Execute the Agent
result = agent_executor.invoke({"input": "Calculate the result of 114.5 + 121.2"})# Execution Result
print(result["output"])
小模型的性能還是會存在解析和 reasoning 方面的錯誤
Tool Calling Agent
在 LangChain 中,工具調用(tool calling)允許模型檢測何時調用一個或多個工具,以及需要將什么輸入傳遞給這些工具。
在進行 API 調用時,你可以定義工具,并智能地引導模型生成結構化對象,例如 JSON,這些對象包含調用這些工具所需的參數。
工具 API 的目標是提供比標準的文本生成或聊天 API 更加可靠的有效和有用的工具調用生成。
你可以創建代理(agents),這些代理會迭代地調用工具并接收結果,直到通過整合這些結構化輸出解決查詢,并將多個工具綁定到一個工具調用的聊天模型,讓模型選擇調用哪些工具。
這代表了一個更加通用的版本,它是為 OpenAI 的特定工具調用風格設計的 OpenAI 工具代理的擴展。
這個代理使用 LangChain 的 ToolCall
接口,支持比 OpenAI 更廣泛的提供者實現,包括 Anthropic
、Google Gemini
和 Mistral
等。
創建工具
LangChain 允許你定義自定義工具,供你的代理與之交互。你可以創建用于搜索新聞或執行 Python 代碼的工具。
@tool
裝飾器用于創建工具:
TavilySearchResults
是一個用于搜索新聞的工具。PythonREPL
是一個用于執行 Python 代碼的工具。
from langchain.tools import tool
from typing import List, Dict, Annotated
from langchain_community.tools import TavilySearchResults
from langchain_experimental.utilities import PythonREPL# Creating tool for searching news
@tool
def search_news(query: str) -> List[Dict[str, str]]:"""Search news by input keyword using Tavily Search API"""news_tool = TavilySearchResults(max_results=3,include_answer=True,include_raw_content=True,include_images=True,# search_depth="advanced",# include_domains = [],# exclude_domains = [])return news_tool.invoke(query, k=3)# Creating tool for executing python code
@tool
def python_repl_tool(code: Annotated[str, "The python code to execute to generate your chart."],
):"""Use this tool to execute Python code. If you want to see the output of a value,you should print it using print(...). This output is visible to the user."""result = ""try:result = PythonREPL().run(code)except BaseException as e:print(f"Failed to execute. Error: {repr(e)}")finally:return resultprint(f"Tool name: {search_news.name}")
print(f"Tool description: {search_news.description}")
print(f"Tool args: {search_news.args}")print('-'*20)
print(f"Tool name: {python_repl_tool.name}")
print(f"Tool description: {python_repl_tool.description}")
print(f"Tool args: {python_repl_tool.args}")
Tool name: search_news
Tool description: Search news by input keyword using Tavily Search API
Tool args: {'query': {'title': 'Query', 'type': 'string'}}
--------------------
Tool name: python_repl_tool
Tool description: Use this tool to execute Python code. If you want to see the output of a value,you should print it using print(...). This output is visible to the user.
Tool args: {'code': {'description': 'The python code to execute to generate your chart.', 'title': 'Code', 'type': 'string'}}
search_news('2024')
[{'url': 'https://en.wikipedia.org/wiki/2024_in_the_United_States','content': "In the Senate, at least six seats, those of Senators Tom Carper from Delaware, Mike Braun from Indiana, Ben Cardin from Maryland, Debbie Stabenow from Michigan, Mitt Romney from Utah, and Joe Manchin from West Virginia, will be open contests; the seat of the late Dianne Feinstein is also expected to be an open contest with Feinstein's immediate replacement, Laphonza Butler, expected to serve on an interim basis.[1][2][3]\nConcerning state governments, 11 states and two territories will hold gubernatorial elections, and most states and territories will hold elections for their legislatures. Contents\n2024 in the United States\nThe following is a list of predicted and scheduled events of the year 2024 in the United States, that have not yet occurred.\n With former president Donald Trump's declaration to run for the office again, the election may possibly be a rematch of the 2020 election, although the June 2023 indictment of Donald Trump may have a significant impact on Trump's presidential campaign. In the federal government, the offices of the president, vice president, all 435 seats of the House of Representatives, and roughly one third of the Senate. ←\n→\nElections[edit]\nThe US general elections will be held on November 5 of this year."},{'url': 'https://abcnews.go.com/Entertainment/abc-news-year-2024-back-years-major-news/story?id=116448091','content': 'ABC News\' \'The Year: 2024\' looks back at this year\'s major news and entertainment events - ABC News ABC News ABC News\' \'The Year: 2024\' looks back at this year\'s major news and entertainment events As the world gears up for 2025, it leaves behind a year of war, political shifts, pop culture moments, sporting triumphs, lost stars and more. ABC News was there to chronicle every moment and will look back at this year\'s defining events in a two-hour special, "The Year: 2024," which airs Thursday, Dec. 26 at 9 p.m. ET, and streams afterwards on Hulu. The special also explores how the love lives of some of our favorite stars evolved this year. ABC News Live'},{'url': 'https://en.wikipedia.org/wiki/2024','content': 'May 8 – In North Macedonian elections, the right-wing party VMRO-DPMNE wins in a landslide in the parliamentary elections, while its presidential candidate Gordana Siljanovska-Davkova is elected as the first female president of the country in the second round of the presidential election.[88][89] July 13 – While campaigning for the 2024 United States presidential election, former President Donald Trump is shot in the right ear in an assassination attempt at a rally he held near Butler, Pennsylvania.[139] July 28 – 2024 Venezuelan presidential election: Incumbent President Nicolás Maduro declares victory against opposition candidate Edmundo González Urrutia amid alleged irregularities, causing numerous South American states to refuse to acknowledge the results or suspend diplomatic relations with the Maduro government and sparking nationwide protests.[151]'}]
# Creating tools
tools = [search_news, python_repl_tool]
構建代理提示
chat_history
:此變量存儲對話歷史記錄,如果你的代理支持多輪對話,則使用此變量。(否則,可以省略此項。)agent_scratchpad
:此變量作為臨時存儲,用于存放中間變量。input
:此變量代表用戶的輸入。
from langchain_core.prompts import ChatPromptTemplate# Creating prompt
# Prompt is a text that describes the task the model should perform. (input the name and role of the tool)
prompt = ChatPromptTemplate.from_messages([("system","You are a helpful assistant. ""Make sure to use the `search_news` tool for searching keyword related news.",),("placeholder", "{chat_history}"),("human", "{input}"),("placeholder", "{agent_scratchpad}"),]
)
創建代理
使用 create_tool_calling_agent
函數定義一個代理。
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent# Creating LLM
llm = ChatOpenAI(base_url='http://localhost:5551/v1',api_key='EMPTY',model_name='Qwen2.5-3B-Instruct',temperature=0.2,
)# Creating Agent
agent = create_tool_calling_agent(llm, tools, prompt)
AgentExecutor
AgentExecutor
是一個用于管理使用工具的代理的類。
關鍵屬性
agent
:負責創建計劃并在執行循環的每個步驟中確定行動的底層代理。tools
:包含代理被授權使用的所有有效工具的列表。return_intermediate_steps
:布爾標志,決定是否返回代理在執行過程中所采取的中間步驟以及最終輸出。max_iterations
:代理在執行循環終止之前可以采取的最大步驟數。max_execution_time
:執行循環允許運行的最長時間。early_stopping_method
:定義當代理未返回AgentFinish
時如何處理的方式。(“force” 或 “generate”)"force"
:返回一個字符串,表示執行循環由于達到時間或迭代限制而被停止。"generate"
:調用代理的 LLM 鏈一次,根據之前的步驟生成最終答案。
handle_parsing_errors
:指定如何處理解析錯誤。(可以設置為True
、False
,或提供自定義錯誤處理函數。)trim_intermediate_steps
:修剪中間步驟的方法。(可以設置為-1
以保留所有步驟,或提供自定義修剪函數。)
關鍵方法
invoke
:執行代理。stream
:流式傳輸達到最終輸出所需的步驟。
關鍵特性
- 工具驗證:確保工具與代理兼容。
- 執行控制:設置最大迭代次數和執行時間限制來管理代理行為。
- 錯誤處理:提供多種處理輸出解析錯誤的選項。
- 中間步驟管理:允許修剪中間步驟或返回調試選項。
- 異步支持:支持異步執行和結果的流式傳輸。
優化建議
- 設置適當的
max_iterations
和max_execution_time
值來管理執行時間。 - 使用
trim_intermediate_steps
來優化內存使用。 - 對于復雜任務,使用
stream
方法來逐步監控結果。
from langchain.agents import AgentExecutor# Create AgentExecutor
agent_executor = AgentExecutor(agent=agent,tools=tools,verbose=True,max_iterations=10,max_execution_time=10,handle_parsing_errors=True,
)# Run AgentExecutor
result = agent_executor.invoke({"input": "Search news about AI Agent in 2025."})print("Agent execution result:")
print(result["output"])
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m
Invoking: `search_news` with `{'query': 'AI Agent 2025'}`[0m[36;1m[1;3m[{'url': 'https://hai.stanford.edu/news/predictions-ai-2025-collaborative-agents-ai-skepticism-and-new-risks', 'content': 'According to leading experts from Stanford Institute for Human-Centered AI, one major trend is the rise of collaborative AI systems where multiple specialized agents work together, with humans providing high-level guidance. I expect to see more focus on multimodal AI models in education, including in processing speech and images. AI Agents Work Together In 2025, we will see a significant shift from relying on individual AI models to using systems where multiple AI agents of diverse expertise work together. As an example, we recently introduced the\xa0Virtual Lab, where a professor AI agent leads a team of AI scientist agents (e.g., AI chemist, AI biologist) to tackle challenging, open-ended research, with a human researcher providing high-level feedback. We will experience an emerging paradigm of research around how humans work together with AI agents.'}, {'url': 'https://www.forbes.com/sites/lutzfinger/2025/01/05/ai-agents-in-2025-what-enterprise-leaders-need-to-know/', 'content': 'AI Agents In 2025: What Enterprise Leaders Need To Know AI Agents In 2025: What Enterprise Leaders Need To Know AI Agents for the Enterprise will be the focus of 2025 To see what AI agents can do in 2025, let’s consider a simple example: an email-answering tool. Let’s improve our tool by building AI agents within a workflow. The Workflow of AI Agents: More Than Generative AI AI models can be connected or "chained" to build workflows where the output of one model becomes the input for the next. AI Agent Workflows: Input - Orchestration - Control - Actions - Synthesizing 2025 - AI Agents for the Enterprise Follow me here on Forbes or on LinkedIn for more of my 2025 AI predictions.'}, {'url': 'https://www.godofprompt.ai/blog/ai-agents-you-cant-miss', 'content': 'Explore 10+ AI agents that are reshaping industries in 2025. From ChatGPT to DeepSeek-R1, discover how AI is becoming more intelligent, efficient, and essential for businesses and individuals alike.'}][0m[32;1m[1;3mHere are some relevant news articles about AI Agents in 2025:1. [According to leading experts from Stanford Institute for Human-Centered AI, one major trend is the rise of collaborative AI systems where multiple specialized agents work together, with humans providing high-level guidance.](https://hai.stanford.edu/news/predictions-ai-2025-collaborative-agents-ai-skepticism-and-new-risks)2. [AI Agents In 2025: What Enterprise Leaders Need To Know](https://www.forbes.com/sites/lutzfinger/2025/01/05/ai-agents-in-2025-what-enterprise-leaders-need-to-know/) - This article discusses AI Agents for the Enterprise, focusing on AI models being connected or "chained" to build workflows where the output of one model becomes the input for the next. It also mentions AI Agent Workflows: Input - Orchestration - Control - Actions - Synthesizing.3. [Explore 10+ AI agents that are reshaping industries in 2025. From ChatGPT to DeepSeek-R1, discover how AI is becoming more intelligent, efficient, and essential for businesses and individuals alike.](https://www.godofprompt.ai/blog/ai-agents-you-cant-miss)These articles provide insights into the future of AI Agents, including their collaborative nature, their potential impact on enterprises, and the development of AI Agent Workflows.[0m[1m> Finished chain.[0m
Agent execution result:
Here are some relevant news articles about AI Agents in 2025:1. [According to leading experts from Stanford Institute for Human-Centered AI, one major trend is the rise of collaborative AI systems where multiple specialized agents work together, with humans providing high-level guidance.](https://hai.stanford.edu/news/predictions-ai-2025-collaborative-agents-ai-skepticism-and-new-risks)2. [AI Agents In 2025: What Enterprise Leaders Need To Know](https://www.forbes.com/sites/lutzfinger/2025/01/05/ai-agents-in-2025-what-enterprise-leaders-need-to-know/) - This article discusses AI Agents for the Enterprise, focusing on AI models being connected or "chained" to build workflows where the output of one model becomes the input for the next. It also mentions AI Agent Workflows: Input - Orchestration - Control - Actions - Synthesizing.3. [Explore 10+ AI agents that are reshaping industries in 2025. From ChatGPT to DeepSeek-R1, discover how AI is becoming more intelligent, efficient, and essential for businesses and individuals alike.](https://www.godofprompt.ai/blog/ai-agents-you-cant-miss)These articles provide insights into the future of AI Agents, including their collaborative nature, their potential impact on enterprises, and the development of AI Agent Workflows.
使用 Stream 輸出檢查逐步結果
我們將使用 AgentExecutor
的 stream()
方法來流式傳輸代理的中間步驟。
stream()
的輸出在 (Action, Observation) 對之間交替,最終如果目標達成,將以代理的答案結束。
流程如下所示:
- Action 輸出
- Observation 輸出
- Action 輸出
- Observation 輸出
…(繼續直到目標達成)…
然后,代理將在目標達成后得出最終答案。
以下表格總結了你將在輸出中遇到的內容:
輸出 | 描述 |
---|---|
Action | actions :表示 AgentAction 或其子類。messages :與動作調用對應的聊天消息。 |
Observation | steps :記錄代理的工作,包括當前的動作和其觀察結果。messages :包含函數調用結果(即觀察結果)的聊天消息。 |
Final Answer | output :表示 AgentFinish 信號。messages :包含最終輸出的聊天消息。 |
from langchain.agents import AgentExecutor# Create AgentExecutor
agent_executor = AgentExecutor(agent=agent,tools=tools,verbose=False,handle_parsing_errors=True,
)# Run in streaming mode
result = agent_executor.stream({"input": "Search news about AI Agent in 2025."})for step in result:# Print intermediate stepsprint(step)print("===" * 20)
{'actions': [ToolAgentAction(tool='search_news', tool_input={'query': 'AI Agent 2025'}, log="\nInvoking: `search_news` with `{'query': 'AI Agent 2025'}`\n\n\n", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'function': {'arguments': '{"query": "AI Agent 2025"}', 'name': 'search_news'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'Qwen2.5-3B-Instruct'}, id='run-a877dfea-5a20-4970-96da-0f3483298f7e', tool_calls=[{'name': 'search_news', 'args': {'query': 'AI Agent 2025'}, 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'type': 'tool_call'}], tool_call_chunks=[{'name': 'search_news', 'args': '{"query": "AI Agent 2025"}', 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'index': 0, 'type': 'tool_call_chunk'}])], tool_call_id='chatcmpl-tool-cf8525019f5847519566061e0e6647c6')], 'messages': [AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'function': {'arguments': '{"query": "AI Agent 2025"}', 'name': 'search_news'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'Qwen2.5-3B-Instruct'}, id='run-a877dfea-5a20-4970-96da-0f3483298f7e', tool_calls=[{'name': 'search_news', 'args': {'query': 'AI Agent 2025'}, 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'type': 'tool_call'}], tool_call_chunks=[{'name': 'search_news', 'args': '{"query": "AI Agent 2025"}', 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'index': 0, 'type': 'tool_call_chunk'}])]}
============================================================
{'steps': [AgentStep(action=ToolAgentAction(tool='search_news', tool_input={'query': 'AI Agent 2025'}, log="\nInvoking: `search_news` with `{'query': 'AI Agent 2025'}`\n\n\n", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'function': {'arguments': '{"query": "AI Agent 2025"}', 'name': 'search_news'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'Qwen2.5-3B-Instruct'}, id='run-a877dfea-5a20-4970-96da-0f3483298f7e', tool_calls=[{'name': 'search_news', 'args': {'query': 'AI Agent 2025'}, 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'type': 'tool_call'}], tool_call_chunks=[{'name': 'search_news', 'args': '{"query": "AI Agent 2025"}', 'id': 'chatcmpl-tool-cf8525019f5847519566061e0e6647c6', 'index': 0, 'type': 'tool_call_chunk'}])], tool_call_id='chatcmpl-tool-cf8525019f5847519566061e0e6647c6'), observation=[{'url': 'https://hai.stanford.edu/news/predictions-ai-2025-collaborative-agents-ai-skepticism-and-new-risks', 'content': 'According to leading experts from Stanford Institute for Human-Centered AI, one major trend is the rise of collaborative AI systems where multiple specialized agents work together, with humans providing high-level guidance. I expect to see more focus on multimodal AI models in education, including in processing speech and images. AI Agents Work Together In 2025, we will see a significant shift from relying on individual AI models to using systems where multiple AI agents of diverse expertise work together. As an example, we recently introduced the\xa0Virtual Lab, where a professor AI agent leads a team of AI scientist agents (e.g., AI chemist, AI biologist) to tackle challenging, open-ended research, with a human researcher providing high-level feedback. We will experience an emerging paradigm of research around how humans work together with AI agents.'}, {'url': 'https://www.forbes.com/sites/lutzfinger/2025/01/05/ai-agents-in-2025-what-enterprise-leaders-need-to-know/', 'content': 'AI Agents In 2025: What Enterprise Leaders Need To Know AI Agents In 2025: What Enterprise Leaders Need To Know AI Agents for the Enterprise will be the focus of 2025 To see what AI agents can do in 2025, let’s consider a simple example: an email-answering tool. Let’s improve our tool by building AI agents within a workflow. The Workflow of AI Agents: More Than Generative AI AI models can be connected or "chained" to build workflows where the output of one model becomes the input for the next. AI Agent Workflows: Input - Orchestration - Control - Actions - Synthesizing 2025 - AI Agents for the Enterprise Follow me here on Forbes or on LinkedIn for more of my 2025 AI predictions.'}, {'url': 'https://www.godofprompt.ai/blog/ai-agents-you-cant-miss', 'content': 'Explore 10+ AI agents that are reshaping industries in 2025. From ChatGPT to DeepSeek-R1, discover how AI is becoming more intelligent, efficient, and essential for businesses and individuals alike.'}])], 'messages': [FunctionMessage(content='[{"url": "https://hai.stanford.edu/news/predictions-ai-2025-collaborative-agents-ai-skepticism-and-new-risks", "content": "According to leading experts from Stanford Institute for Human-Centered AI, one major trend is the rise of collaborative AI systems where multiple specialized agents work together, with humans providing high-level guidance. I expect to see more focus on multimodal AI models in education, including in processing speech and images. AI Agents Work Together In 2025, we will see a significant shift from relying on individual AI models to using systems where multiple AI agents of diverse expertise work together. As an example, we recently introduced the\xa0Virtual Lab, where a professor AI agent leads a team of AI scientist agents (e.g., AI chemist, AI biologist) to tackle challenging, open-ended research, with a human researcher providing high-level feedback. We will experience an emerging paradigm of research around how humans work together with AI agents."}, {"url": "https://www.forbes.com/sites/lutzfinger/2025/01/05/ai-agents-in-2025-what-enterprise-leaders-need-to-know/", "content": "AI Agents In 2025: What Enterprise Leaders Need To Know AI Agents In 2025: What Enterprise Leaders Need To Know AI Agents for the Enterprise will be the focus of 2025 To see what AI agents can do in 2025, let’s consider a simple example: an email-answering tool. Let’s improve our tool by building AI agents within a workflow. The Workflow of AI Agents: More Than Generative AI AI models can be connected or \\"chained\\" to build workflows where the output of one model becomes the input for the next. AI Agent Workflows: Input - Orchestration - Control - Actions - Synthesizing 2025 - AI Agents for the Enterprise Follow me here on Forbes or on LinkedIn for more of my 2025 AI predictions."}, {"url": "https://www.godofprompt.ai/blog/ai-agents-you-cant-miss", "content": "Explore 10+ AI agents that are reshaping industries in 2025. From ChatGPT to DeepSeek-R1, discover how AI is becoming more intelligent, efficient, and essential for businesses and individuals alike."}]', additional_kwargs={}, response_metadata={}, name='search_news')]}
============================================================
{'output': 'Here are some relevant news articles about AI Agents in 2025:\n\n1. [According to leading experts from Stanford Institute for Human-Centered AI, one major trend is the rise of collaborative AI systems where multiple specialized agents work together, with humans providing high-level guidance.](https://hai.stanford.edu/news/predictions-ai-2025-collaborative-agents-ai-skepticism-and-new-risks)\n\n2. [AI Agents In 2025: What Enterprise Leaders Need To Know](https://www.forbes.com/sites/lutzfinger/2025/01/05/ai-agents-in-2025-what-enterprise-leaders-need-to-know/) - This article discusses AI Agents for the Enterprise, focusing on how AI models can be connected or "chained" to build workflows where the output of one model becomes the input for the next.\n\n3. [Explore 10+ AI agents that are reshaping industries in 2025. From ChatGPT to DeepSeek-R1, discover how AI is becoming more intelligent, efficient, and essential for businesses and individuals alike.](https://www.godofprompt.ai/blog/ai-agents-you-cant-miss)\n\nThese articles provide insights into the expected trends and developments in AI Agents for both research and enterprise applications in the year 2025.', 'messages': [AIMessage(content='Here are some relevant news articles about AI Agents in 2025:\n\n1. [According to leading experts from Stanford Institute for Human-Centered AI, one major trend is the rise of collaborative AI systems where multiple specialized agents work together, with humans providing high-level guidance.](https://hai.stanford.edu/news/predictions-ai-2025-collaborative-agents-ai-skepticism-and-new-risks)\n\n2. [AI Agents In 2025: What Enterprise Leaders Need To Know](https://www.forbes.com/sites/lutzfinger/2025/01/05/ai-agents-in-2025-what-enterprise-leaders-need-to-know/) - This article discusses AI Agents for the Enterprise, focusing on how AI models can be connected or "chained" to build workflows where the output of one model becomes the input for the next.\n\n3. [Explore 10+ AI agents that are reshaping industries in 2025. From ChatGPT to DeepSeek-R1, discover how AI is becoming more intelligent, efficient, and essential for businesses and individuals alike.](https://www.godofprompt.ai/blog/ai-agents-you-cant-miss)\n\nThese articles provide insights into the expected trends and developments in AI Agents for both research and enterprise applications in the year 2025.', additional_kwargs={}, response_metadata={})]}
============================================================
使用用戶定義的函數自定義中間步驟輸出
你可以定義以下 3 個函數來自定義中間步驟的輸出:
tool_callback
:此函數處理工具調用生成的輸出。observation_callback
:此函數處理觀察數據輸出。result_callback
:此函數允許你處理最終答案的輸出。
from typing import Dict, Any# Create AgentStreamParser class
class AgentStreamParser:def __init__(self):passdef tool_callback(self, tool: Dict[str, Any]) -> None:print("\n=== Tool Called ===")print(f"Tool: {tool.get('tool')}")print(f"Input: {tool.get('tool_input')}")print("==================\n")def observation_callback(self, step: Dict[str, Any]) -> None:print("\n=== Observation ===")observation_data = step["steps"][0].observationprint(f"Observation: {observation_data}")print("===================\n")def result_callback(self, result: str) -> None:print("\n=== Final Answer ===")print(result)print("====================\n")def process_agent_steps(self, step: Dict[str, Any]) -> None:if "actions" in step:for action in step["actions"]:self.tool_callback({"tool": action.tool, "tool_input": action.tool_input})elif "output" in step:self.result_callback(step["output"])else:self.observation_callback(step)# Create AgentStreamParser instance
agent_stream_parser = AgentStreamParser()
# Run in streaming mode
result = agent_executor.stream({"input": "Generate a array from 0 to 1 with the stride of 0.1 using numpy."})
# result = agent_executor.stream({"input": "Search news about AI Agent in 2025."})for step in result:agent_stream_parser.process_agent_steps(step)
=== Tool Called ===
Tool: numpy_array_generator
Input: {'start': 1, 'step': 0.1}
===================== Observation ===
Observation: numpy_array_generator is not a valid tool, try one of [search_news, python_repl_tool].
====================== Tool Called ===
Tool: python_repl_tool
Input: {'code': 'import numpy as np\nnp.arange(0, 1, 0.1)'}
===================== Observation ===
Observation:
====================== Tool Called ===
Tool: python_repl_tool
Input: {'code': 'import numpy as np\nnp.arange(0, 1, 0.1)'}
===================== Observation ===
Observation:
====================== Tool Called ===
Tool: python_repl_tool
Input: {'code': 'import numpy as np\nnp.arange(0, 1, 0.1)'}
===================== Observation ===
Observation:
====================== Final Answer ===
The numpy array from 0 to 1 with a stride of 0.1 has been successfully generated. Here it is:```
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
```Is there anything else I can assist you with?
====================
與之前的對話歷史進行代理通信
為了記住過去的對話,你可以將 AgentExecutor
包裝在 RunnableWithMessageHistory
中。
有關 RunnableWithMessageHistory
的更多細節,請參閱以下鏈接。
參考
- LangChain Python API Reference > langchain: 0.3.14 > core > runnables > langchain_core.runnables.history > RunnableWithMessageHistory
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory# Create a dictionary to store session_id
store = {}# Function to get session history based on session_id
def get_session_history(session_ids):if session_ids not in store: # If session_id is not in store# Create a new ChatMessageHistory object and store it in storestore[session_ids] = ChatMessageHistory()return store[session_ids] # Return session history for the corresponding session_id# Create an agent with chat message history
agent_with_chat_history = RunnableWithMessageHistory(agent_executor,# Chat session_idget_session_history,# The key for the question input in the prompt: "input"input_messages_key="input",# The key for the message input in the prompt: "chat_history"history_messages_key="chat_history",
)
# Request streaming output for the query
response = agent_with_chat_history.stream({"input": "Hello! My name is Teddy!"},# Set session_idconfig={"configurable": {"session_id": "abc123"}},
)# Check the output
for step in response:agent_stream_parser.process_agent_steps(step)
=== Final Answer ===
Hello Teddy! It's nice to meet you. How can I assist you today? Do you have any specific questions or topics you'd like to explore?
====================
# Request streaming output for the query
response = agent_with_chat_history.stream({"input": "What is my name?"},# Set session_idconfig={"configurable": {"session_id": "abc123"}},
)# Check the output
for step in response:agent_stream_parser.process_agent_steps(step)
=== Final Answer ===
It seems like you've already provided your name as Teddy. If you have any other questions or need information about something else, feel free to ask!
====================
# Request streaming output for the query
response = agent_with_chat_history.stream({"input": "My email address is teddy@teddynote.com. The company name is TeddyNote Co., Ltd."},# Set session_idconfig={"configurable": {"session_id": "abc123"}},
)# Check the output
for step in response:agent_stream_parser.process_agent_steps(step)
=== Tool Called ===
Tool: search_news
Input: {'query': 'TeddyNote Co., Ltd.'}
===================== Observation ===
Observation: [{'url': 'https://www.youtube.com/@teddynote', 'content': '??? ??, ????, ???, LLM ? ?? ??? ????. ????? ??? ??? ???? 🙇\u200d♂?🔥 "????? RAG ????" ???'}, {'url': 'https://github.com/teddynote', 'content': 'By company size. Enterprises Small and medium teams Startups Nonprofits By use case. DevSecOps DevOps CI/CD View all use cases By industry ... teddynote.github.io teddynote.github.io Public. Forked from mmistakes/minimal-mistakes. 📐 Jekyll theme for building a personal site, blog, project documentation, or portfolio.'}, {'url': 'https://github.com/teddylee777', 'content': 'Jupyter Notebook\n1\n4\nConv2d and MaxPool2d Calculator for PyTorch\nPython\n18\n1\nStreamlit ???? 😁\nJupyter Notebook\n13\n12\n?? ?? ?? ???\nJupyter Notebook\n14\n12\n586\ncontributions\nin the last year\nContribution activity\nJanuary 2024\nSeeing something unexpected? Teddy Lee\nteddylee777\nAchievements\nAchievements\nHighlights\nBlock or report teddylee777\nPrevent this user from interacting with your repositories and sending you notifications.\n Jupyter Notebook\n58\n16\nForked from lovedlim/tensorflow\n???? ?? ?? ?????.\n Samsung Electronics\n???? Blog\n???? YouTube\n@teddynote\nLinkedIn\n💻 (This repository is intented for helping whom are interested in machine learning study)\nJupyter Notebook\n2.3k\n789\n????/???(PyTorch, TensorFlow) ?? ?????.'}]
====================== Final Answer ===
Here are some recent news and information related to your company, TeddyNote Co., Ltd.:1. **TeddyNote Co., Ltd. on YouTube**: They have a YouTube channel where they discuss topics related to data analysis, machine learning, deep learning, and Large Language Models (LLM). They seem to have a focus on development rather than research. You can check out their channel [here](https://www.youtube.com/@teddynote).2. **TeddyNote Co., Ltd. on GitHub**: - **Company Size**: They cater to small and medium-sized teams, startups, and nonprofits.- **Use Cases**: They support DevSecOps and DevOps, CI/CD.- **Public Repository**: You can view their public repository [here](https://github.com/teddynote).- **Personal Site**: They have a personal site and blog available at [teddynote.github.io](https://teddynote.github.io/).- **Contributions**: They have contributed to various projects, including a Jupyter Notebook for a Conv2d and MaxPool2d Calculator for PyTorch, a Streamlit tutorial, and a stock price pattern analysis tool. You can see their contributions [here](https://github.com/teddylee777).3. **TeddyLee777 on GitHub**: This is likely a personal GitHub account associated with TeddyNote Co., Ltd. They have contributed to various projects, including a TensorFlow book example repository and a Docker image for machine learning study.If you need more detailed information or have any specific questions about these resources, feel free to ask!
====================
# Request streaming output for the query
response = agent_with_chat_history.stream({"input": "Search the latest news and write it as the body of the email. ""The recipient is `Ms. Sally` and the sender is my personal information.""Write in a polite tone, and include appropriate greetings and closings at the beginning and end of the email."},# Set session_idconfig={"configurable": {"session_id": "abc123"}},
)# Check the output
for step in response:agent_stream_parser.process_agent_steps(step)
=== Tool Called ===
Tool: search_news
Input: {'query': 'TeddyNote Co., Ltd latest news'}
===================== Tool Called ===
Tool: python_repl_tool
Input: {'code': 'import email; from email.mime.multipart import MIMEMultipart; from email.mime.text import MIMEText; msg = MIMEMultipart(); msg[\'From\'] = \'teddy@teddynote.com\'; msg[\'To\'] = \'sally@example.com\'; msg[\'Subject\'] = \'Latest News from TeddyNote Co., Ltd\'; body = """Here are the latest news and updates from TeddyNote Co., Ltd.:\n\n1. **TeddyNote Co., Ltd. on YouTube**: They have a YouTube channel where they discuss topics related to data analysis, machine learning, deep learning, and Large Language Models (LLM). They seem to have a focus on development rather than research. You can check out their channel [here](https://www.youtube.com/@teddynote).\n\n2. **TeddyNote Co., Ltd. on GitHub**: \n - **Company Size**: They cater to small and medium-sized teams, startups, and nonprofits.\n - **Use Cases**: They support DevSecOps and DevOps, CI/CD.\n - **Public Repository**: You can view their public repository [here](https://github.com/teddynote).\n - **Personal Site**: They have a personal site and blog available at [teddynote.github.io](https://teddynote.github.io/).\n - **Contributions**: They have contributed to various projects, including a Jupyter Notebook for a Conv2d and MaxPool2d Calculator for PyTorch, a Streamlit tutorial, and a stock price pattern analysis tool. You can see their contributions [here](https://github.com/teddylee777).\n\n3. **TeddyLee777 on GitHub**: This is likely a personal GitHub account associated with TeddyNote Co., Ltd. They have contributed to various projects, including a TensorFlow book example repository and a Docker image for machine learning study.\n\nIf you need more detailed information or have any specific questions about these resources, feel free to ask!"""; msg.attach(MIMEText(body, \'plain\')); return msg.as_string()'}
===================== Tool Called ===
Tool: send_email
Input: {'to': 'sally@example.com', 'subject': 'Latest News from TeddyNote Co., Ltd', 'body': '...', 'sender': 'teddy@teddynote.com'}
===================== Tool Called ===
Tool: email_status
Input: {'email_id': '...'}
===================== Observation ===
Observation: [{'url': 'https://www.threads.net/@teddynote', 'content': '60 Followers ? 44 Threads ? ??? & AI. See the latest conversations with @teddynote.'}, {'url': 'https://github.com/teddylee777', 'content': 'Jupyter Notebook\n1\n4\nConv2d and MaxPool2d Calculator for PyTorch\nPython\n18\n1\nStreamlit ???? 😁\nJupyter Notebook\n13\n12\n?? ?? ?? ???\nJupyter Notebook\n14\n12\n586\ncontributions\nin the last year\nContribution activity\nJanuary 2024\nSeeing something unexpected? Teddy Lee\nteddylee777\nAchievements\nAchievements\nHighlights\nBlock or report teddylee777\nPrevent this user from interacting with your repositories and sending you notifications.\n Jupyter Notebook\n58\n16\nForked from lovedlim/tensorflow\n???? ?? ?? ?????.\n Samsung Electronics\n???? Blog\n???? YouTube\n@teddynote\nLinkedIn\n💻 (This repository is intented for helping whom are interested in machine learning study)\nJupyter Notebook\n2.3k\n789\n????/???(PyTorch, TensorFlow) ?? ?????.'}, {'url': 'https://langchain-opentutorial.gitbook.io/langchain-opentutorial/15-agent/03-agent', 'content': 'Best regards, Teddy teddy@teddynote.com TeddyNote Co., Ltd. --- Feel free to modify any part of the email as you see fit! >>>>>'}]
====================== Observation ===
Observation: SyntaxError("'return' outside function", ('<string>', 14, 152, None, 14, 174))
====================== Observation ===
Observation: send_email is not a valid tool, try one of [search_news, python_repl_tool].
====================== Observation ===
Observation: email_status is not a valid tool, try one of [search_news, python_repl_tool].
====================== Final Answer ===
It seems there was an issue with the previous steps. Let's proceed with creating the email body using the news and information we gathered. Here is the body of the email:---Hello Ms. Sally,I hope this email finds you well. I wanted to share some recent news and updates from TeddyNote Co., Ltd.:1. **TeddyNote Co., Ltd. on YouTube**: They have a YouTube channel where they discuss topics related to data analysis, machine learning, deep learning, and Large Language Models (LLM). They seem to have a focus on development rather than research. You can check out their channel [here](https://www.youtube.com/@teddynote).2. **TeddyNote Co., Ltd. on GitHub**:- **Company Size**: They cater to small and medium-sized teams, startups, and nonprofits.- **Use Cases**: They support DevSecOps and DevOps, CI/CD.- **Public Repository**: You can view their public repository [here](https://github.com/teddynote).- **Personal Site**: They have a personal site and blog available at [teddynote.github.io](https://teddynote.github.io/).- **Contributions**: They have contributed to various projects, including a Jupyter Notebook for a Conv2d and MaxPool2d Calculator for PyTorch, a Streamlit tutorial, and a stock price pattern analysis tool. You can see their contributions [here](https://github.com/teddylee777).3. **TeddyLee777 on GitHub**: This is likely a personal GitHub account associated with TeddyNote Co., Ltd. They have contributed to various projects, including a TensorFlow book example repository and a Docker image for machine learning study.If you need more detailed information or have any specific questions about these resources, feel free to ask!Best regards,
Teddy
teddy@teddynote.com
TeddyNote Co., Ltd.---Please let me know if you need any further assistance or if there are any specific details you would like to include.
====================
Agentic RAG
Agentic RAG 擴展了傳統的 RAG(檢索增強生成)系統,通過結合基于代理的方法,實現更復雜的信息檢索和響應生成。該系統不僅僅局限于簡單的文檔檢索和響應生成,還允許代理利用各種工具進行更智能的信息處理。這些工具包括用于訪問最新信息的 Tavily Search
、執行 Python 代碼的能力以及自定義功能實現,所有這些都集成在 LangChain
框架中,為信息處理和生成任務提供全面的解決方案。
本教程演示了如何構建一個文檔檢索系統,使用 FAISS DB
來有效地處理和搜索 PDF 文檔。以軟件政策研究所的 AI Brief 為示例文檔,我們將探索如何將基于 Web 的文檔加載器、文本拆分器、向量存儲和 OpenAI
嵌入結合起來,創建一個實際的 Agentic RAG 系統。該實現展示了如何將 Retriever
工具與各種 LangChain
組件有效結合,創建一個強大的文檔搜索和響應生成管道。
創建工具
from langchain_community.tools.tavily_search import TavilySearchResults# Create a search tool instance that returns up to 6 results
search = TavilySearchResults(k=6)
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_community.embeddings import DashScopeEmbeddings
from langchain.document_loaders import PyPDFLoader
from langchain.tools.retriever import create_retriever_tool# Load and process the PDF
loader = PyPDFLoader("data/What-is-AI.pdf")# Create text splitter
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)# Split the document
split_docs = loader.load_and_split(text_splitter)# Create vector store
embeddings = DashScopeEmbeddings(model="text-embedding-v2",
)
vector = FAISS.from_documents(split_docs, embeddings)# Create retriever
retriever = vector.as_retriever()# Create retriever tool
retriever_tool = create_retriever_tool(retriever,name="pdf_search",description="use this tool to search information from the PDF document",
)
創建代理
tools = [search, retriever_tool]
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain.agents import create_tool_calling_agent, AgentExecutor# Initialize LLM
llm = ChatOpenAI(base_url='http://localhost:5551/v1',api_key='EMPTY',model_name='Qwen2.5-3B-Instruct',temperature=0.2,
)# Define prompt template
prompt = ChatPromptTemplate.from_messages([("system","You are a helpful assistant. ""Make sure to use the `pdf_search` tool for searching information from the PDF document. ""If you can't find the information from the PDF document, use the `search` tool for searching information from the web.",),("placeholder", "{chat_history}"),("human", "{input}"),("placeholder", "{agent_scratchpad}"),]
)# Create agent
agent = create_tool_calling_agent(llm, tools, prompt)# Create agent executor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
對話歷史
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory# Create a store for session histories
store = {}def get_session_history(session_ids):if session_ids not in store:store[session_ids] = ChatMessageHistory()return store[session_ids]# Create agent with chat history
agent_with_chat_history = RunnableWithMessageHistory(agent_executor,get_session_history,input_messages_key="input",history_messages_key="chat_history",
)
def process_response(response):"""Process and display streaming response from the agent.Args:response: Agent's streaming response iterator"""for chunk in response:if chunk.get("output"):print(chunk["output"])elif chunk.get("actions"):for action in chunk["actions"]:print(f"\nTool Used: {action.tool}")print(f"Tool Input: {action.tool_input}")if action.log:print(f"Tool Log: {action.log}")
# Example 1: Searching in PDF
response = agent_with_chat_history.stream({"input": "What information can you find about Samsung's AI model in the document?"},config={"configurable": {"session_id": "tutorial_session_1"}},
)
process_response(response)
Tool Used: pdf_search
Tool Input: {'query': 'Samsung AI model'}
Tool Log:
Invoking: `pdf_search` with `{'query': 'Samsung AI model'}`Tool Used: search
Tool Input: {'query': 'Samsung AI model'}
Tool Log:
Invoking: `search` with `{'query': 'Samsung AI model'}`
responded: The provided text does not contain specific information about Samsung's AI model. It seems to be a general introduction to AI, its components, and some real-world applications. To find information about Samsung's AI model, we might need to look for more detailed or specific documents or articles. Let me try searching the web for more relevant information.Tool Used: tavily_search_results_json
Tool Input: {'query': 'Samsung AI model'}
Tool Log:
Invoking: `tavily_search_results_json` with `{'query': 'Samsung AI model'}`
responded: It appears that the 'search' tool is not available. Let's try using the 'tavily_search_results_json' tool to search the web for information about Samsung's AI model.The search results provide information about Samsung's AI model, specifically Samsung Gauss 2, which is described as a new GenAI model that improves Galaxy AI performance and efficiency. Here are some key points from the search results:1. **Samsung Gauss 2**: This model supports 9 to 14 human languages and several programming languages. Samsung claims that Balanced and Supreme models match or beat other AI models on tasks.2. **Galaxy S25 Series**: The Galaxy S25 series features advanced, efficient AI image processing with ProScaler11, achieving a 40% improvement in display image scaling quality. It also incorporates custom technology with Samsung’s mobile Digital Natural Image engine (mDNIe) embedded within the processor using Galaxy IP to enable greater display power efficiency.3. **Galaxy AI**: Samsung's Galaxy AI is described as a set of generative AI tools that brings features like live translation, generative photo editing, and more. The AI features are available on newer Samsung phones, but Samsung is making efforts to support these features on older models as well.4. **Samsung Gauss 2 on Device**: Samsung Gauss 2 is an on-device AI model, which means it processes data locally on the device rather than sending it to a cloud server.These results suggest that Samsung Gauss 2 is a significant advancement in their AI capabilities, particularly in improving Galaxy AI performance and efficiency. If you need more detailed information, you might want to look into the specific features and capabilities of Samsung Gauss 2 in more detail.
# Example 1: Searching in PDF
response = agent_with_chat_history.stream({"input": "List the devices using ai in your previous responese."},config={"configurable": {"session_id": "tutorial_session_1"}},
)
process_response(response)
Tool Used: pdf_search
Tool Input: {'query': 'devices using ai'}
Tool Log:
Invoking: `pdf_search` with `{'query': 'devices using ai'}`Based on the information provided in the document, the devices mentioned that use AI are:1. **Smartphones**: The document mentions that AI is available on newer Samsung phones, indicating that smartphones are one of the devices using AI.
2. **Galaxy S25 Series**: The document describes the Galaxy S25 series as featuring advanced, efficient AI image processing, which implies that this device uses AI.
3. **Galaxy AI**: The document states that Galaxy AI is a set of generative AI tools available on newer Samsung phones, suggesting that the Galaxy S25 series and possibly other newer Samsung devices use AI.
4. **Smart City Initiative**: The document provides an example of a government initiative using AI for real-time video analytics, advanced forensic investigation capabilities, and comprehensive operational intelligence. While it doesn't specify which devices are used, it implies that AI is used across various devices in this context.Therefore, the devices using AI mentioned in the document are:
- Smartphones (Samsung)
- Galaxy S25 Series
- Galaxy AI (presumably available on newer Samsung phones)