File size: 13,566 Bytes
90dcb0c
 
 
1e0350f
 
 
 
5e58a2d
 
 
 
 
 
 
1e0350f
90dcb0c
5e58a2d
90dcb0c
b7719bf
5e58a2d
 
 
1e0350f
85e6b5b
b7719bf
5e58a2d
 
 
1e0350f
 
90dcb0c
 
 
5e58a2d
1e0350f
5e58a2d
 
b7719bf
 
 
90dcb0c
b7719bf
90dcb0c
 
 
1e0350f
90dcb0c
 
b7719bf
 
 
5e58a2d
b7719bf
90dcb0c
b7719bf
 
 
5e58a2d
1e0350f
 
90dcb0c
b7719bf
 
a1bb249
90dcb0c
 
 
b7719bf
90dcb0c
 
 
b7719bf
90dcb0c
b7719bf
 
 
5e58a2d
b7719bf
09db53f
90dcb0c
5e58a2d
09db53f
90dcb0c
 
 
5e58a2d
b7719bf
09db53f
5e58a2d
 
b7719bf
90dcb0c
5e58a2d
 
 
 
 
90dcb0c
5e58a2d
 
 
 
 
 
1e0350f
5e58a2d
b7719bf
90dcb0c
b7719bf
 
5e58a2d
b7719bf
 
90dcb0c
b7719bf
 
5e58a2d
 
b7719bf
 
90dcb0c
b7719bf
 
 
 
 
 
90dcb0c
5e58a2d
 
 
90dcb0c
 
5e58a2d
 
90dcb0c
5e58a2d
 
90dcb0c
5e58a2d
 
90dcb0c
5e58a2d
 
90dcb0c
5e58a2d
 
1e0350f
5e58a2d
b7719bf
90dcb0c
 
 
5e58a2d
09db53f
 
5e58a2d
90dcb0c
 
b7719bf
5e58a2d
85e6b5b
 
5e58a2d
85e6b5b
1e0350f
90dcb0c
 
 
5e58a2d
 
1e0350f
90dcb0c
1e0350f
90dcb0c
 
b7719bf
 
5e58a2d
 
 
90dcb0c
b7719bf
 
5e58a2d
b7719bf
 
90dcb0c
5e58a2d
 
 
 
 
90dcb0c
b7719bf
 
90dcb0c
 
 
 
5e58a2d
b7719bf
 
90dcb0c
5e58a2d
b7719bf
 
 
 
 
 
90dcb0c
5e58a2d
 
 
 
 
 
b7719bf
90dcb0c
 
 
5e58a2d
 
09db53f
5e58a2d
90dcb0c
b7719bf
 
5e58a2d
b7719bf
 
90dcb0c
b7719bf
 
90dcb0c
 
 
 
5e58a2d
b7719bf
 
90dcb0c
5e58a2d
b7719bf
 
 
 
 
 
90dcb0c
5e58a2d
 
90dcb0c
5e58a2d
 
 
 
 
 
b7719bf
90dcb0c
 
 
5e58a2d
90dcb0c
5e58a2d
09db53f
 
 
90dcb0c
5e58a2d
 
 
85e6b5b
5e58a2d
85e6b5b
1e0350f
90dcb0c
 
 
5e58a2d
90dcb0c
 
5e58a2d
 
 
 
 
90dcb0c
 
5e58a2d
90dcb0c
 
 
 
 
 
 
 
 
 
 
 
5e58a2d
 
 
90dcb0c
 
5e58a2d
b7719bf
90dcb0c
 
 
5e58a2d
90dcb0c
1e0350f
 
 
 
 
90dcb0c
 
 
1e0350f
09db53f
5e58a2d
09db53f
 
85e6b5b
90dcb0c
 
b7719bf
 
90dcb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7719bf
 
 
90dcb0c
b7719bf
5e58a2d
90dcb0c
5e58a2d
b7719bf
 
90dcb0c
5e58a2d
b7719bf
5e58a2d
b7719bf
90dcb0c
5e58a2d
b7719bf
90dcb0c
 
5e58a2d
90dcb0c
1e0350f
 
5e58a2d
1e0350f
5e58a2d
90dcb0c
5e58a2d
90dcb0c
 
1e0350f
 
5e58a2d
 
 
 
 
 
 
1e0350f
b7719bf
1e0350f
 
5e58a2d
90dcb0c
1e0350f
09db53f
5e58a2d
 
 
 
b7719bf
5e58a2d
 
 
 
09db53f
b7719bf
a1bb249
 
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langgraph.graph import END, StateGraph, START
from langgraph.prebuilt import ToolNode
from langgraph.graph.message import add_messages
from typing_extensions import TypedDict, Annotated
from typing import Sequence
import re
import os
import streamlit as st
import requests
from langchain.tools.retriever import create_retriever_tool

# --------------------------
# Create Dummy Data
# --------------------------
research_texts = [
    "Research Report: Results of a New AI Model Improving Image Recognition Accuracy to 98%",
    "Academic Paper Summary: Why Transformers Became the Mainstream Architecture in Natural Language Processing",
    "Latest Trends in Machine Learning Methods Using Quantum Computing"
]

development_texts = [
    "Project A: UI Design Completed, API Integration in Progress",
    "Project B: Testing New Feature X, Bug Fixes Needed",
    "Product Y: In the Performance Optimization Stage Before Release"
]

# --------------------------
# Process the Data
# --------------------------
# Text splitting settings
splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10)

# Generate Document objects from text
research_docs = splitter.create_documents(research_texts)
development_docs = splitter.create_documents(development_texts)

# Create vector embeddings
embeddings = OpenAIEmbeddings(
    model="text-embedding-3-large",
    # For text-embedding-3 class models, you can specify dimensions if needed.
    # dimensions=1024
)

# Create vector stores
research_vectorstore = Chroma.from_documents(
    documents=research_docs,
    embedding=embeddings,
    collection_name="research_collection"
)

development_vectorstore = Chroma.from_documents(
    documents=development_docs,
    embedding=embeddings,
    collection_name="development_collection"
)

# Create retrievers from the vector stores
research_retriever = research_vectorstore.as_retriever()
development_retriever = development_vectorstore.as_retriever()

# --------------------------
# Create Retriever Tools
# --------------------------
research_tool = create_retriever_tool(
    research_retriever,  # Retriever object
    "research_db_tool",  # Name of the tool to create
    "Search information from the research database."  # Description of the tool
)

development_tool = create_retriever_tool(
    development_retriever,
    "development_db_tool",
    "Search information from the development database."
)

# Combine the tools into a list
tools = [research_tool, development_tool]

# --------------------------
# Define the Agent Function
# --------------------------
class AgentState(TypedDict):
    messages: Annotated[Sequence[AIMessage | HumanMessage | ToolMessage], add_messages]

def agent(state: AgentState):
    print("---CALL AGENT---")
    messages = state["messages"]

    if isinstance(messages[0], tuple):
        user_message = messages[0][1]
    else:
        user_message = messages[0].content

    # Structure prompt for consistent text output
    prompt = f"""Given this user question: "{user_message}"
If it's about research or academic topics, respond EXACTLY in this format:
SEARCH_RESEARCH: <search terms>

If it's about development status, respond EXACTLY in this format:
SEARCH_DEV: <search terms>

Otherwise, just answer directly.
"""

    headers = {
        "Accept": "application/json",
        "Authorization": f"Bearer sk-1cddf19f9dc4466fa3ecea6fe10abec0",
        "Content-Type": "application/json"
    }
    
    data = {
        "model": "deepseek-chat",
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.7,
        "max_tokens": 1024
    }
    
    response = requests.post(
        "https://api.deepseek.com/v1/chat/completions",
        headers=headers,
        json=data,
        verify=False
    )
    
    if response.status_code == 200:
        response_text = response.json()['choices'][0]['message']['content']
        print("Raw response:", response_text)
        
        # Format the response into expected tool format
        if "SEARCH_RESEARCH:" in response_text:
            query = response_text.split("SEARCH_RESEARCH:")[1].strip()
            # Use direct call to research retriever
            results = research_retriever.invoke(query)
            return {"messages": [AIMessage(content=f'Action: research_db_tool\n{{"query": "{query}"}}\n\nResults: {str(results)}')]}

        elif "SEARCH_DEV:" in response_text:
            query = response_text.split("SEARCH_DEV:")[1].strip()
            # Use direct call to development retriever
            results = development_retriever.invoke(query)
            return {"messages": [AIMessage(content=f'Action: development_db_tool\n{{"query": "{query}"}}\n\nResults: {str(results)}')]}

        else:
            return {"messages": [AIMessage(content=response_text)]}
    else:
        raise Exception(f"API call failed: {response.text}")

# --------------------------
# Grading Function
# --------------------------
def simple_grade_documents(state: AgentState):
    messages = state["messages"]
    last_message = messages[-1]
    print("Evaluating message:", last_message.content)
    
    # Check if the content contains retrieved documents
    if "Results: [Document" in last_message.content:
        print("---DOCS FOUND, GO TO GENERATE---")
        return "generate"
    else:
        print("---NO DOCS FOUND, TRY REWRITE---")
        return "rewrite"

# --------------------------
# Generation Function
# --------------------------
def generate(state: AgentState):
    print("---GENERATE FINAL ANSWER---")
    messages = state["messages"]
    question = messages[0].content if isinstance(messages[0], tuple) else messages[0].content
    last_message = messages[-1]

    # Extract the document content from the results
    docs = ""
    if "Results: [" in last_message.content:
        results_start = last_message.content.find("Results: [")
        docs = last_message.content[results_start:]
    print("Documents found:", docs)

    headers = {
        "Accept": "application/json",
        "Authorization": f"Bearer sk-1cddf19f9dc4466fa3ecea6fe10abec0",
        "Content-Type": "application/json"
    }
    
    prompt = f"""Based on these research documents, summarize the latest advancements in AI:
Question: {question}
Documents: {docs}
Focus on extracting and synthesizing the key findings from the research papers.
"""
    
    data = {
        "model": "deepseek-chat",
        "messages": [{
            "role": "user",
            "content": prompt
        }],
        "temperature": 0.7,
        "max_tokens": 1024
    }
    
    print("Sending generate request to API...")
    response = requests.post(
        "https://api.deepseek.com/v1/chat/completions",
        headers=headers,
        json=data,
        verify=False
    )
    
    if response.status_code == 200:
        response_text = response.json()['choices'][0]['message']['content']
        print("Final Answer:", response_text)
        return {"messages": [AIMessage(content=response_text)]}
    else:
        raise Exception(f"API call failed: {response.text}")

# --------------------------
# Rewrite Function
# --------------------------
def rewrite(state: AgentState):
    print("---REWRITE QUESTION---")
    messages = state["messages"]
    original_question = messages[0].content if len(messages) > 0 else "N/A"

    headers = {
        "Accept": "application/json",
        "Authorization": f"Bearer sk-1cddf19f9dc4466fa3ecea6fe10abec0",
        "Content-Type": "application/json"
    }
    
    data = {
        "model": "deepseek-chat",
        "messages": [{
            "role": "user",
            "content": f"Rewrite this question to be more specific and clearer: {original_question}"
        }],
        "temperature": 0.7,
        "max_tokens": 1024
    }
    
    print("Sending rewrite request...")
    response = requests.post(
        "https://api.deepseek.com/v1/chat/completions",
        headers=headers,
        json=data,
        verify=False
    )
    
    print("Status Code:", response.status_code)
    print("Response:", response.text)
    
    if response.status_code == 200:
        response_text = response.json()['choices'][0]['message']['content']
        print("Rewritten question:", response_text)
        return {"messages": [AIMessage(content=response_text)]}
    else:
        raise Exception(f"API call failed: {response.text}")

# --------------------------
# Tools Decision Function
# --------------------------
tools_pattern = re.compile(r"Action: .*")

def custom_tools_condition(state: AgentState):
    messages = state["messages"]
    last_message = messages[-1]
    content = last_message.content

    print("Checking tools condition:", content)
    if tools_pattern.match(content):
        print("Moving to retrieve...")
        return "tools"
    print("Moving to END...")
    return END

# --------------------------
# LangGraph Workflow Setup
# --------------------------
workflow = StateGraph(AgentState)

# Define the workflow nodes
workflow.add_node("agent", agent)
retrieve_node = ToolNode(tools)
workflow.add_node("retrieve", retrieve_node)
workflow.add_node("rewrite", rewrite)
workflow.add_node("generate", generate)

# Set up the initial edge
workflow.add_edge(START, "agent")

# Conditional edge from agent to either retrieve (if tool is called) or END
workflow.add_conditional_edges(
    "agent",
    custom_tools_condition,
    {
        "tools": "retrieve",
        END: END
    }
)

# After retrieval, decide to generate or rewrite based on document grading
workflow.add_conditional_edges("retrieve", simple_grade_documents)
workflow.add_edge("generate", END)
workflow.add_edge("rewrite", "agent")

# Compile the workflow to make it executable
app = workflow.compile()

# --------------------------
# Process Question Function
# --------------------------
def process_question(user_question, app, config):
    """Process user question through the workflow"""
    events = []
    for event in app.stream({"messages": [("user", user_question)]}, config):
        events.append(event)
    return events

# --------------------------
# Streamlit Application
# --------------------------
def main():
    st.set_page_config(
        page_title="AI Research & Development Assistant",
        layout="wide",
        initial_sidebar_state="expanded"
    )

    # Custom CSS
    st.markdown("""
    <style>
    .stApp {
        background-color: #f8f9fa;
    }
    .stButton > button {
        width: 100%;
        margin-top: 20px;
    }
    .data-box {
        padding: 20px;
        border-radius: 10px;
        margin: 10px 0;
    }
    .research-box {
        background-color: #e3f2fd;
        border-left: 5px solid #1976d2;
    }
    .dev-box {
        background-color: #e8f5e9;
        border-left: 5px solid #43a047;
    }
    </style>
    """, unsafe_allow_html=True)

    # Sidebar with Data Display
    with st.sidebar:
        st.header("πŸ“š Available Data")
        
        st.subheader("Research Database")
        for text in research_texts:
            st.markdown(f'<div class="data-box research-box">{text}</div>', unsafe_allow_html=True)
            
        st.subheader("Development Database")
        for text in development_texts:
            st.markdown(f'<div class="data-box dev-box">{text}</div>', unsafe_allow_html=True)

    # Main Content
    st.title("πŸ€– AI Research & Development Assistant")
    st.markdown("---")

    # Query Input
    query = st.text_area("Enter your question:", height=100, placeholder="e.g., What is the latest advancement in AI research?")

    col1, col2 = st.columns([1, 2])
    with col1:
        if st.button("πŸ” Get Answer", use_container_width=True):
            if query:
                with st.spinner('Processing your question...'):
                    # Process query through workflow
                    events = process_question(query, app, {"configurable": {"thread_id": "1"}})
                    
                    # Display results
                    for event in events:
                        if 'agent' in event:
                            with st.expander("πŸ”„ Processing Step", expanded=True):
                                content = event['agent']['messages'][0].content
                                if "Results:" in content:
                                    st.markdown("### πŸ“‘ Retrieved Documents:")
                                    docs_start = content.find("Results:")
                                    docs = content[docs_start:]
                                    st.info(docs)
                        elif 'generate' in event:
                            st.markdown("### ✨ Final Answer:")
                            st.success(event['generate']['messages'][0].content)
            else:
                st.warning("⚠️ Please enter a question first!")

    with col2:
        st.markdown("""
        ### 🎯 How to Use
        1. Type your question in the text box
        2. Click "Get Answer" to process
        3. View retrieved documents and final answer
        
        ### πŸ’‘ Example Questions
        - What are the latest advancements in AI research?
        - What is the status of Project A?
        - What are the current trends in machine learning?
        """)

if __name__ == "__main__":
    main()