NazaninMn commited on
Commit
43fdeb3
Β·
unverified Β·
1 Parent(s): 638847a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -32
app.py CHANGED
@@ -1,58 +1,76 @@
 
 
 
 
1
  import streamlit as st
2
  from langchain_groq import ChatGroq
3
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
4
  from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
5
- from langchain_core.prompts import PromptTemplate
6
  from langchain.agents import initialize_agent, AgentType
7
  from langchain.callbacks import StreamlitCallbackHandler
8
  from dotenv import load_dotenv
9
- import os
10
 
 
 
 
11
  load_dotenv()
12
 
13
- st.title("πŸ”Ž LangChain - Chat with Search")
 
14
  st.sidebar.title("Settings")
 
 
15
  api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
16
 
17
- # ---- Tools ----
18
- arxiv = ArxivQueryRun(api_wrapper=ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200))
19
- wiki = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200))
 
 
 
 
 
 
20
  search = DuckDuckGoSearchRun(name="Search")
21
  tools = [search, arxiv, wiki]
22
 
23
- # ---- Session ----
 
 
24
  if "messages" not in st.session_state:
25
  st.session_state["messages"] = [
26
- {"role": "assistant",
27
- "content": "Hi, I'm a chatbot that can search Arxiv, Wikipedia, and the web!"}
28
  ]
29
 
 
30
  for msg in st.session_state.messages:
31
  st.chat_message(msg["role"]).write(msg["content"])
32
 
33
- # ---- Chat ----
34
- if user_input := st.chat_input("Ask your question..."):
 
 
35
  st.session_state.messages.append({"role": "user", "content": user_input})
36
  st.chat_message("user").write(user_input)
37
 
38
- llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
39
-
40
- prompt = PromptTemplate.from_template(
41
- "You are a helpful assistant with access to the following tools: search, arxiv, and wikipedia.\n"
42
- "Use them if needed before answering.\n\nQuestion: {input}"
43
- )
44
-
45
- # βœ… Uses the stable zero-shot ReAct agent
46
- agent = initialize_agent(
47
- tools=tools,
48
- llm=llm,
49
- agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
50
- verbose=True,
51
- handle_parsing_errors=True,
52
- )
53
-
54
- with st.chat_message("assistant"):
55
- st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
56
- response = agent.run(user_input, callbacks=[st_cb])
57
- st.session_state["messages"].append({"role": "assistant", "content": response})
58
- st.write(response)
 
1
+ # ------------------------------------------------------------
2
+ # πŸ”Ž LangChain + Groq + Streamlit (Stable Version for Hugging Face)
3
+ # ------------------------------------------------------------
4
+
5
  import streamlit as st
6
  from langchain_groq import ChatGroq
7
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
8
  from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
 
9
  from langchain.agents import initialize_agent, AgentType
10
  from langchain.callbacks import StreamlitCallbackHandler
11
  from dotenv import load_dotenv
 
12
 
13
+ # ------------------------------------------------------------
14
+ # 🌍 Load environment variables
15
+ # ------------------------------------------------------------
16
  load_dotenv()
17
 
18
+ st.set_page_config(page_title="Search Engine LLM", page_icon="πŸ”Ž")
19
+ st.title("πŸ”Ž LangChain - Chat with Search (Groq + Streamlit)")
20
  st.sidebar.title("Settings")
21
+
22
+ # API key input
23
  api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
24
 
25
+ # ------------------------------------------------------------
26
+ # 🧰 Initialize Tools
27
+ # ------------------------------------------------------------
28
+ arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
29
+ arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
30
+
31
+ wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
32
+ wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
33
+
34
  search = DuckDuckGoSearchRun(name="Search")
35
  tools = [search, arxiv, wiki]
36
 
37
+ # ------------------------------------------------------------
38
+ # πŸ’¬ Session state
39
+ # ------------------------------------------------------------
40
  if "messages" not in st.session_state:
41
  st.session_state["messages"] = [
42
+ {"role": "assistant", "content": "Hi πŸ‘‹ I'm a chatbot that can search Arxiv, Wikipedia, and the web!"}
 
43
  ]
44
 
45
+ # Display previous chat history
46
  for msg in st.session_state.messages:
47
  st.chat_message(msg["role"]).write(msg["content"])
48
 
49
+ # ------------------------------------------------------------
50
+ # πŸ’‘ User Input
51
+ # ------------------------------------------------------------
52
+ if user_input := st.chat_input("Ask me anything!"):
53
  st.session_state.messages.append({"role": "user", "content": user_input})
54
  st.chat_message("user").write(user_input)
55
 
56
+ if not api_key:
57
+ st.error("Please enter your Groq API key in the sidebar before chatting.")
58
+ else:
59
+ # Initialize Groq LLM
60
+ llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
61
+
62
+ # Create the agent (ReAct-style)
63
+ agent = initialize_agent(
64
+ tools,
65
+ llm,
66
+ agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
67
+ handle_parsing_errors=True,
68
+ verbose=True
69
+ )
70
+
71
+ # Stream response
72
+ with st.chat_message("assistant"):
73
+ st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
74
+ response = agent.run(user_input, callbacks=[st_cb])
75
+ st.session_state.messages.append({"role": "assistant", "content": response})
76
+ st.write(response)