File size: 2,066 Bytes
8a577a0
 
d0df26d
19a23b5
d0df26d
8a577a0
 
d0df26d
 
 
8a577a0
d0df26d
 
8a577a0
 
d0df26d
8a577a0
 
 
d0df26d
8a577a0
 
d0df26d
 
8a577a0
 
 
 
 
d0df26d
 
8a577a0
 
d0df26d
 
8a577a0
d0df26d
 
 
8a577a0
 
d0df26d
 
8a577a0
d0df26d
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import streamlit as st
from langchain_groq import ChatGroq
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun
from langchain.agents import initialize_agent, AgentType
from langchain.callbacks import StreamlitCallbackHandler

## Initialize Tools
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)

wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)


## Streamlit UI
st.title("πŸ”Ž LangChain - Chat with search")

st.sidebar.title("Settings")
api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")

if "messages" not in st.session_state:
    st.session_state["messages"] = [
        {"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
    ]

for msg in st.session_state.messages:
    st.chat_message(msg["role"]).write(msg['content'])

if prompt := st.chat_input(placeholder="What is machine learning?"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    st.chat_message("user").write(prompt)

    llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
    tools = [search, arxiv, wiki]

    search_agent = initialize_agent(
        tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True
    )

    with st.chat_message("assistant"):
        st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
        user_query = st.session_state.messages[-1]["content"]

        try:
            response = search_agent.run(user_query, callbacks=[st_cb])
            response = response if response else "Sorry, I couldn't find any relevant information."
        except Exception as e:
            response = f"An error occurred: {str(e)}"

        st.session_state.messages.append({'role': 'assistant', "content": response})
        st.write(response)