{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### libraries import" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true, "id": "oOnNfKjX4IAV" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/transformers/utils/generic.py:441: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n", " _torch_pytree._register_pytree_node(\n", "/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/transformers/utils/generic.py:309: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n", " _torch_pytree._register_pytree_node(\n", "/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/transformers/utils/generic.py:309: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n", " _torch_pytree._register_pytree_node(\n" ] } ], "source": [ "import os\n", "\n", "#gradio interface\n", "import gradio as gr\n", "\n", "from transformers import AutoModelForCausalLM,AutoTokenizer\n", "import torch\n", "\n", "#STT (speech to text)\n", "from transformers import WhisperProcessor, WhisperForConditionalGeneration\n", "import librosa\n", "\n", "#TTS (text to speech)\n", "import torch\n", "\n", "#regular expressions\n", "import re\n", "\n", "#langchain and function calling\n", "from typing import List, Literal, Union\n", "import requests\n", "from functools import partial\n", "import math\n", "\n", "\n", "#langchain, not used anymore since I had to find another way fast to stop using the endpoint, but could be interesting to reuse \n", "from langchain.tools.base import StructuredTool\n", "from langchain.schema import AgentAction, AgentFinish, OutputParserException\n", "from langchain.prompts import StringPromptTemplate\n", "\n", "\n", "\n", "from datetime import datetime, timedelta, timezone\n", "from transformers import pipeline\n", "import inspect" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from langchain.memory import ConversationSummaryBufferMemory" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# load api key from .env file\n", "# weather api and tomtom api key\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "WHEATHER_API_KEY = os.getenv(\"WEATHER_API_KEY\")\n", "TOMTOM_KEY = os.getenv(\"TOMTOM_API_KEY\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from apis import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Models loads" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from langchain_community.llms import Ollama\n", "# https://github.com/ollama/ollama/blob/main/docs/api.md\n", "\n", "llm = Ollama(model=\"nexusraven\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from skills import execute_function_call" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "get_weather(**{'location': '49.611622,6.131935'})\n" ] }, { "data": { "text/plain": [ "'Dry run successful'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "execute_function_call(\"Call: get_weather(location='49.611622,6.131935') \", dry_run=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function calling with NexusRaven " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import skills\n", "from skills import get_weather, get_forecast, find_route" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OPTION:\n", "get_weather(location: str = '', **kwargs)\n", "\n", "Returns the CURRENT weather in a specified location.\n", "Args:\n", "location (string) : Required. The name of the location, could be a city or lat/longitude in the following format latitude,longitude (example: 37.7749,-122.4194).\n", "\n", "OPTION:\n", "get_forecast(city_name: str = '', when=0, **kwargs)\n", "\n", "Returns the weather forecast in a specified number of days for a specified city .\n", "Args:\n", "city_name (string) : Required. The name of the city.\n", "when (int) : Required. in number of days (until the day for which we want to know the forecast) (example: tomorrow is 1, in two days is 2, etc.)\n", "\n", "OPTION:\n", "find_route(lat_depart='0', lon_depart='0', city_depart='', address_destination='', depart_time='', **kwargs)\n", "\n", "Return the distance and the estimated time to go to a specific destination from the current place, at a specified depart time.\n", ":param lat_depart (string): latitude of depart\n", ":param lon_depart (string): longitude of depart\n", ":param city_depart (string): Required. city of depart\n", ":param address_destination (string): Required. The destination\n", ":param depart_time (string): departure hour, in the format '08:00:20'.\n", "\n" ] } ], "source": [ "print(skills.SKILLS_PROMPT)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# Moved to skills/__init__.py here only for experimentation\n", "def format_functions_for_prompt_raven(*functions):\n", " formatted_functions = []\n", " for func in functions:\n", " signature = f\"{func.__name__}{inspect.signature(func)}\"\n", " docstring = inspect.getdoc(func)\n", " formatted_functions.append(\n", " f\"OPTION:\\n{signature}\\n\\n{docstring}\\n\"\n", " )\n", " return \"\\n\".join(formatted_functions)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "formatted_prompt = format_functions_for_prompt_raven(get_weather, find_points_of_interest, find_route, get_forecast, search_along_route)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OPTION:\n", "get_weather(location: str = '', **kwargs)\n", "\n", "Returns the CURRENT weather in a specified location.\n", "Args:\n", "location (string) : Required. The name of the location, could be a city or lat/longitude in the following format latitude,longitude (example: 37.7749,-122.4194).\n", "\n", "OPTION:\n", "find_points_of_interest(lat='0', lon='0', city='', type_of_poi='restaurant', **kwargs)\n", "\n", "Return some of the closest points of interest for a specific location and type of point of interest. The more parameters there are, the more precise.\n", ":param lat (string): latitude\n", ":param lon (string): longitude\n", ":param city (string): Required. city\n", ":param type_of_poi (string): Required. type of point of interest depending on what the user wants to do.\n", "\n", "OPTION:\n", "find_route(lat_depart='0', lon_depart='0', city_depart='', address_destination='', depart_time='', **kwargs)\n", "\n", "Return the distance and the estimated time to go to a specific destination from the current place, at a specified depart time.\n", ":param lat_depart (string): latitude of depart\n", ":param lon_depart (string): longitude of depart\n", ":param city_depart (string): Required. city of depart\n", ":param address_destination (string): Required. The destination\n", ":param depart_time (string): departure hour, in the format '08:00:20'.\n", "\n", "OPTION:\n", "get_forecast(city_name: str = '', when=0, **kwargs)\n", "\n", "Returns the weather forecast in a specified number of days for a specified city .\n", "Args:\n", "city_name (string) : Required. The name of the city.\n", "when (int) : Required. in number of days (until the day for which we want to know the forecast) (example: tomorrow is 1, in two days is 2, etc.)\n", "\n", "OPTION:\n", "search_along_route(latitude_depart, longitude_depart, city_destination, type_of_poi)\n", "\n", "Return some of the closest points of interest along the route from the depart point, specified by its coordinates and a city destination.\n", ":param latitude_depart (string): Required. Latitude of depart location\n", ":param longitude_depart (string): Required. Longitude of depart location\n", ":param city_destination (string): Required. City destination\n", ":param type_of_poi (string): Required. type of point of interest depending on what the user wants to do.\n", "\n" ] } ], "source": [ "print(formatted_prompt)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "from langchain_core.prompts import PromptTemplate, PipelinePromptTemplate\n", "from langchain.memory import ChatMessageHistory\n", "from langchain_core.messages import AIMessage, HumanMessage, SystemMessage" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "prompt = PromptTemplate.from_template(\"What is the weather in {location}?\")" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "status_template = \"\"\"\n", "car coordinates: lat:{lat}, lon:{lon}\n", "current weather: {weather}\n", "current temperature: {temperature}\n", "current time: {time}\n", "current date: {date}\n", "destination: {destination}\n", "\"\"\"\n", "status_prompt = PromptTemplate.from_template(status_template)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PromptTemplate(input_variables=['date', 'destination', 'lat', 'lon', 'temperature', 'time', 'weather'], template='\\ncar coordinates: lat:{lat}, lon:{lon}\\ncurrent weather: {weather}\\ncurrent temperature: {temperature}\\ncurrent time: {time}\\ncurrent date: {date}\\ndestination: {destination}\\n')" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "status_prompt" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "prompt = SystemMessage(content=\"You are a nice pirate\")" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "full_template = \"\"\"\n", "\n", "\n", "{skills}\n", "\n", "Current status of the vehicle:\n", "{status}\n", "\n", "User Query: Question: {question}\n", "\n", "\n", "Please pick a function from the above options that best answers the user query and fill in the appropriate arguments.\n", "\"\"\"\n", "full_prompt = PromptTemplate.from_template(full_template)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "question_prompt = HumanMessage(content=\"How is the weather in Paris?\")\n", "input_prompts = [\n", " # (\"skills\", skills.SKILLS_PROMPT),\n", " (\"status\", status_prompt),\n", " # (\"question\", question_prompt),\n", "]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "pipeline_prompt = PipelinePromptTemplate(\n", " final_prompt=full_prompt, \n", " pipeline_prompts=input_prompts\n", ")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PipelinePromptTemplate(input_variables=['date', 'time', 'weather', 'lon', 'lat', 'destination', 'temperature'], final_prompt=PromptTemplate(input_variables=['question', 'skills', 'status'], template='\\n\\n\\n{skills}\\n\\nCurrent status of the vehicle:\\n{status}\\n\\nUser Query: Question: {question}\\n\\n\\nPlease pick a function from the above options that best answers the user query and fill in the appropriate arguments.\\n'), pipeline_prompts=[('status', PromptTemplate(input_variables=['date', 'destination', 'lat', 'lon', 'temperature', 'time', 'weather'], template='\\ncar coordinates: lat:{lat}, lon:{lon}\\ncurrent weather: {weather}\\ncurrent temperature: {temperature}\\ncurrent time: {time}\\ncurrent date: {date}\\ndestination: {destination}\\n'))])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipeline_prompt" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "history = ChatMessageHistory()\n", "history.add_message(question_prompt)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ChatMessageHistory(messages=[HumanMessage(content='How is the weather in Paris?')])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "history" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "history.add_ai_message(pipeline_prompt.format(\n", " date=datetime.now().strftime(\"%Y-%m-%d\"),\n", " time=datetime.now().strftime(\"%H:%M:%S\"),\n", " weather=\"sunny\",\n", " temperature=\"20°C\",\n", " destination=\"Paris\",\n", " lat=\"48.8566\",\n", " lon=\"2.3522\",\n", " question=\"How is the weather in Paris?\",\n", " # status=status,\n", " skills=skills.SKILLS_PROMPT,\n", " # system=system,\n", " # query=question,\n", " ))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content=\"\\n\\n\\nOPTION:\\nget_weather(location: str = '', **kwargs)\\n\\nReturns the CURRENT weather in a specified location.\\nArgs:\\nlocation (string) : Required. The name of the location, could be a city or lat/longitude in the following format latitude,longitude (example: 37.7749,-122.4194).\\n\\nOPTION:\\nget_forecast(city_name: str = '', when=0, **kwargs)\\n\\nReturns the weather forecast in a specified number of days for a specified city .\\nArgs:\\ncity_name (string) : Required. The name of the city.\\nwhen (int) : Required. in number of days (until the day for which we want to know the forecast) (example: tomorrow is 1, in two days is 2, etc.)\\n\\nOPTION:\\nfind_route(lat_depart='0', lon_depart='0', city_depart='', address_destination='', depart_time='', **kwargs)\\n\\nReturn the distance and the estimated time to go to a specific destination from the current place, at a specified depart time.\\n:param lat_depart (string): latitude of depart\\n:param lon_depart (string): longitude of depart\\n:param city_depart (string): Required. city of depart\\n:param address_destination (string): Required. The destination\\n:param depart_time (string): departure hour, in the format '08:00:20'.\\n\\n\\nCurrent status of the vehicle:\\n\\ncar coordinates: lat:48.8566, lon:2.3522\\ncurrent weather: sunny\\ncurrent temperature: 20°C\\ncurrent time: 17:04:03\\ncurrent date: 2024-04-30\\ndestination: Paris\\n\\n\\nUser Query: Question: How is the weather in Paris?\\n\\n\\nPlease pick a function from the above options that best answers the user query and fill in the appropriate arguments.\\n\")" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "history.messages[-1]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "OPTION:\n", "get_weather(location: str = '', **kwargs)\n", "\n", "Returns the CURRENT weather in a specified location.\n", "Args:\n", "location (string) : Required. The name of the location, could be a city or lat/longitude in the following format latitude,longitude (example: 37.7749,-122.4194).\n", "\n", "OPTION:\n", "get_forecast(city_name: str = '', when=0, **kwargs)\n", "\n", "Returns the weather forecast in a specified number of days for a specified city .\n", "Args:\n", "city_name (string) : Required. The name of the city.\n", "when (int) : Required. in number of days (until the day for which we want to know the forecast) (example: tomorrow is 1, in two days is 2, etc.)\n", "\n", "OPTION:\n", "find_route(lat_depart='0', lon_depart='0', city_depart='', address_destination='', depart_time='', **kwargs)\n", "\n", "Return the distance and the estimated time to go to a specific destination from the current place, at a specified depart time.\n", ":param lat_depart (string): latitude of depart\n", ":param lon_depart (string): longitude of depart\n", ":param city_depart (string): Required. city of depart\n", ":param address_destination (string): Required. The destination\n", ":param depart_time (string): departure hour, in the format '08:00:20'.\n", "\n", "\n", "Current status of the vehicle:\n", "\n", "car coordinates: lat:48.8566, lon:2.3522\n", "current weather: sunny\n", "current temperature: 20°C\n", "current time: 17:04:04\n", "current date: 2024-04-30\n", "destination: Paris\n", "\n", "\n", "User Query: Question: How is the weather in Paris?\n", "\n", "\n", "Please pick a function from the above options that best answers the user query and fill in the appropriate arguments.\n", "\n" ] } ], "source": [ "print(\n", " pipeline_prompt.format(\n", " date=datetime.now().strftime(\"%Y-%m-%d\"),\n", " time=datetime.now().strftime(\"%H:%M:%S\"),\n", " weather=\"sunny\",\n", " temperature=\"20°C\",\n", " destination=\"Paris\",\n", " lat=\"48.8566\",\n", " lon=\"2.3522\",\n", " question=\"How is the weather in Paris?\",\n", " # status=status,\n", " skills=skills.SKILLS_PROMPT,\n", " # system=system,\n", " # query=question,\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "from langchain_community.llms import Ollama\n", "\n", "llm = Ollama(model=\"nexusraven\", stop=[\"\\nReflection:\", \"\\nThought:\"])" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "chain = pipeline_prompt | llm" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "q = dict(\n", " date=datetime.now().strftime(\"%Y-%m-%d\"),\n", " time=datetime.now().strftime(\"%H:%M:%S\"),\n", " weather=\"sunny\",\n", " temperature=\"20°C\",\n", " destination=\"Paris\",\n", " lat=\"48.8566\",\n", " lon=\"2.3522\",\n", " question=\"How is the weather in Paris?\",\n", " # status=status,\n", " skills=skills.SKILLS_PROMPT,\n", " # system=system,\n", " # query=question,\n", " )" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "out_1 = chain.invoke(q)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "http://api.weatherapi.com/v1/current.json?key=d1c4d0d8ef6847339a0125737240903&q=Paris&aqi=no\n", "http://api.weatherapi.com/v1/current.json?key=d1c4d0d8ef6847339a0125737240903&q=Paris&aqi=no\n" ] } ], "source": [ "func_out_1 = execute_function_call(out_1)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The current weather in Paris, Ile-de-France, France is Light rain with a temperature of 17.0°C that feels like 17.0°C. Humidity is at 68%. Wind speed is 12.5 mph.'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "func_out_1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from langchain_core.runnables.history import RunnableWithMessageHistory\n", "from langchain.agents import create_self_ask_with_search_agent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test Langchain Agent" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from langchain_core.prompts import PromptTemplate" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import langchain\n", "langchain.debug=False\n", "from skills import get_weather, find_route, get_forecast, vehicle_status, vehicle" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from typing import Union, List\n", "\n", "from langchain.chains import LLMChain\n", "from langchain.prompts import StringPromptTemplate\n", "from langchain.tools.base import StructuredTool\n", "from langchain.schema import AgentAction, AgentFinish, OutputParserException\n", "from skills import extract_func_args\n", "from langchain.agents import (\n", " Tool,\n", " AgentExecutor,\n", " LLMSingleActionAgent,\n", " AgentOutputParser,\n", ")\n", "\n", "# https://github.com/nexusflowai/NexusRaven/blob/main/scripts/langchain_example.py\n", "class RavenOutputParser(AgentOutputParser):\n", " def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]:\n", " print(f\"llm_output: {llm_output}\")\n", " if \"Call: \" in llm_output:\n", " # llm_output = llm_output.replace(\"Initial Answer: \", \"Call: \")\n", " func_name, args = extract_func_args(llm_output)\n", " return AgentAction(\n", " tool=func_name,\n", " tool_input=args,\n", " log=f\"Calling {func_name} with args: {args}\",\n", " )\n", "\n", " # Check if agent should finish\n", " # if \"Initial Answer:\" in llm_output:\n", " # return AgentFinish(\n", " # return_values={\n", " # \"output\": llm_output.strip()\n", " # .split(\"\\n\")[1]\n", " # .replace(\"Initial Answer: \", \"\")\n", " # .strip()\n", " # },\n", " # log=llm_output,\n", " # )\n", " \n", " return AgentFinish(\n", " return_values={\n", " \"output\": llm_output.strip()\n", " },\n", " log=llm_output,\n", " )\n", " # else:\n", " # raise OutputParserException(f\"Could not parse LLM output: `{llm_output}`\")\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "tools = [\n", " StructuredTool.from_function(get_weather),\n", " StructuredTool.from_function(find_route),\n", " StructuredTool.from_function(vehicle_status),\n", "]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "RAVEN_PROMPT_FUNC = \"\"\"You are a helpful AI assistant in a car (vehicle), that follows instructions extremely well. You are aware of car's location through status information and take that into account before responding. Answer questions concisely and do not mention what you base your reply on.\"\n", "\n", "{raven_tools}\n", "\n", "Previous conversation history:\n", "{history}\n", "\n", "User Query: Question: {input}\n", "\"\"\"\n", "\n", "RAVEN_PROMPT_ANS = \"\"\"\n", "\n", "You are a useful AI assistant in a car, that follows instructions extremely well. Help as much as you can. Answer questions concisely and do not mention what you base your reply on.\n", "\n", "Previous conversation history:\n", "{history}\n", "\n", "Observation:{observation}\n", "\n", "User Query: Question: {input}\n", "\n", "Please respond in natural language. Do not refer to the provided context and observation in your response.\n", "\n", "Answer: \"\"\"\n", "\n", "\n", "STATUS_TEMPLATE = \"\"\"We are at these geo coordinates: lat:{lat}, lon:{lon}, current time: {time}, current date: {date} and our destination is: {destination}\n", "\"\"\"\n", "status_prompt = PromptTemplate.from_template(STATUS_TEMPLATE)\n", "\n", "class RavenPromptTemplate(StringPromptTemplate):\n", " template_func: str\n", " template_ans: str\n", " status_prompt: PromptTemplate\n", " # The list of tools available\n", " tools: List[Tool]\n", "\n", " def format(self, **kwargs) -> str:\n", " print(f\"kwargs: {kwargs}\")\n", " intermediate_steps = kwargs.pop(\"intermediate_steps\")\n", " # if \"input\" in kwargs and type(kwargs[\"input\"]) == dict:\n", " # inputs = kwargs[\"input\"]\n", " # kwargs[\"status\"] = inputs[\"status\"]\n", " # kwargs[\"input\"] = inputs[\"input\"]\n", "\n", " if intermediate_steps:\n", " step = intermediate_steps[-1]\n", " prompt = json.dumps(step[1][1], indent=4)\n", " kwargs[\"observation\"] = prompt\n", " pp = self.template_ans.format(**kwargs).replace(\"{{\", \"{\").replace(\"}}\", \"}\")\n", " return pp\n", "\n", " # if \"status\" in kwargs:\n", " # kwargs[\"status\"] = self.status_prompt.format(**kwargs[\"status\"])\n", "\n", " prompt = \":\\n\"\n", " for tool in self.tools:\n", " func_signature, func_docstring = tool.description.split(\" - \", 1)\n", " prompt += f'Function:\\ndef {func_signature}\\n\\n\"\"\"\\n{func_docstring}\\n\"\"\"\\n\\n'\n", " kwargs[\"raven_tools\"] = prompt\n", " pp = self.template_func.format(**kwargs).replace(\"{{\", \"{\").replace(\"}}\", \"}\")\n", " return pp\n", "\n", "\n", "raven_prompt = RavenPromptTemplate(\n", " template_func=RAVEN_PROMPT_FUNC,\n", " template_ans=RAVEN_PROMPT_ANS,\n", " status_prompt=status_prompt,\n", " tools=tools,\n", " input_variables=[\"input\", \"history\", \"intermediate_steps\"],\n", ")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "from langchain.memory import ConversationBufferWindowMemory\n", "memory = ConversationBufferWindowMemory(k=2, input_key=\"input\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from langchain_community.llms import Ollama\n", "\n", "# llm = Ollama(model=\"new-nexus\")\n", "llm = Ollama(model=\"nexusraven\")\n", "output_parser = RavenOutputParser()\n", "llm_chain = LLMChain(llm=llm, prompt=raven_prompt)\n", "agent = LLMSingleActionAgent(\n", " llm_chain=llm_chain,\n", " output_parser=output_parser,\n", " stop=[\"\\nReflection:\", \"\\nThought:\"],\n", " # stop=[\"\\nReflection:\"],\n", " allowed_tools=tools,\n", ")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, memory=memory, verbose=True)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'agent_chain' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43magent_chain\u001b[49m\n", "\u001b[0;31mNameError\u001b[0m: name 'agent_chain' is not defined" ] } ], "source": [ "agent_chain" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "\n", "status = dict(\n", " date=datetime.now().strftime(\"%Y-%m-%d\"),\n", " time=datetime.now().strftime(\"%H:%M:%S\"),\n", " # temperature=\"20°C\",\n", " destination=\"Kirchberg, Luxembourg\",\n", " lat=\"48.8566\",\n", " lon=\"2.3522\",\n", ")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "kwargs: {'input': \"What's our current location?\", 'history': '', 'intermediate_steps': []}\n", "llm_output: Call: vehicle_status(prompt='Question: What is our current location?') \n", "\u001b[32;1m\u001b[1;3mCalling vehicle_status with args: {'prompt': 'Question: What is our current location?'}\u001b[0m\n", "\n", "Reflection:\u001b[38;5;200m\u001b[1;3m('We are at Mondorf-les-Bains, Luxembourg, current time: 08:00:20, current date: 2025-03-29 and our destination is: Kirchberg Campus, Kirchberg.\\n', {'location': 'Mondorf-les-Bains, Luxembourg', 'date': '2025-03-29', 'time': '08:00:20', 'destination': 'Kirchberg Campus, Kirchberg'})\u001b[0m\n", "kwargs: {'input': \"What's our current location?\", 'history': '', 'intermediate_steps': [(AgentAction(tool='vehicle_status', tool_input={'prompt': 'Question: What is our current location?'}, log=\"Calling vehicle_status with args: {'prompt': 'Question: What is our current location?'}\"), ('We are at Mondorf-les-Bains, Luxembourg, current time: 08:00:20, current date: 2025-03-29 and our destination is: Kirchberg Campus, Kirchberg.\\n', {'location': 'Mondorf-les-Bains, Luxembourg', 'date': '2025-03-29', 'time': '08:00:20', 'destination': 'Kirchberg Campus, Kirchberg'}))]}\n", "llm_output: The current location of the car is the location that was last observed by the user, which is \"Mondorf-les-Bains, Luxembourg\".\n", "\u001b[32;1m\u001b[1;3mThe current location of the car is the location that was last observed by the user, which is \"Mondorf-les-Bains, Luxembourg\".\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "call = agent_chain.run(\n", " input=\"What's our current location?\",\n", " # status= status\n", ")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "vehicle.vehicle.location = \"Mondorf-les-Bains, Luxembourg\"" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The current weather conditions in Tokyo are partly cloudy, with a temperature of 17 degrees Celsius and a wind speed of 17.4 miles per hour.'" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "call" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "kwargs: {'input': 'How about in Tokyo?', 'status': {'date': '2024-05-02', 'time': '15:35:33', 'destination': 'Kirchberg, Luxembourg', 'lat': '48.8566', 'lon': '2.3522'}, 'history': 'Human: How is the weather in Luxembourg?\\nAI: The weather in Luxembourg is moderate or heavy rain with thunder, with a temperature of 15 degrees Celsius. The wind is blowing from the west at a speed of 28.1 kilometers per hour, and there is a 75% chance of cloud cover. It feels like 13.7 degrees Celsius outside.', 'intermediate_steps': []}\n", "You are a useful AI assistant in a car, that follows instructions extremely well. Help as much as you can. Answer questions concisely and do not mention what you base your reply on.\n", ":\n", "\\Function:\n", "def get_weather(location: str = '', **kwargs)\n", "\n", "\"\"\"\n", "Returns the CURRENT weather in a specified location.\n", " Args:\n", " location (string) : Required. The name of the location, could be a city or lat/longitude in the following format latitude,longitude (example: 37.7749,-122.4194).\n", "\"\"\"\n", "\n", "\\Function:\n", "def find_route(lat_depart='0', lon_depart='0', city_depart='', address_destination='', depart_time='', **kwargs)\n", "\n", "\"\"\"\n", "Return the distance and the estimated time to go to a specific destination from the current place, at a specified depart time.\n", " :param lat_depart (string): latitude of depart\n", " :param lon_depart (string): longitude of depart\n", " :param city_depart (string): Required. city of depart\n", " :param address_destination (string): Required. The destination\n", " :param depart_time (string): departure hour, in the format '08:00:20'.\n", "\"\"\"\n", "\n", "\n", "\n", "Current status of the vehicle:\n", "{'date': '2024-05-02', 'time': '15:35:33', 'destination': 'Kirchberg, Luxembourg', 'lat': '48.8566', 'lon': '2.3522'}\n", "\n", "Previous conversation history:\n", "Human: How is the weather in Luxembourg?\n", "AI: The weather in Luxembourg is moderate or heavy rain with thunder, with a temperature of 15 degrees Celsius. The wind is blowing from the west at a speed of 28.1 kilometers per hour, and there is a 75% chance of cloud cover. It feels like 13.7 degrees Celsius outside.\n", "\n", "User Query: Question: How about in Tokyo?\n", "\n", "If it helps to answer the question please pick a function from the above options that best answers the user query and fill in the appropriate arguments.\n", "llm_output: Call: get_weather(location='Tokyo') \n", "\u001b[32;1m\u001b[1;3mCalling get_weather with args: {'location': 'Tokyo'}\u001b[0mhttp://api.weatherapi.com/v1/current.json?key=d1c4d0d8ef6847339a0125737240903&q=Tokyo&aqi=no\n", "\n", "\n", "Reflection:\u001b[36;1m\u001b[1;3m('The current weather in Tokyo, Tokyo, Japan is Clear with a temperature of 14.2°C that feels like 13.5°C. Humidity is at 69%. Wind speed is 6.1 kph.', {'location': {'name': 'Tokyo', 'region': 'Tokyo', 'country': 'Japan', 'lat': 35.69, 'lon': 139.69, 'tz_id': 'Asia/Tokyo', 'localtime_epoch': 1714657466, 'localtime': '2024-05-02 22:44'}, 'current': {'last_updated': '2024-05-02 22:30', 'temp_c': 14.2, 'condition': {'text': 'Clear'}, 'wind_kph': 6.1, 'wind_dir': 'W', 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 69, 'cloud': 0, 'feelslike_c': 13.5}})\u001b[0m\n", "kwargs: {'input': 'How about in Tokyo?', 'status': {'date': '2024-05-02', 'time': '15:35:33', 'destination': 'Kirchberg, Luxembourg', 'lat': '48.8566', 'lon': '2.3522'}, 'history': 'Human: How is the weather in Luxembourg?\\nAI: The weather in Luxembourg is moderate or heavy rain with thunder, with a temperature of 15 degrees Celsius. The wind is blowing from the west at a speed of 28.1 kilometers per hour, and there is a 75% chance of cloud cover. It feels like 13.7 degrees Celsius outside.', 'intermediate_steps': [(AgentAction(tool='get_weather', tool_input={'location': 'Tokyo'}, log=\"Calling get_weather with args: {'location': 'Tokyo'}\"), ('The current weather in Tokyo, Tokyo, Japan is Clear with a temperature of 14.2°C that feels like 13.5°C. Humidity is at 69%. Wind speed is 6.1 kph.', {'location': {'name': 'Tokyo', 'region': 'Tokyo', 'country': 'Japan', 'lat': 35.69, 'lon': 139.69, 'tz_id': 'Asia/Tokyo', 'localtime_epoch': 1714657466, 'localtime': '2024-05-02 22:44'}, 'current': {'last_updated': '2024-05-02 22:30', 'temp_c': 14.2, 'condition': {'text': 'Clear'}, 'wind_kph': 6.1, 'wind_dir': 'W', 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 69, 'cloud': 0, 'feelslike_c': 13.5}}))]}\n", "llm_output: The weather in Tokyo is currently clear with a temperature of 14.2 degrees Celsius and a wind speed of 6.1 kilometers per hour. There is a 0% chance of precipitation, and the humidity is at 69%. It feels like 13.5 degrees Celsius outside.\n", "\u001b[32;1m\u001b[1;3mThe weather in Tokyo is currently clear with a temperature of 14.2 degrees Celsius and a wind speed of 6.1 kilometers per hour. There is a 0% chance of precipitation, and the humidity is at 69%. It feels like 13.5 degrees Celsius outside.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] } ], "source": [ "call = agent_chain.run(\n", " input=\"How about in Tokyo?\",\n", " status= status\n", ")" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "kwargs: {'input': \"What's our destination?\", 'status': {'date': '2024-05-02', 'time': '15:53:47', 'destination': 'Kirchberg, Luxembourg', 'lat': '48.8566', 'lon': '2.3522'}, 'history': '', 'intermediate_steps': []}\n", "llm_output: Call: find_route(city_depart='Kirchberg', address_destination='Luxembourg') \n", "\u001b[32;1m\u001b[1;3mCalling find_route with args: {'city_depart': 'Kirchberg', 'address_destination': 'Luxembourg'}\u001b[0mLuxembourg\n" ] }, { "ename": "NameError", "evalue": "name 'check_city_coordinates' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[67], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m call \u001b[38;5;241m=\u001b[39m \u001b[43magent_chain\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 2\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mWhat\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ms our destination?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43mstatus\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mstatus\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 144\u001b[0m emit_warning()\n\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:550\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[0m\n\u001b[1;32m 545\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(args[\u001b[38;5;241m0\u001b[39m], callbacks\u001b[38;5;241m=\u001b[39mcallbacks, tags\u001b[38;5;241m=\u001b[39mtags, metadata\u001b[38;5;241m=\u001b[39mmetadata)[\n\u001b[1;32m 546\u001b[0m _output_key\n\u001b[1;32m 547\u001b[0m ]\n\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[0;32m--> 550\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n\u001b[1;32m 551\u001b[0m _output_key\n\u001b[1;32m 552\u001b[0m ]\n\u001b[1;32m 554\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[1;32m 555\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 556\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` supported with either positional arguments or keyword arguments,\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 557\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m but none were provided.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 558\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 144\u001b[0m emit_warning()\n\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:378\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[1;32m 346\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Execute the chain.\u001b[39;00m\n\u001b[1;32m 347\u001b[0m \n\u001b[1;32m 348\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 369\u001b[0m \u001b[38;5;124;03m `Chain.output_keys`.\u001b[39;00m\n\u001b[1;32m 370\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 371\u001b[0m config \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 372\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcallbacks\u001b[39m\u001b[38;5;124m\"\u001b[39m: callbacks,\n\u001b[1;32m 373\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtags\u001b[39m\u001b[38;5;124m\"\u001b[39m: tags,\n\u001b[1;32m 374\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmetadata\u001b[39m\u001b[38;5;124m\"\u001b[39m: metadata,\n\u001b[1;32m 375\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_name\u001b[39m\u001b[38;5;124m\"\u001b[39m: run_name,\n\u001b[1;32m 376\u001b[0m }\n\u001b[0;32m--> 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 379\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 380\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mRunnableConfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43mk\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 381\u001b[0m \u001b[43m \u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 382\u001b[0m \u001b[43m \u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude_run_info\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 383\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:163\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 162\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n\u001b[0;32m--> 163\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m 164\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_end(outputs)\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m include_run_info:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:153\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 151\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_inputs(inputs)\n\u001b[1;32m 152\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 153\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 155\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(inputs)\n\u001b[1;32m 156\u001b[0m )\n\u001b[1;32m 158\u001b[0m final_outputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprep_outputs(\n\u001b[1;32m 159\u001b[0m inputs, outputs, return_only_outputs\n\u001b[1;32m 160\u001b[0m )\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1432\u001b[0m, in \u001b[0;36mAgentExecutor._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m 1430\u001b[0m \u001b[38;5;66;03m# We now enter the agent loop (until it returns something).\u001b[39;00m\n\u001b[1;32m 1431\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_should_continue(iterations, time_elapsed):\n\u001b[0;32m-> 1432\u001b[0m next_step_output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_take_next_step\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1433\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1434\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1435\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1436\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1437\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1438\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1439\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(next_step_output, AgentFinish):\n\u001b[1;32m 1440\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_return(\n\u001b[1;32m 1441\u001b[0m next_step_output, intermediate_steps, run_manager\u001b[38;5;241m=\u001b[39mrun_manager\n\u001b[1;32m 1442\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m, in \u001b[0;36mAgentExecutor._take_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n\u001b[1;32m 1129\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_take_next_step\u001b[39m(\n\u001b[1;32m 1130\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1131\u001b[0m name_to_tool_map: Dict[\u001b[38;5;28mstr\u001b[39m, BaseTool],\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1135\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1136\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentFinish, List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]]]:\n\u001b[1;32m 1137\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_consume_next_step(\n\u001b[0;32m-> 1138\u001b[0m \u001b[43m[\u001b[49m\n\u001b[1;32m 1139\u001b[0m \u001b[43m \u001b[49m\u001b[43ma\u001b[49m\n\u001b[1;32m 1140\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_iter_next_step\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1141\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1142\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1143\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1144\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1145\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1146\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1147\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 1148\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m, in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 1129\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_take_next_step\u001b[39m(\n\u001b[1;32m 1130\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1131\u001b[0m name_to_tool_map: Dict[\u001b[38;5;28mstr\u001b[39m, BaseTool],\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1135\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1136\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentFinish, List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]]]:\n\u001b[1;32m 1137\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_consume_next_step(\n\u001b[0;32m-> 1138\u001b[0m \u001b[43m[\u001b[49m\n\u001b[1;32m 1139\u001b[0m \u001b[43m \u001b[49m\u001b[43ma\u001b[49m\n\u001b[1;32m 1140\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_iter_next_step\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1141\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1142\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1143\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1144\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1145\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1146\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1147\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 1148\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1223\u001b[0m, in \u001b[0;36mAgentExecutor._iter_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n\u001b[1;32m 1221\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m agent_action\n\u001b[1;32m 1222\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m agent_action \u001b[38;5;129;01min\u001b[39;00m actions:\n\u001b[0;32m-> 1223\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_perform_agent_action\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1224\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43magent_action\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\n\u001b[1;32m 1225\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1245\u001b[0m, in \u001b[0;36mAgentExecutor._perform_agent_action\u001b[0;34m(self, name_to_tool_map, color_mapping, agent_action, run_manager)\u001b[0m\n\u001b[1;32m 1243\u001b[0m tool_run_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mllm_prefix\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1244\u001b[0m \u001b[38;5;66;03m# We then call the tool on the tool input to get an observation\u001b[39;00m\n\u001b[0;32m-> 1245\u001b[0m observation \u001b[38;5;241m=\u001b[39m \u001b[43mtool\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1246\u001b[0m \u001b[43m \u001b[49m\u001b[43magent_action\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtool_input\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1247\u001b[0m \u001b[43m \u001b[49m\u001b[43mverbose\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mverbose\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1248\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolor\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1249\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_child\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1250\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mtool_run_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1251\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1252\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1253\u001b[0m tool_run_kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39magent\u001b[38;5;241m.\u001b[39mtool_run_logging_kwargs()\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/tools.py:417\u001b[0m, in \u001b[0;36mBaseTool.run\u001b[0;34m(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, **kwargs)\u001b[0m\n\u001b[1;32m 415\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mException\u001b[39;00m, \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 416\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_tool_error(e)\n\u001b[0;32m--> 417\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m 418\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 419\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_tool_end(observation, color\u001b[38;5;241m=\u001b[39mcolor, name\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/tools.py:376\u001b[0m, in \u001b[0;36mBaseTool.run\u001b[0;34m(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, **kwargs)\u001b[0m\n\u001b[1;32m 373\u001b[0m parsed_input \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parse_input(tool_input)\n\u001b[1;32m 374\u001b[0m tool_args, tool_kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_to_args_and_kwargs(parsed_input)\n\u001b[1;32m 375\u001b[0m observation \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 376\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_run\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mtool_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mtool_kwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 377\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 378\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_run(\u001b[38;5;241m*\u001b[39mtool_args, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mtool_kwargs)\n\u001b[1;32m 379\u001b[0m )\n\u001b[1;32m 380\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m ValidationError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 381\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandle_validation_error:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/tools.py:697\u001b[0m, in \u001b[0;36mStructuredTool._run\u001b[0;34m(self, run_manager, *args, **kwargs)\u001b[0m\n\u001b[1;32m 688\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfunc:\n\u001b[1;32m 689\u001b[0m new_argument_supported \u001b[38;5;241m=\u001b[39m signature(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfunc)\u001b[38;5;241m.\u001b[39mparameters\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcallbacks\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 690\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m (\n\u001b[1;32m 691\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfunc(\n\u001b[1;32m 692\u001b[0m \u001b[38;5;241m*\u001b[39margs,\n\u001b[1;32m 693\u001b[0m callbacks\u001b[38;5;241m=\u001b[39mrun_manager\u001b[38;5;241m.\u001b[39mget_child() \u001b[38;5;28;01mif\u001b[39;00m run_manager \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 694\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[1;32m 695\u001b[0m )\n\u001b[1;32m 696\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_argument_supported\n\u001b[0;32m--> 697\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 698\u001b[0m )\n\u001b[1;32m 699\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTool does not support sync\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m~/dev/uni/talking-car/skills/routing.py:39\u001b[0m, in \u001b[0;36mfind_route\u001b[0;34m(lat_depart, lon_depart, city_depart, address_destination, depart_time, **kwargs)\u001b[0m\n\u001b[1;32m 37\u001b[0m date \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m2025-03-29T\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 38\u001b[0m departure_time \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2024-02-01T\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m depart_time\n\u001b[0;32m---> 39\u001b[0m lat, lon, city \u001b[38;5;241m=\u001b[39m \u001b[43mcheck_city_coordinates\u001b[49m(lat_depart,lon_depart,city_depart)\n\u001b[1;32m 40\u001b[0m lat_dest, lon_dest \u001b[38;5;241m=\u001b[39m find_coordinates(address_destination)\n\u001b[1;32m 41\u001b[0m \u001b[38;5;66;03m#print(lat_dest, lon_dest)\u001b[39;00m\n\u001b[1;32m 42\u001b[0m \n\u001b[1;32m 43\u001b[0m \u001b[38;5;66;03m#print(departure_time)\u001b[39;00m\n", "\u001b[0;31mNameError\u001b[0m: name 'check_city_coordinates' is not defined" ] } ], "source": [ "call = agent_chain.run(\n", " input=\"What's our destination?\",\n", " status= status\n", ")" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "kwargs: {'input': 'How is the weather in Tokyo?', 'intermediate_steps': []}\n" ] }, { "ename": "KeyError", "evalue": "'history'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)\n", "Cell \u001b[0;32mIn[94], line 1\u001b[0m\n", "\u001b[0;32m----> 1\u001b[0m call \u001b[38;5;241m=\u001b[39m \u001b[43magent_chain\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 2\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHow is the weather in Tokyo?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n", "\u001b[1;32m 3\u001b[0m \u001b[43m)\u001b[49m\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n", "\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n", "\u001b[1;32m 144\u001b[0m emit_warning()\n", "\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:545\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[0m\n", "\u001b[1;32m 543\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(args) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m1\u001b[39m:\n", "\u001b[1;32m 544\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` supports only one positional argument.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;32m--> 545\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n", "\u001b[1;32m 546\u001b[0m _output_key\n", "\u001b[1;32m 547\u001b[0m ]\n", "\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n", "\u001b[1;32m 550\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(kwargs, callbacks\u001b[38;5;241m=\u001b[39mcallbacks, tags\u001b[38;5;241m=\u001b[39mtags, metadata\u001b[38;5;241m=\u001b[39mmetadata)[\n", "\u001b[1;32m 551\u001b[0m _output_key\n", "\u001b[1;32m 552\u001b[0m ]\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n", "\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n", "\u001b[1;32m 144\u001b[0m emit_warning()\n", "\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:378\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n", "\u001b[1;32m 346\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Execute the chain.\u001b[39;00m\n", "\u001b[1;32m 347\u001b[0m \n", "\u001b[1;32m 348\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n", "\u001b[0;32m (...)\u001b[0m\n", "\u001b[1;32m 369\u001b[0m \u001b[38;5;124;03m `Chain.output_keys`.\u001b[39;00m\n", "\u001b[1;32m 370\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n", "\u001b[1;32m 371\u001b[0m config \u001b[38;5;241m=\u001b[39m {\n", "\u001b[1;32m 372\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcallbacks\u001b[39m\u001b[38;5;124m\"\u001b[39m: callbacks,\n", "\u001b[1;32m 373\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtags\u001b[39m\u001b[38;5;124m\"\u001b[39m: tags,\n", "\u001b[1;32m 374\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmetadata\u001b[39m\u001b[38;5;124m\"\u001b[39m: metadata,\n", "\u001b[1;32m 375\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_name\u001b[39m\u001b[38;5;124m\"\u001b[39m: run_name,\n", "\u001b[1;32m 376\u001b[0m }\n", "\u001b[0;32m--> 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 379\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 380\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mRunnableConfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43mk\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 381\u001b[0m \u001b[43m \u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 382\u001b[0m \u001b[43m \u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude_run_info\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 383\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:163\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n", "\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", "\u001b[1;32m 162\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n", "\u001b[0;32m--> 163\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n", "\u001b[1;32m 164\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_end(outputs)\n", "\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m include_run_info:\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:153\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n", "\u001b[1;32m 150\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", "\u001b[1;32m 151\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_inputs(inputs)\n", "\u001b[1;32m 152\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n", "\u001b[0;32m--> 153\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n", "\u001b[1;32m 155\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(inputs)\n", "\u001b[1;32m 156\u001b[0m )\n", "\u001b[1;32m 158\u001b[0m final_outputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprep_outputs(\n", "\u001b[1;32m 159\u001b[0m inputs, outputs, return_only_outputs\n", "\u001b[1;32m 160\u001b[0m )\n", "\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1432\u001b[0m, in \u001b[0;36mAgentExecutor._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n", "\u001b[1;32m 1430\u001b[0m \u001b[38;5;66;03m# We now enter the agent loop (until it returns something).\u001b[39;00m\n", "\u001b[1;32m 1431\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_should_continue(iterations, time_elapsed):\n", "\u001b[0;32m-> 1432\u001b[0m next_step_output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_take_next_step\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 1433\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1434\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1435\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1436\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1437\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1438\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 1439\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(next_step_output, AgentFinish):\n", "\u001b[1;32m 1440\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_return(\n", "\u001b[1;32m 1441\u001b[0m next_step_output, intermediate_steps, run_manager\u001b[38;5;241m=\u001b[39mrun_manager\n", "\u001b[1;32m 1442\u001b[0m )\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m, in \u001b[0;36mAgentExecutor._take_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n", "\u001b[1;32m 1129\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_take_next_step\u001b[39m(\n", "\u001b[1;32m 1130\u001b[0m \u001b[38;5;28mself\u001b[39m,\n", "\u001b[1;32m 1131\u001b[0m name_to_tool_map: Dict[\u001b[38;5;28mstr\u001b[39m, BaseTool],\n", "\u001b[0;32m (...)\u001b[0m\n", "\u001b[1;32m 1135\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n", "\u001b[1;32m 1136\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentFinish, List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]]]:\n", "\u001b[1;32m 1137\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_consume_next_step(\n", "\u001b[0;32m-> 1138\u001b[0m \u001b[43m[\u001b[49m\n", "\u001b[1;32m 1139\u001b[0m \u001b[43m \u001b[49m\u001b[43ma\u001b[49m\n", "\u001b[1;32m 1140\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_iter_next_step\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 1141\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1142\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1143\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1144\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1145\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1146\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 1147\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n", "\u001b[1;32m 1148\u001b[0m )\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m, in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n", "\u001b[1;32m 1129\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_take_next_step\u001b[39m(\n", "\u001b[1;32m 1130\u001b[0m \u001b[38;5;28mself\u001b[39m,\n", "\u001b[1;32m 1131\u001b[0m name_to_tool_map: Dict[\u001b[38;5;28mstr\u001b[39m, BaseTool],\n", "\u001b[0;32m (...)\u001b[0m\n", "\u001b[1;32m 1135\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n", "\u001b[1;32m 1136\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentFinish, List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]]]:\n", "\u001b[1;32m 1137\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_consume_next_step(\n", "\u001b[0;32m-> 1138\u001b[0m \u001b[43m[\u001b[49m\n", "\u001b[1;32m 1139\u001b[0m \u001b[43m \u001b[49m\u001b[43ma\u001b[49m\n", "\u001b[1;32m 1140\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_iter_next_step\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 1141\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1142\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1143\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1144\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1145\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1146\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 1147\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n", "\u001b[1;32m 1148\u001b[0m )\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1166\u001b[0m, in \u001b[0;36mAgentExecutor._iter_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n", "\u001b[1;32m 1163\u001b[0m intermediate_steps \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_intermediate_steps(intermediate_steps)\n", "\u001b[1;32m 1165\u001b[0m \u001b[38;5;66;03m# Call the LLM to see what to do.\u001b[39;00m\n", "\u001b[0;32m-> 1166\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplan\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 1167\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1168\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_child\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1169\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 1170\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 1171\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m OutputParserException \u001b[38;5;28;01mas\u001b[39;00m e:\n", "\u001b[1;32m 1172\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandle_parsing_errors, \u001b[38;5;28mbool\u001b[39m):\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:618\u001b[0m, in \u001b[0;36mLLMSingleActionAgent.plan\u001b[0;34m(self, intermediate_steps, callbacks, **kwargs)\u001b[0m\n", "\u001b[1;32m 601\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mplan\u001b[39m(\n", "\u001b[1;32m 602\u001b[0m \u001b[38;5;28mself\u001b[39m,\n", "\u001b[1;32m 603\u001b[0m intermediate_steps: List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]],\n", "\u001b[1;32m 604\u001b[0m callbacks: Callbacks \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n", "\u001b[1;32m 605\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any,\n", "\u001b[1;32m 606\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentAction, AgentFinish]:\n", "\u001b[1;32m 607\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Given input, decided what to do.\u001b[39;00m\n", "\u001b[1;32m 608\u001b[0m \n", "\u001b[1;32m 609\u001b[0m \u001b[38;5;124;03m Args:\u001b[39;00m\n", "\u001b[0;32m (...)\u001b[0m\n", "\u001b[1;32m 616\u001b[0m \u001b[38;5;124;03m Action specifying what tool to use.\u001b[39;00m\n", "\u001b[1;32m 617\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n", "\u001b[0;32m--> 618\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mllm_chain\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 619\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 620\u001b[0m \u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 621\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 622\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 623\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 624\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_parser\u001b[38;5;241m.\u001b[39mparse(output)\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n", "\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n", "\u001b[1;32m 144\u001b[0m emit_warning()\n", "\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:550\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[0m\n", "\u001b[1;32m 545\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(args[\u001b[38;5;241m0\u001b[39m], callbacks\u001b[38;5;241m=\u001b[39mcallbacks, tags\u001b[38;5;241m=\u001b[39mtags, metadata\u001b[38;5;241m=\u001b[39mmetadata)[\n", "\u001b[1;32m 546\u001b[0m _output_key\n", "\u001b[1;32m 547\u001b[0m ]\n", "\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n", "\u001b[0;32m--> 550\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n", "\u001b[1;32m 551\u001b[0m _output_key\n", "\u001b[1;32m 552\u001b[0m ]\n", "\u001b[1;32m 554\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n", "\u001b[1;32m 555\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n", "\u001b[1;32m 556\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` supported with either positional arguments or keyword arguments,\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[1;32m 557\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m but none were provided.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[1;32m 558\u001b[0m )\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n", "\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n", "\u001b[1;32m 144\u001b[0m emit_warning()\n", "\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:378\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n", "\u001b[1;32m 346\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Execute the chain.\u001b[39;00m\n", "\u001b[1;32m 347\u001b[0m \n", "\u001b[1;32m 348\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n", "\u001b[0;32m (...)\u001b[0m\n", "\u001b[1;32m 369\u001b[0m \u001b[38;5;124;03m `Chain.output_keys`.\u001b[39;00m\n", "\u001b[1;32m 370\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n", "\u001b[1;32m 371\u001b[0m config \u001b[38;5;241m=\u001b[39m {\n", "\u001b[1;32m 372\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcallbacks\u001b[39m\u001b[38;5;124m\"\u001b[39m: callbacks,\n", "\u001b[1;32m 373\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtags\u001b[39m\u001b[38;5;124m\"\u001b[39m: tags,\n", "\u001b[1;32m 374\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmetadata\u001b[39m\u001b[38;5;124m\"\u001b[39m: metadata,\n", "\u001b[1;32m 375\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_name\u001b[39m\u001b[38;5;124m\"\u001b[39m: run_name,\n", "\u001b[1;32m 376\u001b[0m }\n", "\u001b[0;32m--> 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n", "\u001b[1;32m 379\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 380\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mRunnableConfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43mk\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 381\u001b[0m \u001b[43m \u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 382\u001b[0m \u001b[43m \u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude_run_info\u001b[49m\u001b[43m,\u001b[49m\n", "\u001b[1;32m 383\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:163\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n", "\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", "\u001b[1;32m 162\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n", "\u001b[0;32m--> 163\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n", "\u001b[1;32m 164\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_end(outputs)\n", "\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m include_run_info:\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:153\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n", "\u001b[1;32m 150\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", "\u001b[1;32m 151\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_inputs(inputs)\n", "\u001b[1;32m 152\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n", "\u001b[0;32m--> 153\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n", "\u001b[1;32m 155\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(inputs)\n", "\u001b[1;32m 156\u001b[0m )\n", "\u001b[1;32m 158\u001b[0m final_outputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprep_outputs(\n", "\u001b[1;32m 159\u001b[0m inputs, outputs, return_only_outputs\n", "\u001b[1;32m 160\u001b[0m )\n", "\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/llm.py:103\u001b[0m, in \u001b[0;36mLLMChain._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n", "\u001b[1;32m 98\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_call\u001b[39m(\n", "\u001b[1;32m 99\u001b[0m \u001b[38;5;28mself\u001b[39m,\n", "\u001b[1;32m 100\u001b[0m inputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any],\n", "\u001b[1;32m 101\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n", "\u001b[1;32m 102\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Dict[\u001b[38;5;28mstr\u001b[39m, \u001b[38;5;28mstr\u001b[39m]:\n", "\u001b[0;32m--> 103\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 104\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcreate_outputs(response)[\u001b[38;5;241m0\u001b[39m]\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/llm.py:112\u001b[0m, in \u001b[0;36mLLMChain.generate\u001b[0;34m(self, input_list, run_manager)\u001b[0m\n", "\u001b[1;32m 106\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mgenerate\u001b[39m(\n", "\u001b[1;32m 107\u001b[0m \u001b[38;5;28mself\u001b[39m,\n", "\u001b[1;32m 108\u001b[0m input_list: List[Dict[\u001b[38;5;28mstr\u001b[39m, Any]],\n", "\u001b[1;32m 109\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n", "\u001b[1;32m 110\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m LLMResult:\n", "\u001b[1;32m 111\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Generate LLM result from inputs.\"\"\"\u001b[39;00m\n", "\u001b[0;32m--> 112\u001b[0m prompts, stop \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprep_prompts\u001b[49m\u001b[43m(\u001b[49m\u001b[43minput_list\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 113\u001b[0m callbacks \u001b[38;5;241m=\u001b[39m run_manager\u001b[38;5;241m.\u001b[39mget_child() \u001b[38;5;28;01mif\u001b[39;00m run_manager \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "\u001b[1;32m 114\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mllm, BaseLanguageModel):\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/llm.py:174\u001b[0m, in \u001b[0;36mLLMChain.prep_prompts\u001b[0;34m(self, input_list, run_manager)\u001b[0m\n", "\u001b[1;32m 172\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m inputs \u001b[38;5;129;01min\u001b[39;00m input_list:\n", "\u001b[1;32m 173\u001b[0m selected_inputs \u001b[38;5;241m=\u001b[39m {k: inputs[k] \u001b[38;5;28;01mfor\u001b[39;00m k \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprompt\u001b[38;5;241m.\u001b[39minput_variables}\n", "\u001b[0;32m--> 174\u001b[0m prompt \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprompt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat_prompt\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mselected_inputs\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;32m 175\u001b[0m _colored_text \u001b[38;5;241m=\u001b[39m get_colored_text(prompt\u001b[38;5;241m.\u001b[39mto_string(), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgreen\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[1;32m 176\u001b[0m _text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPrompt after formatting:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m _colored_text\n", "\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/prompts/string.py:164\u001b[0m, in \u001b[0;36mStringPromptTemplate.format_prompt\u001b[0;34m(self, **kwargs)\u001b[0m\n", "\u001b[1;32m 162\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mformat_prompt\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m PromptValue:\n", "\u001b[1;32m 163\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Create Chat Messages.\"\"\"\u001b[39;00m\n", "\u001b[0;32m--> 164\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m StringPromptValue(text\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m)\n", "\n", "Cell \u001b[0;32mIn[88], line 50\u001b[0m, in \u001b[0;36mRavenPromptTemplate.format\u001b[0;34m(self, **kwargs)\u001b[0m\n", "\u001b[1;32m 48\u001b[0m prompt \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mFunction:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mdef \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfunc_signature\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mfunc_docstring\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n", "\u001b[1;32m 49\u001b[0m kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraven_tools\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m prompt\n", "\u001b[0;32m---> 50\u001b[0m pp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtemplate_func\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m{{\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m{\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m}}\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m}\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[1;32m 51\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m pp\n", "\n", "\u001b[0;31mKeyError\u001b[0m: 'history'" ] } ], "source": [ "call = agent_chain.run(\n", " \"How is the weather in Tokyo?\"\n", ")" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The current weather conditions in Tokyo, Japan are partly cloudy with temperatures of 17°C and a wind speed of 17.4 mph.'" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "call" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "from langchain.memory import ConversationBufferWindowMemory\n", "memory = ConversationBufferWindowMemory(k=2)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)\n" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "kwargs: {'input': 'How is the weather in Luxembourg?', 'intermediate_steps': []}\n" ] }, { "ename": "KeyError", "evalue": "'history'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[96], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43magent_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHow is the weather in Luxembourg?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 144\u001b[0m emit_warning()\n\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:545\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[0m\n\u001b[1;32m 543\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(args) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 544\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` supports only one positional argument.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 545\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n\u001b[1;32m 546\u001b[0m _output_key\n\u001b[1;32m 547\u001b[0m ]\n\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[1;32m 550\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(kwargs, callbacks\u001b[38;5;241m=\u001b[39mcallbacks, tags\u001b[38;5;241m=\u001b[39mtags, metadata\u001b[38;5;241m=\u001b[39mmetadata)[\n\u001b[1;32m 551\u001b[0m _output_key\n\u001b[1;32m 552\u001b[0m ]\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 144\u001b[0m emit_warning()\n\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:378\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[1;32m 346\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Execute the chain.\u001b[39;00m\n\u001b[1;32m 347\u001b[0m \n\u001b[1;32m 348\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 369\u001b[0m \u001b[38;5;124;03m `Chain.output_keys`.\u001b[39;00m\n\u001b[1;32m 370\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 371\u001b[0m config \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 372\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcallbacks\u001b[39m\u001b[38;5;124m\"\u001b[39m: callbacks,\n\u001b[1;32m 373\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtags\u001b[39m\u001b[38;5;124m\"\u001b[39m: tags,\n\u001b[1;32m 374\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmetadata\u001b[39m\u001b[38;5;124m\"\u001b[39m: metadata,\n\u001b[1;32m 375\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_name\u001b[39m\u001b[38;5;124m\"\u001b[39m: run_name,\n\u001b[1;32m 376\u001b[0m }\n\u001b[0;32m--> 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 379\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 380\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mRunnableConfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43mk\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 381\u001b[0m \u001b[43m \u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 382\u001b[0m \u001b[43m \u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude_run_info\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 383\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:163\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 162\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n\u001b[0;32m--> 163\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m 164\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_end(outputs)\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m include_run_info:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:153\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 151\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_inputs(inputs)\n\u001b[1;32m 152\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 153\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 155\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(inputs)\n\u001b[1;32m 156\u001b[0m )\n\u001b[1;32m 158\u001b[0m final_outputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprep_outputs(\n\u001b[1;32m 159\u001b[0m inputs, outputs, return_only_outputs\n\u001b[1;32m 160\u001b[0m )\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1432\u001b[0m, in \u001b[0;36mAgentExecutor._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m 1430\u001b[0m \u001b[38;5;66;03m# We now enter the agent loop (until it returns something).\u001b[39;00m\n\u001b[1;32m 1431\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_should_continue(iterations, time_elapsed):\n\u001b[0;32m-> 1432\u001b[0m next_step_output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_take_next_step\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1433\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1434\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1435\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1436\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1437\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1438\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1439\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(next_step_output, AgentFinish):\n\u001b[1;32m 1440\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_return(\n\u001b[1;32m 1441\u001b[0m next_step_output, intermediate_steps, run_manager\u001b[38;5;241m=\u001b[39mrun_manager\n\u001b[1;32m 1442\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m, in \u001b[0;36mAgentExecutor._take_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n\u001b[1;32m 1129\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_take_next_step\u001b[39m(\n\u001b[1;32m 1130\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1131\u001b[0m name_to_tool_map: Dict[\u001b[38;5;28mstr\u001b[39m, BaseTool],\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1135\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1136\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentFinish, List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]]]:\n\u001b[1;32m 1137\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_consume_next_step(\n\u001b[0;32m-> 1138\u001b[0m \u001b[43m[\u001b[49m\n\u001b[1;32m 1139\u001b[0m \u001b[43m \u001b[49m\u001b[43ma\u001b[49m\n\u001b[1;32m 1140\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_iter_next_step\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1141\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1142\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1143\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1144\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1145\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1146\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1147\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 1148\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m, in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 1129\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_take_next_step\u001b[39m(\n\u001b[1;32m 1130\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1131\u001b[0m name_to_tool_map: Dict[\u001b[38;5;28mstr\u001b[39m, BaseTool],\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1135\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 1136\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentFinish, List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]]]:\n\u001b[1;32m 1137\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_consume_next_step(\n\u001b[0;32m-> 1138\u001b[0m \u001b[43m[\u001b[49m\n\u001b[1;32m 1139\u001b[0m \u001b[43m \u001b[49m\u001b[43ma\u001b[49m\n\u001b[1;32m 1140\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_iter_next_step\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1141\u001b[0m \u001b[43m \u001b[49m\u001b[43mname_to_tool_map\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1142\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolor_mapping\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1143\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1144\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1145\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1146\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1147\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 1148\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1166\u001b[0m, in \u001b[0;36mAgentExecutor._iter_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n\u001b[1;32m 1163\u001b[0m intermediate_steps \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_intermediate_steps(intermediate_steps)\n\u001b[1;32m 1165\u001b[0m \u001b[38;5;66;03m# Call the LLM to see what to do.\u001b[39;00m\n\u001b[0;32m-> 1166\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplan\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1167\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1168\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_child\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1169\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1170\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1171\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m OutputParserException \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 1172\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandle_parsing_errors, \u001b[38;5;28mbool\u001b[39m):\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:618\u001b[0m, in \u001b[0;36mLLMSingleActionAgent.plan\u001b[0;34m(self, intermediate_steps, callbacks, **kwargs)\u001b[0m\n\u001b[1;32m 601\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mplan\u001b[39m(\n\u001b[1;32m 602\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 603\u001b[0m intermediate_steps: List[Tuple[AgentAction, \u001b[38;5;28mstr\u001b[39m]],\n\u001b[1;32m 604\u001b[0m callbacks: Callbacks \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 605\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any,\n\u001b[1;32m 606\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentAction, AgentFinish]:\n\u001b[1;32m 607\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Given input, decided what to do.\u001b[39;00m\n\u001b[1;32m 608\u001b[0m \n\u001b[1;32m 609\u001b[0m \u001b[38;5;124;03m Args:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 616\u001b[0m \u001b[38;5;124;03m Action specifying what tool to use.\u001b[39;00m\n\u001b[1;32m 617\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 618\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mllm_chain\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 619\u001b[0m \u001b[43m \u001b[49m\u001b[43mintermediate_steps\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mintermediate_steps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 620\u001b[0m \u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 621\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 622\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 623\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 624\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_parser\u001b[38;5;241m.\u001b[39mparse(output)\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 144\u001b[0m emit_warning()\n\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:550\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[0m\n\u001b[1;32m 545\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(args[\u001b[38;5;241m0\u001b[39m], callbacks\u001b[38;5;241m=\u001b[39mcallbacks, tags\u001b[38;5;241m=\u001b[39mtags, metadata\u001b[38;5;241m=\u001b[39mmetadata)[\n\u001b[1;32m 546\u001b[0m _output_key\n\u001b[1;32m 547\u001b[0m ]\n\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[0;32m--> 550\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n\u001b[1;32m 551\u001b[0m _output_key\n\u001b[1;32m 552\u001b[0m ]\n\u001b[1;32m 554\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[1;32m 555\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 556\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`run` supported with either positional arguments or keyword arguments,\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 557\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m but none were provided.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 558\u001b[0m )\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m, in \u001b[0;36mdeprecated..deprecate..warning_emitting_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 143\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 144\u001b[0m emit_warning()\n\u001b[0;32m--> 145\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:378\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[0m\n\u001b[1;32m 346\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Execute the chain.\u001b[39;00m\n\u001b[1;32m 347\u001b[0m \n\u001b[1;32m 348\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 369\u001b[0m \u001b[38;5;124;03m `Chain.output_keys`.\u001b[39;00m\n\u001b[1;32m 370\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 371\u001b[0m config \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 372\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcallbacks\u001b[39m\u001b[38;5;124m\"\u001b[39m: callbacks,\n\u001b[1;32m 373\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtags\u001b[39m\u001b[38;5;124m\"\u001b[39m: tags,\n\u001b[1;32m 374\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmetadata\u001b[39m\u001b[38;5;124m\"\u001b[39m: metadata,\n\u001b[1;32m 375\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_name\u001b[39m\u001b[38;5;124m\"\u001b[39m: run_name,\n\u001b[1;32m 376\u001b[0m }\n\u001b[0;32m--> 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 379\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 380\u001b[0m \u001b[43m \u001b[49m\u001b[43mcast\u001b[49m\u001b[43m(\u001b[49m\u001b[43mRunnableConfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43mk\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 381\u001b[0m \u001b[43m \u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 382\u001b[0m \u001b[43m \u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude_run_info\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 383\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:163\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 162\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n\u001b[0;32m--> 163\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m 164\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_end(outputs)\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m include_run_info:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:153\u001b[0m, in \u001b[0;36mChain.invoke\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 151\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_inputs(inputs)\n\u001b[1;32m 152\u001b[0m outputs \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 153\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 155\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call(inputs)\n\u001b[1;32m 156\u001b[0m )\n\u001b[1;32m 158\u001b[0m final_outputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprep_outputs(\n\u001b[1;32m 159\u001b[0m inputs, outputs, return_only_outputs\n\u001b[1;32m 160\u001b[0m )\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/llm.py:103\u001b[0m, in \u001b[0;36mLLMChain._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m 98\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_call\u001b[39m(\n\u001b[1;32m 99\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 100\u001b[0m inputs: Dict[\u001b[38;5;28mstr\u001b[39m, Any],\n\u001b[1;32m 101\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 102\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Dict[\u001b[38;5;28mstr\u001b[39m, \u001b[38;5;28mstr\u001b[39m]:\n\u001b[0;32m--> 103\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 104\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcreate_outputs(response)[\u001b[38;5;241m0\u001b[39m]\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/llm.py:112\u001b[0m, in \u001b[0;36mLLMChain.generate\u001b[0;34m(self, input_list, run_manager)\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mgenerate\u001b[39m(\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 108\u001b[0m input_list: List[Dict[\u001b[38;5;28mstr\u001b[39m, Any]],\n\u001b[1;32m 109\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 110\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m LLMResult:\n\u001b[1;32m 111\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Generate LLM result from inputs.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 112\u001b[0m prompts, stop \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprep_prompts\u001b[49m\u001b[43m(\u001b[49m\u001b[43minput_list\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 113\u001b[0m callbacks \u001b[38;5;241m=\u001b[39m run_manager\u001b[38;5;241m.\u001b[39mget_child() \u001b[38;5;28;01mif\u001b[39;00m run_manager \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 114\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mllm, BaseLanguageModel):\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/llm.py:174\u001b[0m, in \u001b[0;36mLLMChain.prep_prompts\u001b[0;34m(self, input_list, run_manager)\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m inputs \u001b[38;5;129;01min\u001b[39;00m input_list:\n\u001b[1;32m 173\u001b[0m selected_inputs \u001b[38;5;241m=\u001b[39m {k: inputs[k] \u001b[38;5;28;01mfor\u001b[39;00m k \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprompt\u001b[38;5;241m.\u001b[39minput_variables}\n\u001b[0;32m--> 174\u001b[0m prompt \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprompt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat_prompt\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mselected_inputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 175\u001b[0m _colored_text \u001b[38;5;241m=\u001b[39m get_colored_text(prompt\u001b[38;5;241m.\u001b[39mto_string(), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgreen\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 176\u001b[0m _text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPrompt after formatting:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m _colored_text\n", "File \u001b[0;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/prompts/string.py:164\u001b[0m, in \u001b[0;36mStringPromptTemplate.format_prompt\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mformat_prompt\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m PromptValue:\n\u001b[1;32m 163\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Create Chat Messages.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 164\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m StringPromptValue(text\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m)\n", "Cell \u001b[0;32mIn[88], line 50\u001b[0m, in \u001b[0;36mRavenPromptTemplate.format\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 48\u001b[0m prompt \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mFunction:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mdef \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfunc_signature\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mfunc_docstring\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 49\u001b[0m kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraven_tools\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m prompt\n\u001b[0;32m---> 50\u001b[0m pp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtemplate_func\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m{{\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m{\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m}}\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m}\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 51\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m pp\n", "\u001b[0;31mKeyError\u001b[0m: 'history'" ] } ], "source": [ "agent_executor.run(\"How is the weather in Luxembourg?\")" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "kwargs: {'input': 'How about in Dubai?', 'intermediate_steps': []}\n", "llm_output: \n", "Call: find_route(city_depart='Dubai', address_destination='', depart_time=) \n" ] }, { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/IPython/core/interactiveshell.py:3577\u001b[0m in \u001b[1;35mrun_code\u001b[0m\n exec(code_obj, self.user_global_ns, self.user_ns)\u001b[0m\n", "\u001b[0m Cell \u001b[1;32mIn[87], line 1\u001b[0m\n agent_executor.run(\"How about in Dubai?\")\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m in \u001b[1;35mwarning_emitting_wrapper\u001b[0m\n return wrapped(*args, **kwargs)\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:545\u001b[0m in \u001b[1;35mrun\u001b[0m\n return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:145\u001b[0m in \u001b[1;35mwarning_emitting_wrapper\u001b[0m\n return wrapped(*args, **kwargs)\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:378\u001b[0m in \u001b[1;35m__call__\u001b[0m\n return self.invoke(\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:163\u001b[0m in \u001b[1;35minvoke\u001b[0m\n raise e\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/chains/base.py:153\u001b[0m in \u001b[1;35minvoke\u001b[0m\n self._call(inputs, run_manager=run_manager)\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1432\u001b[0m in \u001b[1;35m_call\u001b[0m\n next_step_output = self._take_next_step(\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m in \u001b[1;35m_take_next_step\u001b[0m\n [\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1138\u001b[0m in \u001b[1;35m\u001b[0m\n [\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:1166\u001b[0m in \u001b[1;35m_iter_next_step\u001b[0m\n output = self.agent.plan(\u001b[0m\n", "\u001b[0m File \u001b[1;32m/opt/homebrew/Caskroom/miniconda/base/envs/llm/lib/python3.11/site-packages/langchain/agents/agent.py:624\u001b[0m in \u001b[1;35mplan\u001b[0m\n return self.output_parser.parse(output)\u001b[0m\n", "\u001b[0m Cell \u001b[1;32mIn[51], line 21\u001b[0m in \u001b[1;35mparse\u001b[0m\n func_name, args = extract_func_args(llm_output)\u001b[0m\n", "\u001b[0;36m File \u001b[0;32m~/dev/uni/talking-car/skills/common.py:41\u001b[0;36m in \u001b[0;35mextract_func_args\u001b[0;36m\n\u001b[0;31m arguments = eval(f\"dict{text.split(function_name)[1].strip()}\")\u001b[0;36m\n", "\u001b[0;36m File \u001b[0;32m:1\u001b[0;36m\u001b[0m\n\u001b[0;31m dict(city_depart='Dubai', address_destination='', depart_time=)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "agent_executor.run(\"How about in Dubai?\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n", "\n", "demo_ephemeral_chat_history_for_chain = ChatMessageHistory()\n", "conversational_agent_executor = RunnableWithMessageHistory(\n", " agent_executor,\n", " lambda session_id: demo_ephemeral_chat_history_for_chain,\n", " input_messages_key=\"input\",\n", " output_messages_key=\"output\",\n", " history_messages_key=\"chat_history\",\n", ")" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "\n", "##############################\n", "# Step 3: Construct Prompt ###\n", "##############################\n", "\n", "\n", "def construct_prompt(user_query: str, context):\n", " formatted_prompt = format_functions_for_prompt(get_weather, find_points_of_interest, find_route, get_forecast, search_along_route)\n", " formatted_prompt += f'\\n\\nContext : {context}'\n", " formatted_prompt += f\"\\n\\nUser Query: Question: {user_query}\\n\"\n", "\n", " prompt = (\n", " \":\\n\"\n", " + formatted_prompt\n", " + \"Please pick a function from the above options that best answers the user query and fill in the appropriate arguments.\"\n", " )\n", " return prompt\n", "\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "# convert bytes to megabytes\n", "def get_cuda_usage(): return round(torch.cuda.memory_allocated(\"cuda:0\")/1024/1024,2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# might be deleted\n", "# Compute a Simple equation\n", "print(f\"before everything: {get_cuda_usage()}\")\n", "prompt = construct_prompt(\"What restaurants are there on the road from Luxembourg Gare, which coordinates are lat 49.5999681, lon 6.1342493, to Thionville?\", \"\")\n", "print(f\"after creating prompt: {get_cuda_usage()}\")\n", "model_output = pipe(\n", " prompt, do_sample=False, max_new_tokens=300, return_full_text=False\n", " )\n", "print(model_output[0][\"generated_text\"])\n", "#execute_function_call(pipe(construct_prompt(\"Is it raining in Belval, ?\"), do_sample=False, max_new_tokens=300, return_full_text=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"creating the pipe of model output: {get_cuda_usage()}\")\n", "result = execute_function_call(model_output)\n", "print(f\"after execute function call: {get_cuda_usage()}\")\n", "del model_output\n", "import gc # garbage collect library\n", "gc.collect()\n", "torch.cuda.empty_cache() \n", "print(f\"after garbage collect and empty_cache: {get_cuda_usage()}\")\n", "#print(\"Model Output:\", model_output)\n", "# print(\"Execution Result:\", result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## functions to process the anwser and the question" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#generation of text with Stable beluga \n", "def gen(p, maxlen=15, sample=True):\n", " toks = tokr(p, return_tensors=\"pt\")\n", " res = model.generate(**toks.to(\"cuda\"), max_new_tokens=maxlen, do_sample=sample).to('cpu')\n", " return tokr.batch_decode(res)\n", "\n", "#to have a prompt corresponding to the specific format required by the fine-tuned model Stable Beluga\n", "def mk_prompt(user, syst=\"### System:\\nYou are a useful AI assistant in a car, that follows instructions extremely well. Help as much as you can. Answer questions concisely and do not mention what you base your reply on.\\n\\n\"): return f\"{syst}### User: {user}\\n\\n### Assistant:\\n\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "yAJI0WyOLE8G" }, "outputs": [], "source": [ "def car_answer_only(complete_answer, general_context):\n", " \"\"\"returns only the AI assistant answer, without all context, to reply to the user\"\"\"\n", " pattern = r\"Assistant:\\\\n(.*)(|[.!?](\\s|$))\" #pattern = r\"Assistant:\\\\n(.*?)\"\n", "\n", " match = re.search(pattern, complete_answer, re.DOTALL)\n", "\n", " if match:\n", " # Extracting the text\n", " model_answer = match.group(1)\n", " #print(complete_answer)\n", " else:\n", " #print(complete_answer)\n", " model_answer = \"There has been an error with the generated response.\" \n", "\n", " general_context += model_answer\n", " return (model_answer, general_context)\n", "#print(model_answer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ViCEgogaENNV" }, "outputs": [], "source": [ "def FnAnswer(general_context, ques, place, time, delete_history, state):\n", " \"\"\"function to manage the two different llms (function calling and basic answer) and call them one after the other\"\"\"\n", " # Initialize state if it is None\n", " if delete_history == \"Yes\":\n", " state = None\n", " if state is None:\n", " conv_context = []\n", " conv_context.append(general_context)\n", " state = {}\n", " state['context'] = conv_context\n", " state['number'] = 0\n", " state['last_question'] = \"\"\n", " \n", " if type(ques) != str: \n", " ques = ques[0]\n", " \n", " place = definePlace(place) #which on the predefined places it is\n", " \n", " formatted_context = '\\n'.join(state['context'])\n", " \n", " #updated at every question\n", " general_context = f\"\"\"\n", " Recent conversation history: '{formatted_context}' (If empty, this indicates the beginning of the conversation).\n", "\n", " Previous question from the user: '{state['last_question']}' (This may or may not be related to the current question).\n", "\n", " User information: The user is inside a car in {place[0]}, with latitude {place[1]} and longitude {place[2]}. The user is mobile and can drive to different destinations. It is currently {time}\n", "\n", " \"\"\"\n", " #first llm call (function calling model, NexusRaven)\n", " model_output= pipe(construct_prompt(ques, general_context), do_sample=False, max_new_tokens=300, return_full_text=False)\n", " call = execute_function_call(model_output) #call variable is formatted to as a call to a specific function with the required parameters\n", " print(call)\n", " #this is what will erase the model_output from the GPU memory to free up space\n", " del model_output\n", " import gc # garbage collect library\n", " gc.collect()\n", " torch.cuda.empty_cache() \n", " \n", " #updated at every question\n", " general_context += f'This information might be of help, use if it seems relevant, and ignore if not relevant to reply to the user: \"{call}\". '\n", " \n", " #question formatted for the StableBeluga llm (second llm), using the output of the first llm as context in general_context\n", " question=f\"\"\"Reply to the user and answer any question with the help of the provided context.\n", "\n", " ## Context\n", "\n", " {general_context} .\n", "\n", " ## Question\n", "\n", " {ques}\"\"\"\n", "\n", " complete_answer = str(gen(mk_prompt(question), 100)) #answer generation with StableBeluga (2nd llm)\n", "\n", " model_answer, general_context= car_answer_only(complete_answer, general_context) #to retrieve only the car answer \n", " \n", " language = pipe_language(model_answer, top_k=1, truncation=True)[0]['label'] #detect the language of the answer, to modify the text-to-speech consequently\n", " \n", " state['last_question'] = ques #add the current question as 'last question' for the next question's context\n", " \n", " state['number']= state['number'] + 1 #adds 1 to the number of interactions with the car\n", "\n", " state['context'].append(str(state['number']) + '. User question: '+ ques + ', Model answer: ' + model_answer) #modifies the context\n", " \n", " #print(\"contexte : \" + '\\n'.join(state['context']))\n", " \n", " if len(state['context'])>5: #6 questions maximum in the context to avoid having too many information\n", " state['context'] = state['context'][1:]\n", "\n", " return model_answer, state['context'], state, language" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9WQlYePVLrTN" }, "outputs": [], "source": [ "def transcript(general_context, link_to_audio, voice, place, time, delete_history, state):\n", " \"\"\"this function manages speech-to-text to input Fnanswer function and text-to-speech with the Fnanswer output\"\"\"\n", " # load audio from a specific path\n", " audio_path = link_to_audio\n", " audio_array, sampling_rate = librosa.load(link_to_audio, sr=16000) # \"sr=16000\" ensures that the sampling rate is as required\n", "\n", "\n", " # process the audio array\n", " input_features = processor(audio_array, sampling_rate, return_tensors=\"pt\").input_features\n", "\n", "\n", " predicted_ids = modelw.generate(input_features)\n", "\n", " transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)\n", "\n", " quest_processing = FnAnswer(general_context, transcription, place, time, delete_history, state)\n", " \n", " state=quest_processing[2]\n", " \n", " print(\"langue \" + quest_processing[3])\n", "\n", " tts.tts_to_file(text= str(quest_processing[0]),\n", " file_path=\"output.wav\",\n", " speaker_wav=f'Audio_Files/{voice}.wav',\n", " language=quest_processing[3],\n", " emotion = \"angry\")\n", "\n", " audio_path = \"output.wav\"\n", " return audio_path, state['context'], state" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def definePlace(place):\n", " if(place == 'Luxembourg Gare, Luxembourg'):\n", " return('Luxembourg Gare', '49.5999681', '6.1342493' )\n", " elif (place =='Kirchberg Campus, Kirchberg'):\n", " return('Kirchberg Campus, Luxembourg', '49.62571206478235', '6.160082636815114')\n", " elif (place =='Belval Campus, Belval'):\n", " return('Belval-Université, Esch-sur-Alzette', '49.499531', '5.9462903')\n", " elif (place =='Eiffel Tower, Paris'):\n", " return('Eiffel Tower, Paris', '48.8582599', '2.2945006')\n", " elif (place=='Thionville, France'):\n", " return('Thionville, France', '49.357927', '6.167587')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interfaces (text and audio)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#INTERFACE WITH ONLY TEXT\n", "\n", "# Generate options for hours (00-23) \n", "hour_options = [f\"{i:02d}:00:00\" for i in range(24)]\n", "\n", "model_answer= ''\n", "general_context= ''\n", "# Define the initial state with some initial context.\n", "print(general_context)\n", "initial_state = {'context': general_context}\n", "initial_context= initial_state['context']\n", "# Create the Gradio interface.\n", "iface = gr.Interface(\n", " fn=FnAnswer,\n", " inputs=[\n", " gr.Textbox(value=initial_context, visible=False),\n", " gr.Textbox(lines=2, placeholder=\"Type your message here...\"),\n", " gr.Radio(choices=['Luxembourg Gare, Luxembourg', 'Kirchberg Campus, Kirchberg', 'Belval Campus, Belval', 'Eiffel Tower, Paris', 'Thionville, France'], label='Choose a location for your car', value= 'Kirchberg Campus, Kirchberg', show_label=True),\n", " gr.Dropdown(choices=hour_options, label=\"What time is it?\", value = \"08:00:00\"),\n", " gr.Radio([\"Yes\", \"No\"], label=\"Delete the conversation history?\", value = 'No'),\n", " gr.State() # This will keep track of the context state across interactions.\n", " ],\n", " outputs=[\n", " gr.Textbox(),\n", " gr.Textbox(visible=False),\n", " gr.State()\n", " ]\n", ")\n", "gr.close_all()\n", "# Launch the interface.\n", "iface.launch(debug=True, share=True, server_name=\"0.0.0.0\", server_port=7860)\n", "#contextual=gr.Textbox(value=general_context, visible=False)\n", "#demo = gr.Interface(fn=FnAnswer, inputs=[contextual,\"text\"], outputs=[\"text\", contextual])\n", "\n", "#demo.launch()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other possible APIs to use" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "def search_nearby(lat, lon, city, key):\n", " \"\"\"\n", " :param lat: latitude\n", " :param lon: longitude\n", " :param key: api key\n", " :param type: type of poi\n", " :return: [5] results ['poi']['name']/['freeformAddress'] || ['position']['lat']/['lon']\n", " \"\"\"\n", " results = []\n", "\n", " r = requests.get('https://api.tomtom.com/search/2/nearbySearch/.json?key={0}&lat={1}&lon={2}&radius=10000&limit=50'.format(\n", " key,\n", " lat,\n", " lon\n", " ))\n", "\n", " for result in r.json()['results']:\n", " results.append(f\"The {' '.join(result['poi']['categories'])} {result['poi']['name']} is {int(result['dist'])} meters far from {city}\")\n", " if len(results) == 7:\n", " break\n", "\n", " return \". \".join(results)\n", "\n", "\n", "print(search_nearby('49.625892805337514', '6.160417066963513', 'your location', TOMTOM_KEY))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [] }, "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.11.8" } }, "nbformat": 4, "nbformat_minor": 1 }