mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-07 18:27:52 +02:00
update
This commit is contained in:
committed by
Nuno Campos
parent
b6e3cd9044
commit
51a538c6fc
+54
-18
@@ -64,12 +64,12 @@
|
||||
"tool_actor = Actor(\"tools\", RunnableLambda(execute_tools))\n",
|
||||
"end = End()\n",
|
||||
"\n",
|
||||
"workflow.register_node(llm_agent)\n",
|
||||
"workflow.register_node(tool_actor)\n",
|
||||
"workflow.add_node(llm_agent)\n",
|
||||
"workflow.add_node(tool_actor)\n",
|
||||
"\n",
|
||||
"workflow.set_entry_point(llm_agent.key)\n",
|
||||
"\n",
|
||||
"workflow.register_conditional_edges(\n",
|
||||
"workflow.add_conditional_edges(\n",
|
||||
" llm_agent.key,\n",
|
||||
" should_continue,\n",
|
||||
" {\n",
|
||||
@@ -77,16 +77,41 @@
|
||||
" \"exit\": end.key\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"workflow.register_edge(tool_actor.key, llm_agent.key)\n",
|
||||
"workflow.add_edge(tool_actor.key, llm_agent.key)\n",
|
||||
"chain = workflow.compile()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "c46bd262-9605-4449-9391-f6b6e0fe440e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Retrying langchain_community.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised ServiceUnavailableError: The server is overloaded or not ready yet..\n",
|
||||
"Retrying langchain_community.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised ServiceUnavailableError: The server is overloaded or not ready yet..\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'input': 'what is the weather in sf',\n",
|
||||
" 'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\"query\":\"weather in San Francisco\"}'}})]),\n",
|
||||
" [{'url': 'https://www.cbsnews.com/sanfrancisco/news/california-begins-2024-with-below-normal-snowpack-a-year-after-one-of-the-best-starts-in-decades/',\n",
|
||||
" 'content': 'January 2, 2024 / 3:27 PM PST / AP More from CBS News First published on January 2, 2024 / 2:28 PM PST Watch CBS News California begins 2024 with below-normal snowpack a year after one of the best starts in decades between January and April.New storm packing significant rain, strong winds approaches Bay Area 02:16. California is beginning 2024 with a below-normal mountain snowpack a year after it had one of its best starts in decades ...'}]),\n",
|
||||
" (AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'current weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'current weather in San Francisco'}`\\nresponded: It seems that the search results did not return the current weather in San Francisco. Let me try another method to fetch the weather information for you.\\n\\n\", message_log=[AIMessage(content='It seems that the search results did not return the current weather in San Francisco. Let me try another method to fetch the weather information for you.', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\"query\":\"current weather in San Francisco\"}'}})]),\n",
|
||||
" [])],\n",
|
||||
" 'agent_outcome': AgentFinish(return_values={'output': \"I'm sorry, but it seems that I'm unable to fetch the current weather information for San Francisco at the moment. I recommend using a weather website or app to get the most up-to-date weather forecast for San Francisco.\"}, log=\"I'm sorry, but it seems that I'm unable to fetch the current weather information for San Francisco at the moment. I recommend using a weather website or app to get the most up-to-date weather forecast for San Francisco.\")}"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"chain.invoke({\"input\": \"what is the weather in sf\", \"intermediate_steps\": []})"
|
||||
]
|
||||
@@ -101,7 +126,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "f6f96e81-4a20-4599-a625-8d18df6fa76d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -235,7 +260,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"id": "7708fa95-547b-4bea-b126-3656de7d5873",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -301,10 +326,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"id": "d6cdd1cd-e480-4dd7-99b4-9018eb243b4d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AgentFinish(return_values={'output': 'The current weather in San Francisco (SF) is characterized by a mild climate with January daytime maximum temperatures averaging around 13°C (55°F). The city experiences microclimates due to its topography and coastal location, leading to significant weather variations across different neighborhoods[1]. Historically, SF has wet winters and dry summers, with average temperatures ranging from the mid-40s to the low 70s Fahrenheit (7-22 degrees Celsius). The warmest months are typically September and October. Fog is frequent, especially in summer, which can lead to cooler temperatures. The rainy season spans from November to March, with an annual average rainfall of about 23 inches (584 mm). Wind is also a notable factor, particularly in coastal areas.\\n\\nReferences:\\n[1] https://www.weather2travel.com/california/san-francisco/january/', 'initial_answer': \"The weather in San Francisco (SF) is characterized by a mild, Mediterranean-like climate with wet winters and dry summers. The city's unique topography and coastal location result in microclimates, where weather conditions can vary significantly from one neighborhood to another. Average temperatures typically range from the mid-40s to the low 70s Fahrenheit (7-22 degrees Celsius), with the warmest months being September and October. Fog is a common occurrence, particularly in the summer, leading to cooler temperatures compared to the surrounding areas. Rainfall is concentrated from November to March, with the city receiving an average of about 23 inches (584 mm) annually. Wind is another factor to consider, as it can be quite strong, especially near the Golden Gate Bridge. It's always advisable to dress in layers when visiting SF due to the potential for rapid weather changes.\"}, log='Reached max steps.')"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"workflow = Graph()\n",
|
||||
"initial_answer_actor = Actor(\"initial\", initial_chain)\n",
|
||||
@@ -312,17 +348,17 @@
|
||||
"finish_actor = Actor(\"finish\", RunnableLambda(finish))\n",
|
||||
"tool_actor = Actor(\"tools\", RunnableLambda(execute_tools))\n",
|
||||
"\n",
|
||||
"# Register actors\n",
|
||||
"workflow.register_node(initial_answer_actor)\n",
|
||||
"workflow.register_node(next_step_actor)\n",
|
||||
"workflow.register_node(finish_actor)\n",
|
||||
"workflow.register_node(tool_actor)\n",
|
||||
"# add actors\n",
|
||||
"workflow.add_node(initial_answer_actor)\n",
|
||||
"workflow.add_node(next_step_actor)\n",
|
||||
"workflow.add_node(finish_actor)\n",
|
||||
"workflow.add_node(tool_actor)\n",
|
||||
"\n",
|
||||
"# Enter with initial actor, then loop through tools -> next steps until finished\n",
|
||||
"workflow.set_entry_point(initial_answer_actor.key)\n",
|
||||
"\n",
|
||||
"workflow.register_edge(initial_answer_actor.key, tool_actor.key)\n",
|
||||
"workflow.register_conditional_edges(\n",
|
||||
"workflow.add_edge(initial_answer_actor.key, tool_actor.key)\n",
|
||||
"workflow.add_conditional_edges(\n",
|
||||
" tool_actor.key,\n",
|
||||
" lambda x: \"exit\" if len(x['intermediate_steps']) >= 2 else \"continue\",\n",
|
||||
" {\n",
|
||||
@@ -330,7 +366,7 @@
|
||||
" \"exit\": finish_actor.key\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"workflow.register_edge(next_step_actor.key, tool_actor.key)\n",
|
||||
"workflow.add_edge(next_step_actor.key, tool_actor.key)\n",
|
||||
"workflow.set_finish_point(finish_actor.key)\n",
|
||||
"\n",
|
||||
"chain = workflow.compile()\n",
|
||||
|
||||
@@ -126,17 +126,17 @@ class Graph:
|
||||
# self.branches = {}
|
||||
self.entry_point: Optional[str] = None
|
||||
|
||||
def register_node(self, node: Actor):
|
||||
def add_node(self, node: Actor):
|
||||
if node.key in self.nodes:
|
||||
raise ValueError(f"Actor `{node.key}` already present.")
|
||||
self.nodes[node.key] = node
|
||||
|
||||
|
||||
def register_edge(self, start_key: str, end_key: str):
|
||||
def add_edge(self, start_key: str, end_key: str):
|
||||
if start_key not in self.nodes:
|
||||
raise ValueError(f"Need to register_node `{start_key}` first")
|
||||
raise ValueError(f"Need to add_node `{start_key}` first")
|
||||
if end_key not in self.nodes:
|
||||
raise ValueError(f"Need to register_node `{end_key}` first")
|
||||
raise ValueError(f"Need to add_node `{end_key}` first")
|
||||
|
||||
# TODO: support multiple message passing
|
||||
if start_key in set(edge.start_key for edge in self.edges):
|
||||
@@ -144,7 +144,7 @@ class Graph:
|
||||
|
||||
self.edges.append(LangGraphEdge(start_key, end_key))
|
||||
|
||||
def register_conditional_edges(
|
||||
def add_conditional_edges(
|
||||
self,
|
||||
start_key: str,
|
||||
condition: Callable[Any, str],
|
||||
@@ -156,11 +156,11 @@ class Graph:
|
||||
condition
|
||||
)
|
||||
|
||||
self.register_node(conditional_node)
|
||||
self.register_edge(start_key, conditional_node.key)
|
||||
self.add_node(conditional_node)
|
||||
self.add_edge(start_key, conditional_node.key)
|
||||
|
||||
for branch in conditional_node.branches:
|
||||
self.register_node(branch)
|
||||
self.add_node(branch)
|
||||
self.edges.append(ConditionalEdge(conditional_node.key, branch.key))
|
||||
self.edges.append(
|
||||
BranchEdge(branch.key, conditional_node.conditional_edge_mapping[branch.condition])
|
||||
@@ -169,13 +169,13 @@ class Graph:
|
||||
|
||||
def set_entry_point(self, key: str):
|
||||
if key not in self.nodes:
|
||||
raise ValueError(f"Need to register_node `{node.key}` first")
|
||||
raise ValueError(f"Need to add_node `{node.key}` first")
|
||||
self.entry_point = key
|
||||
|
||||
def set_finish_point(self, key: str):
|
||||
if key not in self.nodes:
|
||||
raise ValueError(f"Need to register_node `{node.key}` first")
|
||||
self.register_edge(key, "end")
|
||||
raise ValueError(f"Need to add_node `{node.key}` first")
|
||||
self.add_edge(key, "end")
|
||||
|
||||
def compile(self):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user