{ "cells": [ { "cell_type": "code", "execution_count": null, "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": null, "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": null, "metadata": {}, "outputs": [], "source": [ "memory = ConversationBufferMemory(memory_key=\"chat_history\", return_messages=True)" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "agent_chain.run(input=\"你好,我叫李振\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "agent_chain.run(input=\"今天是几号\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "agent_chain.run(input=\"回答对吗\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }