Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,53 @@
|
|
| 1 |
-
from langgraph.graph import
|
| 2 |
-
from typing import TypedDict
|
|
|
|
| 3 |
|
| 4 |
-
# Define the state type
|
| 5 |
class State(TypedDict):
|
| 6 |
-
messages: list[str]
|
| 7 |
current_step: str
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return {
|
| 12 |
-
|
| 13 |
-
"messages": state["messages"] + ["Information collected"],
|
| 14 |
"current_step": "process"
|
| 15 |
}
|
| 16 |
|
| 17 |
-
def process_info(state: State) ->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return {
|
| 19 |
-
|
| 20 |
-
"messages": state["messages"] + ["Information processed"],
|
| 21 |
"current_step": "end"
|
| 22 |
}
|
| 23 |
|
| 24 |
-
# Create
|
| 25 |
-
workflow =
|
| 26 |
|
| 27 |
-
# Add nodes
|
| 28 |
workflow.add_node("collect", collect_info)
|
| 29 |
workflow.add_node("process", process_info)
|
|
|
|
|
|
|
| 30 |
workflow.add_edge("collect", "process")
|
| 31 |
|
| 32 |
-
# Set
|
| 33 |
workflow.set_entry_point("collect")
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langgraph.graph import StateGraph
|
| 2 |
+
from typing import TypedDict, Annotated
|
| 3 |
+
from langgraph.graph.message import add_messages
|
| 4 |
|
|
|
|
| 5 |
class State(TypedDict):
|
| 6 |
+
messages: Annotated[list[str], add_messages]
|
| 7 |
current_step: str
|
| 8 |
|
| 9 |
+
def collect_info(state: State) -> dict:
|
| 10 |
+
print("\n--> In collect_info")
|
| 11 |
+
print(f"Messages before: {state['messages']}")
|
| 12 |
+
|
| 13 |
+
messages = state["messages"] + ["Information collected"]
|
| 14 |
+
print(f"Messages after: {messages}")
|
| 15 |
+
|
| 16 |
return {
|
| 17 |
+
"messages": messages,
|
|
|
|
| 18 |
"current_step": "process"
|
| 19 |
}
|
| 20 |
|
| 21 |
+
def process_info(state: State) -> dict:
|
| 22 |
+
print("\n--> In process_info")
|
| 23 |
+
print(f"Messages before: {state['messages']}")
|
| 24 |
+
|
| 25 |
+
messages = state["messages"] + ["Information processed"]
|
| 26 |
+
print(f"Messages after: {messages}")
|
| 27 |
+
|
| 28 |
return {
|
| 29 |
+
"messages": messages,
|
|
|
|
| 30 |
"current_step": "end"
|
| 31 |
}
|
| 32 |
|
| 33 |
+
# Create and setup graph
|
| 34 |
+
workflow = StateGraph(State)
|
| 35 |
|
| 36 |
+
# Add nodes
|
| 37 |
workflow.add_node("collect", collect_info)
|
| 38 |
workflow.add_node("process", process_info)
|
| 39 |
+
|
| 40 |
+
# Add edges
|
| 41 |
workflow.add_edge("collect", "process")
|
| 42 |
|
| 43 |
+
# Set entry and finish points
|
| 44 |
workflow.set_entry_point("collect")
|
| 45 |
+
workflow.set_finish_point("process")
|
| 46 |
+
|
| 47 |
+
app = workflow.compile()
|
| 48 |
+
|
| 49 |
+
# Run workflow
|
| 50 |
+
print("\nStarting workflow...")
|
| 51 |
+
initial_state = State(messages=["Starting"], current_step="collect")
|
| 52 |
+
final_state = app.invoke(initial_state)
|
| 53 |
+
print(f"\nFinal messages: {final_state['messages']}")
|