宠物托运网站开发,wordpress漂浮框,深圳网络市场推广,淄博网站的建设AI 代理教程#xff1a;如何创建信息检索聊天机器人
介绍
在本教程中#xff0c;我们将指导您使用 AI 代理创建用于信息检索的复杂聊天机器人的过程。探索如何利用 AI 的强大功能构建能够高效地从各种来源检索数据的聊天机器人。
设置环境
我们的计划是使用 AI 代理…
AI 代理教程如何创建信息检索聊天机器人
介绍
在本教程中我们将指导您使用 AI 代理创建用于信息检索的复杂聊天机器人的过程。探索如何利用 AI 的强大功能构建能够高效地从各种来源检索数据的聊天机器人。
设置环境
我们的计划是使用 AI 代理LangChain创建一个聊天机器人并使用 Chainlit 创建一个简单的 UI。
我们希望我们的聊天机器人能够分两个阶段响应查询规划和检索。代理应该可以访问维基百科和网络搜索。
准备和依赖
让我们从创建一个新项目开始。我将从创建新目录开始
mkdir agents-chatbot
cd agents-chatbot让我们创建虚拟环境并安装依赖项
python3 -m venv venv# Linux/MacOS
source venv/bin/activate# Windows
venv\Scripts\activate.batpip install langchain chainlit python-dotenv wikipedia duckduckgo-search现在我们可以创建我们的 app.py 文件Chainlit 需要名称
touch app.py最后一步是导入我们的依赖项
import osimport chainlit as cl
from dotenv import load_dotenv
from langchain import PromptTemplate
from langchain.agents import AgentType, Tool, initialize_agent
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAI
from langchain.tools import DuckDuckGoSearchRun
from langchain.utilities import WikipediaAPIWrapperload_dotenv()# OpenAI API key
OPENAI_API_KEY os.getenv(OPENAI_API_KEY)免责声明我在 .env 文件中定义了我的环境变量您可以执行相同的操作或在代码中定义秘密。
编码
现在是时候初始化 LLM 和工具了。我将在本教程中使用 GPT-4但如果您愿意也可以使用其他模型。我还将使用 DuckDuckGoSearchRun 和 WikipediaAPIWrapper 作为我的工具。
llm ChatOpenAI(temperature0, modelgpt-4)search DuckDuckGoSearchRun()
wikipedia WikipediaAPIWrapper()# Web Search Tool
search_tool Tool(nameWeb Search,funcsearch.run,descriptionA useful tool for searching the Internet to find information on world events, issues, etc. Worth using for general topics. Use precise questions.,
)# Wikipedia Tool
wikipedia_tool Tool(nameWikipedia,funcwikipedia.run,descriptionA useful tool for searching the Internet to find information on world events, issues, etc. Worth using for general topics. Use precise questions.,
)下一步是准备 PromptTemplates。我会准备两个。一个用于规划过程一个用于生成最终响应的过程。
prompt PromptTemplate(templatePlan: {input}History: {chat_history}Lets think about answer step by step.
If its information retrieval task, solve it like a professor in particular field.,input_variables[input, chat_history],
)plan_prompt PromptTemplate(input_variables[input, chat_history],templatePrepare plan for task execution. (e.g. retrieve current date to find weather forecast)Tools to use: wikipedia, web searchREMEMBER: Keep in mind that you dont have information about current date, temperature, informations after September 2021. Because of that you need to use tools to find them.Question: {input}History: {chat_history}Output look like this:
Question: {input}Execution plan: [execution_plan]Rest of needed information: [rest_of_needed_information]
IMPORTANT: if there is no question, or plan is not need (YOU HAVE TO DECIDE!), just populate {input} (pass it as a result). Then output should look like this:
input: {input},
)现在是启动代理和计划链的时候了。此外我将添加内存以便它们可以保存有关先前消息的信息。
memory ConversationBufferMemory(memory_keychat_history, return_messagesTrue)plan_chain ConversationChain(llmllm,memorymemory,input_keyinput,promptplan_prompt,output_keyoutput,
)# Initialize Agent
agent initialize_agent(agentAgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,tools[search_tool, wikipedia_tool],llmllm,verboseTrue, # verbose option is for printing logs (only for development)max_iterations3,promptprompt,memorymemory,
)UI部分
现在是时候创建 UI 了。我将使用 Chainlit 来实现此目的。我将利用 factory 函数将我们的代理传递给 Chainlit。但在触发 factory 函数之前Chainlit 使用 run 函数准备将输入传递给模型的管道。我将覆盖它以稍微改变流程。我想首先执行规划然后生成响应。
cl.langchain_run
def run(agent, input_str):# Plan executionplan_result plan_chain.run(input_str)# Agent executionres agent(plan_result)# Send messagecl.Message(contentres[output]).send()cl.langchain_factory
def factory():return agent好的我们快完成了。最后一步是运行我们的应用程序
chainlit run app.py -w # -w flag is for restarting app after each change结果
太好了现在让我们测试一下我们的应用程序。我将通过说“你好”来启动我们的应用程序然后问它一个问题。让我们看看结果会是什么 太棒了正如您最初看到的那样模型绕过规划直到收到提示 — 然后它组织任务并根据预期制定响应
勇往直前构建您独特的 AI 代理应用程序不要错过 6 月 9 日开始的 AI 代理黑客马拉松。通过我们的 AI 教程提升您的知识并利用 AI 的威力塑造未来