Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +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.agents import initialize_agent,AgentType
|
| 6 |
from langchain.callbacks import StreamlitCallbackHandler
|
| 7 |
import os
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
-
## Code
|
| 10 |
-
## COMMIT
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
arxiv=ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
| 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 |
-
|
| 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":"
|
| 35 |
]
|
| 36 |
|
|
|
|
| 37 |
for msg in st.session_state.messages:
|
| 38 |
-
st.chat_message(msg["role"]).write(msg[
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
st.chat_message("user").write(prompt)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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 |
import os
|
| 8 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load environment variables
|
| 11 |
+
load_dotenv()
|
|
|
|
| 12 |
|
| 13 |
+
## Arxiv and Wikipedia Tools
|
| 14 |
+
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 15 |
+
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 16 |
|
| 17 |
+
wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 18 |
+
wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
|
| 19 |
|
| 20 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 21 |
|
| 22 |
+
# Streamlit UI
|
| 23 |
st.title("π LangChain - Chat with search")
|
| 24 |
+
st.markdown("""
|
| 25 |
In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
|
| 26 |
Try more LangChain π€ Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
|
| 27 |
+
""")
|
| 28 |
|
| 29 |
+
# Sidebar for settings
|
| 30 |
st.sidebar.title("Settings")
|
| 31 |
+
api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
|
| 32 |
|
| 33 |
+
# Initialize session state for messages if not already
|
| 34 |
if "messages" not in st.session_state:
|
| 35 |
+
st.session_state["messages"] = [
|
| 36 |
+
{"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
|
| 37 |
]
|
| 38 |
|
| 39 |
+
# Display previous chat messages
|
| 40 |
for msg in st.session_state.messages:
|
| 41 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 42 |
|
| 43 |
+
# Capture user input from chat
|
| 44 |
+
if prompt := st.chat_input(placeholder="What is machine learning?"):
|
| 45 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 46 |
st.chat_message("user").write(prompt)
|
| 47 |
|
| 48 |
+
# Initialize the language model
|
| 49 |
+
if api_key: # Ensure API key is entered
|
| 50 |
+
llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
|
| 51 |
+
tools = [search, arxiv, wiki]
|
| 52 |
|
| 53 |
+
# Initialize the search agent with tools and the language model
|
| 54 |
+
search_agent = initialize_agent(
|
| 55 |
+
tools,
|
| 56 |
+
llm,
|
| 57 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 58 |
+
handle_parsing_errors=True # Enable error handling for parsing issues
|
| 59 |
+
)
|
| 60 |
|
| 61 |
+
with st.chat_message("assistant"):
|
| 62 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
# Try running the agent
|
| 66 |
+
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 67 |
+
st.session_state.messages.append({'role': 'assistant', "content": response})
|
| 68 |
+
st.write(response)
|
| 69 |
+
|
| 70 |
+
except ValueError as e:
|
| 71 |
+
# Catch and display output parsing errors
|
| 72 |
+
st.error(f"An error occurred while parsing the LLM's output: {str(e)}")
|
| 73 |
+
st.session_state.messages.append({'role': 'assistant', "content": "Sorry, I encountered an error processing your request."})
|
| 74 |
+
st.write("Sorry, I encountered an error processing your request.")
|
| 75 |
+
else:
|
| 76 |
+
st.error("Please enter your Groq API Key in the settings.")
|