LangChain-24 Agengts 通过TavilySearch Agent实现检索内容并回答 AgentExecutor转换Search 借助Prompt Tools工具

简介: LangChain-24 Agengts 通过TavilySearch Agent实现检索内容并回答 AgentExecutor转换Search 借助Prompt Tools工具

安装依赖

pip install -qU langchain-core langchain-openai
• 1

Prompt

# 从Hub加载Prompt,自己写加载进来也一样
# SYSTEM
#
# You are a helpful assistant
#
# PLACEHOLDER
#
# chat_history
# HUMAN
#
# {input}
#
# PLACEHOLDER
#

编写代码

from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.tools.retriever import create_retriever_tool
from langchain_openai import ChatOpenAI
from langchain.agents import create_openai_functions_agent
from langchain.agents import AgentExecutor
from langchain import hub


# 这里需要配置KEY 免费
search = TavilySearchResults()
# message1 = search.invoke("what is the weather in SF")
# print(f"message1: {message1}")

loader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(
    chunk_size=1000, chunk_overlap=200
).split_documents(docs)
vector = FAISS.from_documents(documents, OpenAIEmbeddings())
retriever = vector.as_retriever()
result1 = retriever.get_relevant_documents("how to upload a dataset")[0]
print(f"result1: {result1}")

# 转换为工具
retriever_tool = create_retriever_tool(
    retriever,
    "langsmith_search",
    "Search for information about LangSmith. For any questions about LangSmith, you must use this tool!",
)

# 定义工具
tools = [search, retriever_tool]
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)

# 从Hub加载Prompt,自己写加载进来也一样
# SYSTEM
#
# You are a helpful assistant
#
# PLACEHOLDER
#
# chat_history
# HUMAN
#
# {input}
#
# PLACEHOLDER
#
# agent_scratchpad
prompt = hub.pull("hwchase17/openai-functions-agent")
print(f"prompt message: {prompt.messages}")

# 新建Agent
agent = create_openai_functions_agent(llm, tools, prompt)
# Agent执行器
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# 尝试执行
message2 = agent_executor.invoke({"input": "hi!"})
print(f"message2: {message2}")

# 执行任务
message3 = agent_executor.invoke({"input": "how can langsmith help with testing?"})
print(f"message3: {message3}")


运行结果

➜ python3 test24.py
result1: page_content="about LangSmith:User Guide: Learn about the workflows LangSmith supports at each stage of the LLM application lifecycle.Setup: Learn how to create an account, obtain an API key, and configure your environment.Pricing: Learn about the pricing model for LangSmith.Self-Hosting: Learn about self-hosting options for LangSmith.Proxy: Learn about the proxy capabilities of LangSmith.Tracing: Learn about the tracing capabilities of LangSmith.Evaluation: Learn about the evaluation capabilities of LangSmith.Prompt Hub Learn about the Prompt Hub, a prompt management tool built into LangSmith.Additional Resources\u200bLangSmith Cookbook: A collection of tutorials and end-to-end walkthroughs using LangSmith.LangChain Python: Docs for the Python LangChain library.LangChain Python API Reference: documentation to review the core APIs of LangChain.LangChain JS: Docs for the TypeScript LangChain libraryDiscord: Join us on our Discord to discuss all things LangChain!Contact SalesIf you're interested in" metadata={'source': 'https://docs.smith.langchain.com/overview', 'title': 'LangSmith | 🦜️🛠️ LangSmith', 'description': 'Introduction', 'language': 'en'}
prompt message: [SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='You are a helpful assistant')), MessagesPlaceholder(variable_name='chat_history', optional=True), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], template='{input}')), MessagesPlaceholder(variable_name='agent_scratchpad')]


> Entering new AgentExecutor chain...
Hello! How can I assist you today?

> Finished chain.
message2: {'input': 'hi!', 'output': 'Hello! How can I assist you today?'}


> Entering new AgentExecutor chain...
LangSmith is a search engine that can help you find information about testing. You can use it to search for best practices, testing frameworks, testing tools, and other resources related to testing. Additionally, LangSmith can help you find answers to specific questions you may have about testing. For example, you can search for information on how to write effective test cases, how to automate testing, or how to perform load testing. By using LangSmith, you can save time and effort in your testing efforts by quickly finding the information you need.

> Finished chain.
message3: {'input': 'how can langsmith help with testing?', 'output': 'LangSmith is a search engine that can help you find information about testing. You can use it to search for best practices, testing frameworks, testing tools, and other resources related to testing. Additionally, LangSmith can help you find answers to specific questions you may have about testing. For example, you can search for information on how to write effective test cases, how to automate testing, or how to perform load testing. By using LangSmith, you can save time and effort in your testing efforts by quickly finding the information you need.'}


相关实践学习
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
目录
相关文章
|
API 数据库 决策智能
基于百炼平台qwen-max的api 打造一套 检索增强 图谱增强 智能工具调用决策的智能体
本文介绍了一种基于阿里云百炼平台的`qwen-max` API构建的智能体方案,该方案集成了检索增强、图谱增强及智能工具调用决策三大模块,旨在通过结合外部数据源、知识图谱和自动化决策提高智能回答的准确性和丰富度。通过具体代码示例展示了如何实现这些功能,最终形成一个能灵活应对多种查询需求的智能系统。
1060 11
|
自然语言处理 NoSQL API
基于百炼平台qwen-max的api 打造一套 检索增强 图谱增强 基于指令的智能工具调用决策 智能体
基于百炼平台的 `qwen-max` API,设计了一套融合检索增强、图谱增强及指令驱动的智能工具调用决策系统。该系统通过解析用户指令,智能选择调用检索、图谱推理或模型生成等工具,以提高问题回答的准确性和丰富性。系统设计包括指令解析、工具调用决策、检索增强、图谱增强等模块,旨在通过多种技术手段综合提升智能体的能力。
1012 5
|
11月前
|
机器学习/深度学习 自然语言处理 自动驾驶
如何看待LangChain与智能Agent,二者有什么区别
LangChain是一种专注于自然语言处理的框架,通过链式结构连接多个模型组件,实现复杂任务如问答、对话生成等。其六大核心组件包括模型、检索、代理、链、记忆和回调,帮助开发者快速构建基于大语言模型的应用。智能Agent则是一种能够感知环境、推理决策并采取行动的智能体,涵盖更广泛的智能行为,如自动驾驶、智能家居等。两者分别侧重于语言处理和全面智能行为的技术实现,为不同应用场景提供强大支持。
698 0
LangChain-26 Custom Agent 自定义一个Agent并通过@tool绑定对应的工具 同时让大模型自己调用编写的@tools函数
LangChain-26 Custom Agent 自定义一个Agent并通过@tool绑定对应的工具 同时让大模型自己调用编写的@tools函数
705 3
LangChain-26 Custom Agent 自定义一个Agent并通过@tool绑定对应的工具 同时让大模型自己调用编写的@tools函数
|
传感器 机器学习/深度学习 自然语言处理
智能代理(Agent)在工具调用与协作中的应用实践
随着人工智能技术的飞速发展,智能代理(Agent)技术已成为解决复杂任务的关键手段。本文深入探讨了如何设计灵活的工具调用机制和构建高效的单/多Agent系统以提升任务执行效率。文章不仅涵盖了相关的理论知识,还提供了丰富的实践案例和代码实现,旨在帮助读者深入理解和应用智能代理技术。
1768 2
|
机器学习/深度学习 存储 自然语言处理
LangChain-22 Text Embedding 续接21节 文本切分后 对文本进行embedding向量化处理 后续可保存到向量数据库后进行检索 从而扩展大模型的能力
LangChain-22 Text Embedding 续接21节 文本切分后 对文本进行embedding向量化处理 后续可保存到向量数据库后进行检索 从而扩展大模型的能力
400 0
|
机器学习/深度学习 JSON JavaScript
LangChain-21 Text Splitters 内容切分器 支持多种格式 HTML JSON md Code(JS/Py/TS/etc) 进行切分并输出 方便将数据进行结构化后检索
LangChain-21 Text Splitters 内容切分器 支持多种格式 HTML JSON md Code(JS/Py/TS/etc) 进行切分并输出 方便将数据进行结构化后检索
636 0
|
存储 人工智能 自然语言处理
AI经营|多Agent择优生成商品标题
商品标题中关键词的好坏是商品能否被主搜检索到的关键因素,使用大模型自动优化标题成为【AI经营】中的核心能力之一,本文讲述大模型如何帮助商家优化商品素材,提升商品竞争力。
1536 62
AI经营|多Agent择优生成商品标题
|
机器学习/深度学习 人工智能 自然语言处理
Gemini 2.0:谷歌推出的原生多模态输入输出 + Agent 为核心的 AI 模型
谷歌最新推出的Gemini 2.0是一款原生多模态输入输出的AI模型,以Agent技术为核心,支持多种数据类型的输入与输出,具备强大的性能和多语言音频输出能力。本文将详细介绍Gemini 2.0的主要功能、技术原理及其在多个领域的应用场景。
1334 20
Gemini 2.0:谷歌推出的原生多模态输入输出 + Agent 为核心的 AI 模型
|
人工智能 自然语言处理 前端开发
Director:构建视频智能体的 AI 框架,用自然语言执行搜索、编辑、合成和生成等复杂视频任务
Director 是一个构建视频智能体的 AI 框架,用户可以通过自然语言命令执行复杂的视频任务,如搜索、编辑、合成和生成视频内容。该框架基于 VideoDB 的“视频即数据”基础设施,集成了多个预构建的视频代理和 AI API,支持高度定制化,适用于开发者和创作者。
799 9
Director:构建视频智能体的 AI 框架,用自然语言执行搜索、编辑、合成和生成等复杂视频任务

热门文章

最新文章