Spaces:
Sleeping
Sleeping
Update pipeline.py
Browse files- pipeline.py +17 -30
pipeline.py
CHANGED
|
@@ -4,54 +4,41 @@ from weather_node import fetch_weather
|
|
| 4 |
from rag_node import rag_answer
|
| 5 |
from llm_node import refine_answer
|
| 6 |
|
| 7 |
-
# Graph state definition
|
| 8 |
class AgentState(TypedDict):
|
| 9 |
query: str
|
| 10 |
result: str
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
city = state["query"].split()[-1]
|
| 15 |
-
state["result"] = fetch_weather(city)
|
| 16 |
return state
|
| 17 |
|
| 18 |
-
|
| 19 |
-
def rag_node_fn(state: AgentState) -> AgentState:
|
| 20 |
state["result"] = rag_answer(state["query"])
|
| 21 |
return state
|
| 22 |
|
| 23 |
-
|
| 24 |
-
def decision_node(state: AgentState) -> str:
|
| 25 |
keywords = ["weather", "temperature", "forecast", "climate"]
|
| 26 |
-
if any(
|
| 27 |
-
return "weather"
|
| 28 |
-
return "rag"
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
state["result"] = refine_answer(f"Summarize this: {state['result']}")
|
| 33 |
return state
|
| 34 |
|
| 35 |
-
# Build pipeline with LangGraph
|
| 36 |
def build_pipeline():
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
workflow.add_node("llm", llm_node_fn)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
workflow.add_conditional_edges(
|
| 46 |
START,
|
| 47 |
decision_node,
|
| 48 |
-
{"weather": "weather", "rag": "rag"}
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
workflow.add_edge("llm", END)
|
| 55 |
-
|
| 56 |
-
return workflow.compile()
|
| 57 |
|
|
|
|
|
|
| 4 |
from rag_node import rag_answer
|
| 5 |
from llm_node import refine_answer
|
| 6 |
|
|
|
|
| 7 |
class AgentState(TypedDict):
|
| 8 |
query: str
|
| 9 |
result: str
|
| 10 |
|
| 11 |
+
def weather_node_fn(state: AgentState):
|
| 12 |
+
state["result"] = fetch_weather(state["query"])
|
|
|
|
|
|
|
| 13 |
return state
|
| 14 |
|
| 15 |
+
def rag_node_fn(state: AgentState):
|
|
|
|
| 16 |
state["result"] = rag_answer(state["query"])
|
| 17 |
return state
|
| 18 |
|
| 19 |
+
def decision_node(state: AgentState):
|
|
|
|
| 20 |
keywords = ["weather", "temperature", "forecast", "climate"]
|
| 21 |
+
return "weather" if any(k in state["query"].lower() for k in keywords) else "rag"
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def llm_node_fn(state: AgentState):
|
| 24 |
+
state["result"] = refine_answer(state["result"])
|
|
|
|
| 25 |
return state
|
| 26 |
|
|
|
|
| 27 |
def build_pipeline():
|
| 28 |
+
graph = StateGraph(AgentState)
|
| 29 |
|
| 30 |
+
graph.add_node("weather", weather_node_fn)
|
| 31 |
+
graph.add_node("rag", rag_node_fn)
|
| 32 |
+
graph.add_node("llm", llm_node_fn)
|
|
|
|
| 33 |
|
| 34 |
+
graph.add_conditional_edges(
|
|
|
|
| 35 |
START,
|
| 36 |
decision_node,
|
| 37 |
+
{"weather": "weather", "rag": "rag"}
|
| 38 |
)
|
| 39 |
|
| 40 |
+
graph.add_edge("weather", "llm")
|
| 41 |
+
graph.add_edge("rag", "llm")
|
| 42 |
+
graph.add_edge("llm", END)
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
return graph.compile()
|