Spaces:
Runtime error
Runtime error
Update workflow.py
Browse files- workflow.py +27 -23
workflow.py
CHANGED
|
@@ -24,6 +24,8 @@ class AgentState(TypedDict):
|
|
| 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()
|
|
@@ -58,7 +60,12 @@ class ResearchWorkflow:
|
|
| 58 |
query = state["messages"][-1].content
|
| 59 |
# Retrieve the domain from the state's context (defaulting to Biomedical Research)
|
| 60 |
domain = state.get("context", {}).get("domain", "Biomedical Research")
|
| 61 |
-
new_context = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
logger.info(f"Query ingested. Domain: {domain}")
|
| 63 |
return {
|
| 64 |
"messages": [AIMessage(content="Query ingested successfully")],
|
|
@@ -73,7 +80,7 @@ class ResearchWorkflow:
|
|
| 73 |
try:
|
| 74 |
query = state["context"]["raw_query"]
|
| 75 |
# For demonstration, we use an empty document list.
|
| 76 |
-
#
|
| 77 |
docs = []
|
| 78 |
logger.info(f"Retrieved {len(docs)} documents for query.")
|
| 79 |
return {
|
|
@@ -93,28 +100,25 @@ class ResearchWorkflow:
|
|
| 93 |
def analyze_content(self, state: Dict) -> Dict:
|
| 94 |
try:
|
| 95 |
domain = state["context"].get("domain", "Biomedical Research").strip().lower()
|
| 96 |
-
|
| 97 |
-
#
|
| 98 |
-
if
|
| 99 |
-
logger.info(f"Using fallback analysis for domain: {state['context'].get('domain')}")
|
| 100 |
-
return {
|
| 101 |
-
"messages": [AIMessage(content=fallback_analyses[domain].strip())],
|
| 102 |
-
"context": state["context"]
|
| 103 |
-
}
|
| 104 |
-
else:
|
| 105 |
-
docs = state["context"].get("documents", [])
|
| 106 |
docs_text = "\n\n".join([d.page_content for d in docs])
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
logger.exception("Error during content analysis.")
|
| 120 |
return self._error_state(f"Analysis Error: {str(e)}")
|
|
|
|
| 24 |
class ResearchWorkflow:
|
| 25 |
"""
|
| 26 |
Defines a multi-step research workflow using a state graph.
|
| 27 |
+
This workflow now employs a Retrieval-Augmented Generation (RAG) approach.
|
| 28 |
+
When no documents are retrieved, the model dynamically synthesizes an analysis using its internal knowledge.
|
| 29 |
"""
|
| 30 |
def __init__(self) -> None:
|
| 31 |
self.processor = EnhancedCognitiveProcessor()
|
|
|
|
| 60 |
query = state["messages"][-1].content
|
| 61 |
# Retrieve the domain from the state's context (defaulting to Biomedical Research)
|
| 62 |
domain = state.get("context", {}).get("domain", "Biomedical Research")
|
| 63 |
+
new_context = {
|
| 64 |
+
"raw_query": query,
|
| 65 |
+
"domain": domain,
|
| 66 |
+
"refine_count": 0,
|
| 67 |
+
"refinement_history": []
|
| 68 |
+
}
|
| 69 |
logger.info(f"Query ingested. Domain: {domain}")
|
| 70 |
return {
|
| 71 |
"messages": [AIMessage(content="Query ingested successfully")],
|
|
|
|
| 80 |
try:
|
| 81 |
query = state["context"]["raw_query"]
|
| 82 |
# For demonstration, we use an empty document list.
|
| 83 |
+
# In a full RAG system, you would retrieve relevant documents from an external index.
|
| 84 |
docs = []
|
| 85 |
logger.info(f"Retrieved {len(docs)} documents for query.")
|
| 86 |
return {
|
|
|
|
| 100 |
def analyze_content(self, state: Dict) -> Dict:
|
| 101 |
try:
|
| 102 |
domain = state["context"].get("domain", "Biomedical Research").strip().lower()
|
| 103 |
+
docs = state["context"].get("documents", [])
|
| 104 |
+
# Use retrieved documents if available; otherwise, use the raw query.
|
| 105 |
+
if docs:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
docs_text = "\n\n".join([d.page_content for d in docs])
|
| 107 |
+
else:
|
| 108 |
+
docs_text = state["context"].get("raw_query", "")
|
| 109 |
+
logger.info("No documents retrieved; switching to dynamic RAG mode.")
|
| 110 |
+
domain_prompt = ResearchConfig.DOMAIN_PROMPTS.get(domain, "")
|
| 111 |
+
# RAG approach: combine domain prompt with retrieved docs or raw query.
|
| 112 |
+
full_prompt = f"{domain_prompt}\n\n" + ResearchConfig.ANALYSIS_TEMPLATE.format(context=docs_text)
|
| 113 |
+
response = self.processor.process_query(full_prompt)
|
| 114 |
+
if "error" in response:
|
| 115 |
+
logger.error("Backend response error during analysis.")
|
| 116 |
+
return self._error_state(response["error"])
|
| 117 |
+
logger.info("Content analysis completed using RAG approach.")
|
| 118 |
+
return {
|
| 119 |
+
"messages": [AIMessage(content=response.get('choices', [{}])[0].get('message', {}).get('content', ''))],
|
| 120 |
+
"context": state["context"]
|
| 121 |
+
}
|
| 122 |
except Exception as e:
|
| 123 |
logger.exception("Error during content analysis.")
|
| 124 |
return self._error_state(f"Analysis Error: {str(e)}")
|