{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import dotenv\n", "import os\n", "from langchain.tools import BaseTool\n", "from math import pi\n", "from typing import Union\n", "\n", "dotenv.load_dotenv()\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI\n", "from langchain.chains.conversation.memory import ConversationBufferWindowMemory" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# initialize LLM (we use ChatOpenAI because we'll later define a `chat` agent)\n", "llm = ChatOpenAI(\n", " temperature=0,\n", " model_name='gpt-3.5-turbo'\n", ")\n", "# initialize conversational memory\n", "conversational_memory = ConversationBufferWindowMemory(\n", " memory_key='chat_history',\n", " k=5,\n", " return_messages=True\n", ")\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'homeassistant_api'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mtyping\u001b[39;00m \u001b[39mimport\u001b[39;00m Optional, List, Union\n\u001b[0;32m----> 2\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mhomeassistant_api\u001b[39;00m \u001b[39mimport\u001b[39;00m Client\n\u001b[1;32m 5\u001b[0m haclient \u001b[39m=\u001b[39m Client(\n\u001b[1;32m 6\u001b[0m \u001b[39m'\u001b[39m\u001b[39mhttp://homeassistant.local:8123/api\u001b[39m\u001b[39m'\u001b[39m,\n\u001b[1;32m 7\u001b[0m \u001b[39m'\u001b[39m\u001b[39meyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkNTVlNzMxNzAyMTE0ZmM4OTQ4NjAwMjFlN2UxNjc2YSIsImlhdCI6MTY4MjY3MTIwMCwiZXhwIjoxOTk4MDMxMjAwfQ.uwuasJHvbgesb6mQ_QdcmptYAgZxE4NTOnnpCWxLaek\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m 8\u001b[0m )\n\u001b[1;32m 10\u001b[0m desc \u001b[39m=\u001b[39m (\n\u001b[1;32m 11\u001b[0m \u001b[39m\"\u001b[39m\u001b[39muse this tool when you need to turn on or off the light in the room. \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 12\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mgiven the light entity name and an action like turnon or turnoff, this tool will turn the lights on or off. \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 13\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mTo use the tool you must provide exactly two of the following parameters \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 14\u001b[0m \u001b[39m\"\u001b[39m\u001b[39m[\u001b[39m\u001b[39m'\u001b[39m\u001b[39mentity_name\u001b[39m\u001b[39m'\u001b[39m\u001b[39m, \u001b[39m\u001b[39m'\u001b[39m\u001b[39maction\u001b[39m\u001b[39m'\u001b[39m\u001b[39m]\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 15\u001b[0m )\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'homeassistant_api'" ] } ], "source": [ "from typing import Optional, List, Union\n", "from homeassistant_api import Client\n", "\n", "\n", "haclient = Client(\n", " 'http://homeassistant.local:8123/api',\n", " 'REPLACE_WITH'\n", ")\n", "\n", "desc = (\n", " \"use this tool when you need to turn on or off the light in the room. \"\n", " \"given the light entity name and an action like turnon or turnoff, this tool will turn the lights on or off. \"\n", " \"To use the tool you must provide exactly two of the following parameters \"\n", " \"['entity_name', 'action']\"\n", ")\n", "\n", "class HALightControl(BaseTool):\n", " name = \"Control the lights in the room\"\n", " description = desc\n", "\n", "\n", " def _run(\n", " self,\n", " action: str,\n", " entity_name: str,\n", " ):\n", " light = haclient.get_domain(\"light\")\n", " if entity_name:\n", " entity = \"light.protoboxer_{entity_name}\".format(entity_name=entity_name)\n", " if action == \"turnon\":\n", " light.turn_on(entity_id=entity)\n", " return \"{entity_name} turned on\".format(entity_name=entity_name)\n", " elif action == \"turnoff\":\n", " light.turn_off(entity_id=entity)\n", " return \"{entity_name} turned off\".format(entity_name=entity_name)\n", " return \"\"\n", " \n", " def _arun(self, query: str):\n", " raise NotImplementedError(\"This tool does not support async\")\n", "\n", "tools = [HALightControl()]\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain.agents import initialize_agent\n", "\n", "sys_msg = \"\"\"Assistant is a large language model trained by OpenAI.\n", "\n", "Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n", "\n", "Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n", "\n", "Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.\n", "\"\"\"\n", "\n", "agent = initialize_agent(\n", " agent='chat-conversational-react-description',\n", " system_message=sys_msg,\n", " tools=tools,\n", " llm=llm,\n", " verbose=True,\n", " max_iterations=1,\n", " early_stopping_method='generate',\n", " memory=conversational_memory\n", ")\n" ] }, { "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", "\u001b[32;1m\u001b[1;3m{\n", " \"action\": \"Control the lights in the room\",\n", " \"action_input\": {\n", " \"entity_name\": \"led_2\",\n", " \"action\": \"turnoff\"\n", " }\n", "}\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mled_2 turned off\u001b[0m\n", "Thought:" ] }, { "ename": "ValueError", "evalue": "variable agent_scratchpad should be a list of base messages, got {\n \"action\": \"Control the lights in the room\",\n \"action_input\": {\n \"entity_name\": \"led_2\",\n \"action\": \"turnoff\"\n }\n}\nObservation: led_2 turned off\nThought:\n\nI now need to return a final answer based on the previous steps:", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[73], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m agent(\u001b[39m\"\u001b[39;49m\u001b[39mTurn off the light led_2\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/base.py:116\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m 114\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 115\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_error(e, verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose)\n\u001b[0;32m--> 116\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[1;32m 117\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_end(outputs, verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose)\n\u001b[1;32m 118\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_outputs(inputs, outputs, return_only_outputs)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/base.py:113\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_start(\n\u001b[1;32m 108\u001b[0m {\u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m},\n\u001b[1;32m 109\u001b[0m inputs,\n\u001b[1;32m 110\u001b[0m verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose,\n\u001b[1;32m 111\u001b[0m )\n\u001b[1;32m 112\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 113\u001b[0m outputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call(inputs)\n\u001b[1;32m 114\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 115\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_error(e, verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/agents/agent.py:807\u001b[0m, in \u001b[0;36mAgentExecutor._call\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m 805\u001b[0m iterations \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n\u001b[1;32m 806\u001b[0m time_elapsed \u001b[39m=\u001b[39m time\u001b[39m.\u001b[39mtime() \u001b[39m-\u001b[39m start_time\n\u001b[0;32m--> 807\u001b[0m output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49magent\u001b[39m.\u001b[39;49mreturn_stopped_response(\n\u001b[1;32m 808\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mearly_stopping_method, intermediate_steps, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49minputs\n\u001b[1;32m 809\u001b[0m )\n\u001b[1;32m 810\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_return(output, intermediate_steps)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/agents/agent.py:515\u001b[0m, in \u001b[0;36mAgent.return_stopped_response\u001b[0;34m(self, early_stopping_method, intermediate_steps, **kwargs)\u001b[0m\n\u001b[1;32m 513\u001b[0m new_inputs \u001b[39m=\u001b[39m {\u001b[39m\"\u001b[39m\u001b[39magent_scratchpad\u001b[39m\u001b[39m\"\u001b[39m: thoughts, \u001b[39m\"\u001b[39m\u001b[39mstop\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_stop}\n\u001b[1;32m 514\u001b[0m full_inputs \u001b[39m=\u001b[39m {\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mnew_inputs}\n\u001b[0;32m--> 515\u001b[0m full_output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mllm_chain\u001b[39m.\u001b[39;49mpredict(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mfull_inputs)\n\u001b[1;32m 516\u001b[0m \u001b[39m# We try to extract a final answer\u001b[39;00m\n\u001b[1;32m 517\u001b[0m parsed_output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_parser\u001b[39m.\u001b[39mparse(full_output)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/llm.py:151\u001b[0m, in \u001b[0;36mLLMChain.predict\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 137\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mpredict\u001b[39m(\u001b[39mself\u001b[39m, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m \u001b[39mstr\u001b[39m:\n\u001b[1;32m 138\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Format prompt with kwargs and pass to LLM.\u001b[39;00m\n\u001b[1;32m 139\u001b[0m \n\u001b[1;32m 140\u001b[0m \u001b[39m Args:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[39m completion = llm.predict(adjective=\"funny\")\u001b[39;00m\n\u001b[1;32m 150\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 151\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m(kwargs)[\u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_key]\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/base.py:116\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m 114\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 115\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_error(e, verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose)\n\u001b[0;32m--> 116\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[1;32m 117\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_end(outputs, verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose)\n\u001b[1;32m 118\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_outputs(inputs, outputs, return_only_outputs)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/base.py:113\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_start(\n\u001b[1;32m 108\u001b[0m {\u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m},\n\u001b[1;32m 109\u001b[0m inputs,\n\u001b[1;32m 110\u001b[0m verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose,\n\u001b[1;32m 111\u001b[0m )\n\u001b[1;32m 112\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 113\u001b[0m outputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call(inputs)\n\u001b[1;32m 114\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 115\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcallback_manager\u001b[39m.\u001b[39mon_chain_error(e, verbose\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mverbose)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/llm.py:57\u001b[0m, in \u001b[0;36mLLMChain._call\u001b[0;34m(self, inputs)\u001b[0m\n\u001b[1;32m 56\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_call\u001b[39m(\u001b[39mself\u001b[39m, inputs: Dict[\u001b[39mstr\u001b[39m, Any]) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Dict[\u001b[39mstr\u001b[39m, \u001b[39mstr\u001b[39m]:\n\u001b[0;32m---> 57\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mapply([inputs])[\u001b[39m0\u001b[39m]\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/llm.py:118\u001b[0m, in \u001b[0;36mLLMChain.apply\u001b[0;34m(self, input_list)\u001b[0m\n\u001b[1;32m 116\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mapply\u001b[39m(\u001b[39mself\u001b[39m, input_list: List[Dict[\u001b[39mstr\u001b[39m, Any]]) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m List[Dict[\u001b[39mstr\u001b[39m, \u001b[39mstr\u001b[39m]]:\n\u001b[1;32m 117\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Utilize the LLM generate method for speed gains.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 118\u001b[0m response \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mgenerate(input_list)\n\u001b[1;32m 119\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcreate_outputs(response)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/llm.py:61\u001b[0m, in \u001b[0;36mLLMChain.generate\u001b[0;34m(self, input_list)\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mgenerate\u001b[39m(\u001b[39mself\u001b[39m, input_list: List[Dict[\u001b[39mstr\u001b[39m, Any]]) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m LLMResult:\n\u001b[1;32m 60\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Generate LLM result from inputs.\"\"\"\u001b[39;00m\n\u001b[0;32m---> 61\u001b[0m prompts, stop \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mprep_prompts(input_list)\n\u001b[1;32m 62\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mllm\u001b[39m.\u001b[39mgenerate_prompt(prompts, stop)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/chains/llm.py:79\u001b[0m, in \u001b[0;36mLLMChain.prep_prompts\u001b[0;34m(self, input_list)\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[39mfor\u001b[39;00m inputs \u001b[39min\u001b[39;00m input_list:\n\u001b[1;32m 78\u001b[0m selected_inputs \u001b[39m=\u001b[39m {k: inputs[k] \u001b[39mfor\u001b[39;00m k \u001b[39min\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprompt\u001b[39m.\u001b[39minput_variables}\n\u001b[0;32m---> 79\u001b[0m prompt \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mprompt\u001b[39m.\u001b[39;49mformat_prompt(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mselected_inputs)\n\u001b[1;32m 80\u001b[0m _colored_text \u001b[39m=\u001b[39m get_colored_text(prompt\u001b[39m.\u001b[39mto_string(), \u001b[39m\"\u001b[39m\u001b[39mgreen\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 81\u001b[0m _text \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mPrompt after formatting:\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m _colored_text\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/prompts/chat.py:127\u001b[0m, in \u001b[0;36mBaseChatPromptTemplate.format_prompt\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mformat_prompt\u001b[39m(\u001b[39mself\u001b[39m, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m PromptValue:\n\u001b[0;32m--> 127\u001b[0m messages \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mformat_messages(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m 128\u001b[0m \u001b[39mreturn\u001b[39;00m ChatPromptValue(messages\u001b[39m=\u001b[39mmessages)\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/prompts/chat.py:186\u001b[0m, in \u001b[0;36mChatPromptTemplate.format_messages\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 180\u001b[0m \u001b[39melif\u001b[39;00m \u001b[39misinstance\u001b[39m(message_template, BaseMessagePromptTemplate):\n\u001b[1;32m 181\u001b[0m rel_params \u001b[39m=\u001b[39m {\n\u001b[1;32m 182\u001b[0m k: v\n\u001b[1;32m 183\u001b[0m \u001b[39mfor\u001b[39;00m k, v \u001b[39min\u001b[39;00m kwargs\u001b[39m.\u001b[39mitems()\n\u001b[1;32m 184\u001b[0m \u001b[39mif\u001b[39;00m k \u001b[39min\u001b[39;00m message_template\u001b[39m.\u001b[39minput_variables\n\u001b[1;32m 185\u001b[0m }\n\u001b[0;32m--> 186\u001b[0m message \u001b[39m=\u001b[39m message_template\u001b[39m.\u001b[39;49mformat_messages(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mrel_params)\n\u001b[1;32m 187\u001b[0m result\u001b[39m.\u001b[39mextend(message)\n\u001b[1;32m 188\u001b[0m \u001b[39melse\u001b[39;00m:\n", "File \u001b[0;32m~/opt/anaconda3/envs/openai/lib/python3.10/site-packages/langchain/prompts/chat.py:43\u001b[0m, in \u001b[0;36mMessagesPlaceholder.format_messages\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 41\u001b[0m value \u001b[39m=\u001b[39m kwargs[\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mvariable_name]\n\u001b[1;32m 42\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39misinstance\u001b[39m(value, \u001b[39mlist\u001b[39m):\n\u001b[0;32m---> 43\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 44\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mvariable \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mvariable_name\u001b[39m}\u001b[39;00m\u001b[39m should be a list of base messages, \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 45\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mgot \u001b[39m\u001b[39m{\u001b[39;00mvalue\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[1;32m 46\u001b[0m )\n\u001b[1;32m 47\u001b[0m \u001b[39mfor\u001b[39;00m v \u001b[39min\u001b[39;00m value:\n\u001b[1;32m 48\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39misinstance\u001b[39m(v, BaseMessage):\n", "\u001b[0;31mValueError\u001b[0m: variable agent_scratchpad should be a list of base messages, got {\n \"action\": \"Control the lights in the room\",\n \"action_input\": {\n \"entity_name\": \"led_2\",\n \"action\": \"turnoff\"\n }\n}\nObservation: led_2 turned off\nThought:\n\nI now need to return a final answer based on the previous steps:" ] } ], "source": [ "agent(\"Turn off the light led_1\")" ] } ], "metadata": { "kernelspec": { "display_name": "openai", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }