components/llm_service/notebooks/LCAgent2.ipynb (89 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "ade4d2b9-3677-4c72-8eb9-53c0b5a40384", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser\n", "from langchain.prompts import StringPromptTemplate\n", "from langchain.llms import OpenAI\n", "from langchain.utilities import SerpAPIWrapper\n", "from langchain.chains import LLMChain\n", "from typing import List, Union\n", "from langchain.schema import AgentAction, AgentFinish, OutputParserException\n", "import re" ] }, { "cell_type": "code", "execution_count": null, "id": "557b771b-b68d-48f6-9762-5c451d72e9f4", "metadata": {}, "outputs": [], "source": [ "# Define which tools the agent can use to answer user queries\n", "search = SerpAPIWrapper()\n", "tools = [\n", " Tool(\n", " name=\"Search\",\n", " func=search.run,\n", " description=\"useful for when you need to answer questions about current events\"\n", " )\n", "]" ] }, { "cell_type": "code", "execution_count": null, "id": "839fd98f-ff9e-4d0e-b35d-42ac25118af0", "metadata": {}, "outputs": [], "source": [ "# Set up the base template\n", "template = \"\"\"Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:\n", "\n", "{tools}\n", "\n", "Use the following format:\n", "\n", "Question: the input question you must answer\n", "Thought: you should always think about what to do\n", "Action: the action to take, should be one of [{tool_names}]\n", "Action Input: the input to the action\n", "Observation: the result of the action\n", "... (this Thought/Action/Action Input/Observation can repeat N times)\n", "Thought: I now know the final answer\n", "Final Answer: the final answer to the original input question\n", "\n", "Begin! Remember to speak as a pirate when giving your final answer. Use lots of \"Arg\"s\n", "\n", "Question: {input}\n", "{agent_scratchpad}\"\"\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }