Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,39 @@
|
|
| 1 |
-
#
|
| 2 |
-
# Imports &
|
| 3 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from langchain_openai import OpenAIEmbeddings
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
-
from langchain_core.messages import HumanMessage, AIMessage,
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langgraph.graph import END, StateGraph
|
|
|
|
|
|
|
| 9 |
from typing_extensions import TypedDict, Annotated
|
| 10 |
from typing import Sequence, Dict, List, Optional, Any
|
| 11 |
import chromadb
|
| 12 |
import os
|
| 13 |
-
import streamlit as st
|
| 14 |
import requests
|
| 15 |
import hashlib
|
| 16 |
-
import json
|
| 17 |
import time
|
| 18 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 19 |
from datetime import datetime
|
| 20 |
-
from pydantic import BaseModel, ValidationError
|
| 21 |
-
import traceback
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
#
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
class ResearchConfig:
|
| 27 |
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
|
| 28 |
CHROMA_PATH = "chroma_db"
|
|
@@ -30,171 +41,139 @@ class ResearchConfig:
|
|
| 30 |
CHUNK_OVERLAP = 64
|
| 31 |
MAX_CONCURRENT_REQUESTS = 5
|
| 32 |
EMBEDDING_DIMENSIONS = 1536
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
{context}
|
| 35 |
|
| 36 |
Respond with:
|
| 37 |
-
1. Key Technical
|
| 38 |
-
2.
|
| 39 |
-
3.
|
| 40 |
-
4.
|
| 41 |
-
5.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
$$\\mathcal{L}_{total} = \\alpha\\mathcal{L}_{CE} + \\beta\\mathcal{L}_{SSIM}$$""",
|
| 60 |
-
"metadata": {
|
| 61 |
-
"year": 2024,
|
| 62 |
-
"domain": "computer_vision",
|
| 63 |
-
"citations": 142
|
| 64 |
-
}
|
| 65 |
-
},
|
| 66 |
-
"Quantum ML Advances": {
|
| 67 |
-
"content": """## Quantum Machine Learning Breakthroughs
|
| 68 |
-
**Authors**: Quantum AI Lab
|
| 69 |
-
|
| 70 |
-
### Achievements:
|
| 71 |
-
- Quantum-enhanced SGD (40% faster convergence)
|
| 72 |
-
- 5-qubit QNN achieving 98% accuracy
|
| 73 |
-
- Hybrid quantum-classical GANs
|
| 74 |
-
|
| 75 |
-
$$\\mathcal{H} = -\\sum_{i<j} J_{ij}\\sigma_i^z\\sigma_j^z - \\Gamma\\sum_i\\sigma_i^x$$""",
|
| 76 |
-
"metadata": {
|
| 77 |
-
"year": 2023,
|
| 78 |
-
"domain": "quantum_ml",
|
| 79 |
-
"citations": 89
|
| 80 |
-
}
|
| 81 |
-
}
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
class DocumentSchema(BaseModel):
|
| 85 |
-
content: str
|
| 86 |
-
metadata: dict
|
| 87 |
-
doc_id: str
|
| 88 |
-
|
| 89 |
-
# ------------------------------
|
| 90 |
-
# State Management
|
| 91 |
-
# ------------------------------
|
| 92 |
-
class ResearchState(TypedDict):
|
| 93 |
-
messages: Annotated[List[BaseMessage], add_messages]
|
| 94 |
-
context: Annotated[Dict[str, Any], "research_context"]
|
| 95 |
-
metadata: Annotated[Dict[str, str], "system_metadata"]
|
| 96 |
-
|
| 97 |
-
# ------------------------------
|
| 98 |
-
# Document Processing
|
| 99 |
-
# ------------------------------
|
| 100 |
-
class DocumentManager:
|
| 101 |
def __init__(self):
|
| 102 |
self.client = chromadb.PersistentClient(path=ResearchConfig.CHROMA_PATH)
|
| 103 |
self.embeddings = OpenAIEmbeddings(
|
| 104 |
model="text-embedding-3-large",
|
| 105 |
dimensions=ResearchConfig.EMBEDDING_DIMENSIONS
|
| 106 |
)
|
| 107 |
-
|
| 108 |
-
def initialize_collections(self):
|
| 109 |
-
try:
|
| 110 |
-
self.research_col = self._create_collection("research")
|
| 111 |
-
self.dev_col = self._create_collection("development")
|
| 112 |
-
except Exception as e:
|
| 113 |
-
st.error(f"Collection initialization failed: {str(e)}")
|
| 114 |
-
traceback.print_exc()
|
| 115 |
-
|
| 116 |
-
def _create_collection(self, name: str) -> Chroma:
|
| 117 |
-
documents, metadatas, ids = [], [], []
|
| 118 |
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
-
doc = DocumentSchema(
|
| 122 |
-
content=data["content"],
|
| 123 |
-
metadata=data["metadata"],
|
| 124 |
-
doc_id=hashlib.sha256(title.encode()).hexdigest()[:16]
|
| 125 |
-
)
|
| 126 |
-
documents.append(doc.content)
|
| 127 |
-
metadatas.append(doc.metadata)
|
| 128 |
-
ids.append(doc.doc_id)
|
| 129 |
-
except ValidationError as e:
|
| 130 |
-
st.error(f"Invalid document format: {title} - {str(e)}")
|
| 131 |
-
continue
|
| 132 |
-
|
| 133 |
splitter = RecursiveCharacterTextSplitter(
|
| 134 |
chunk_size=ResearchConfig.CHUNK_SIZE,
|
| 135 |
chunk_overlap=ResearchConfig.CHUNK_OVERLAP,
|
| 136 |
-
separators=["\n
|
| 137 |
)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
#
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
class ResearchRetriever:
|
| 155 |
def __init__(self):
|
| 156 |
-
self.
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
def retrieve(self, query: str, domain: str) -> List[
|
|
|
|
| 160 |
try:
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
results = collection.as_retriever(
|
| 166 |
-
search_type="mmr",
|
| 167 |
-
search_kwargs={'k': 4, 'fetch_k': 20}
|
| 168 |
-
).invoke(query)
|
| 169 |
-
|
| 170 |
-
return [DocumentSchema(
|
| 171 |
-
content=doc.page_content,
|
| 172 |
-
metadata=doc.metadata,
|
| 173 |
-
doc_id=doc.metadata.get("doc_id", "")
|
| 174 |
-
) for doc in results if doc.page_content]
|
| 175 |
-
|
| 176 |
-
except Exception as e:
|
| 177 |
-
st.error(f"Retrieval failure: {str(e)}")
|
| 178 |
-
traceback.print_exc()
|
| 179 |
return []
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
#
|
| 184 |
-
|
|
|
|
|
|
|
| 185 |
def __init__(self):
|
| 186 |
self.executor = ThreadPoolExecutor(max_workers=ResearchConfig.MAX_CONCURRENT_REQUESTS)
|
| 187 |
-
self.
|
| 188 |
-
|
| 189 |
-
def
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
headers = {
|
| 195 |
"Authorization": f"Bearer {ResearchConfig.DEEPSEEK_API_KEY}",
|
| 196 |
-
"
|
| 197 |
-
"
|
| 198 |
}
|
| 199 |
|
| 200 |
try:
|
|
@@ -203,252 +182,293 @@ class AnalysisEngine:
|
|
| 203 |
headers=headers,
|
| 204 |
json={
|
| 205 |
"model": "deepseek-chat",
|
| 206 |
-
"messages": [{
|
|
|
|
|
|
|
|
|
|
| 207 |
"temperature": 0.7,
|
| 208 |
-
"max_tokens":
|
|
|
|
| 209 |
},
|
| 210 |
-
timeout=
|
| 211 |
)
|
| 212 |
response.raise_for_status()
|
| 213 |
return response.json()
|
| 214 |
-
except
|
| 215 |
-
return {"error": str(e)
|
| 216 |
-
|
| 217 |
-
def
|
|
|
|
| 218 |
valid = [r for r in results if "error" not in r]
|
| 219 |
if not valid:
|
| 220 |
-
return {"error": "All
|
| 221 |
-
|
| 222 |
-
best = max(valid, key=lambda x: len(x.get('choices', [{}])[0].get('message', {}).get('content', ''))
|
| 223 |
-
return best
|
| 224 |
|
| 225 |
-
#
|
| 226 |
-
# Workflow
|
| 227 |
-
#
|
| 228 |
class ResearchWorkflow:
|
| 229 |
def __init__(self):
|
| 230 |
-
self.
|
| 231 |
-
self.
|
| 232 |
-
self.
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
self.workflow.add_node("ingest", self.
|
| 237 |
-
self.workflow.add_node("retrieve", self.
|
| 238 |
-
self.workflow.add_node("analyze", self.
|
| 239 |
-
self.workflow.add_node("validate", self.
|
| 240 |
-
self.workflow.add_node("refine", self.
|
| 241 |
-
|
|
|
|
| 242 |
self.workflow.set_entry_point("ingest")
|
| 243 |
self.workflow.add_edge("ingest", "retrieve")
|
| 244 |
self.workflow.add_edge("retrieve", "analyze")
|
| 245 |
self.workflow.add_conditional_edges(
|
| 246 |
"analyze",
|
| 247 |
-
self.
|
| 248 |
{"valid": "validate", "invalid": "refine"}
|
| 249 |
)
|
| 250 |
self.workflow.add_edge("validate", END)
|
| 251 |
self.workflow.add_edge("refine", "retrieve")
|
| 252 |
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
try:
|
| 255 |
-
query =
|
| 256 |
-
if isinstance(msg, HumanMessage))
|
| 257 |
return {
|
| 258 |
-
"messages": [AIMessage(content="Query ingested")],
|
| 259 |
-
"context": {
|
| 260 |
-
|
| 261 |
-
"documents": [],
|
| 262 |
-
"errors": []
|
| 263 |
-
},
|
| 264 |
-
"metadata": {
|
| 265 |
-
"session_id": hashlib.sha256(str(time.time()).encode()).hexdigest()[:8],
|
| 266 |
-
"timestamp": datetime.now().isoformat()
|
| 267 |
-
}
|
| 268 |
}
|
| 269 |
except Exception as e:
|
| 270 |
-
return self.
|
| 271 |
|
| 272 |
-
def
|
|
|
|
| 273 |
try:
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
return {
|
| 276 |
"messages": [AIMessage(content=f"Retrieved {len(docs)} documents")],
|
| 277 |
"context": {
|
| 278 |
-
**state["context"],
|
| 279 |
"documents": docs,
|
| 280 |
"retrieval_time": time.time()
|
| 281 |
-
}
|
| 282 |
-
"metadata": state["metadata"]
|
| 283 |
}
|
| 284 |
except Exception as e:
|
| 285 |
-
return self.
|
| 286 |
|
| 287 |
-
def
|
| 288 |
-
|
| 289 |
-
if not docs:
|
| 290 |
-
return self._handle_error("No documents for analysis", state)
|
| 291 |
-
|
| 292 |
try:
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
result = self.engine.analyze(prompt)
|
| 296 |
-
|
| 297 |
-
if "error" in result:
|
| 298 |
-
raise RuntimeError(result["error"])
|
| 299 |
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
-
if
|
| 303 |
-
|
| 304 |
|
| 305 |
return {
|
| 306 |
-
"messages": [AIMessage(content=content)],
|
| 307 |
-
"context":
|
| 308 |
-
"metadata": state["metadata"]
|
| 309 |
}
|
| 310 |
except Exception as e:
|
| 311 |
-
return self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
-
def
|
| 314 |
-
|
| 315 |
-
|
|
|
|
| 316 |
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
-
def
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
return "valid" if
|
| 325 |
|
| 326 |
-
def
|
|
|
|
|
|
|
| 327 |
return {
|
| 328 |
-
"messages": [AIMessage(content=f"
|
| 329 |
-
"context": {
|
| 330 |
-
|
| 331 |
-
"errors": state["context"]["errors"] + [message]
|
| 332 |
-
},
|
| 333 |
-
"metadata": state["metadata"]
|
| 334 |
}
|
| 335 |
|
| 336 |
-
#
|
| 337 |
-
#
|
| 338 |
-
#
|
| 339 |
class ResearchInterface:
|
| 340 |
def __init__(self):
|
| 341 |
-
self.workflow = ResearchWorkflow()
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
def _setup_interface(self):
|
| 345 |
-
st.set_page_config(
|
| 346 |
-
page_title="Research Assistant",
|
| 347 |
-
layout="wide",
|
| 348 |
-
initial_sidebar_state="expanded"
|
| 349 |
-
)
|
| 350 |
-
self._apply_styles()
|
| 351 |
self._build_sidebar()
|
| 352 |
-
self.
|
| 353 |
|
| 354 |
-
def
|
|
|
|
| 355 |
st.markdown("""
|
| 356 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
.stApp {
|
| 358 |
-
background:
|
| 359 |
-
color:
|
|
|
|
| 360 |
}
|
|
|
|
| 361 |
.stTextArea textarea {
|
| 362 |
-
background: #
|
| 363 |
-
color:
|
|
|
|
|
|
|
|
|
|
| 364 |
}
|
|
|
|
| 365 |
.stButton>button {
|
| 366 |
-
background:
|
| 367 |
-
border:
|
|
|
|
|
|
|
|
|
|
| 368 |
}
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
margin: 1rem 0;
|
| 374 |
}
|
| 375 |
</style>
|
| 376 |
""", unsafe_allow_html=True)
|
| 377 |
|
| 378 |
def _build_sidebar(self):
|
|
|
|
| 379 |
with st.sidebar:
|
| 380 |
-
st.title("π
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
-
if st.button("
|
| 390 |
-
self.
|
| 391 |
|
| 392 |
-
def
|
|
|
|
| 393 |
try:
|
| 394 |
-
with st.spinner("
|
| 395 |
-
|
| 396 |
-
"messages": [HumanMessage(content=query)],
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
},
|
| 402 |
-
"metadata": {}
|
| 403 |
-
}
|
| 404 |
-
|
| 405 |
-
for event in self.workflow.stream(state):
|
| 406 |
-
self._display_progress(event)
|
| 407 |
-
|
| 408 |
-
final_state = self.workflow.invoke(state)
|
| 409 |
-
self._show_results(final_state)
|
| 410 |
-
|
| 411 |
except Exception as e:
|
| 412 |
st.error(f"""**Analysis Failed**
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
def
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
st.
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
with
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
st.
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
else:
|
| 440 |
-
st.
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
if state["context"].get("errors"):
|
| 444 |
-
st.error("Analysis completed with errors")
|
| 445 |
-
with st.expander("Error Details"):
|
| 446 |
-
for error in state["context"]["errors"]:
|
| 447 |
-
st.markdown(f"- {error}")
|
| 448 |
-
else:
|
| 449 |
-
st.success("Analysis completed successfully β
")
|
| 450 |
-
with st.expander("Full Report"):
|
| 451 |
-
st.markdown(state["messages"][-1].content)
|
| 452 |
|
|
|
|
|
|
|
|
|
|
| 453 |
if __name__ == "__main__":
|
| 454 |
-
ResearchInterface()
|
|
|
|
| 1 |
+
# -----------------------------------------------------
|
| 2 |
+
# Imports & Initial Configuration
|
| 3 |
+
# -----------------------------------------------------
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# IMPORTANT: Must be the first Streamlit command
|
| 7 |
+
st.set_page_config(page_title="NeuroResearch AI", layout="wide", initial_sidebar_state="expanded")
|
| 8 |
+
|
| 9 |
from langchain_openai import OpenAIEmbeddings
|
| 10 |
from langchain_community.vectorstores import Chroma
|
| 11 |
+
from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
|
| 12 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 13 |
from langgraph.graph import END, StateGraph
|
| 14 |
+
from langgraph.prebuilt import ToolNode
|
| 15 |
+
from langgraph.graph.message import add_messages
|
| 16 |
from typing_extensions import TypedDict, Annotated
|
| 17 |
from typing import Sequence, Dict, List, Optional, Any
|
| 18 |
import chromadb
|
| 19 |
import os
|
|
|
|
| 20 |
import requests
|
| 21 |
import hashlib
|
|
|
|
| 22 |
import time
|
| 23 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 24 |
from datetime import datetime
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# -----------------------------------------------------
|
| 27 |
+
# State Schema Definition
|
| 28 |
+
# -----------------------------------------------------
|
| 29 |
+
class AgentState(TypedDict):
|
| 30 |
+
messages: Annotated[Sequence[AIMessage | HumanMessage | ToolMessage], add_messages]
|
| 31 |
+
context: Dict[str, Any]
|
| 32 |
+
metadata: Dict[str, Any]
|
| 33 |
+
|
| 34 |
+
# -----------------------------------------------------
|
| 35 |
+
# Configuration
|
| 36 |
+
# -----------------------------------------------------
|
| 37 |
class ResearchConfig:
|
| 38 |
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
|
| 39 |
CHROMA_PATH = "chroma_db"
|
|
|
|
| 41 |
CHUNK_OVERLAP = 64
|
| 42 |
MAX_CONCURRENT_REQUESTS = 5
|
| 43 |
EMBEDDING_DIMENSIONS = 1536
|
| 44 |
+
DOCUMENT_MAP = {
|
| 45 |
+
"Research Report: Results of a New AI Model Improving Image Recognition Accuracy to 98%":
|
| 46 |
+
"CV-Transformer Hybrid Architecture",
|
| 47 |
+
"Academic Paper Summary: Why Transformers Became the Mainstream Architecture in Natural Language Processing":
|
| 48 |
+
"Transformer Architecture Analysis",
|
| 49 |
+
"Latest Trends in Machine Learning Methods Using Quantum Computing":
|
| 50 |
+
"Quantum ML Frontiers"
|
| 51 |
+
}
|
| 52 |
+
ANALYSIS_TEMPLATE = """Analyze these technical documents with scientific rigor:
|
| 53 |
{context}
|
| 54 |
|
| 55 |
Respond with:
|
| 56 |
+
1. Key Technical Contributions (bullet points)
|
| 57 |
+
2. Novel Methodologies
|
| 58 |
+
3. Empirical Results (with metrics)
|
| 59 |
+
4. Potential Applications
|
| 60 |
+
5. Limitations & Future Directions
|
| 61 |
+
|
| 62 |
+
Format: Markdown with LaTeX mathematical notation where applicable
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
# Validate API key configuration
|
| 66 |
+
if not ResearchConfig.DEEPSEEK_API_KEY:
|
| 67 |
+
st.error("""**Research Portal Configuration Required**
|
| 68 |
+
1. Obtain DeepSeek API key: [platform.deepseek.com](https://platform.deepseek.com/)
|
| 69 |
+
2. Configure secret: `DEEPSEEK_API_KEY` in Space settings
|
| 70 |
+
3. Rebuild deployment""")
|
| 71 |
+
st.stop()
|
| 72 |
+
|
| 73 |
+
# -----------------------------------------------------
|
| 74 |
+
# Quantum Document Processing
|
| 75 |
+
# -----------------------------------------------------
|
| 76 |
+
class QuantumDocumentManager:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def __init__(self):
|
| 78 |
self.client = chromadb.PersistentClient(path=ResearchConfig.CHROMA_PATH)
|
| 79 |
self.embeddings = OpenAIEmbeddings(
|
| 80 |
model="text-embedding-3-large",
|
| 81 |
dimensions=ResearchConfig.EMBEDDING_DIMENSIONS
|
| 82 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
def create_collection(self, documents: List[str], collection_name: str) -> Chroma:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
splitter = RecursiveCharacterTextSplitter(
|
| 86 |
chunk_size=ResearchConfig.CHUNK_SIZE,
|
| 87 |
chunk_overlap=ResearchConfig.CHUNK_OVERLAP,
|
| 88 |
+
separators=["\n\n", "\n", "|||"]
|
| 89 |
)
|
| 90 |
+
docs = splitter.create_documents(documents)
|
| 91 |
+
# Debug lines about chunk creation removed
|
| 92 |
+
return Chroma.from_documents(
|
| 93 |
+
documents=docs,
|
| 94 |
+
embedding=self.embeddings,
|
| 95 |
+
client=self.client,
|
| 96 |
+
collection_name=collection_name,
|
| 97 |
+
ids=[self._document_id(doc.page_content) for doc in docs]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
def _document_id(self, content: str) -> str:
|
| 101 |
+
"""Create a unique ID for each document chunk."""
|
| 102 |
+
return f"{hashlib.sha256(content.encode()).hexdigest()[:16]}-{int(time.time())}"
|
| 103 |
+
|
| 104 |
+
# Initialize document collections
|
| 105 |
+
qdm = QuantumDocumentManager()
|
| 106 |
+
research_docs = qdm.create_collection([
|
| 107 |
+
"Research Report: Results of a New AI Model Improving Image Recognition Accuracy to 98%",
|
| 108 |
+
"Academic Paper Summary: Why Transformers Became the Mainstream Architecture in Natural Language Processing",
|
| 109 |
+
"Latest Trends in Machine Learning Methods Using Quantum Computing"
|
| 110 |
+
], "research")
|
| 111 |
+
|
| 112 |
+
development_docs = qdm.create_collection([
|
| 113 |
+
"Project A: UI Design Completed, API Integration in Progress",
|
| 114 |
+
"Project B: Testing New Feature X, Bug Fixes Needed",
|
| 115 |
+
"Product Y: In the Performance Optimization Stage Before Release"
|
| 116 |
+
], "development")
|
| 117 |
+
|
| 118 |
+
# -----------------------------------------------------
|
| 119 |
+
# Advanced Retrieval System
|
| 120 |
+
# -----------------------------------------------------
|
| 121 |
class ResearchRetriever:
|
| 122 |
def __init__(self):
|
| 123 |
+
self.retrievers = {
|
| 124 |
+
"research": research_docs.as_retriever(
|
| 125 |
+
search_type="mmr",
|
| 126 |
+
search_kwargs={
|
| 127 |
+
'k': 4,
|
| 128 |
+
'fetch_k': 20,
|
| 129 |
+
'lambda_mult': 0.85
|
| 130 |
+
}
|
| 131 |
+
),
|
| 132 |
+
"development": development_docs.as_retriever(
|
| 133 |
+
search_type="similarity",
|
| 134 |
+
search_kwargs={'k': 3}
|
| 135 |
+
)
|
| 136 |
+
}
|
| 137 |
|
| 138 |
+
def retrieve(self, query: str, domain: str) -> List[Any]:
|
| 139 |
+
"""Retrieve documents from the specified domain."""
|
| 140 |
try:
|
| 141 |
+
return self.retrievers[domain].invoke(query)
|
| 142 |
+
except KeyError:
|
| 143 |
+
st.error(f"[ERROR] Retrieval domain '{domain}' not found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
return []
|
| 145 |
|
| 146 |
+
retriever = ResearchRetriever()
|
| 147 |
+
|
| 148 |
+
# -----------------------------------------------------
|
| 149 |
+
# Cognitive Processing Unit
|
| 150 |
+
# -----------------------------------------------------
|
| 151 |
+
class CognitiveProcessor:
|
| 152 |
def __init__(self):
|
| 153 |
self.executor = ThreadPoolExecutor(max_workers=ResearchConfig.MAX_CONCURRENT_REQUESTS)
|
| 154 |
+
self.session_id = hashlib.sha256(datetime.now().isoformat().encode()).hexdigest()[:12]
|
| 155 |
+
|
| 156 |
+
def process_query(self, prompt: str) -> Dict:
|
| 157 |
+
"""Send the prompt to the DeepSeek API using triple redundancy for robustness."""
|
| 158 |
+
futures = []
|
| 159 |
+
for _ in range(3):
|
| 160 |
+
futures.append(self.executor.submit(self._execute_api_request, prompt))
|
| 161 |
+
|
| 162 |
+
results = []
|
| 163 |
+
for future in as_completed(futures):
|
| 164 |
+
try:
|
| 165 |
+
results.append(future.result())
|
| 166 |
+
except Exception as e:
|
| 167 |
+
st.error(f"Processing Error: {str(e)}")
|
| 168 |
+
|
| 169 |
+
return self._consensus_check(results)
|
| 170 |
+
|
| 171 |
+
def _execute_api_request(self, prompt: str) -> Dict:
|
| 172 |
+
"""Make a single request to the DeepSeek API."""
|
| 173 |
headers = {
|
| 174 |
"Authorization": f"Bearer {ResearchConfig.DEEPSEEK_API_KEY}",
|
| 175 |
+
"Content-Type": "application/json",
|
| 176 |
+
"X-Research-Session": self.session_id
|
| 177 |
}
|
| 178 |
|
| 179 |
try:
|
|
|
|
| 182 |
headers=headers,
|
| 183 |
json={
|
| 184 |
"model": "deepseek-chat",
|
| 185 |
+
"messages": [{
|
| 186 |
+
"role": "user",
|
| 187 |
+
"content": f"Respond as Senior AI Researcher:\n{prompt}"
|
| 188 |
+
}],
|
| 189 |
"temperature": 0.7,
|
| 190 |
+
"max_tokens": 1500,
|
| 191 |
+
"top_p": 0.9
|
| 192 |
},
|
| 193 |
+
timeout=45
|
| 194 |
)
|
| 195 |
response.raise_for_status()
|
| 196 |
return response.json()
|
| 197 |
+
except requests.exceptions.RequestException as e:
|
| 198 |
+
return {"error": str(e)}
|
| 199 |
+
|
| 200 |
+
def _consensus_check(self, results: List[Dict]) -> Dict:
|
| 201 |
+
"""Pick the best result by comparing content length among successful responses."""
|
| 202 |
valid = [r for r in results if "error" not in r]
|
| 203 |
if not valid:
|
| 204 |
+
return {"error": "All API requests failed"}
|
| 205 |
+
return max(valid, key=lambda x: len(x.get('choices', [{}])[0].get('message', {}).get('content', '')))
|
|
|
|
|
|
|
| 206 |
|
| 207 |
+
# -----------------------------------------------------
|
| 208 |
+
# Research Workflow Engine
|
| 209 |
+
# -----------------------------------------------------
|
| 210 |
class ResearchWorkflow:
|
| 211 |
def __init__(self):
|
| 212 |
+
self.processor = CognitiveProcessor()
|
| 213 |
+
self.workflow = StateGraph(AgentState)
|
| 214 |
+
self._build_workflow()
|
| 215 |
+
|
| 216 |
+
def _build_workflow(self):
|
| 217 |
+
# Register nodes in the state graph
|
| 218 |
+
self.workflow.add_node("ingest", self.ingest_query)
|
| 219 |
+
self.workflow.add_node("retrieve", self.retrieve_documents)
|
| 220 |
+
self.workflow.add_node("analyze", self.analyze_content)
|
| 221 |
+
self.workflow.add_node("validate", self.validate_output)
|
| 222 |
+
self.workflow.add_node("refine", self.refine_results)
|
| 223 |
+
|
| 224 |
+
# Define workflow transitions
|
| 225 |
self.workflow.set_entry_point("ingest")
|
| 226 |
self.workflow.add_edge("ingest", "retrieve")
|
| 227 |
self.workflow.add_edge("retrieve", "analyze")
|
| 228 |
self.workflow.add_conditional_edges(
|
| 229 |
"analyze",
|
| 230 |
+
self._quality_check,
|
| 231 |
{"valid": "validate", "invalid": "refine"}
|
| 232 |
)
|
| 233 |
self.workflow.add_edge("validate", END)
|
| 234 |
self.workflow.add_edge("refine", "retrieve")
|
| 235 |
|
| 236 |
+
# Compile the final state machine
|
| 237 |
+
self.app = self.workflow.compile()
|
| 238 |
+
|
| 239 |
+
def ingest_query(self, state: AgentState) -> Dict:
|
| 240 |
+
"""Extract the user query and store it in the state."""
|
| 241 |
try:
|
| 242 |
+
query = state["messages"][-1].content
|
|
|
|
| 243 |
return {
|
| 244 |
+
"messages": [AIMessage(content="Query ingested successfully")],
|
| 245 |
+
"context": {"raw_query": query},
|
| 246 |
+
"metadata": {"timestamp": datetime.now().isoformat()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
}
|
| 248 |
except Exception as e:
|
| 249 |
+
return self._error_state(f"Ingestion Error: {str(e)}")
|
| 250 |
|
| 251 |
+
def retrieve_documents(self, state: AgentState) -> Dict:
|
| 252 |
+
"""Retrieve relevant documents from the 'research' domain."""
|
| 253 |
try:
|
| 254 |
+
# Fallback check for 'raw_query'
|
| 255 |
+
if "raw_query" not in state["context"]:
|
| 256 |
+
return self._error_state("No 'raw_query' found in context. Make sure the ingest step has run.")
|
| 257 |
+
|
| 258 |
+
query = state["context"]["raw_query"]
|
| 259 |
+
docs = retriever.retrieve(query, "research")
|
| 260 |
return {
|
| 261 |
"messages": [AIMessage(content=f"Retrieved {len(docs)} documents")],
|
| 262 |
"context": {
|
|
|
|
| 263 |
"documents": docs,
|
| 264 |
"retrieval_time": time.time()
|
| 265 |
+
}
|
|
|
|
| 266 |
}
|
| 267 |
except Exception as e:
|
| 268 |
+
return self._error_state(f"Retrieval Error: {str(e)}")
|
| 269 |
|
| 270 |
+
def analyze_content(self, state: AgentState) -> Dict:
|
| 271 |
+
"""Concatenate document contents and analyze them using the CognitiveProcessor."""
|
|
|
|
|
|
|
|
|
|
| 272 |
try:
|
| 273 |
+
if "documents" not in state["context"] or not state["context"]["documents"]:
|
| 274 |
+
return self._error_state("No documents retrieved; please check your query or retrieval process.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
+
docs = "\n\n".join([
|
| 277 |
+
d.page_content for d in state["context"]["documents"]
|
| 278 |
+
if hasattr(d, "page_content") and d.page_content
|
| 279 |
+
])
|
| 280 |
+
prompt = ResearchConfig.ANALYSIS_TEMPLATE.format(context=docs)
|
| 281 |
+
response = self.processor.process_query(prompt)
|
| 282 |
|
| 283 |
+
if "error" in response:
|
| 284 |
+
return self._error_state(response["error"])
|
| 285 |
|
| 286 |
return {
|
| 287 |
+
"messages": [AIMessage(content=response['choices'][0]['message']['content'])],
|
| 288 |
+
"context": {"analysis": response}
|
|
|
|
| 289 |
}
|
| 290 |
except Exception as e:
|
| 291 |
+
return self._error_state(f"Analysis Error: {str(e)}")
|
| 292 |
+
|
| 293 |
+
def validate_output(self, state: AgentState) -> Dict:
|
| 294 |
+
"""Validate the technical correctness of the analysis output."""
|
| 295 |
+
analysis = state["messages"][-1].content
|
| 296 |
+
validation_prompt = f"""Validate research analysis:
|
| 297 |
+
{analysis}
|
| 298 |
+
|
| 299 |
+
Check for:
|
| 300 |
+
1. Technical accuracy
|
| 301 |
+
2. Citation support
|
| 302 |
+
3. Logical consistency
|
| 303 |
+
4. Methodological soundness
|
| 304 |
+
|
| 305 |
+
Respond with 'VALID' or 'INVALID'"""
|
| 306 |
+
|
| 307 |
+
response = self.processor.process_query(validation_prompt)
|
| 308 |
+
return {
|
| 309 |
+
"messages": [AIMessage(content=analysis + f"\n\nValidation: {response.get('choices', [{}])[0].get('message', {}).get('content', '')}")]
|
| 310 |
+
}
|
| 311 |
|
| 312 |
+
def refine_results(self, state: AgentState) -> Dict:
|
| 313 |
+
"""Refine the analysis based on the validation feedback."""
|
| 314 |
+
refinement_prompt = f"""Refine this analysis:
|
| 315 |
+
{state["messages"][-1].content}
|
| 316 |
|
| 317 |
+
Improve:
|
| 318 |
+
1. Technical precision
|
| 319 |
+
2. Empirical grounding
|
| 320 |
+
3. Theoretical coherence"""
|
| 321 |
+
|
| 322 |
+
response = self.processor.process_query(refinement_prompt)
|
| 323 |
+
return {
|
| 324 |
+
"messages": [AIMessage(content=response.get('choices', [{}])[0].get('message', {}).get('content', ''))],
|
| 325 |
+
"context": state["context"]
|
| 326 |
+
}
|
| 327 |
|
| 328 |
+
def _quality_check(self, state: AgentState) -> str:
|
| 329 |
+
"""Check if the validation step indicates a 'VALID' or 'INVALID' output."""
|
| 330 |
+
content = state["messages"][-1].content
|
| 331 |
+
return "valid" if "VALID" in content else "invalid"
|
| 332 |
|
| 333 |
+
def _error_state(self, message: str) -> Dict:
|
| 334 |
+
"""Return an error message and mark the state as erroneous."""
|
| 335 |
+
st.error(f"[ERROR] {message}")
|
| 336 |
return {
|
| 337 |
+
"messages": [AIMessage(content=f"β {message}")],
|
| 338 |
+
"context": {"error": True},
|
| 339 |
+
"metadata": {"status": "error"}
|
|
|
|
|
|
|
|
|
|
| 340 |
}
|
| 341 |
|
| 342 |
+
# -----------------------------------------------------
|
| 343 |
+
# Research Interface
|
| 344 |
+
# -----------------------------------------------------
|
| 345 |
class ResearchInterface:
|
| 346 |
def __init__(self):
|
| 347 |
+
self.workflow = ResearchWorkflow()
|
| 348 |
+
# Page config already set at the top.
|
| 349 |
+
self._inject_styles()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
self._build_sidebar()
|
| 351 |
+
self._build_main_interface()
|
| 352 |
|
| 353 |
+
def _inject_styles(self):
|
| 354 |
+
"""Inject custom CSS for a sleek interface."""
|
| 355 |
st.markdown("""
|
| 356 |
<style>
|
| 357 |
+
:root {
|
| 358 |
+
--primary: #2ecc71;
|
| 359 |
+
--secondary: #3498db;
|
| 360 |
+
--background: #0a0a0a;
|
| 361 |
+
--text: #ecf0f1;
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
.stApp {
|
| 365 |
+
background: var(--background);
|
| 366 |
+
color: var(--text);
|
| 367 |
+
font-family: 'Roboto', sans-serif;
|
| 368 |
}
|
| 369 |
+
|
| 370 |
.stTextArea textarea {
|
| 371 |
+
background: #1a1a1a !important;
|
| 372 |
+
color: var(--text) !important;
|
| 373 |
+
border: 2px solid var(--secondary);
|
| 374 |
+
border-radius: 8px;
|
| 375 |
+
padding: 1rem;
|
| 376 |
}
|
| 377 |
+
|
| 378 |
.stButton>button {
|
| 379 |
+
background: linear-gradient(135deg, var(--primary), var(--secondary));
|
| 380 |
+
border: none;
|
| 381 |
+
border-radius: 8px;
|
| 382 |
+
padding: 1rem 2rem;
|
| 383 |
+
transition: all 0.3s;
|
| 384 |
}
|
| 385 |
+
|
| 386 |
+
.stButton>button:hover {
|
| 387 |
+
transform: translateY(-2px);
|
| 388 |
+
box-shadow: 0 4px 12px rgba(46, 204, 113, 0.3);
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
.stExpander {
|
| 392 |
+
background: #1a1a1a;
|
| 393 |
+
border: 1px solid #2a2a2a;
|
| 394 |
+
border-radius: 8px;
|
| 395 |
margin: 1rem 0;
|
| 396 |
}
|
| 397 |
</style>
|
| 398 |
""", unsafe_allow_html=True)
|
| 399 |
|
| 400 |
def _build_sidebar(self):
|
| 401 |
+
"""Construct the left sidebar with document info and metrics."""
|
| 402 |
with st.sidebar:
|
| 403 |
+
st.title("π Research Database")
|
| 404 |
+
st.subheader("Technical Papers")
|
| 405 |
+
for title, short in ResearchConfig.DOCUMENT_MAP.items():
|
| 406 |
+
with st.expander(short):
|
| 407 |
+
st.markdown(f"```\n{title}\n```")
|
| 408 |
+
|
| 409 |
+
st.subheader("Analysis Metrics")
|
| 410 |
+
st.metric("Vector Collections", 2)
|
| 411 |
+
st.metric("Embedding Dimensions", ResearchConfig.EMBEDDING_DIMENSIONS)
|
| 412 |
+
|
| 413 |
+
def _build_main_interface(self):
|
| 414 |
+
"""Construct the main interface for query input and result display."""
|
| 415 |
+
st.title("π§ NeuroResearch AI")
|
| 416 |
+
query = st.text_area("Research Query:", height=200,
|
| 417 |
+
placeholder="Enter technical research question...")
|
| 418 |
|
| 419 |
+
if st.button("Execute Analysis", type="primary"):
|
| 420 |
+
self._execute_analysis(query)
|
| 421 |
|
| 422 |
+
def _execute_analysis(self, query: str):
|
| 423 |
+
"""Execute the entire research workflow and render the results."""
|
| 424 |
try:
|
| 425 |
+
with st.spinner("Initializing Quantum Analysis..."):
|
| 426 |
+
results = self.workflow.app.stream(
|
| 427 |
+
{"messages": [HumanMessage(content=query)], "context": {}, "metadata": {}}
|
| 428 |
+
)
|
| 429 |
+
for event in results:
|
| 430 |
+
self._render_event(event)
|
| 431 |
+
st.success("β
Analysis Completed Successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
except Exception as e:
|
| 433 |
st.error(f"""**Analysis Failed**
|
| 434 |
+
{str(e)}
|
| 435 |
+
Potential issues:
|
| 436 |
+
- Complex query structure
|
| 437 |
+
- Document correlation failure
|
| 438 |
+
- Temporal processing constraints""")
|
| 439 |
+
|
| 440 |
+
def _render_event(self, event: Dict):
|
| 441 |
+
"""Render each node's output in the UI as it streams through the workflow."""
|
| 442 |
+
if 'ingest' in event:
|
| 443 |
+
with st.container():
|
| 444 |
+
st.success("β
Query Ingested")
|
| 445 |
+
elif 'retrieve' in event:
|
| 446 |
+
with st.container():
|
| 447 |
+
docs = event['retrieve']['context']['documents']
|
| 448 |
+
st.info(f"π Retrieved {len(docs)} documents")
|
| 449 |
+
with st.expander("View Retrieved Documents", expanded=False):
|
| 450 |
+
for i, doc in enumerate(docs, 1):
|
| 451 |
+
st.markdown(f"**Document {i}**")
|
| 452 |
+
st.code(doc.page_content, language='text')
|
| 453 |
+
elif 'analyze' in event:
|
| 454 |
+
with st.container():
|
| 455 |
+
content = event['analyze']['messages'][0].content
|
| 456 |
+
with st.expander("Technical Analysis Report", expanded=True):
|
| 457 |
+
st.markdown(content)
|
| 458 |
+
elif 'validate' in event:
|
| 459 |
+
with st.container():
|
| 460 |
+
content = event['validate']['messages'][0].content
|
| 461 |
+
if "VALID" in content:
|
| 462 |
+
st.success("β
Validation Passed")
|
| 463 |
+
with st.expander("View Validated Analysis", expanded=True):
|
| 464 |
+
st.markdown(content.split("Validation:")[0])
|
| 465 |
else:
|
| 466 |
+
st.warning("β οΈ Validation Issues Detected")
|
| 467 |
+
with st.expander("View Validation Details", expanded=True):
|
| 468 |
+
st.markdown(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
+
# -----------------------------------------------------
|
| 471 |
+
# Main Execution
|
| 472 |
+
# -----------------------------------------------------
|
| 473 |
if __name__ == "__main__":
|
| 474 |
+
ResearchInterface()
|