mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-12 04:37:51 +02:00
Update llmcompiler
This commit is contained in:
@@ -102,6 +102,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"LANGCHAIN_PROJECT\"] = \"brex\""
|
||||
]
|
||||
},
|
||||
@@ -148,6 +149,7 @@
|
||||
"from langchain import hub\n",
|
||||
"from langchain.agents import create_openai_functions_agent\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"\n",
|
||||
"# Get the prompt to use - you can modify this!\n",
|
||||
"prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n",
|
||||
"# Choose the LLM that will drive the agent\n",
|
||||
@@ -198,7 +200,9 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"agent_executor.invoke({\"input\": \"who is the winnner of the us open\", \"chat_history\": []})"
|
||||
"agent_executor.invoke(\n",
|
||||
" {\"input\": \"who is the winnner of the us open\", \"chat_history\": []}\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -230,8 +234,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"class PlanExecute(TypedDict):\n",
|
||||
"\n",
|
||||
" input: str \n",
|
||||
" input: str\n",
|
||||
" plan: List[str]\n",
|
||||
" past_steps: Annotated[List[Tuple], operator.add]\n",
|
||||
" response: str"
|
||||
@@ -259,7 +262,10 @@
|
||||
"\n",
|
||||
"class Plan(BaseModel):\n",
|
||||
" \"\"\"Plan to follow in future\"\"\"\n",
|
||||
" steps: List[str] = Field(description=\"different steps to follow, should be in sorted order\")\n"
|
||||
"\n",
|
||||
" steps: List[str] = Field(\n",
|
||||
" description=\"different steps to follow, should be in sorted order\"\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -272,12 +278,16 @@
|
||||
"from langchain.chains.openai_functions import create_structured_output_runnable\n",
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"\n",
|
||||
"planner_prompt = ChatPromptTemplate.from_template(\"\"\"For the given objective, come up with a simple step by step plan. \\\n",
|
||||
"planner_prompt = ChatPromptTemplate.from_template(\n",
|
||||
" \"\"\"For the given objective, come up with a simple step by step plan. \\\n",
|
||||
"This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \\\n",
|
||||
"The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.\n",
|
||||
"\n",
|
||||
"{objective}\"\"\")\n",
|
||||
"planner = create_structured_output_runnable(Plan, ChatOpenAI(model=\"gpt-4-turbo-preview\", temperature=0), planner_prompt)"
|
||||
"{objective}\"\"\"\n",
|
||||
")\n",
|
||||
"planner = create_structured_output_runnable(\n",
|
||||
" Plan, ChatOpenAI(model=\"gpt-4-turbo-preview\", temperature=0), planner_prompt\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -298,7 +308,9 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"planner.invoke({'objective': 'what is the hometown of the current Australia open winner?'})"
|
||||
"planner.invoke(\n",
|
||||
" {\"objective\": \"what is the hometown of the current Australia open winner?\"}\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -319,11 +331,16 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains.openai_functions import create_openai_fn_runnable\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Response(BaseModel):\n",
|
||||
" \"\"\"Response to user.\"\"\"\n",
|
||||
"\n",
|
||||
" response: str\n",
|
||||
"\n",
|
||||
"replanner_prompt = ChatPromptTemplate.from_template(\"\"\"For the given objective, come up with a simple step by step plan. \\\n",
|
||||
"\n",
|
||||
"replanner_prompt = ChatPromptTemplate.from_template(\n",
|
||||
" \"\"\"For the given objective, come up with a simple step by step plan. \\\n",
|
||||
"This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \\\n",
|
||||
"The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.\n",
|
||||
"\n",
|
||||
@@ -336,10 +353,15 @@
|
||||
"You have currently done the follow steps:\n",
|
||||
"{past_steps}\n",
|
||||
"\n",
|
||||
"Update your plan accordingly. If no more steps are needed and you can return to the user, then respond with that. Otherwise, fill out the plan. Only add steps to the plan that still NEED to be done. Do not return previously done steps as part of the plan.\"\"\")\n",
|
||||
"Update your plan accordingly. If no more steps are needed and you can return to the user, then respond with that. Otherwise, fill out the plan. Only add steps to the plan that still NEED to be done. Do not return previously done steps as part of the plan.\"\"\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"replanner = create_openai_fn_runnable([Plan, Response], ChatOpenAI(model=\"gpt-4-turbo-preview\", temperature=0), replanner_prompt)\n"
|
||||
"replanner = create_openai_fn_runnable(\n",
|
||||
" [Plan, Response],\n",
|
||||
" ChatOpenAI(model=\"gpt-4-turbo-preview\", temperature=0),\n",
|
||||
" replanner_prompt,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -360,14 +382,18 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"async def execute_step(state: PlanExecute):\n",
|
||||
" task = state['plan'][0]\n",
|
||||
" task = state[\"plan\"][0]\n",
|
||||
" agent_response = await agent_executor.ainvoke({\"input\": task, \"chat_history\": []})\n",
|
||||
" return {\"past_steps\": (task, agent_response['agent_outcome'].return_values['output'])}\n",
|
||||
" return {\n",
|
||||
" \"past_steps\": (task, agent_response[\"agent_outcome\"].return_values[\"output\"])\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"async def plan_step(state: PlanExecute):\n",
|
||||
" plan = await planner.ainvoke({\"objective\": state[\"input\"]})\n",
|
||||
" return {\"plan\": plan.steps}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"async def replan_step(state: PlanExecute):\n",
|
||||
" output = await replanner.ainvoke(state)\n",
|
||||
" if isinstance(output, Response):\n",
|
||||
@@ -375,8 +401,9 @@
|
||||
" else:\n",
|
||||
" return {\"plan\": output.steps}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def should_end(state: PlanExecute):\n",
|
||||
" if state['response']:\n",
|
||||
" if state[\"response\"]:\n",
|
||||
" return True\n",
|
||||
" else:\n",
|
||||
" return False"
|
||||
@@ -405,7 +432,7 @@
|
||||
"workflow.set_entry_point(\"planner\")\n",
|
||||
"\n",
|
||||
"# From plan we go to agent\n",
|
||||
"workflow.add_edge('planner', 'agent')\n",
|
||||
"workflow.add_edge(\"planner\", \"agent\")\n",
|
||||
"\n",
|
||||
"# From agent, we replan\n",
|
||||
"workflow.add_edge(\"agent\", \"replan\")\n",
|
||||
@@ -418,7 +445,7 @@
|
||||
" # If `tools`, then we call the tool node.\n",
|
||||
" True: END,\n",
|
||||
" False: \"agent\",\n",
|
||||
" }\n",
|
||||
" },\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Finally, we compile it!\n",
|
||||
@@ -449,6 +476,7 @@
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"\n",
|
||||
"config = {\"recursion_limit\": 50}\n",
|
||||
"inputs = {\"input\": \"what is the hometown of the 2024 Australia open winner?\"}\n",
|
||||
"async for event in app.astream(inputs, config=config):\n",
|
||||
|
||||
Reference in New Issue
Block a user