Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ------------------------------
|
| 2 |
-
# Imports
|
| 3 |
# ------------------------------
|
| 4 |
from langchain_openai import OpenAIEmbeddings
|
| 5 |
from langchain_community.vectorstores import Chroma
|
|
@@ -9,306 +9,454 @@ from langgraph.graph import END, StateGraph
|
|
| 9 |
from langgraph.prebuilt import ToolNode
|
| 10 |
from langgraph.graph.message import add_messages
|
| 11 |
from typing_extensions import TypedDict, Annotated
|
| 12 |
-
from typing import Sequence
|
| 13 |
import chromadb
|
| 14 |
import re
|
| 15 |
import os
|
| 16 |
import streamlit as st
|
| 17 |
import requests
|
| 18 |
-
import time
|
| 19 |
-
import hashlib
|
| 20 |
from langchain.tools.retriever import create_retriever_tool
|
| 21 |
-
from datetime import datetime
|
| 22 |
|
| 23 |
# ------------------------------
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# ------------------------------
|
| 26 |
research_texts = [
|
| 27 |
"Research Report: Results of a New AI Model Improving Image Recognition Accuracy to 98%",
|
| 28 |
"Academic Paper Summary: Why Transformers Became the Mainstream Architecture in Natural Language Processing",
|
| 29 |
-
"Latest Trends in Machine Learning Methods Using Quantum Computing"
|
| 30 |
-
"Advancements in Neuromorphic Computing for Energy-Efficient AI Systems",
|
| 31 |
-
"Cross-Modal Learning: Integrating Visual and Textual Representations for Multimodal AI"
|
| 32 |
]
|
| 33 |
|
| 34 |
development_texts = [
|
| 35 |
"Project A: UI Design Completed, API Integration in Progress",
|
| 36 |
"Project B: Testing New Feature X, Bug Fixes Needed",
|
| 37 |
-
"Product Y: In the Performance Optimization Stage Before Release"
|
| 38 |
-
"Framework Z: Version 3.2 Released with Enhanced Distributed Training Support",
|
| 39 |
-
"DevOps Pipeline: Automated CI/CD Implementation for ML Model Deployment"
|
| 40 |
]
|
| 41 |
|
| 42 |
# ------------------------------
|
| 43 |
-
#
|
| 44 |
# ------------------------------
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
self.CHROMA_PATH = "chroma_db"
|
| 49 |
-
self.MAX_RETRIES = 3
|
| 50 |
-
self.RETRY_DELAY = 1.5
|
| 51 |
-
self.DOCUMENT_CHUNK_SIZE = 300
|
| 52 |
-
self.DOCUMENT_OVERLAP = 50
|
| 53 |
-
self.SEARCH_K = 5
|
| 54 |
-
self.SEARCH_TYPE = "mmr"
|
| 55 |
-
|
| 56 |
-
def validate(self):
|
| 57 |
-
if not self.DEEPSEEK_API_KEY:
|
| 58 |
-
st.error("""
|
| 59 |
-
**Configuration Error**
|
| 60 |
-
π Missing DeepSeek API key.
|
| 61 |
-
Configure through Hugging Face Space secrets:
|
| 62 |
-
1. Space Settings β Repository secrets
|
| 63 |
-
2. Add secret: DEEPSEEK_API_KEY=your_key
|
| 64 |
-
3. Rebuild Space
|
| 65 |
-
""")
|
| 66 |
-
st.stop()
|
| 67 |
|
| 68 |
# ------------------------------
|
| 69 |
-
#
|
| 70 |
# ------------------------------
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
self.embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
|
| 76 |
-
|
| 77 |
-
self.research_collection = self._create_collection(
|
| 78 |
-
research_texts,
|
| 79 |
-
"research_collection",
|
| 80 |
-
{"category": "research"}
|
| 81 |
-
)
|
| 82 |
-
self.dev_collection = self._create_collection(
|
| 83 |
-
development_texts,
|
| 84 |
-
"development_collection",
|
| 85 |
-
{"category": "development"}
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# ------------------------------
|
| 104 |
-
#
|
| 105 |
# ------------------------------
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
"nlp": ["transformer", "language"]
|
| 120 |
-
}
|
| 121 |
-
return "\n".join(sorted({
|
| 122 |
-
"- " + {
|
| 123 |
-
"quantum": "Quantum computing breakthroughs",
|
| 124 |
-
"vision": "Computer vision advancements",
|
| 125 |
-
"nlp": "NLP architecture innovations"
|
| 126 |
-
}[cat]
|
| 127 |
-
for doc in docs
|
| 128 |
-
for cat, kw in categories.items()
|
| 129 |
-
if any(k in doc.page_content.lower() for k in kw)
|
| 130 |
-
}))
|
| 131 |
|
| 132 |
# ------------------------------
|
| 133 |
-
# Workflow
|
| 134 |
# ------------------------------
|
| 135 |
class AgentState(TypedDict):
|
| 136 |
messages: Annotated[Sequence[AIMessage | HumanMessage | ToolMessage], add_messages]
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
| 174 |
)
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
response = requests.post(
|
| 186 |
-
"https://api.deepseek.com/v1/chat/completions",
|
| 187 |
-
headers={"Authorization": f"Bearer {config.DEEPSEEK_API_KEY}"},
|
| 188 |
-
json={
|
| 189 |
-
"model": "deepseek-chat",
|
| 190 |
-
"messages": [{
|
| 191 |
-
"role": "user",
|
| 192 |
-
"content": f"""Analyze this query: "{query}"
|
| 193 |
-
Respond EXACTLY as:
|
| 194 |
-
- SEARCH_RESEARCH: <terms> (for research topics)
|
| 195 |
-
- SEARCH_DEV: <terms> (for development updates)
|
| 196 |
-
- DIRECT: <answer> (otherwise)"""
|
| 197 |
-
}]
|
| 198 |
-
}
|
| 199 |
-
).json()
|
| 200 |
-
|
| 201 |
-
content = response['choices'][0]['message']['content']
|
| 202 |
-
if "SEARCH_RESEARCH:" in content:
|
| 203 |
-
terms = content.split("SEARCH_RESEARCH:")[1].strip()
|
| 204 |
-
results = self.chroma.research_collection.similarity_search(terms)
|
| 205 |
-
return {"messages": [AIMessage(content=f"Research Results: {str(results)}")]}
|
| 206 |
-
elif "SEARCH_DEV:" in content:
|
| 207 |
-
terms = content.split("SEARCH_DEV:")[1].strip()
|
| 208 |
-
results = self.chroma.dev_collection.similarity_search(terms)
|
| 209 |
-
return {"messages": [AIMessage(content=f"Development Results: {str(results)}")]}
|
| 210 |
-
return {"messages": [AIMessage(content=content)]}
|
| 211 |
-
|
| 212 |
-
except Exception as e:
|
| 213 |
-
return {"messages": [AIMessage(content=f"Error: {str(e)}")]}
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
response = requests.post(
|
| 220 |
"https://api.deepseek.com/v1/chat/completions",
|
| 221 |
-
headers=
|
| 222 |
-
json=
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
}]
|
| 228 |
-
}
|
| 229 |
-
).json()
|
| 230 |
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
response = requests.post(
|
| 236 |
"https://api.deepseek.com/v1/chat/completions",
|
| 237 |
-
headers=
|
| 238 |
-
json=
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
return {"messages": [AIMessage(content=
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
return "
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
# ------------------------------
|
| 255 |
-
#
|
| 256 |
# ------------------------------
|
| 257 |
-
|
| 258 |
-
st.markdown("""
|
| 259 |
-
<style>
|
| 260 |
-
.stApp { background: #1a1a1a; color: white; }
|
| 261 |
-
.stTextArea textarea { background: #2d2d2d !important; color: white !important; }
|
| 262 |
-
.stButton>button { background: #2E86C1; transition: 0.3s; }
|
| 263 |
-
.stButton>button:hover { background: #1B4F72; transform: scale(1.02); }
|
| 264 |
-
.data-box { background: #2d2d2d; border-left: 4px solid #2E86C1; padding: 15px; margin: 10px 0; }
|
| 265 |
-
</style>
|
| 266 |
-
""", unsafe_allow_html=True)
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
# ------------------------------
|
| 299 |
-
#
|
| 300 |
# ------------------------------
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
st.set_page_config(
|
| 303 |
-
page_title="AI Research Assistant",
|
| 304 |
layout="wide",
|
| 305 |
initial_sidebar_state="expanded"
|
| 306 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ------------------------------
|
| 2 |
+
# Imports & Dependencies
|
| 3 |
# ------------------------------
|
| 4 |
from langchain_openai import OpenAIEmbeddings
|
| 5 |
from langchain_community.vectorstores import Chroma
|
|
|
|
| 9 |
from langgraph.prebuilt import ToolNode
|
| 10 |
from langgraph.graph.message import add_messages
|
| 11 |
from typing_extensions import TypedDict, Annotated
|
| 12 |
+
from typing import Sequence
|
| 13 |
import chromadb
|
| 14 |
import re
|
| 15 |
import os
|
| 16 |
import streamlit as st
|
| 17 |
import requests
|
|
|
|
|
|
|
| 18 |
from langchain.tools.retriever import create_retriever_tool
|
|
|
|
| 19 |
|
| 20 |
# ------------------------------
|
| 21 |
+
# Configuration
|
| 22 |
+
# ------------------------------
|
| 23 |
+
# Get DeepSeek API key from Hugging Face Space secrets
|
| 24 |
+
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
|
| 25 |
+
|
| 26 |
+
if not DEEPSEEK_API_KEY:
|
| 27 |
+
st.error("""
|
| 28 |
+
**Missing API Configuration**
|
| 29 |
+
Please configure your DeepSeek API key in Hugging Face Space secrets:
|
| 30 |
+
1. Go to your Space's Settings
|
| 31 |
+
2. Click on 'Repository secrets'
|
| 32 |
+
3. Add a secret named DEEPSEEK_API_KEY
|
| 33 |
+
""")
|
| 34 |
+
st.stop()
|
| 35 |
+
|
| 36 |
+
# Create directory for Chroma persistence
|
| 37 |
+
os.makedirs("chroma_db", exist_ok=True)
|
| 38 |
+
|
| 39 |
+
# ------------------------------
|
| 40 |
+
# ChromaDB Client Configuration
|
| 41 |
+
# ------------------------------
|
| 42 |
+
chroma_client = chromadb.PersistentClient(path="chroma_db")
|
| 43 |
+
|
| 44 |
+
# ------------------------------
|
| 45 |
+
# Dummy Data: Research & Development Texts
|
| 46 |
# ------------------------------
|
| 47 |
research_texts = [
|
| 48 |
"Research Report: Results of a New AI Model Improving Image Recognition Accuracy to 98%",
|
| 49 |
"Academic Paper Summary: Why Transformers Became the Mainstream Architecture in Natural Language Processing",
|
| 50 |
+
"Latest Trends in Machine Learning Methods Using Quantum Computing"
|
|
|
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
development_texts = [
|
| 54 |
"Project A: UI Design Completed, API Integration in Progress",
|
| 55 |
"Project B: Testing New Feature X, Bug Fixes Needed",
|
| 56 |
+
"Product Y: In the Performance Optimization Stage Before Release"
|
|
|
|
|
|
|
| 57 |
]
|
| 58 |
|
| 59 |
# ------------------------------
|
| 60 |
+
# Text Splitting & Document Creation
|
| 61 |
# ------------------------------
|
| 62 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10)
|
| 63 |
+
research_docs = splitter.create_documents(research_texts)
|
| 64 |
+
development_docs = splitter.create_documents(development_texts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# ------------------------------
|
| 67 |
+
# Creating Vector Stores with Embeddings
|
| 68 |
# ------------------------------
|
| 69 |
+
embeddings = OpenAIEmbeddings(
|
| 70 |
+
model="text-embedding-3-large",
|
| 71 |
+
# dimensions=1024 # Uncomment if needed
|
| 72 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
research_vectorstore = Chroma.from_documents(
|
| 75 |
+
documents=research_docs,
|
| 76 |
+
embedding=embeddings,
|
| 77 |
+
client=chroma_client,
|
| 78 |
+
collection_name="research_collection"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
development_vectorstore = Chroma.from_documents(
|
| 82 |
+
documents=development_docs,
|
| 83 |
+
embedding=embeddings,
|
| 84 |
+
client=chroma_client,
|
| 85 |
+
collection_name="development_collection"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
research_retriever = research_vectorstore.as_retriever()
|
| 89 |
+
development_retriever = development_vectorstore.as_retriever()
|
| 90 |
|
| 91 |
# ------------------------------
|
| 92 |
+
# Creating Retriever Tools
|
| 93 |
# ------------------------------
|
| 94 |
+
research_tool = create_retriever_tool(
|
| 95 |
+
research_retriever,
|
| 96 |
+
"research_db_tool",
|
| 97 |
+
"Search information from the research database."
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
development_tool = create_retriever_tool(
|
| 101 |
+
development_retriever,
|
| 102 |
+
"development_db_tool",
|
| 103 |
+
"Search information from the development database."
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
tools = [research_tool, development_tool]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# ------------------------------
|
| 109 |
+
# Agent Function & Workflow Functions
|
| 110 |
# ------------------------------
|
| 111 |
class AgentState(TypedDict):
|
| 112 |
messages: Annotated[Sequence[AIMessage | HumanMessage | ToolMessage], add_messages]
|
| 113 |
|
| 114 |
+
def agent(state: AgentState):
|
| 115 |
+
print("---CALL AGENT---")
|
| 116 |
+
messages = state["messages"]
|
| 117 |
+
|
| 118 |
+
if isinstance(messages[0], tuple):
|
| 119 |
+
user_message = messages[0][1]
|
| 120 |
+
else:
|
| 121 |
+
user_message = messages[0].content
|
| 122 |
+
|
| 123 |
+
prompt = f"""Given this user question: "{user_message}"
|
| 124 |
+
If it's about research or academic topics, respond EXACTLY in this format:
|
| 125 |
+
SEARCH_RESEARCH: <search terms>
|
| 126 |
+
|
| 127 |
+
If it's about development status, respond EXACTLY in this format:
|
| 128 |
+
SEARCH_DEV: <search terms>
|
| 129 |
+
|
| 130 |
+
Otherwise, just answer directly.
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
headers = {
|
| 134 |
+
"Accept": "application/json",
|
| 135 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 136 |
+
"Content-Type": "application/json"
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
data = {
|
| 140 |
+
"model": "deepseek-chat",
|
| 141 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 142 |
+
"temperature": 0.7,
|
| 143 |
+
"max_tokens": 1024
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
response = requests.post(
|
| 148 |
+
"https://api.deepseek.com/v1/chat/completions",
|
| 149 |
+
headers=headers,
|
| 150 |
+
json=data,
|
| 151 |
+
verify=False,
|
| 152 |
+
timeout=30
|
| 153 |
)
|
| 154 |
+
response.raise_for_status()
|
| 155 |
+
|
| 156 |
+
response_text = response.json()['choices'][0]['message']['content']
|
| 157 |
+
print("Raw response:", response_text)
|
| 158 |
|
| 159 |
+
if "SEARCH_RESEARCH:" in response_text:
|
| 160 |
+
query = response_text.split("SEARCH_RESEARCH:")[1].strip()
|
| 161 |
+
results = research_retriever.invoke(query)
|
| 162 |
+
return {"messages": [AIMessage(content=f'Action: research_db_tool\n{{"query": "{query}"}}\n\nResults: {str(results)}')]}
|
| 163 |
|
| 164 |
+
elif "SEARCH_DEV:" in response_text:
|
| 165 |
+
query = response_text.split("SEARCH_DEV:")[1].strip()
|
| 166 |
+
results = development_retriever.invoke(query)
|
| 167 |
+
return {"messages": [AIMessage(content=f'Action: development_db_tool\n{{"query": "{query}"}}\n\nResults: {str(results)}')]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
else:
|
| 170 |
+
return {"messages": [AIMessage(content=response_text)]}
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
error_msg = f"API Error: {str(e)}"
|
| 174 |
+
if "Insufficient Balance" in str(e):
|
| 175 |
+
error_msg += "\n\nPlease check your DeepSeek API account balance."
|
| 176 |
+
return {"messages": [AIMessage(content=error_msg)]}
|
| 177 |
+
|
| 178 |
+
def simple_grade_documents(state: AgentState):
|
| 179 |
+
messages = state["messages"]
|
| 180 |
+
last_message = messages[-1]
|
| 181 |
+
print("Evaluating message:", last_message.content)
|
| 182 |
+
|
| 183 |
+
if "Results: [Document" in last_message.content:
|
| 184 |
+
print("---DOCS FOUND, GO TO GENERATE---")
|
| 185 |
+
return "generate"
|
| 186 |
+
else:
|
| 187 |
+
print("---NO DOCS FOUND, TRY REWRITE---")
|
| 188 |
+
return "rewrite"
|
| 189 |
+
|
| 190 |
+
def generate(state: AgentState):
|
| 191 |
+
print("---GENERATE FINAL ANSWER---")
|
| 192 |
+
messages = state["messages"]
|
| 193 |
+
question = messages[0].content if isinstance(messages[0], tuple) else messages[0].content
|
| 194 |
+
last_message = messages[-1]
|
| 195 |
+
|
| 196 |
+
docs = ""
|
| 197 |
+
if "Results: [" in last_message.content:
|
| 198 |
+
results_start = last_message.content.find("Results: [")
|
| 199 |
+
docs = last_message.content[results_start:]
|
| 200 |
+
print("Documents found:", docs)
|
| 201 |
+
|
| 202 |
+
headers = {
|
| 203 |
+
"Accept": "application/json",
|
| 204 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 205 |
+
"Content-Type": "application/json"
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
prompt = f"""Based on these research documents, summarize the latest advancements in AI:
|
| 209 |
+
Question: {question}
|
| 210 |
+
Documents: {docs}
|
| 211 |
+
Focus on extracting and synthesizing the key findings from the research papers.
|
| 212 |
+
"""
|
| 213 |
+
|
| 214 |
+
data = {
|
| 215 |
+
"model": "deepseek-chat",
|
| 216 |
+
"messages": [{
|
| 217 |
+
"role": "user",
|
| 218 |
+
"content": prompt
|
| 219 |
+
}],
|
| 220 |
+
"temperature": 0.7,
|
| 221 |
+
"max_tokens": 1024
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
try:
|
| 225 |
+
print("Sending generate request to API...")
|
| 226 |
response = requests.post(
|
| 227 |
"https://api.deepseek.com/v1/chat/completions",
|
| 228 |
+
headers=headers,
|
| 229 |
+
json=data,
|
| 230 |
+
verify=False,
|
| 231 |
+
timeout=30
|
| 232 |
+
)
|
| 233 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
response_text = response.json()['choices'][0]['message']['content']
|
| 236 |
+
print("Final Answer:", response_text)
|
| 237 |
+
return {"messages": [AIMessage(content=response_text)]}
|
| 238 |
+
except Exception as e:
|
| 239 |
+
error_msg = f"Generation Error: {str(e)}"
|
| 240 |
+
return {"messages": [AIMessage(content=error_msg)]}
|
| 241 |
+
|
| 242 |
+
def rewrite(state: AgentState):
|
| 243 |
+
print("---REWRITE QUESTION---")
|
| 244 |
+
messages = state["messages"]
|
| 245 |
+
original_question = messages[0].content if len(messages) > 0 else "N/A"
|
| 246 |
|
| 247 |
+
headers = {
|
| 248 |
+
"Accept": "application/json",
|
| 249 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 250 |
+
"Content-Type": "application/json"
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
data = {
|
| 254 |
+
"model": "deepseek-chat",
|
| 255 |
+
"messages": [{
|
| 256 |
+
"role": "user",
|
| 257 |
+
"content": f"Rewrite this question to be more specific and clearer: {original_question}"
|
| 258 |
+
}],
|
| 259 |
+
"temperature": 0.7,
|
| 260 |
+
"max_tokens": 1024
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
try:
|
| 264 |
+
print("Sending rewrite request...")
|
| 265 |
response = requests.post(
|
| 266 |
"https://api.deepseek.com/v1/chat/completions",
|
| 267 |
+
headers=headers,
|
| 268 |
+
json=data,
|
| 269 |
+
verify=False,
|
| 270 |
+
timeout=30
|
| 271 |
+
)
|
| 272 |
+
response.raise_for_status()
|
| 273 |
+
|
| 274 |
+
response_text = response.json()['choices'][0]['message']['content']
|
| 275 |
+
print("Rewritten question:", response_text)
|
| 276 |
+
return {"messages": [AIMessage(content=response_text)]}
|
| 277 |
+
except Exception as e:
|
| 278 |
+
error_msg = f"Rewrite Error: {str(e)}"
|
| 279 |
+
return {"messages": [AIMessage(content=error_msg)]}
|
| 280 |
+
|
| 281 |
+
tools_pattern = re.compile(r"Action: .*")
|
| 282 |
+
|
| 283 |
+
def custom_tools_condition(state: AgentState):
|
| 284 |
+
messages = state["messages"]
|
| 285 |
+
last_message = messages[-1]
|
| 286 |
+
content = last_message.content
|
| 287 |
+
|
| 288 |
+
print("Checking tools condition:", content)
|
| 289 |
+
if tools_pattern.match(content):
|
| 290 |
+
print("Moving to retrieve...")
|
| 291 |
+
return "tools"
|
| 292 |
+
print("Moving to END...")
|
| 293 |
+
return END
|
| 294 |
|
| 295 |
# ------------------------------
|
| 296 |
+
# Workflow Configuration using LangGraph
|
| 297 |
# ------------------------------
|
| 298 |
+
workflow = StateGraph(AgentState)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
+
# Add nodes
|
| 301 |
+
workflow.add_node("agent", agent)
|
| 302 |
+
retrieve_node = ToolNode(tools)
|
| 303 |
+
workflow.add_node("retrieve", retrieve_node)
|
| 304 |
+
workflow.add_node("rewrite", rewrite)
|
| 305 |
+
workflow.add_node("generate", generate)
|
| 306 |
+
|
| 307 |
+
# Set entry point
|
| 308 |
+
workflow.set_entry_point("agent")
|
| 309 |
+
|
| 310 |
+
# Define transitions
|
| 311 |
+
workflow.add_conditional_edges(
|
| 312 |
+
"agent",
|
| 313 |
+
custom_tools_condition,
|
| 314 |
+
{
|
| 315 |
+
"tools": "retrieve",
|
| 316 |
+
END: END
|
| 317 |
+
}
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
workflow.add_conditional_edges(
|
| 321 |
+
"retrieve",
|
| 322 |
+
simple_grade_documents,
|
| 323 |
+
{
|
| 324 |
+
"generate": "generate",
|
| 325 |
+
"rewrite": "rewrite"
|
| 326 |
+
}
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
workflow.add_edge("generate", END)
|
| 330 |
+
workflow.add_edge("rewrite", "agent")
|
| 331 |
+
|
| 332 |
+
# Compile the workflow
|
| 333 |
+
app = workflow.compile()
|
| 334 |
|
| 335 |
# ------------------------------
|
| 336 |
+
# Processing Function
|
| 337 |
# ------------------------------
|
| 338 |
+
def process_question(user_question, app, config):
|
| 339 |
+
"""Process user question through the workflow"""
|
| 340 |
+
events = []
|
| 341 |
+
for event in app.stream({"messages": [("user", user_question)]}, config):
|
| 342 |
+
events.append(event)
|
| 343 |
+
return events
|
| 344 |
+
|
| 345 |
+
# ------------------------------
|
| 346 |
+
# Streamlit App UI (Dark Theme)
|
| 347 |
+
# ------------------------------
|
| 348 |
+
def main():
|
| 349 |
st.set_page_config(
|
| 350 |
+
page_title="AI Research & Development Assistant",
|
| 351 |
layout="wide",
|
| 352 |
initial_sidebar_state="expanded"
|
| 353 |
)
|
| 354 |
+
|
| 355 |
+
st.markdown("""
|
| 356 |
+
<style>
|
| 357 |
+
.stApp {
|
| 358 |
+
background-color: #1a1a1a;
|
| 359 |
+
color: #ffffff;
|
| 360 |
+
}
|
| 361 |
|
| 362 |
+
.stTextArea textarea {
|
| 363 |
+
background-color: #2d2d2d !important;
|
| 364 |
+
color: #ffffff !important;
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
.stButton > button {
|
| 368 |
+
background-color: #4CAF50;
|
| 369 |
+
color: white;
|
| 370 |
+
transition: all 0.3s;
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
.stButton > button:hover {
|
| 374 |
+
background-color: #45a049;
|
| 375 |
+
transform: scale(1.02);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
.data-box {
|
| 379 |
+
background-color: #2d2d2d;
|
| 380 |
+
border-left: 5px solid #2196F3;
|
| 381 |
+
}
|
| 382 |
+
|
| 383 |
+
.dev-box {
|
| 384 |
+
border-left: 5px solid #4CAF50;
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
.st-expander {
|
| 388 |
+
background-color: #2d2d2d;
|
| 389 |
+
border: 1px solid #3d3d3d;
|
| 390 |
+
}
|
| 391 |
+
</style>
|
| 392 |
+
""", unsafe_allow_html=True)
|
| 393 |
+
|
| 394 |
+
with st.sidebar:
|
| 395 |
+
st.header("π Available Data")
|
| 396 |
+
st.subheader("Research Database")
|
| 397 |
+
for text in research_texts:
|
| 398 |
+
st.markdown(f'<div class="data-box research-box" style="padding: 15px; margin: 10px 0; border-radius: 5px;">{text}</div>', unsafe_allow_html=True)
|
| 399 |
+
|
| 400 |
+
st.subheader("Development Database")
|
| 401 |
+
for text in development_texts:
|
| 402 |
+
st.markdown(f'<div class="data-box dev-box" style="padding: 15px; margin: 10px 0; border-radius: 5px;">{text}</div>', unsafe_allow_html=True)
|
| 403 |
+
|
| 404 |
+
st.title("π€ AI Research & Development Assistant")
|
| 405 |
+
st.markdown("---")
|
| 406 |
+
|
| 407 |
+
query = st.text_area("Enter your question:", height=100, placeholder="e.g., What is the latest advancement in AI research?")
|
| 408 |
+
|
| 409 |
+
col1, col2 = st.columns([1, 2])
|
| 410 |
+
with col1:
|
| 411 |
+
if st.button("π Get Answer", use_container_width=True):
|
| 412 |
+
if query:
|
| 413 |
+
try:
|
| 414 |
+
with st.spinner('Processing your question...'):
|
| 415 |
+
events = process_question(query, app, {"configurable": {"thread_id": "1"}})
|
| 416 |
+
|
| 417 |
+
for event in events:
|
| 418 |
+
if 'agent' in event:
|
| 419 |
+
with st.expander("π Processing Step", expanded=True):
|
| 420 |
+
content = event['agent']['messages'][0].content
|
| 421 |
+
if "Error" in content:
|
| 422 |
+
st.error(content)
|
| 423 |
+
elif "Results:" in content:
|
| 424 |
+
st.markdown("### π Retrieved Documents:")
|
| 425 |
+
docs_start = content.find("Results:")
|
| 426 |
+
docs = content[docs_start:]
|
| 427 |
+
st.info(docs)
|
| 428 |
+
elif 'generate' in event:
|
| 429 |
+
content = event['generate']['messages'][0].content
|
| 430 |
+
if "Error" in content:
|
| 431 |
+
st.error(content)
|
| 432 |
+
else:
|
| 433 |
+
st.markdown("### β¨ Final Answer:")
|
| 434 |
+
st.success(content)
|
| 435 |
+
except Exception as e:
|
| 436 |
+
st.error(f"""
|
| 437 |
+
**Processing Error**
|
| 438 |
+
{str(e)}
|
| 439 |
+
Please check:
|
| 440 |
+
- API key configuration
|
| 441 |
+
- Account balance
|
| 442 |
+
- Network connection
|
| 443 |
+
""")
|
| 444 |
+
else:
|
| 445 |
+
st.warning("β οΈ Please enter a question first!")
|
| 446 |
+
|
| 447 |
+
with col2:
|
| 448 |
+
st.markdown("""
|
| 449 |
+
### π― How to Use
|
| 450 |
+
1. Enter your question in the text box
|
| 451 |
+
2. Click the search button
|
| 452 |
+
3. Review processing steps
|
| 453 |
+
4. See final answer
|
| 454 |
+
|
| 455 |
+
### π‘ Example Questions
|
| 456 |
+
- What's new in AI image recognition?
|
| 457 |
+
- How is Project B progressing?
|
| 458 |
+
- Recent machine learning trends?
|
| 459 |
+
""")
|
| 460 |
+
|
| 461 |
+
if __name__ == "__main__":
|
| 462 |
+
main()
|