Spaces:
Running
Running
File size: 10,328 Bytes
0f83924 bd23f77 0f83924 06ee039 bd23f77 06ee039 0f83924 bd23f77 06ee039 0f83924 8588a31 b68b7bd bd23f77 0f83924 bd23f77 1e0350f b26cbe4 bd23f77 b26cbe4 bd23f77 b7719bf 06ee039 bd23f77 06ee039 bd23f77 a1bb249 a2dbafb bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 0f83924 bd23f77 90dcb0c bd23f77 a2dbafb bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 0f83924 bd23f77 a2dbafb bd23f77 5e58a2d bd23f77 90dcb0c bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 0f83924 bd23f77 0f83924 bd23f77 a2dbafb bd23f77 a2dbafb bd23f77 b7719bf a1bb249 bd23f77 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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 |
"""
AI Research Assistant
"""
# ------------------------------
# Core Imports & Configuration
# ------------------------------
import os
import re
import time
import chromadb
import requests
import streamlit as st
from typing import Sequence, Tuple
from typing_extensions import TypedDict, Annotated
from langchain_core.messages import HumanMessage, AIMessage
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain.tools.retriever import create_retriever_tool
from langgraph.graph import END, StateGraph
from langgraph.prebuilt import ToolNode
from langgraph.graph.message import add_messages
from chromadb.config import Settings
from langchain_openai import OpenAIEmbeddings
# ------------------------------
# Type Definitions
# ------------------------------
class AgentState(TypedDict):
messages: Annotated[Sequence[AIMessage | HumanMessage], add_messages]
# ------------------------------
# Configuration & Constants
# ------------------------------
class Config:
API_KEY = os.environ.get("DEEPSEEK_API_KEY")
CHROMA_PATH = "chroma_db"
TEXT_SPLITTER_CONFIG = {
"chunk_size": 512,
"chunk_overlap": 128,
"separators": ["\n\n", "\n", ". ", "! ", "? "]
}
# ------------------------------
# Core System Components
# ------------------------------
class ResearchAssistant:
def __init__(self):
self.embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
self.vector_stores = self._init_vector_stores()
self.tools = self._create_tools()
self.workflow = self._build_workflow()
def _init_vector_stores(self) -> Tuple[Chroma, Chroma]:
"""Initialize vector stores with proper document processing"""
splitter = RecursiveCharacterTextSplitter(**Config.TEXT_SPLITTER_CONFIG)
research_docs = splitter.create_documents([
"Research Report: New AI Model Achieves 98% Image Recognition Accuracy",
"Transformers: The New NLP Architecture Standard",
"Quantum Machine Learning: Emerging Trends and Applications"
])
development_docs = splitter.create_documents([
"Project A: UI Design Finalized, API Integration Phase",
"Project B: Feature Testing and Bug Fixes",
"Product Y: Performance Optimization Pre-Release"
])
client = chromadb.PersistentClient(
path=Config.CHROMA_PATH,
settings=Settings(anonymized_telemetry=False)
return (
Chroma.from_documents(research_docs, self.embeddings,
client=client, collection_name="research"),
Chroma.from_documents(development_docs, self.embeddings,
client=client, collection_name="development")
)
def _create_tools(self):
"""Create retrieval tools with optimized search parameters"""
research_retriever = self.vector_stores[0].as_retriever(
search_kwargs={"k": 3, "score_threshold": 0.7}
)
development_retriever = self.vector_stores[1].as_retriever(
search_kwargs={"k": 3, "score_threshold": 0.7}
)
return [
create_retriever_tool(
research_retriever,
"research_db",
"Access technical research papers and reports"
),
create_retriever_tool(
development_retriever,
"development_db",
"Retrieve project development status updates"
)
]
def _build_workflow(self):
"""Construct and return the processing workflow"""
workflow = StateGraph(AgentState)
workflow.add_node("analyze", self.analyze_query)
workflow.add_node("retrieve", ToolNode(self.tools))
workflow.add_node("synthesize", self.synthesize_response)
workflow.set_entry_point("analyze")
workflow.add_conditional_edges(
"analyze",
self._needs_retrieval,
{"retrieve": "retrieve", "direct": "synthesize"}
)
workflow.add_edge("retrieve", "synthesize")
workflow.add_edge("synthesize", END)
return workflow.compile()
def _needs_retrieval(self, state: AgentState) -> str:
"""Determine if document retrieval is needed"""
query = state["messages"][-1].content.lower()
return "retrieve" if any(kw in query for kw in {
"research", "study", "project", "develop", "trend"
}) else "direct"
def analyze_query(self, state: AgentState):
"""Analyze user query and determine next steps"""
try:
user_input = state["messages"][-1].content
headers = {
"Authorization": f"Bearer {Config.API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(
"https://api.deepseek.com/v1/chat/completions",
headers=headers,
json={
"model": "deepseek-chat",
"messages": [{
"role": "user",
"content": f"""Analyze this query and format as:
CATEGORY: [RESEARCH|DEVELOPMENT|GENERAL]
KEY_TERMS: comma-separated list
{user_input}"""
}],
"temperature": 0.3
},
timeout=15
)
response.raise_for_status()
analysis = response.json()["choices"][0]["message"]["content"]
return {"messages": [AIMessage(content=analysis)]}
except Exception as e:
return {"messages": [AIMessage(
content=f"Analysis Error: {str(e)}. Please rephrase your question."
)]}
def synthesize_response(self, state: AgentState):
"""Generate final response with citations"""
try:
context = "\n".join([msg.content for msg in state["messages"]])
headers = {
"Authorization": f"Bearer {Config.API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(
"https://api.deepseek.com/v1/chat/completions",
headers=headers,
json={
"model": "deepseek-chat",
"messages": [{
"role": "user",
"content": f"""Synthesize this information:
{context}
Include:
1. Key findings
2. Supporting evidence
3. Technical details
4. Potential applications"""
}],
"temperature": 0.5
},
timeout=20
)
response.raise_for_status()
return {"messages": [AIMessage(
content=response.json()["choices"][0]["message"]["content"]
)]}
except Exception as e:
return {"messages": [AIMessage(
content=f"Synthesis Error: {str(e)}. Please try again later."
)]}
# ------------------------------
# Professional UI Interface
# ------------------------------
def main():
st.set_page_config(
page_title="Research Assistant Pro",
layout="wide",
initial_sidebar_state="expanded"
)
# Dark theme implementation
st.markdown("""
<style>
.stApp {
background-color: #0f1114;
color: #ffffff;
}
.stTextInput input, .stTextArea textarea {
background-color: #1e1e24 !important;
color: #ffffff !important;
}
.stButton>button {
background: #2563eb;
transition: all 0.2s;
}
.stButton>button:hover {
background: #1d4ed8;
transform: scale(1.02);
}
.result-card {
background: #1a1a1f;
border-radius: 8px;
padding: 1.5rem;
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
st.title("π Research Assistant Pro")
st.write("Advanced AI-Powered Research Analysis")
col1, col2 = st.columns([1, 2])
with col1:
with st.form("query_form"):
query = st.text_area("Research Query:", height=150,
placeholder="Enter your research question...")
submitted = st.form_submit_button("Analyze")
if submitted and query:
with st.spinner("Processing..."):
try:
assistant = ResearchAssistant()
result = assistant.workflow.invoke({"messages": [
HumanMessage(content=query)
]})
with st.expander("Analysis Details", expanded=True):
st.markdown(f"""
<div class="result-card">
{result['messages'][-1].content}
</div>
""", unsafe_allow_html=True)
except Exception as e:
st.error(f"Processing Error: {str(e)}")
with col2:
st.subheader("Knowledge Base")
with st.expander("Research Documents"):
st.info("""
- Advanced Image Recognition Systems
- Transformer Architecture Analysis
- Quantum ML Research
""")
with st.expander("Development Updates"):
st.info("""
- Project A: API Integration Phase
- Project B: Feature Testing
- Product Y: Optimization Stage
""")
if __name__ == "__main__":
if not Config.API_KEY:
st.error("""
π Configuration Required:
Set DEEPSEEK_API_KEY environment variable
""")
st.stop()
main() |