{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from langchain.agents import Tool\n", "from langchain.memory import ConversationBufferMemory\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.utilities import SerpAPIWrapper\n", "from langchain.agents import initialize_agent\n", "from langchain.agents import AgentType" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "search = SerpAPIWrapper()\n", "tools = [\n", " Tool(\n", " name = \"Current Search\",\n", " func=search.run,\n", " description=\"useful for when you need to answer questions about current events or the current state of the world. the input to this should be a single search term.\"\n", " ),\n", "]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "memory = ConversationBufferMemory(memory_key=\"chat_history\", return_messages=True)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "llm=ChatOpenAI(temperature=0)\n", "agent_chain = initialize_agent(tools, llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m{\n", " \"action\": \"Final Answer\",\n", " \"action_input\": \"你好,李振!有什么我可以帮你的吗?\"\n", "}\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'你好,李振!有什么我可以帮你的吗?'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent_chain.run(input=\"你好,我叫李振\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m```json\n", "{\n", " \"action\": \"Current Search\",\n", " \"action_input\": \"今天日期\"\n", "}\n", "```\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m随着Today'sDate365,迅速得到当前日期。 无论你有一台电脑,平板电脑或智能手机,当前日期会立即出现。 今天的日期是:. 星期六; 2023 六月24; 2023/06/24 ...\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m```json\n", "{\n", " \"action\": \"Final Answer\",\n", " \"action_input\": \"今天的日期是:星期六; 2023 六月24; 2023/06/24\"\n", "}\n", "```\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'今天的日期是:星期六; 2023 六月24; 2023/06/24'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent_chain.run(input=\"今天是几号\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m{\n", " \"action\": \"Current Search\",\n", " \"action_input\": \"回答对吗\"\n", "}\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m对吗?对吗是疑问对吧是反问|“对吗?”,是自己有疑问,想知道对还是不对。 “对吧?”,是自己认为对,但不是很肯定。|对吗? 是问别人的意见对吧?\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m{\n", " \"action\": \"Final Answer\",\n", " \"action_input\": \"回答对吗? 是问别人的意见对吧?\"\n", "}\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'回答对吗? 是问别人的意见对吧?'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent_chain.run(input=\"回答对吗\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m```json\n", "{\n", " \"action\": \"Current Search\",\n", " \"action_input\": \"what is my name\"\n", "}\n", "```\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m\"My Name Is\" is a song by American rapper Eminem from his second album The Slim Shady LP. It is also the opening song and lead single of the album. The song samples British singer Labi Siffre's 1975 track \"I Got The...\" as a bass and guitar riff by British pop rock duo Chas & Dave.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m```json\n", "{\n", " \"action\": \"Final Answer\",\n", " \"action_input\": \"I'm sorry, but the response to your last comment was not related to your question of 'what is my name'. Would you like me to look up your name for you?\"\n", "}\n", "```\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\"I'm sorry, but the response to your last comment was not related to your question of 'what is my name'. Would you like me to look up your name for you?\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent_chain.run(input=\"我叫什么\")" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }