|
import os |
|
from dotenv import load_dotenv |
|
import streamlit as st |
|
from langchain_groq import ChatGroq |
|
from langchain.chains import LLMChain |
|
from langchain.prompts import PromptTemplate |
|
from langchain_community.utilities import WikipediaAPIWrapper |
|
from langchain.agents.agent_types import AgentType |
|
from langchain.agents import Tool, initialize_agent |
|
from langchain.callbacks import StreamlitCallbackHandler |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
groq_api_key = os.getenv("GROQ_API_KEY") |
|
|
|
|
|
if not groq_api_key: |
|
st.info("Please set your Groq API key in the .env file to continue") |
|
st.stop() |
|
|
|
|
|
st.set_page_config(page_title="General Knowledge Assistant", page_icon="🧭") |
|
st.title("General Knowledge Assistant") |
|
|
|
|
|
llm = ChatGroq(model="meta-llama/llama-4-maverick-17b-128e-instruct", groq_api_key=groq_api_key) |
|
|
|
|
|
wikipedia_wrapper = WikipediaAPIWrapper() |
|
wikipedia_tool = Tool( |
|
name="Wikipedia", |
|
func=wikipedia_wrapper.run, |
|
description="A tool for searching the Internet to find information on various topics, including general knowledge." |
|
) |
|
|
|
|
|
prompt = """ |
|
You are a knowledgeable assistant. Your task is to answer the user's questions accurately, using your general knowledge. |
|
If the answer is not readily available in your knowledge base, search Wikipedia for relevant information. |
|
Your information should be accurate and up to date. Whenever I tell you to write essay give a title also to the essay. |
|
Question: {question} |
|
Answer: |
|
""" |
|
|
|
|
|
prompt_template = PromptTemplate( |
|
input_variables=["question"], |
|
template=prompt |
|
) |
|
|
|
|
|
chain = LLMChain(llm=llm, prompt=prompt_template) |
|
|
|
|
|
reasoning_tool = Tool( |
|
name="Reasoning tool", |
|
func=chain.run, |
|
description="A tool for answering general knowledge questions using logical reasoning and factual information. Try to use the latest information" |
|
) |
|
|
|
|
|
assistant_agent = initialize_agent( |
|
tools=[wikipedia_tool, reasoning_tool], |
|
llm=llm, |
|
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, |
|
verbose=False, |
|
handle_parsing_errors=True |
|
) |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state["messages"] = [ |
|
{"role": "assistant", "content": "Hi, I'm your general knowledge assistant. Feel free to ask me any question!"} |
|
] |
|
|
|
|
|
for msg in st.session_state.messages: |
|
st.chat_message(msg["role"]).write(msg['content']) |
|
|
|
|
|
question = st.text_area("Enter your question:", "Please enter your general knowledge question here") |
|
|
|
|
|
if st.button("find my answer"): |
|
if question: |
|
with st.spinner("Generate response.."): |
|
st.session_state.messages.append({"role": "user", "content": question}) |
|
st.chat_message("user").write(question) |
|
|
|
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False) |
|
response = assistant_agent.run(st.session_state.messages, callbacks=[st_cb]) |
|
st.session_state.messages.append({'role': 'assistant', "content": response}) |
|
st.write('### Response:') |
|
st.success(response) |
|
else: |
|
st.warning("Please enter the question") |
|
|