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."} )
from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda
python_tool = PythonREPLTool() # A function that executes Python code, outputs intermediate steps, and returns the tool execution results. defprint_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."))
import feedparser from urllib.parse import quote from typing importList, Dict, Optional
classGoogleNews: """ 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. """ ifnot 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 result
defsearch_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) returnself._collect_news(news_list)
defsearch_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) returnself._collect_news(news_list)
'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 装饰器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from langchain.tools import tool from typing importList, Dict
# Create a tool for searching news by keyword @tool defsearch_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)
defexecute_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 results for tool_call_result in tool_call_results: # Tool name (function name) tool_name = tool_call_result["type"] # Tool arguments tool_args = tool_call_result["args"]
# Find the tool that matches the name and execute it # Use the next() function to find the first matching tool matching_tool = next((tool for tool in tools if tool.name == tool_name), None) if matching_tool: # Execute the tool result = 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:执行工具调用结果的函数。
[流程摘要]
将工具绑定到模型。
解析工具调用的结果。
执行工具调用的结果。
1 2 3 4
from langchain_core.output_parsers.openai_tools import JsonOutputToolsParser
from langchain.tools import tool from typing importList, Dict, Annotated from langchain_community.tools import TavilySearchResults from langchain_experimental.utilities import PythonREPL
# Creating tool for executing python code @tool defpython_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 result
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'}}
1
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]'}]
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 函数定义一个代理。
1 2 3 4 5 6 7 8 9 10 11 12 13
from langchain_openai import ChatOpenAI from langchain.agents import create_tool_calling_agent
[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.
# Run in streaming mode result = agent_executor.stream({"input": "Search news about AI Agent in 2025."})
for step in result: # Print intermediate steps print(step) print("===" * 20)
1 2 3 4 5 6
{'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={})]} ============================================================
# 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)
=== 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? ====================
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 defget_session_history(session_ids): if session_ids notin store: # If session_id is not in store # Create a new ChatMessageHistory object and store it in store store[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_id get_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", )
1 2 3 4 5 6 7 8 9 10
# Request streaming output for the query response = agent_with_chat_history.stream( {"input": "Hello! My name is Teddy!"}, # Set session_id config={"configurable": {"session_id": "abc123"}}, )
# Check the output for step in response: agent_stream_parser.process_agent_steps(step)
1 2 3
=== 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? ====================
1 2 3 4 5 6 7 8 9 10
# Request streaming output for the query response = agent_with_chat_history.stream( {"input": "What is my name?"}, # Set session_id config={"configurable": {"session_id": "abc123"}}, )
# Check the output for step in response: agent_stream_parser.process_agent_steps(step)
1 2 3
=== 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! ====================
1 2 3 4 5 6 7 8 9 10 11 12
# 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_id config={"configurable": {"session_id": "abc123"}}, )
# Check the output for step in response: agent_stream_parser.process_agent_steps(step)
=== 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! ====================
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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_id config={"configurable": {"session_id": "abc123"}}, )
# Check the output for step in response: agent_stream_parser.process_agent_steps(step)
=== 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()'} ==================
=== 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: 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. ====================
本教程演示了如何构建一个文档检索系统,使用 FAISS DB 来有效地处理和搜索 PDF 文档。以软件政策研究所的 AI Brief 为示例文档,我们将探索如何将基于 Web 的文档加载器、文本拆分器、向量存储和 OpenAI 嵌入结合起来,创建一个实际的 Agentic RAG 系统。该实现展示了如何将 Retriever 工具与各种 LangChain 组件有效结合,创建一个强大的文档搜索和响应生成管道。
创建工具
1 2 3 4
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 retriever tool retriever_tool = create_retriever_tool( retriever, name="pdf_search", description="use this tool to search information from the PDF document", )
from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain.agents import create_tool_calling_agent, AgentExecutor
# 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}"), ] )
from langchain_community.chat_message_histories import ChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory
# Create a store for session histories store = {}
defget_session_history(session_ids): if session_ids notin 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", )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defprocess_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}")
1 2 3 4 5 6 7 8
# 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.
1 2 3 4 5 6 7 8
# 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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)