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

本文涉及的产品
阿里云百炼推荐规格 ADB PostgreSQL,4核16GB 100GB 1个月
简介: 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推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
目录
相关文章
|
2月前
|
人工智能 缓存 监控
使用LangChain4j构建Java AI智能体:让大模型学会使用工具
AI智能体是大模型技术的重要演进方向,它使模型能够主动使用工具、与环境交互,以完成复杂任务。本文详细介绍如何在Java应用中,借助LangChain4j框架构建一个具备工具使用能力的AI智能体。我们将创建一个能够进行数学计算和实时信息查询的智能体,涵盖工具定义、智能体组装、记忆管理以及Spring Boot集成等关键步骤,并展示如何通过简单的对话界面与智能体交互。
807 1
|
2月前
|
测试技术 API
LangChain中的Prompt模板如何使用?
本文介绍了LangChain中的Prompt模板功能,涵盖其基本用法、动态生成提示词的实现方式,以及如何设置默认值、从文件加载模板和应用于聊天模型的场景。通过示例代码演示了模板的创建与格式化过程,帮助提升提示词管理效率,适用于测试用例设计等场景。
297 121
|
3月前
|
存储 人工智能 前端开发
​​LangChain默认工具正在污染你的知识库!PDF解析崩溃真相​
本文深入探讨RAG项目中PDF解析的痛点与解决方案,分析LangChain默认工具的局限性,提出专业级文档处理架构设计与工具选型策略,涵盖表格图像处理、多模态解析与可扩展管道实现,助力提升RAG系统效果。
245 6
|
11月前
|
API 数据库 决策智能
基于百炼平台qwen-max的api 打造一套 检索增强 图谱增强 智能工具调用决策的智能体
本文介绍了一种基于阿里云百炼平台的`qwen-max` API构建的智能体方案,该方案集成了检索增强、图谱增强及智能工具调用决策三大模块,旨在通过结合外部数据源、知识图谱和自动化决策提高智能回答的准确性和丰富度。通过具体代码示例展示了如何实现这些功能,最终形成一个能灵活应对多种查询需求的智能系统。
819 11
|
11月前
|
自然语言处理 NoSQL API
基于百炼平台qwen-max的api 打造一套 检索增强 图谱增强 基于指令的智能工具调用决策 智能体
基于百炼平台的 `qwen-max` API,设计了一套融合检索增强、图谱增强及指令驱动的智能工具调用决策系统。该系统通过解析用户指令,智能选择调用检索、图谱推理或模型生成等工具,以提高问题回答的准确性和丰富性。系统设计包括指令解析、工具调用决策、检索增强、图谱增强等模块,旨在通过多种技术手段综合提升智能体的能力。
779 5
|
8月前
|
机器学习/深度学习 自然语言处理 自动驾驶
如何看待LangChain与智能Agent,二者有什么区别
LangChain是一种专注于自然语言处理的框架,通过链式结构连接多个模型组件,实现复杂任务如问答、对话生成等。其六大核心组件包括模型、检索、代理、链、记忆和回调,帮助开发者快速构建基于大语言模型的应用。智能Agent则是一种能够感知环境、推理决策并采取行动的智能体,涵盖更广泛的智能行为,如自动驾驶、智能家居等。两者分别侧重于语言处理和全面智能行为的技术实现,为不同应用场景提供强大支持。
485 0
LangChain-26 Custom Agent 自定义一个Agent并通过@tool绑定对应的工具 同时让大模型自己调用编写的@tools函数
LangChain-26 Custom Agent 自定义一个Agent并通过@tool绑定对应的工具 同时让大模型自己调用编写的@tools函数
456 3
LangChain-26 Custom Agent 自定义一个Agent并通过@tool绑定对应的工具 同时让大模型自己调用编写的@tools函数
|
12月前
|
传感器 机器学习/深度学习 自然语言处理
智能代理(Agent)在工具调用与协作中的应用实践
随着人工智能技术的飞速发展,智能代理(Agent)技术已成为解决复杂任务的关键手段。本文深入探讨了如何设计灵活的工具调用机制和构建高效的单/多Agent系统以提升任务执行效率。文章不仅涵盖了相关的理论知识,还提供了丰富的实践案例和代码实现,旨在帮助读者深入理解和应用智能代理技术。
1385 2
|
存储 人工智能 自然语言处理
AI经营|多Agent择优生成商品标题
商品标题中关键词的好坏是商品能否被主搜检索到的关键因素,使用大模型自动优化标题成为【AI经营】中的核心能力之一,本文讲述大模型如何帮助商家优化商品素材,提升商品竞争力。
1278 62
AI经营|多Agent择优生成商品标题
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
Gemini 2.0:谷歌推出的原生多模态输入输出 + Agent 为核心的 AI 模型
谷歌最新推出的Gemini 2.0是一款原生多模态输入输出的AI模型,以Agent技术为核心,支持多种数据类型的输入与输出,具备强大的性能和多语言音频输出能力。本文将详细介绍Gemini 2.0的主要功能、技术原理及其在多个领域的应用场景。
1073 20
Gemini 2.0:谷歌推出的原生多模态输入输出 + Agent 为核心的 AI 模型

热门文章

最新文章