Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from langchain_groq import ChatGroq
|
3 |
from langchain.chains import LLMChain
|
4 |
from langchain.prompts import PromptTemplate
|
@@ -6,130 +7,76 @@ from langchain_community.utilities import WikipediaAPIWrapper
|
|
6 |
from langchain.agents.agent_types import AgentType
|
7 |
from langchain.agents import Tool, initialize_agent
|
8 |
from langchain.callbacks import StreamlitCallbackHandler
|
9 |
-
import datetime # Import datetime to add current date context
|
10 |
|
11 |
-
# Set up Streamlit page configuration
|
12 |
st.set_page_config(page_title="General Knowledge Assistant", page_icon="🧭")
|
13 |
st.title("General Knowledge Assistant")
|
14 |
|
15 |
-
# API Key input for Groq
|
16 |
groq_api_key = st.sidebar.text_input(label="Groq API Key", type="password")
|
17 |
|
18 |
if not groq_api_key:
|
19 |
st.info("Please add your Groq API key to continue")
|
20 |
st.stop()
|
21 |
|
22 |
-
# Initialize the LLM (Using the model from your original code)
|
23 |
-
# You might consider trying 'llama3-70b-8192' if Maverick struggles with tool selection
|
24 |
llm = ChatGroq(model="meta-llama/llama-4-maverick-17b-128e-instruct", groq_api_key=groq_api_key)
|
25 |
|
26 |
-
|
27 |
-
wikipedia_wrapper = WikipediaAPIWrapper(top_k_results=2, doc_content_chars_max=2000) # Limit results slightly
|
28 |
wikipedia_tool = Tool(
|
29 |
-
name="Wikipedia
|
30 |
func=wikipedia_wrapper.run,
|
31 |
-
description=
|
32 |
-
"Use this tool to find specific information, facts, or details about people, places, events, or topics. "
|
33 |
-
"It is especially useful for getting CURRENT and UP-TO-DATE information or checking facts that might change over time. "
|
34 |
-
"Input should be a clear search query."
|
35 |
-
)
|
36 |
)
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
If you are asked to write an essay, please provide a title for the essay.
|
45 |
-
Your information should be accurate and up-to-date based on your internal knowledge cutoff.
|
46 |
-
If you suspect your internal knowledge might be outdated for the question, mention that the information might not be the absolute latest.
|
47 |
-
|
48 |
-
Question: {{question}}
|
49 |
Answer:
|
50 |
"""
|
51 |
|
52 |
-
# Initialize the prompt template
|
53 |
prompt_template = PromptTemplate(
|
54 |
input_variables=["question"],
|
55 |
-
template=
|
56 |
)
|
57 |
|
58 |
-
# Create the LLMChain for the Reasoning tool
|
59 |
chain = LLMChain(llm=llm, prompt=prompt_template)
|
60 |
|
61 |
-
# Reasoning tool for logic-based or factual questions
|
62 |
reasoning_tool = Tool(
|
63 |
-
name="
|
64 |
func=chain.run,
|
65 |
-
description=
|
66 |
-
"Use this tool to answer general knowledge questions, perform reasoning tasks, or explain concepts based on the AI's internal knowledge base. "
|
67 |
-
"This tool relies on the AI's trained data, which might have a knowledge cut-off date. Do NOT use this tool if the question likely requires very recent information (use Wikipedia Search instead)."
|
68 |
-
)
|
69 |
)
|
70 |
|
71 |
-
# Initialize the agent with the tools and LLM
|
72 |
-
# Ensure verbose=False and handle_parsing_errors=True as per your original code
|
73 |
assistant_agent = initialize_agent(
|
74 |
tools=[wikipedia_tool, reasoning_tool],
|
75 |
llm=llm,
|
76 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
77 |
-
verbose=False,
|
78 |
-
handle_parsing_errors=True
|
79 |
-
# Add max_iterations to prevent potential infinite loops if the agent gets stuck
|
80 |
-
max_iterations=5,
|
81 |
-
early_stopping_method="generate" # Stop generating if it thinks it's done
|
82 |
)
|
83 |
|
84 |
-
# Initialize session state for message history if it doesn't exist
|
85 |
if "messages" not in st.session_state:
|
86 |
st.session_state["messages"] = [
|
87 |
{"role": "assistant", "content": "Hi, I'm your general knowledge assistant. Feel free to ask me any question!"}
|
88 |
]
|
89 |
|
90 |
-
# Display the conversation history
|
91 |
for msg in st.session_state.messages:
|
92 |
st.chat_message(msg["role"]).write(msg['content'])
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
with st.chat_message("assistant"):
|
111 |
-
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
112 |
-
# *** FIX: Pass only the user_query string to the agent's run method ***
|
113 |
-
response = assistant_agent.run(agent_input, callbacks=[st_cb])
|
114 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
115 |
-
st.write(response) # Display the final response
|
116 |
-
|
117 |
-
# Note: Removed the text_area + button combo in favor of st.chat_input for a cleaner chat interface.
|
118 |
-
# If you prefer the text_area and button:
|
119 |
-
# user_question = st.text_area("Enter your question:", "Please enter your general knowledge question here", key="user_q_input")
|
120 |
-
# if st.button("Find my answer", key="submit_q"):
|
121 |
-
# if user_question and user_question != "Please enter your general knowledge question here":
|
122 |
-
# # Add user message to state and display it
|
123 |
-
# st.session_state.messages.append({"role": "user", "content": user_question})
|
124 |
-
# st.chat_message("user").write(user_question)
|
125 |
-
|
126 |
-
# agent_input = user_question # Use the text_area content
|
127 |
-
|
128 |
-
# # Generate and display response
|
129 |
-
# with st.chat_message("assistant"):
|
130 |
-
# st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
131 |
-
# response = assistant_agent.run(agent_input, callbacks=[st_cb])
|
132 |
-
# st.session_state.messages.append({"role": "assistant", "content": response})
|
133 |
-
# st.write(response)
|
134 |
-
# else:
|
135 |
-
# st.warning("Please enter a question.")
|
|
|
1 |
import streamlit as st
|
2 |
+
|
3 |
from langchain_groq import ChatGroq
|
4 |
from langchain.chains import LLMChain
|
5 |
from langchain.prompts import PromptTemplate
|
|
|
7 |
from langchain.agents.agent_types import AgentType
|
8 |
from langchain.agents import Tool, initialize_agent
|
9 |
from langchain.callbacks import StreamlitCallbackHandler
|
|
|
10 |
|
|
|
11 |
st.set_page_config(page_title="General Knowledge Assistant", page_icon="🧭")
|
12 |
st.title("General Knowledge Assistant")
|
13 |
|
|
|
14 |
groq_api_key = st.sidebar.text_input(label="Groq API Key", type="password")
|
15 |
|
16 |
if not groq_api_key:
|
17 |
st.info("Please add your Groq API key to continue")
|
18 |
st.stop()
|
19 |
|
|
|
|
|
20 |
llm = ChatGroq(model="meta-llama/llama-4-maverick-17b-128e-instruct", groq_api_key=groq_api_key)
|
21 |
|
22 |
+
wikipedia_wrapper = WikipediaAPIWrapper()
|
|
|
23 |
wikipedia_tool = Tool(
|
24 |
+
name="Wikipedia",
|
25 |
func=wikipedia_wrapper.run,
|
26 |
+
description="Use this tool to fetch updated information from the internet when your base knowledge is outdated or incomplete."
|
|
|
|
|
|
|
|
|
27 |
)
|
28 |
|
29 |
+
prompt = """
|
30 |
+
You are a knowledgeable assistant. Your task is to answer the user's questions accurately with your general knowledge.
|
31 |
+
If you detect that your stored information is outdated or missing recent details, immediately search Wikipedia for updated info.
|
32 |
+
Always ensure your answer is up to date. Whenever I tell you to write essay give a title also to the essay.
|
33 |
+
|
34 |
+
Question: {question}
|
|
|
|
|
|
|
|
|
|
|
35 |
Answer:
|
36 |
"""
|
37 |
|
|
|
38 |
prompt_template = PromptTemplate(
|
39 |
input_variables=["question"],
|
40 |
+
template=prompt
|
41 |
)
|
42 |
|
|
|
43 |
chain = LLMChain(llm=llm, prompt=prompt_template)
|
44 |
|
|
|
45 |
reasoning_tool = Tool(
|
46 |
+
name="Reasoning tool",
|
47 |
func=chain.run,
|
48 |
+
description="A tool for answering general knowledge questions using logical reasoning and factual information. Use Wikipedia if your answer might be outdated."
|
|
|
|
|
|
|
49 |
)
|
50 |
|
|
|
|
|
51 |
assistant_agent = initialize_agent(
|
52 |
tools=[wikipedia_tool, reasoning_tool],
|
53 |
llm=llm,
|
54 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
55 |
+
verbose=False,
|
56 |
+
handle_parsing_errors=True
|
|
|
|
|
|
|
57 |
)
|
58 |
|
|
|
59 |
if "messages" not in st.session_state:
|
60 |
st.session_state["messages"] = [
|
61 |
{"role": "assistant", "content": "Hi, I'm your general knowledge assistant. Feel free to ask me any question!"}
|
62 |
]
|
63 |
|
|
|
64 |
for msg in st.session_state.messages:
|
65 |
st.chat_message(msg["role"]).write(msg['content'])
|
66 |
|
67 |
+
question = st.text_area("Enter your question:", "Please enter your general knowledge question here")
|
68 |
+
|
69 |
+
if st.button("find my answer"):
|
70 |
+
if question:
|
71 |
+
with st.spinner("Generate response.."):
|
72 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
73 |
+
st.chat_message("user").write(question)
|
74 |
+
|
75 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
76 |
+
response = assistant_agent.run(st.session_state.messages, callbacks=[st_cb])
|
77 |
+
|
78 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
79 |
+
st.write("### Response:")
|
80 |
+
st.success(response)
|
81 |
+
else:
|
82 |
+
st.warning("Please enter the question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|