Spaces:
Running
Running
Update workflow.py
Browse files- workflow.py +15 -15
workflow.py
CHANGED
@@ -2,34 +2,33 @@
|
|
2 |
|
3 |
import time
|
4 |
from datetime import datetime
|
5 |
-
from typing import Dict,
|
6 |
|
7 |
-
from langchain_core.messages import AIMessage, HumanMessage
|
8 |
from langgraph.graph import END, StateGraph
|
9 |
from langgraph.graph.message import add_messages
|
10 |
-
import
|
11 |
|
12 |
from processor import EnhancedCognitiveProcessor
|
13 |
from config import ResearchConfig
|
14 |
|
15 |
-
|
16 |
-
|
17 |
|
|
|
18 |
class AgentState(TypedDict):
|
19 |
-
messages: Annotated[Sequence[
|
20 |
-
context:
|
21 |
-
metadata:
|
22 |
-
|
23 |
-
logger = logging.getLogger(__name__)
|
24 |
|
25 |
class ResearchWorkflow:
|
26 |
"""
|
27 |
Defines a multi-step research workflow using a state graph.
|
28 |
"""
|
29 |
def __init__(self) -> None:
|
30 |
-
# Provide the state schema to StateGraph
|
31 |
-
self.workflow = StateGraph(AgentState)
|
32 |
self.processor = EnhancedCognitiveProcessor()
|
|
|
|
|
33 |
self._build_workflow()
|
34 |
self.app = self.workflow.compile()
|
35 |
|
@@ -134,7 +133,8 @@ class ResearchWorkflow:
|
|
134 |
response = self.processor.process_query(validation_prompt)
|
135 |
logger.info("Output validation completed.")
|
136 |
return {
|
137 |
-
"messages": [AIMessage(content=analysis + f"\n\nValidation: {response.get('choices', [{}])[0].get('message', {}).get('content', '')}")]
|
|
|
138 |
}
|
139 |
except Exception as e:
|
140 |
logger.exception("Error during output validation.")
|
@@ -196,7 +196,7 @@ class ResearchWorkflow:
|
|
196 |
def _error_state(self, message: str) -> Dict:
|
197 |
logger.error(message)
|
198 |
return {
|
199 |
-
"messages": [
|
200 |
"context": {"error": True},
|
201 |
"metadata": {"status": "error"}
|
202 |
}
|
@@ -215,7 +215,7 @@ class ResearchWorkflow:
|
|
215 |
enhanced += f"{code}\n"
|
216 |
enhanced += "```"
|
217 |
return {
|
218 |
-
"messages": [
|
219 |
"context": state["context"]
|
220 |
}
|
221 |
except Exception as e:
|
|
|
2 |
|
3 |
import time
|
4 |
from datetime import datetime
|
5 |
+
from typing import Dict, Any, Sequence
|
6 |
|
7 |
+
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
|
8 |
from langgraph.graph import END, StateGraph
|
9 |
from langgraph.graph.message import add_messages
|
10 |
+
from typing_extensions import TypedDict, Annotated
|
11 |
|
12 |
from processor import EnhancedCognitiveProcessor
|
13 |
from config import ResearchConfig
|
14 |
|
15 |
+
import logging
|
16 |
+
logger = logging.getLogger(__name__)
|
17 |
|
18 |
+
# Define the state schema
|
19 |
class AgentState(TypedDict):
|
20 |
+
messages: Annotated[Sequence[AIMessage | HumanMessage | ToolMessage], add_messages]
|
21 |
+
context: Dict[str, Any]
|
22 |
+
metadata: Dict[str, Any]
|
|
|
|
|
23 |
|
24 |
class ResearchWorkflow:
|
25 |
"""
|
26 |
Defines a multi-step research workflow using a state graph.
|
27 |
"""
|
28 |
def __init__(self) -> None:
|
|
|
|
|
29 |
self.processor = EnhancedCognitiveProcessor()
|
30 |
+
# Provide the state schema to the StateGraph constructor
|
31 |
+
self.workflow = StateGraph(AgentState)
|
32 |
self._build_workflow()
|
33 |
self.app = self.workflow.compile()
|
34 |
|
|
|
133 |
response = self.processor.process_query(validation_prompt)
|
134 |
logger.info("Output validation completed.")
|
135 |
return {
|
136 |
+
"messages": [AIMessage(content=analysis + f"\n\nValidation: {response.get('choices', [{}])[0].get('message', {}).get('content', '')}")],
|
137 |
+
"context": state["context"]
|
138 |
}
|
139 |
except Exception as e:
|
140 |
logger.exception("Error during output validation.")
|
|
|
196 |
def _error_state(self, message: str) -> Dict:
|
197 |
logger.error(message)
|
198 |
return {
|
199 |
+
"messages": [AIMessage(content=f"❌ {message}")],
|
200 |
"context": {"error": True},
|
201 |
"metadata": {"status": "error"}
|
202 |
}
|
|
|
215 |
enhanced += f"{code}\n"
|
216 |
enhanced += "```"
|
217 |
return {
|
218 |
+
"messages": [AIMessage(content=enhanced)],
|
219 |
"context": state["context"]
|
220 |
}
|
221 |
except Exception as e:
|