diff --git a/docs/modules/agents/auto_agents/examples/autogpt.ipynb b/docs/modules/agents/auto_agents/examples/autogpt.ipynb index 0f0d722af36..8410d9edc1e 100644 --- a/docs/modules/agents/auto_agents/examples/autogpt.ipynb +++ b/docs/modules/agents/auto_agents/examples/autogpt.ipynb @@ -70,43 +70,268 @@ { "cell_type": "code", "execution_count": 5, - "id": "c032b182", + "id": "297e826c", "metadata": {}, + "outputs": [], + "source": [ + "agent.chain.verbose = True" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "c032b182", + "metadata": { + "scrolled": false + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "\n", + "\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mSystem: You are Tom, Assistant\n", + "Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n", + "\n", + "GOALS:\n", + "\n", + "1. write a weather report for SF today\n", + "\n", + "\n", + "Constraints:\n", + "1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n", + "2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n", + "3. No user assistance\n", + "4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n", + "\n", + "Commands:\n", + "1. Search: useful for when you need to answer questions about current events. You should ask targeted questions\n", + "\n", + "Resources:\n", + "1. Internet access for searches and information gathering.\n", + "2. Long Term memory management.\n", + "3. GPT-3.5 powered Agents for delegation of simple tasks.\n", + "4. File output.\n", + "\n", + "Performance Evaluation:\n", + "1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n", + "2. Constructively self-criticize your big-picture behavior constantly.\n", + "3. Reflect on past decisions and strategies to refine your approach.\n", + "4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n", + "\n", + "You should only respond in JSON format as described below \n", + "Response Format: \n", "{\n", " \"thoughts\": {\n", - " \"text\": \"I should start by reviewing my current businesses and their performance. This will help me identify areas that need improvement and opportunities for growth.\",\n", - " \"reasoning\": \"Before I can make any decisions about how to increase my net worth or grow my Twitter account, I need to have a clear understanding of my current situation. By reviewing my businesses, I can identify areas that need improvement and opportunities for growth.\",\n", - " \"plan\": \"- Review each of my businesses and their financial performance\\n- Identify areas that need improvement and opportunities for growth\\n- Develop a plan to address these areas and capitalize on opportunities\",\n", - " \"criticism\": \"I need to make sure that I am not spending too much time on this review and that I am focusing on actionable insights. I also need to make sure that I am not getting bogged down in details that are not relevant to my goals.\",\n", - " \"speak\": \"I am going to review my current businesses and their performance to identify areas that need improvement and opportunities for growth.\"\n", + " \"text\": \"thought\",\n", + " \"reasoning\": \"reasoning\",\n", + " \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n", + " \"criticism\": \"constructive self-criticism\",\n", + " \"speak\": \"thoughts summary to say to user\"\n", " },\n", " \"command\": {\n", - " \"name\": \"file output\",\n", - " \"input\": \"Review of current businesses and their performance\"\n", + " \"name\": \"tool name\",\n", + " \"input\": \"input to the tool\"\n", + " }\n", + "} \n", + "Ensure the response can be parsed by Python json.loads\n", + "System: The current time and date is Fri Apr 14 16:55:04 2023\n", + "System: This reminds you of these events from your past:\n", + "[]\n", + "\n", + "\n", + "Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "{\n", + " \"thoughts\": {\n", + " \"text\": \"I will start by checking the current weather in San Francisco to write a weather report.\",\n", + " \"reasoning\": \"Knowing the current weather is important to write an accurate weather report.\",\n", + " \"plan\": \"- Use the 'search' command to look up the current weather in San Francisco.\\n- Save the information to a file for later use.\",\n", + " \"criticism\": \"I need to make sure to save the information to a file so I don't have to look it up again later.\",\n", + " \"speak\": \"I will use the 'search' command to look up the current weather in San Francisco.\"\n", + " },\n", + " \"command\": {\n", + " \"name\": \"search\",\n", + " \"input\": \"current weather in San Francisco\"\n", + " }\n", + "}\n", + "\n", + "\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mSystem: You are Tom, Assistant\n", + "Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n", + "\n", + "GOALS:\n", + "\n", + "1. write a weather report for SF today\n", + "\n", + "\n", + "Constraints:\n", + "1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n", + "2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n", + "3. No user assistance\n", + "4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n", + "\n", + "Commands:\n", + "1. Search: useful for when you need to answer questions about current events. You should ask targeted questions\n", + "\n", + "Resources:\n", + "1. Internet access for searches and information gathering.\n", + "2. Long Term memory management.\n", + "3. GPT-3.5 powered Agents for delegation of simple tasks.\n", + "4. File output.\n", + "\n", + "Performance Evaluation:\n", + "1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n", + "2. Constructively self-criticize your big-picture behavior constantly.\n", + "3. Reflect on past decisions and strategies to refine your approach.\n", + "4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n", + "\n", + "You should only respond in JSON format as described below \n", + "Response Format: \n", + "{\n", + " \"thoughts\": {\n", + " \"text\": \"thought\",\n", + " \"reasoning\": \"reasoning\",\n", + " \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n", + " \"criticism\": \"constructive self-criticism\",\n", + " \"speak\": \"thoughts summary to say to user\"\n", + " },\n", + " \"command\": {\n", + " \"name\": \"tool name\",\n", + " \"input\": \"input to the tool\"\n", + " }\n", + "} \n", + "Ensure the response can be parsed by Python json.loads\n", + "System: The current time and date is Fri Apr 14 16:55:11 2023\n", + "System: This reminds you of these events from your past:\n", + "[Document(page_content='Assistant Reply: {\\n \"thoughts\": {\\n \"text\": \"I will start by checking the current weather in San Francisco to write a weather report.\",\\n \"reasoning\": \"Knowing the current weather is important to write an accurate weather report.\",\\n \"plan\": \"- Use the \\'search\\' command to look up the current weather in San Francisco.\\\\n- Save the information to a file for later use.\",\\n \"criticism\": \"I need to make sure to save the information to a file so I don\\'t have to look it up again later.\",\\n \"speak\": \"I will use the \\'search\\' command to look up the current weather in San Francisco.\"\\n },\\n \"command\": {\\n \"name\": \"search\",\\n \"input\": \"current weather in San Francisco\"\\n }\\n} \\nResult: Unknown command \\'search\\'. Please refer to the \\'COMMANDS\\' list for available commands and only respond in the specified JSON format. ', metadata={})]\n", + "\n", + "\n", + "System: Unknown command 'search'. Please refer to the 'COMMANDS' list for available commands and only respond in the specified JSON format.\n", + "Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "{\n", + " \"thoughts\": {\n", + " \"text\": \"I will start by checking the current weather in San Francisco to write a weather report.\",\n", + " \"reasoning\": \"Knowing the current weather is important to write an accurate weather report.\",\n", + " \"plan\": \"- Use the 'Search' command to look up the current weather in San Francisco.\\n- Save the information to a file for later use.\",\n", + " \"criticism\": \"I need to make sure to save the information to a file so I don't have to look it up again later.\",\n", + " \"speak\": \"I will use the 'Search' command to look up the current weather in San Francisco.\"\n", + " },\n", + " \"command\": {\n", + " \"name\": \"Search\",\n", + " \"input\": \"current weather in San Francisco\"\n", + " }\n", + "}\n", + "\n", + "\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mSystem: You are Tom, Assistant\n", + "Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n", + "\n", + "GOALS:\n", + "\n", + "1. write a weather report for SF today\n", + "\n", + "\n", + "Constraints:\n", + "1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n", + "2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n", + "3. No user assistance\n", + "4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n", + "\n", + "Commands:\n", + "1. Search: useful for when you need to answer questions about current events. You should ask targeted questions\n", + "\n", + "Resources:\n", + "1. Internet access for searches and information gathering.\n", + "2. Long Term memory management.\n", + "3. GPT-3.5 powered Agents for delegation of simple tasks.\n", + "4. File output.\n", + "\n", + "Performance Evaluation:\n", + "1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n", + "2. Constructively self-criticize your big-picture behavior constantly.\n", + "3. Reflect on past decisions and strategies to refine your approach.\n", + "4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n", + "\n", + "You should only respond in JSON format as described below \n", + "Response Format: \n", + "{\n", + " \"thoughts\": {\n", + " \"text\": \"thought\",\n", + " \"reasoning\": \"reasoning\",\n", + " \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n", + " \"criticism\": \"constructive self-criticism\",\n", + " \"speak\": \"thoughts summary to say to user\"\n", + " },\n", + " \"command\": {\n", + " \"name\": \"tool name\",\n", + " \"input\": \"input to the tool\"\n", + " }\n", + "} \n", + "Ensure the response can be parsed by Python json.loads\n", + "System: The current time and date is Fri Apr 14 16:55:19 2023\n", + "System: This reminds you of these events from your past:\n", + "[Document(page_content='Assistant Reply: {\\n \"thoughts\": {\\n \"text\": \"I will start by checking the current weather in San Francisco to write a weather report.\",\\n \"reasoning\": \"Knowing the current weather is important to write an accurate weather report.\",\\n \"plan\": \"- Use the \\'search\\' command to look up the current weather in San Francisco.\\\\n- Save the information to a file for later use.\",\\n \"criticism\": \"I need to make sure to save the information to a file so I don\\'t have to look it up again later.\",\\n \"speak\": \"I will use the \\'search\\' command to look up the current weather in San Francisco.\"\\n },\\n \"command\": {\\n \"name\": \"search\",\\n \"input\": \"current weather in San Francisco\"\\n }\\n} \\nResult: Unknown command \\'search\\'. Please refer to the \\'COMMANDS\\' list for available commands and only respond in the specified JSON format. ', metadata={}), Document(page_content='Assistant Reply: {\\n \"thoughts\": {\\n \"text\": \"I will start by checking the current weather in San Francisco to write a weather report.\",\\n \"reasoning\": \"Knowing the current weather is important to write an accurate weather report.\",\\n \"plan\": \"- Use the \\'Search\\' command to look up the current weather in San Francisco.\\\\n- Save the information to a file for later use.\",\\n \"criticism\": \"I need to make sure to save the information to a file so I don\\'t have to look it up again later.\",\\n \"speak\": \"I will use the \\'Search\\' command to look up the current weather in San Francisco.\"\\n },\\n \"command\": {\\n \"name\": \"Search\",\\n \"input\": \"current weather in San Francisco\"\\n }\\n} \\nResult: Command Search returned: Current Weather ; 54°F · Mostly sunny ; RealFeel® 67°. Pleasant. RealFeel Guide. Pleasant. 63° to 81°. Most consider this temperature range ideal. LEARN MORE. ', metadata={})]\n", + "\n", + "\n", + "System: Unknown command 'search'. Please refer to the 'COMMANDS' list for available commands and only respond in the specified JSON format.\n", + "System: Command Search returned: Current Weather ; 54°F · Mostly sunny ; RealFeel® 67°. Pleasant. RealFeel Guide. Pleasant. 63° to 81°. Most consider this temperature range ideal. LEARN MORE.\n", + "Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "{\n", + " \"thoughts\": {\n", + " \"text\": \"I will save the current weather information to a file for later use.\",\n", + " \"reasoning\": \"Saving the information to a file will allow me to access it later without having to look it up again.\",\n", + " \"plan\": \"- Use the \\'File output\\' command to save the current weather information to a file.\",\n", + " \"criticism\": \"I need to make sure to name the file something descriptive so I can easily find it later.\",\n", + " \"speak\": \"I will use the \\'File output\\' command to save the current weather information to a file.\"\n", + " },\n", + " \"command\": {\n", + " \"name\": \"File output\",\n", + " \"input\": {\n", + " \"content\": \"Current Weather ; 54°F · Mostly sunny ; RealFeel® 67°. Pleasant. RealFeel Guide. Pleasant. 63° to 81°. Most consider this temperature range ideal. LEARN MORE.\",\n", + " \"filename\": \"sf_weather.txt\"\n", + " }\n", " }\n", "}\n" ] }, { - "ename": "KeyError", - "evalue": "'file output'", + "ename": "JSONDecodeError", + "evalue": "Invalid \\escape: line 5 column 28 (char 261)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43magent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/workplace/langchain/langchain/auto_agents/autogpt/agent.py:75\u001b[0m, in \u001b[0;36mAgent.run\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;66;03m# Get command name and arguments\u001b[39;00m\n\u001b[1;32m 74\u001b[0m action \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_parser\u001b[38;5;241m.\u001b[39mparse(assistant_reply)\n\u001b[0;32m---> 75\u001b[0m tool \u001b[38;5;241m=\u001b[39m \u001b[43m{\u001b[49m\u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\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[43mtools\u001b[49m\u001b[43m}\u001b[49m\u001b[43m[\u001b[49m\u001b[43maction\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtool\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 76\u001b[0m \u001b[38;5;66;03m# Execute command\u001b[39;00m\n\u001b[1;32m 77\u001b[0m observation \u001b[38;5;241m=\u001b[39m tool\u001b[38;5;241m.\u001b[39mrun(action\u001b[38;5;241m.\u001b[39mtool_input)\n", - "\u001b[0;31mKeyError\u001b[0m: 'file output'" + "\u001b[0;31mJSONDecodeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43magent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mwrite a weather report for SF today\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/workplace/langchain/langchain/auto_agents/autogpt/agent.py:73\u001b[0m, in \u001b[0;36mAgent.run\u001b[0;34m(self, goals)\u001b[0m\n\u001b[1;32m 70\u001b[0m \u001b[38;5;28mprint\u001b[39m(assistant_reply)\n\u001b[1;32m 72\u001b[0m \u001b[38;5;66;03m# Get command name and arguments\u001b[39;00m\n\u001b[0;32m---> 73\u001b[0m action \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moutput_parser\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparse\u001b[49m\u001b[43m(\u001b[49m\u001b[43massistant_reply\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 74\u001b[0m tools \u001b[38;5;241m=\u001b[39m {t\u001b[38;5;241m.\u001b[39mname: t \u001b[38;5;28;01mfor\u001b[39;00m t \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtools}\n\u001b[1;32m 75\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m action\u001b[38;5;241m.\u001b[39mtool \u001b[38;5;129;01min\u001b[39;00m tools:\n", + "File \u001b[0;32m~/workplace/langchain/langchain/auto_agents/autogpt/output_parser.py:10\u001b[0m, in \u001b[0;36mAutoGPTOutputParser.parse\u001b[0;34m(self, text)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mparse\u001b[39m(\u001b[38;5;28mself\u001b[39m, text: \u001b[38;5;28mstr\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Union[AgentAction, AgentFinish]:\n\u001b[0;32m---> 10\u001b[0m parsed \u001b[38;5;241m=\u001b[39m \u001b[43mjson\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloads\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m AgentAction(tool\u001b[38;5;241m=\u001b[39mparsed[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcommand\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m\"\u001b[39m], tool_input\u001b[38;5;241m=\u001b[39mparsed[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcommand\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minput\u001b[39m\u001b[38;5;124m\"\u001b[39m], log\u001b[38;5;241m=\u001b[39mtext)\n", + "File \u001b[0;32m~/.pyenv/versions/3.9.1/lib/python3.9/json/__init__.py:346\u001b[0m, in \u001b[0;36mloads\u001b[0;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[1;32m 341\u001b[0m s \u001b[38;5;241m=\u001b[39m s\u001b[38;5;241m.\u001b[39mdecode(detect_encoding(s), \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msurrogatepass\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 343\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[1;32m 344\u001b[0m parse_int \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m parse_float \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[1;32m 345\u001b[0m parse_constant \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_pairs_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kw):\n\u001b[0;32m--> 346\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_default_decoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 347\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 348\u001b[0m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m JSONDecoder\n", + "File \u001b[0;32m~/.pyenv/versions/3.9.1/lib/python3.9/json/decoder.py:337\u001b[0m, in \u001b[0;36mJSONDecoder.decode\u001b[0;34m(self, s, _w)\u001b[0m\n\u001b[1;32m 332\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdecode\u001b[39m(\u001b[38;5;28mself\u001b[39m, s, _w\u001b[38;5;241m=\u001b[39mWHITESPACE\u001b[38;5;241m.\u001b[39mmatch):\n\u001b[1;32m 333\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001b[39;00m\n\u001b[1;32m 334\u001b[0m \u001b[38;5;124;03m containing a JSON document).\u001b[39;00m\n\u001b[1;32m 335\u001b[0m \n\u001b[1;32m 336\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 337\u001b[0m obj, end \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraw_decode\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43midx\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_w\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mend\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 338\u001b[0m end \u001b[38;5;241m=\u001b[39m _w(s, end)\u001b[38;5;241m.\u001b[39mend()\n\u001b[1;32m 339\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m end \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mlen\u001b[39m(s):\n", + "File \u001b[0;32m~/.pyenv/versions/3.9.1/lib/python3.9/json/decoder.py:353\u001b[0m, in \u001b[0;36mJSONDecoder.raw_decode\u001b[0;34m(self, s, idx)\u001b[0m\n\u001b[1;32m 344\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Decode a JSON document from ``s`` (a ``str`` beginning with\u001b[39;00m\n\u001b[1;32m 345\u001b[0m \u001b[38;5;124;03ma JSON document) and return a 2-tuple of the Python\u001b[39;00m\n\u001b[1;32m 346\u001b[0m \u001b[38;5;124;03mrepresentation and the index in ``s`` where the document ended.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 350\u001b[0m \n\u001b[1;32m 351\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 352\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 353\u001b[0m obj, end \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mscan_once\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43midx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 354\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[1;32m 355\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m JSONDecodeError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExpecting value\u001b[39m\u001b[38;5;124m\"\u001b[39m, s, err\u001b[38;5;241m.\u001b[39mvalue) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28mNone\u001b[39m\n", + "\u001b[0;31mJSONDecodeError\u001b[0m: Invalid \\escape: line 5 column 28 (char 261)" ] } ], "source": [ - "agent.run()" + "agent.run([\"write a weather report for SF today\"])" ] }, { diff --git a/langchain/auto_agents/autogpt/agent.py b/langchain/auto_agents/autogpt/agent.py index e43cb75134c..5a98a740ce3 100644 --- a/langchain/auto_agents/autogpt/agent.py +++ b/langchain/auto_agents/autogpt/agent.py @@ -32,7 +32,6 @@ class Agent: self.memory = memory self.full_message_history = [] self.next_action_count = 0 - self.user_input = "Determine which next command to use, and respond using the format specified above:" self.chain = chain self.output_parser = output_parser self.tools = tools @@ -42,7 +41,7 @@ class Agent: ai_role: str, memory: VectorStoreRetriever, tools: List[BaseTool], llm: BaseChatModel, output_parser: Optional[AgentOutputParser] = None): - prompt = AutoGPTPrompt(ai_name=ai_name, ai_role=ai_role, tools=tools, input_variables=["memory", "messages", "user_input"]) + prompt = AutoGPTPrompt(ai_name=ai_name, ai_role=ai_role, tools=tools, input_variables=["memory", "messages", "goals"]) chain = LLMChain(llm=llm, prompt=prompt) return cls( ai_name, @@ -53,7 +52,7 @@ class Agent: ) - def run(self): + def run(self, goals: List[str]): # Interaction Loop loop_count = 0 while True: @@ -62,7 +61,7 @@ class Agent: # Send message to AI, get response assistant_reply = self.chain.run( - user_input=self.user_input, + goals=goals, messages=self.full_message_history, memory=self.memory ) @@ -72,14 +71,17 @@ class Agent: # Get command name and arguments action = self.output_parser.parse(assistant_reply) - tool = {t.name: t for t in self.tools}[action.tool] - # Execute command - observation = tool.run(action.tool_input) - result = f"Command {tool.name} returned: {observation}" + tools = {t.name: t for t in self.tools} + if action.tool in tools: + tool = tools[action.tool] + observation = tool.run(action.tool_input) + result = f"Command {tool.name} returned: {observation}" + else: + result = f"Unknown command '{action.tool}'. Please refer to the 'COMMANDS' list for available commands and only respond in the specified JSON format." + memory_to_add = (f"Assistant Reply: {assistant_reply} " f"\nResult: {result} " - f"\nHuman Feedback: {self.user_input} " ) self.memory.add_documents([Document(page_content=memory_to_add)]) diff --git a/langchain/auto_agents/autogpt/prompt.py b/langchain/auto_agents/autogpt/prompt.py index 1e7c30a3daa..158a47c1513 100644 --- a/langchain/auto_agents/autogpt/prompt.py +++ b/langchain/auto_agents/autogpt/prompt.py @@ -13,10 +13,8 @@ class AutoGPTPrompt(BaseChatPromptTemplate, BaseModel): ai_name: str ai_role: str tools: List[BaseTool] - ai_goals: List[str] = Field(default=["Increase net worth", "Grow Twitter Account", - "Develop and manage multiple businesses autonomously"]) - def construct_full_prompt(self) -> str: + def construct_full_prompt(self, goals) -> str: """ Returns a prompt to the user with the class information in an organized fashion. @@ -31,7 +29,7 @@ class AutoGPTPrompt(BaseChatPromptTemplate, BaseModel): # Construct full prompt full_prompt = f"You are {self.ai_name}, {self.ai_role}\n{prompt_start}\n\nGOALS:\n\n" - for i, goal in enumerate(self.ai_goals): + for i, goal in enumerate(goals): full_prompt += f"{i+1}. {goal}\n" full_prompt += f"\n\n{get_prompt(self.tools)}" @@ -42,11 +40,11 @@ class AutoGPTPrompt(BaseChatPromptTemplate, BaseModel): memory: VectorStoreRetriever = kwargs["memory"] previous_messages = kwargs["messages"] relevant_memory = memory.get_relevant_documents(str(previous_messages[-2:])) - messages.append(SystemMessage(content=self.construct_full_prompt())) + messages.append(SystemMessage(content=self.construct_full_prompt(kwargs["goals"]))) messages.append(SystemMessage(content=f"The current time and date is {time.strftime('%c')}")) messages.append(SystemMessage(content=f"This reminds you of these events from your past:\n{relevant_memory}\n\n")) messages.extend(previous_messages[-2:]) - messages.append(HumanMessage(content=kwargs["user_input"])) + messages.append(HumanMessage(content="Determine which next command to use, and respond using the format specified above:")) return messages