mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-11-03 17:54:10 +00:00 
			
		
		
		
	Bagatur/docs smith context (#13139)
This commit is contained in:
		@@ -134,7 +134,7 @@
 | 
			
		||||
    "    name=\"langchain assistant\",\n",
 | 
			
		||||
    "    instructions=\"You are a personal math tutor. Write and run code to answer math questions.\",\n",
 | 
			
		||||
    "    tools=[{\"type\": \"code_interpreter\"}],\n",
 | 
			
		||||
    "    model=\"gpt-4-1106-preview\"\n",
 | 
			
		||||
    "    model=\"gpt-4-1106-preview\",\n",
 | 
			
		||||
    ")\n",
 | 
			
		||||
    "output = interpreter_assistant.invoke({\"content\": \"What's 10 - 4 raised to the 2.7\"})\n",
 | 
			
		||||
    "output"
 | 
			
		||||
@@ -184,7 +184,7 @@
 | 
			
		||||
    "    instructions=\"You are a personal math tutor. Write and run code to answer math questions. You can also search the internet.\",\n",
 | 
			
		||||
    "    tools=tools,\n",
 | 
			
		||||
    "    model=\"gpt-4-1106-preview\",\n",
 | 
			
		||||
    "    as_agent=True\n",
 | 
			
		||||
    "    as_agent=True,\n",
 | 
			
		||||
    ")"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
@@ -241,7 +241,7 @@
 | 
			
		||||
    "    instructions=\"You are a personal math tutor. Write and run code to answer math questions.\",\n",
 | 
			
		||||
    "    tools=tools,\n",
 | 
			
		||||
    "    model=\"gpt-4-1106-preview\",\n",
 | 
			
		||||
    "    as_agent=True\n",
 | 
			
		||||
    "    as_agent=True,\n",
 | 
			
		||||
    ")"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
@@ -254,6 +254,7 @@
 | 
			
		||||
   "source": [
 | 
			
		||||
    "from langchain.schema.agent import AgentFinish\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "def execute_agent(agent, tools, input):\n",
 | 
			
		||||
    "    tool_map = {tool.name: tool for tool in tools}\n",
 | 
			
		||||
    "    response = agent.invoke(input)\n",
 | 
			
		||||
@@ -262,9 +263,17 @@
 | 
			
		||||
    "        for action in response:\n",
 | 
			
		||||
    "            tool_output = tool_map[action.tool].invoke(action.tool_input)\n",
 | 
			
		||||
    "            print(action.tool, action.tool_input, tool_output, end=\"\\n\\n\")\n",
 | 
			
		||||
    "            tool_outputs.append({\"output\": tool_output, \"tool_call_id\": action.tool_call_id})\n",
 | 
			
		||||
    "        response = agent.invoke({\"tool_outputs\": tool_outputs, \"run_id\": action.run_id, \"thread_id\": action.thread_id})\n",
 | 
			
		||||
    "        \n",
 | 
			
		||||
    "            tool_outputs.append(\n",
 | 
			
		||||
    "                {\"output\": tool_output, \"tool_call_id\": action.tool_call_id}\n",
 | 
			
		||||
    "            )\n",
 | 
			
		||||
    "        response = agent.invoke(\n",
 | 
			
		||||
    "            {\n",
 | 
			
		||||
    "                \"tool_outputs\": tool_outputs,\n",
 | 
			
		||||
    "                \"run_id\": action.run_id,\n",
 | 
			
		||||
    "                \"thread_id\": action.thread_id,\n",
 | 
			
		||||
    "            }\n",
 | 
			
		||||
    "        )\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "    return response"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
@@ -306,7 +315,9 @@
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "next_response = execute_agent(agent, tools, {\"content\": \"now add 17.241\", \"thread_id\": response.thread_id})\n",
 | 
			
		||||
    "next_response = execute_agent(\n",
 | 
			
		||||
    "    agent, tools, {\"content\": \"now add 17.241\", \"thread_id\": response.thread_id}\n",
 | 
			
		||||
    ")\n",
 | 
			
		||||
    "print(next_response.return_values[\"output\"])"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
@@ -449,16 +460,22 @@
 | 
			
		||||
    "from langchain.prompts import ChatPromptTemplate\n",
 | 
			
		||||
    "from langchain.pydantic_v1 import BaseModel, Field\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "class GetCurrentWeather(BaseModel):\n",
 | 
			
		||||
    "    \"\"\"Get the current weather in a location.\"\"\"\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "    location: str = Field(description=\"The city and state, e.g. San Francisco, CA\")\n",
 | 
			
		||||
    "    unit: Literal[\"celsius\", \"fahrenheit\"] = Field(default=\"fahrenheit\", description=\"The temperature unit, default to fahrenheit\")\n",
 | 
			
		||||
    "    \n",
 | 
			
		||||
    "prompt = ChatPromptTemplate.from_messages([\n",
 | 
			
		||||
    "    (\"system\", \"You are a helpful assistant\"),\n",
 | 
			
		||||
    "    (\"user\", \"{input}\")\n",
 | 
			
		||||
    "])\n",
 | 
			
		||||
    "model = ChatOpenAI(model=\"gpt-3.5-turbo-1106\").bind(tools=[convert_pydantic_to_openai_tool(GetCurrentWeather)])\n",
 | 
			
		||||
    "    unit: Literal[\"celsius\", \"fahrenheit\"] = Field(\n",
 | 
			
		||||
    "        default=\"fahrenheit\", description=\"The temperature unit, default to fahrenheit\"\n",
 | 
			
		||||
    "    )\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "prompt = ChatPromptTemplate.from_messages(\n",
 | 
			
		||||
    "    [(\"system\", \"You are a helpful assistant\"), (\"user\", \"{input}\")]\n",
 | 
			
		||||
    ")\n",
 | 
			
		||||
    "model = ChatOpenAI(model=\"gpt-3.5-turbo-1106\").bind(\n",
 | 
			
		||||
    "    tools=[convert_pydantic_to_openai_tool(GetCurrentWeather)]\n",
 | 
			
		||||
    ")\n",
 | 
			
		||||
    "chain = prompt | model | PydanticToolsParser(tools=[GetCurrentWeather])\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "chain.invoke({\"input\": \"what's the weather in NYC, LA, and SF\"})"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user