RajaVardhan commited on
Commit
d0df26d
Β·
1 Parent(s): 4d68b1e

app.py updated

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -1,54 +1,53 @@
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.agents import initialize_agent,AgentType
6
  from langchain.callbacks import StreamlitCallbackHandler
7
- import os
8
- from dotenv import load_dotenv
9
- ## Code
10
- ####
11
 
12
- ## Arxiv and wikipedia Tools
13
- arxiv_wrapper=ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
14
- arxiv=ArxivQueryRun(api_wrapper=arxiv_wrapper)
15
 
16
- api_wrapper=WikipediaAPIWrapper(top_k_results=1,doc_content_chars_max=200)
17
- wiki=WikipediaQueryRun(api_wrapper=api_wrapper)
18
-
19
- search=DuckDuckGoSearchRun(name="Search")
20
 
 
21
 
 
22
  st.title("πŸ”Ž LangChain - Chat with search")
23
- """
24
- In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
25
- Try more LangChain 🀝 Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
26
- """
27
 
28
- ## Sidebar for settings
29
  st.sidebar.title("Settings")
30
- api_key=st.sidebar.text_input("Enter your Groq API Key:",type="password")
31
 
32
  if "messages" not in st.session_state:
33
- st.session_state["messages"]=[
34
- {"role":"assisstant","content":"Hi,I'm a chatbot who can search the web. How can I help you?"}
35
  ]
36
 
37
  for msg in st.session_state.messages:
38
  st.chat_message(msg["role"]).write(msg['content'])
39
 
40
- if prompt:=st.chat_input(placeholder="What is machine learning?"):
41
- st.session_state.messages.append({"role":"user","content":prompt})
42
  st.chat_message("user").write(prompt)
43
 
44
- llm=ChatGroq(groq_api_key=api_key,model_name="Llama3-8b-8192",streaming=True)
45
- tools=[search,arxiv,wiki]
46
 
47
- search_agent=initialize_agent(tools,llm,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,handling_parsing_errors=True)
 
 
48
 
49
  with st.chat_message("assistant"):
50
- st_cb=StreamlitCallbackHandler(st.container(),expand_new_thoughts=False)
51
- response=search_agent.run(st.session_state.messages,callbacks=[st_cb])
52
- st.session_state.messages.append({'role':'assistant',"content":response})
53
- st.write(response)
54
 
 
 
 
 
 
 
 
 
 
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.agents import initialize_agent, AgentType
6
  from langchain.callbacks import StreamlitCallbackHandler
 
 
 
 
7
 
8
+ ## Initialize Tools
9
+ arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
10
+ arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
11
 
12
+ wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
13
+ wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
 
 
14
 
15
+ search = DuckDuckGoSearchRun(name="Search")
16
 
17
+ ## Streamlit UI
18
  st.title("πŸ”Ž LangChain - Chat with search")
 
 
 
 
19
 
 
20
  st.sidebar.title("Settings")
21
+ api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
22
 
23
  if "messages" not in st.session_state:
24
+ st.session_state["messages"] = [
25
+ {"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
26
  ]
27
 
28
  for msg in st.session_state.messages:
29
  st.chat_message(msg["role"]).write(msg['content'])
30
 
31
+ if prompt := st.chat_input(placeholder="What is machine learning?"):
32
+ st.session_state.messages.append({"role": "user", "content": prompt})
33
  st.chat_message("user").write(prompt)
34
 
35
+ llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
36
+ tools = [search, arxiv, wiki]
37
 
38
+ search_agent = initialize_agent(
39
+ tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True
40
+ )
41
 
42
  with st.chat_message("assistant"):
43
+ st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
44
+ user_query = st.session_state.messages[-1]["content"]
 
 
45
 
46
+ try:
47
+ response = search_agent.run(user_query, callbacks=[st_cb])
48
+ response = response if response else "Sorry, I couldn't find any relevant information."
49
+ except Exception as e:
50
+ response = f"An error occurred: {str(e)}"
51
+
52
+ st.session_state.messages.append({'role': 'assistant', "content": response})
53
+ st.write(response)