Update app.py
Browse files
app.py
CHANGED
@@ -1,94 +1,98 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
|
4 |
-
from
|
5 |
-
from
|
6 |
-
from langchain.
|
7 |
-
from
|
8 |
-
from langchain.
|
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 |
-
st.
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
st.session_state.messages.append({
|
90 |
-
st.write(
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import streamlit as st
|
4 |
+
from langchain_groq import ChatGroq
|
5 |
+
from langchain.chains import LLMChain
|
6 |
+
from langchain.prompts import PromptTemplate
|
7 |
+
from langchain_community.utilities import WikipediaAPIWrapper
|
8 |
+
from langchain.agents.agent_types import AgentType
|
9 |
+
from langchain.agents import Tool, initialize_agent
|
10 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
11 |
+
|
12 |
+
# Load environment variables from the .env file
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Get API key from environment variables
|
16 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
17 |
+
|
18 |
+
# If the API key is not set, show an info message and stop execution
|
19 |
+
if not groq_api_key:
|
20 |
+
st.info("Please set your Groq API key in the .env file to continue")
|
21 |
+
st.stop()
|
22 |
+
|
23 |
+
# Set up Streamlit page configuration
|
24 |
+
st.set_page_config(page_title="General Knowledge Assistant", page_icon="🧭")
|
25 |
+
st.title("General Knowledge Assistant")
|
26 |
+
|
27 |
+
# Initialize the LLM (Groq API - llama-3.1-70b)
|
28 |
+
llm = ChatGroq(model="meta-llama/llama-4-maverick-17b-128e-instruct", groq_api_key=groq_api_key)
|
29 |
+
|
30 |
+
# Initialize Wikipedia tool for information retrieval
|
31 |
+
wikipedia_wrapper = WikipediaAPIWrapper()
|
32 |
+
wikipedia_tool = Tool(
|
33 |
+
name="Wikipedia",
|
34 |
+
func=wikipedia_wrapper.run,
|
35 |
+
description="A tool for searching the Internet to find information on various topics, including general knowledge."
|
36 |
+
)
|
37 |
+
|
38 |
+
# Prompt template for general knowledge questions
|
39 |
+
prompt = """
|
40 |
+
You are a knowledgeable assistant. Your task is to answer the user's questions accurately, using your general knowledge.
|
41 |
+
If the answer is not readily available in your knowledge base, search Wikipedia for relevant information.
|
42 |
+
Your information should be accurate and up to date. Whenever I tell you to write essay give a title also to the essay.
|
43 |
+
Question: {question}
|
44 |
+
Answer:
|
45 |
+
"""
|
46 |
+
|
47 |
+
# Initialize the prompt template
|
48 |
+
prompt_template = PromptTemplate(
|
49 |
+
input_variables=["question"],
|
50 |
+
template=prompt
|
51 |
+
)
|
52 |
+
|
53 |
+
# Combine all the tools into a chain for answering general knowledge questions
|
54 |
+
chain = LLMChain(llm=llm, prompt=prompt_template)
|
55 |
+
|
56 |
+
# Reasoning tool for logic-based or factual questions
|
57 |
+
reasoning_tool = Tool(
|
58 |
+
name="Reasoning tool",
|
59 |
+
func=chain.run,
|
60 |
+
description="A tool for answering general knowledge questions using logical reasoning and factual information. Try to use the latest information"
|
61 |
+
)
|
62 |
+
|
63 |
+
# Initialize the agent with the tools and LLM
|
64 |
+
assistant_agent = initialize_agent(
|
65 |
+
tools=[wikipedia_tool, reasoning_tool],
|
66 |
+
llm=llm,
|
67 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
68 |
+
verbose=False,
|
69 |
+
handle_parsing_errors=True
|
70 |
+
)
|
71 |
+
|
72 |
+
# Initialize session state for message history if it doesn't exist
|
73 |
+
if "messages" not in st.session_state:
|
74 |
+
st.session_state["messages"] = [
|
75 |
+
{"role": "assistant", "content": "Hi, I'm your general knowledge assistant. Feel free to ask me any question!"}
|
76 |
+
]
|
77 |
+
|
78 |
+
# Display the conversation history
|
79 |
+
for msg in st.session_state.messages:
|
80 |
+
st.chat_message(msg["role"]).write(msg['content'])
|
81 |
+
|
82 |
+
# Get the user's question
|
83 |
+
question = st.text_area("Enter your question:", "Please enter your general knowledge question here")
|
84 |
+
|
85 |
+
# Handle the button click to process the question
|
86 |
+
if st.button("find my answer"):
|
87 |
+
if question:
|
88 |
+
with st.spinner("Generate response.."):
|
89 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
90 |
+
st.chat_message("user").write(question)
|
91 |
+
|
92 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
93 |
+
response = assistant_agent.run(st.session_state.messages, callbacks=[st_cb])
|
94 |
+
st.session_state.messages.append({'role': 'assistant', "content": response})
|
95 |
+
st.write('### Response:')
|
96 |
+
st.success(response)
|
97 |
+
else:
|
98 |
+
st.warning("Please enter the question")
|