Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,14 @@
|
|
1 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
# ------------------------------
|
4 |
# Imports & Dependencies
|
@@ -7,8 +17,8 @@ from langchain_openai import OpenAIEmbeddings
|
|
7 |
from langchain_community.vectorstores import Chroma
|
8 |
from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
|
9 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
-
from langgraph.graph import END, StateGraph
|
11 |
-
from langgraph.
|
12 |
from langgraph.graph.message import add_messages
|
13 |
from typing_extensions import TypedDict, Annotated
|
14 |
from typing import Sequence
|
@@ -67,9 +77,9 @@ development_retriever = development_vectorstore.as_retriever()
|
|
67 |
# Creating Retriever Tools
|
68 |
# ------------------------------
|
69 |
research_tool = create_retriever_tool(
|
70 |
-
research_retriever,
|
71 |
-
"research_db_tool",
|
72 |
-
"Search information from the research database."
|
73 |
)
|
74 |
|
75 |
development_tool = create_retriever_tool(
|
@@ -95,7 +105,6 @@ def agent(state: AgentState):
|
|
95 |
else:
|
96 |
user_message = messages[0].content
|
97 |
|
98 |
-
# Structure prompt for consistent text output
|
99 |
prompt = f"""Given this user question: "{user_message}"
|
100 |
If it's about research or academic topics, respond EXACTLY in this format:
|
101 |
SEARCH_RESEARCH: <search terms>
|
@@ -130,7 +139,6 @@ Otherwise, just answer directly.
|
|
130 |
response_text = response.json()['choices'][0]['message']['content']
|
131 |
print("Raw response:", response_text)
|
132 |
|
133 |
-
# Format the response into expected tool format
|
134 |
if "SEARCH_RESEARCH:" in response_text:
|
135 |
query = response_text.split("SEARCH_RESEARCH:")[1].strip()
|
136 |
results = research_retriever.invoke(query)
|
@@ -164,7 +172,6 @@ def generate(state: AgentState):
|
|
164 |
question = messages[0].content if isinstance(messages[0], tuple) else messages[0].content
|
165 |
last_message = messages[-1]
|
166 |
|
167 |
-
# Extract the document content from the results
|
168 |
docs = ""
|
169 |
if "Results: [" in last_message.content:
|
170 |
results_start = last_message.content.find("Results: [")
|
@@ -306,7 +313,6 @@ def main():
|
|
306 |
initial_sidebar_state="expanded"
|
307 |
)
|
308 |
|
309 |
-
# Custom CSS for styling
|
310 |
st.markdown("""
|
311 |
<style>
|
312 |
.stApp {
|
@@ -332,7 +338,6 @@ def main():
|
|
332 |
</style>
|
333 |
""", unsafe_allow_html=True)
|
334 |
|
335 |
-
# Sidebar with available data
|
336 |
with st.sidebar:
|
337 |
st.header("π Available Data")
|
338 |
|
@@ -347,7 +352,6 @@ def main():
|
|
347 |
st.title("π€ AI Research & Development Assistant")
|
348 |
st.markdown("---")
|
349 |
|
350 |
-
# Query input box
|
351 |
query = st.text_area("Enter your question:", height=100, placeholder="e.g., What is the latest advancement in AI research?")
|
352 |
|
353 |
col1, col2 = st.columns([1, 2])
|
@@ -386,4 +390,4 @@ def main():
|
|
386 |
""")
|
387 |
|
388 |
if __name__ == "__main__":
|
389 |
-
main()
|
|
|
1 |
+
# requirements.txt contents:
|
2 |
+
"""
|
3 |
+
langgraph>=0.0.25
|
4 |
+
langchain_openai>=0.0.4
|
5 |
+
langchain_community>=0.0.11
|
6 |
+
chromadb>=0.4.15
|
7 |
+
openai>=1.9.0
|
8 |
+
streamlit>=1.29.0
|
9 |
+
requests>=2.31.0
|
10 |
+
typing-extensions>=4.9.0
|
11 |
+
"""
|
12 |
|
13 |
# ------------------------------
|
14 |
# Imports & Dependencies
|
|
|
17 |
from langchain_community.vectorstores import Chroma
|
18 |
from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
|
19 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
20 |
+
from langgraph.graph import END, StateGraph
|
21 |
+
from langgraph.prebuilt import ToolNode # Corrected import
|
22 |
from langgraph.graph.message import add_messages
|
23 |
from typing_extensions import TypedDict, Annotated
|
24 |
from typing import Sequence
|
|
|
77 |
# Creating Retriever Tools
|
78 |
# ------------------------------
|
79 |
research_tool = create_retriever_tool(
|
80 |
+
research_retriever,
|
81 |
+
"research_db_tool",
|
82 |
+
"Search information from the research database."
|
83 |
)
|
84 |
|
85 |
development_tool = create_retriever_tool(
|
|
|
105 |
else:
|
106 |
user_message = messages[0].content
|
107 |
|
|
|
108 |
prompt = f"""Given this user question: "{user_message}"
|
109 |
If it's about research or academic topics, respond EXACTLY in this format:
|
110 |
SEARCH_RESEARCH: <search terms>
|
|
|
139 |
response_text = response.json()['choices'][0]['message']['content']
|
140 |
print("Raw response:", response_text)
|
141 |
|
|
|
142 |
if "SEARCH_RESEARCH:" in response_text:
|
143 |
query = response_text.split("SEARCH_RESEARCH:")[1].strip()
|
144 |
results = research_retriever.invoke(query)
|
|
|
172 |
question = messages[0].content if isinstance(messages[0], tuple) else messages[0].content
|
173 |
last_message = messages[-1]
|
174 |
|
|
|
175 |
docs = ""
|
176 |
if "Results: [" in last_message.content:
|
177 |
results_start = last_message.content.find("Results: [")
|
|
|
313 |
initial_sidebar_state="expanded"
|
314 |
)
|
315 |
|
|
|
316 |
st.markdown("""
|
317 |
<style>
|
318 |
.stApp {
|
|
|
338 |
</style>
|
339 |
""", unsafe_allow_html=True)
|
340 |
|
|
|
341 |
with st.sidebar:
|
342 |
st.header("π Available Data")
|
343 |
|
|
|
352 |
st.title("π€ AI Research & Development Assistant")
|
353 |
st.markdown("---")
|
354 |
|
|
|
355 |
query = st.text_area("Enter your question:", height=100, placeholder="e.g., What is the latest advancement in AI research?")
|
356 |
|
357 |
col1, col2 = st.columns([1, 2])
|
|
|
390 |
""")
|
391 |
|
392 |
if __name__ == "__main__":
|
393 |
+
main()
|