Ashkchamp commited on
Commit
30bdaf2
·
verified ·
1 Parent(s): 1eda96a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -24,7 +24,7 @@ if not groq_api_key:
24
  st.set_page_config(page_title="General Knowledge Assistant")
25
  st.title("General Knowledge Assistant")
26
 
27
- # Initialize the LLM (Groq API - llama-3.1-70b)
28
  llm = ChatGroq(model="deepseek-r1-distill-llama-70b", groq_api_key=groq_api_key)
29
 
30
  # Initialize Wikipedia tool for information retrieval
@@ -32,14 +32,19 @@ 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
  """
@@ -57,7 +62,7 @@ chain = LLMChain(llm=llm, prompt=prompt_template)
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
@@ -77,22 +82,25 @@ if "messages" not in st.session_state:
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")
 
24
  st.set_page_config(page_title="General Knowledge Assistant")
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
 
32
  wikipedia_tool = Tool(
33
  name="Wikipedia",
34
  func=wikipedia_wrapper.run,
35
+ description="A tool for searching Wikipedia to retrieve up-to-date and detailed information on various topics."
36
  )
37
 
38
+ # Updated prompt template with explicit instructions for ReAct chaining
39
  prompt = """
40
+ You are a knowledgeable assistant. Your task is to answer the user's questions accurately using your general knowledge.
41
+ If you believe that your internal knowledge may be outdated or insufficient, follow these steps:
42
+ 1. Write your internal thought process beginning with 'Thought:'.
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
  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. Use this tool and consult Wikipedia if necessary."
66
  )
67
 
68
  # Initialize the agent with the tools and LLM
 
82
 
83
  # Display the conversation history
84
  for msg in st.session_state.messages:
85
+ st.chat_message(msg["role"]).write(msg["content"])
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("Find my answer"):
92
  if question:
93
+ with st.spinner("Generating response..."):
94
+ # Append the user question to the conversation history and display it
95
  st.session_state.messages.append({"role": "user", "content": question})
96
  st.chat_message("user").write(question)
97
 
98
  st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
99
+ # Pass the question string directly to the agent (instead of the full conversation history)
100
+ response = assistant_agent.run(question, callbacks=[st_cb])
101
  st.session_state.messages.append({'role': 'assistant', "content": response})
102
+
103
  st.write('### Response:')
104
  st.success(response)
105
  else:
106
+ st.warning("Please enter your question")