components/llm_service/notebooks/LCAgent0.ipynb (328 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "5387ecf5-3f68-4466-adf4-e101d31f14af", "metadata": {}, "outputs": [], "source": [ "import os\n", "import langchain" ] }, { "cell_type": "code", "execution_count": 3, "id": "d653215d-e02c-4b3c-9c65-29abbac253cf", "metadata": {}, "outputs": [], "source": [ "os.environ[\"OPENAI_API_KEY\"] = \"\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "5150f969-4a3c-4ea8-b71d-6bec1b2065e7", "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI\n", "llm = ChatOpenAI(temperature=0)" ] }, { "cell_type": "code", "execution_count": 5, "id": "cc8da57e-3721-43b8-a0a5-ac93f0ccaa2c", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import tool\n", "\n", "@tool\n", "def get_word_length(word: str) -> int:\n", " \"\"\"Returns the length of a word.\"\"\"\n", " return len(word)\n", "\n", "tools = [get_word_length]" ] }, { "cell_type": "code", "execution_count": 6, "id": "0c4265b4-5d72-4a66-8ac2-183e5c4a56b2", "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder\n", "prompt = ChatPromptTemplate.from_messages([\n", " (\"system\", \"You are very powerful assistant, but bad at calculating lengths of words.\"),\n", " (\"user\", \"{input}\"),\n", " MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 7, "id": "331e9849-ba13-489a-93d4-1a0148241f36", "metadata": {}, "outputs": [], "source": [ "from langchain.tools.render import format_tool_to_openai_function\n", "llm_with_tools = llm.bind(\n", " functions=[format_tool_to_openai_function(t) for t in tools]\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "id": "5b717ac7-ac0c-432b-816f-20a81aef866d", "metadata": {}, "outputs": [], "source": [ "from langchain.agents.format_scratchpad import format_to_openai_functions\n", "from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser\n", "agent = {\n", " \"input\": lambda x: x[\"input\"],\n", " \"agent_scratchpad\": lambda x: format_to_openai_functions(x['intermediate_steps'])\n", "} | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser()" ] }, { "cell_type": "code", "execution_count": 9, "id": "707c8e3e-95ad-45d8-a3db-091e0ab801fd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AgentActionMessageLog(tool='get_word_length', tool_input={'word': 'educa'}, log=\"\\nInvoking: `get_word_length` with `{'word': 'educa'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_word_length', 'arguments': '{\\n \"word\": \"educa\"\\n}'}})])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent.invoke({\n", " \"input\": \"how many letters in the word educa?\",\n", " \"intermediate_steps\": []\n", "})" ] }, { "cell_type": "code", "execution_count": 10, "id": "e95e173e-0268-4f3f-90a4-c34896c3bec9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "get_word_length {'word': 'educa'}\n", "There are 5 letters in the word \"educa\".\n" ] } ], "source": [ "from langchain.schema.agent import AgentFinish\n", "intermediate_steps = []\n", "while True:\n", " output = agent.invoke({\n", " \"input\": \"how many letters in the word educa?\",\n", " \"intermediate_steps\": intermediate_steps\n", " })\n", " if isinstance(output, AgentFinish):\n", " final_result = output.return_values[\"output\"]\n", " break\n", " else:\n", " print(output.tool, output.tool_input)\n", " tool = {\n", " \"get_word_length\": get_word_length\n", " }[output.tool]\n", " observation = tool.run(output.tool_input)\n", " intermediate_steps.append((output, observation))\n", "print(final_result)" ] }, { "cell_type": "code", "execution_count": 11, "id": "116bfbea-d7d0-45ff-bb59-01c0fea49d1a", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import AgentExecutor\n", "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)" ] }, { "cell_type": "code", "execution_count": 12, "id": "209bc333-2b4e-4fa4-ba0e-a15963804e3a", "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", "Invoking: `get_word_length` with `{'word': 'educa'}`\n", "\n", "\n", "\u001b[0m\u001b[36;1m\u001b[1;3m5\u001b[0m\u001b[32;1m\u001b[1;3mThere are 5 letters in the word \"educa\".\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'input': 'how many letters in the word educa?',\n", " 'output': 'There are 5 letters in the word \"educa\".'}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent_executor.invoke({\"input\": \"how many letters in the word educa?\"})" ] }, { "cell_type": "code", "execution_count": 13, "id": "225797a6-b438-441e-ac2d-b9915f6ff463", "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import MessagesPlaceholder\n", "\n", "MEMORY_KEY = \"chat_history\"\n", "prompt = ChatPromptTemplate.from_messages([\n", " (\"system\", \"You are very powerful assistant, but bad at calculating lengths of words.\"),\n", " MessagesPlaceholder(variable_name=MEMORY_KEY),\n", " (\"user\", \"{input}\"),\n", " MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n", "])" ] }, { "cell_type": "code", "execution_count": 16, "id": "8937cc8c-ad2d-4dd3-bd03-46da8335cbe3", "metadata": {}, "outputs": [], "source": [ "from langchain.schema.messages import HumanMessage, AIMessage\n", "chat_history = []" ] }, { "cell_type": "code", "execution_count": 17, "id": "7c73f96a-b073-4fb4-8972-1d021d268c47", "metadata": {}, "outputs": [], "source": [ "agent = {\n", " \"input\": lambda x: x[\"input\"],\n", " \"agent_scratchpad\": lambda x: format_to_openai_functions(x['intermediate_steps']),\n", " \"chat_history\": lambda x: x[\"chat_history\"]\n", "} | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser()\n", "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)" ] }, { "cell_type": "code", "execution_count": 18, "id": "14a87314-4481-416e-b3f6-57a922b4d879", "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", "Invoking: `get_word_length` with `{'word': 'educa'}`\n", "\n", "\n", "\u001b[0m\u001b[36;1m\u001b[1;3m5\u001b[0m\u001b[32;1m\u001b[1;3mThere are 5 letters in the word \"educa\".\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mNo, \"educa\" is not a real word in English.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'input': 'is that a real word?',\n", " 'chat_history': [HumanMessage(content='how many letters in the word educa?'),\n", " AIMessage(content='There are 5 letters in the word \"educa\".')],\n", " 'output': 'No, \"educa\" is not a real word in English.'}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input1 = \"how many letters in the word educa?\"\n", "result = agent_executor.invoke({\"input\": input1, \"chat_history\": chat_history})\n", "chat_history.append(HumanMessage(content=input1))\n", "chat_history.append(AIMessage(content=result['output']))\n", "agent_executor.invoke({\"input\": \"is that a real word?\", \"chat_history\": chat_history})" ] }, { "cell_type": "code", "execution_count": null, "id": "ea61175c-a945-45f2-867d-2bd6ccc887e4", "metadata": {}, "outputs": [], "source": [] } ], "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 }