Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 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
|
|
@@ -9,42 +7,33 @@ from langchain.agents.agent_types import AgentType
|
|
| 9 |
from langchain.agents import Tool, initialize_agent
|
| 10 |
from langchain.callbacks import StreamlitCallbackHandler
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
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
|
| 21 |
st.stop()
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
st.title("General Knowledge Assistant")
|
| 26 |
-
|
| 27 |
-
# Initialize the LLM (Groq API - deepseek-r1-distill-llama-70b)
|
| 28 |
-
llm = ChatGroq(model="deepseek-r1-distill-llama-70b", 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
|
| 36 |
)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
prompt = """
|
| 40 |
-
You are a knowledgeable assistant. Your task is to answer the user's questions accurately using your general knowledge.
|
| 41 |
-
If
|
| 42 |
-
|
| 43 |
-
2. If you determine that you need updated information, output an 'Action:' line in the following format:
|
| 44 |
-
Action: Wikipedia[search query]
|
| 45 |
-
3. Once you receive additional information, integrate it into your final answer.
|
| 46 |
-
Ensure that you follow this format strictly. Also, whenever I ask you to write an essay, provide a title for the essay.
|
| 47 |
-
|
| 48 |
Question: {question}
|
| 49 |
Answer:
|
| 50 |
"""
|
|
@@ -62,7 +51,7 @@ chain = LLMChain(llm=llm, prompt=prompt_template)
|
|
| 62 |
reasoning_tool = Tool(
|
| 63 |
name="Reasoning tool",
|
| 64 |
func=chain.run,
|
| 65 |
-
description="A tool for answering general knowledge questions using logical reasoning and factual information.
|
| 66 |
)
|
| 67 |
|
| 68 |
# Initialize the agent with the tools and LLM
|
|
@@ -82,25 +71,25 @@ if "messages" not in st.session_state:
|
|
| 82 |
|
| 83 |
# Display the conversation history
|
| 84 |
for msg in st.session_state.messages:
|
| 85 |
-
st.chat_message(msg["role"]).write(msg[
|
| 86 |
|
| 87 |
# Get the user's question
|
| 88 |
question = st.text_area("Enter your question:", "Please enter your general knowledge question here")
|
| 89 |
|
| 90 |
# Handle the button click to process the question
|
| 91 |
-
if st.button("
|
| 92 |
if question:
|
| 93 |
-
with st.spinner("
|
| 94 |
-
|
| 95 |
-
st.session_state.messages.append({"role": "user", "content": question})
|
| 96 |
st.chat_message("user").write(question)
|
| 97 |
|
| 98 |
-
st_cb
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
st.session_state.messages.append({'role':
|
| 102 |
-
|
| 103 |
st.write('### Response:')
|
| 104 |
st.success(response)
|
|
|
|
| 105 |
else:
|
| 106 |
-
st.warning("Please enter
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_groq import ChatGroq
|
| 3 |
from langchain.chains import LLMChain
|
|
|
|
| 7 |
from langchain.agents import Tool, initialize_agent
|
| 8 |
from langchain.callbacks import StreamlitCallbackHandler
|
| 9 |
|
| 10 |
+
# Set up Streamlit page configuration
|
| 11 |
+
st.set_page_config(page_title="General Knowledge Assistant", page_icon="🧭")
|
| 12 |
+
st.title("General Knowledge Assistant")
|
| 13 |
|
| 14 |
+
# API Key input for Groq
|
| 15 |
+
groq_api_key = st.sidebar.text_input(label="Groq API Key", type="password")
|
| 16 |
|
|
|
|
| 17 |
if not groq_api_key:
|
| 18 |
+
st.info("Please add your Groq API key to continue")
|
| 19 |
st.stop()
|
| 20 |
|
| 21 |
+
# Initialize the LLM (Groq API - llama-3.1-70b)
|
| 22 |
+
llm = ChatGroq(model="meta-llama/llama-4-maverick-17b-128e-instruct", groq_api_key=groq_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Initialize Wikipedia tool for information retrieval
|
| 25 |
wikipedia_wrapper = WikipediaAPIWrapper()
|
| 26 |
wikipedia_tool = Tool(
|
| 27 |
name="Wikipedia",
|
| 28 |
func=wikipedia_wrapper.run,
|
| 29 |
+
description="A tool for searching the Internet to find information on various topics, including general knowledge."
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Prompt template for general knowledge questions
|
| 33 |
prompt = """
|
| 34 |
+
You are a knowledgeable assistant. Your task is to answer the user's questions accurately, using your general knowledge.
|
| 35 |
+
If the answer is not readily available in your knowledge base, search Wikipedia for relevant information.
|
| 36 |
+
Your information should be accurate and up to date.Whenever I tell you to write essay give a title also to the essay.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
Question: {question}
|
| 38 |
Answer:
|
| 39 |
"""
|
|
|
|
| 51 |
reasoning_tool = Tool(
|
| 52 |
name="Reasoning tool",
|
| 53 |
func=chain.run,
|
| 54 |
+
description="A tool for answering general knowledge questions using logical reasoning and factual information.Try to use the latest information"
|
| 55 |
)
|
| 56 |
|
| 57 |
# Initialize the agent with the tools and LLM
|
|
|
|
| 71 |
|
| 72 |
# Display the conversation history
|
| 73 |
for msg in st.session_state.messages:
|
| 74 |
+
st.chat_message(msg["role"]).write(msg['content'])
|
| 75 |
|
| 76 |
# Get the user's question
|
| 77 |
question = st.text_area("Enter your question:", "Please enter your general knowledge question here")
|
| 78 |
|
| 79 |
# Handle the button click to process the question
|
| 80 |
+
if st.button("find my answer"):
|
| 81 |
if question:
|
| 82 |
+
with st.spinner("Generate response.."):
|
| 83 |
+
st.session_state.messages.append({"role":"user","content":question})
|
|
|
|
| 84 |
st.chat_message("user").write(question)
|
| 85 |
|
| 86 |
+
st_cb=StreamlitCallbackHandler(st.container(),expand_new_thoughts=False)
|
| 87 |
+
response=assistant_agent.run(st.session_state.messages,callbacks=[st_cb]
|
| 88 |
+
)
|
| 89 |
+
st.session_state.messages.append({'role':'assistant',"content":response})
|
|
|
|
| 90 |
st.write('### Response:')
|
| 91 |
st.success(response)
|
| 92 |
+
|
| 93 |
else:
|
| 94 |
+
st.warning("Please enter the question")
|
| 95 |
+
|