Spaces:
Sleeping
Sleeping
File size: 3,345 Bytes
da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 da9b74a c0335d4 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import streamlit as st
from langchain_groq import ChatGroq
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
from langchain.agents import initialize_agent, AgentType
from langchain.callbacks import StreamlitCallbackHandler
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
## Arxiv and Wikipedia 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)
search = DuckDuckGoSearchRun(name="Search")
# Streamlit UI
st.title("π LangChain - Chat with search")
st.markdown("""
In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
Try more LangChain π€ Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
""")
# Sidebar for settings
st.sidebar.title("Settings")
api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
# Initialize session state for messages if not already
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?"}
]
# Display previous chat messages
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])
# Capture user input from chat
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)
# Initialize the language model
if api_key: # Ensure API key is entered
llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
tools = [search, arxiv, wiki]
# Initialize the search agent with tools and the language model
search_agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
handle_parsing_errors=True # Enable error handling for parsing issues
)
with st.chat_message("assistant"):
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
try:
# Try running the agent
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
st.session_state.messages.append({'role': 'assistant', "content": response})
st.write(response)
except ValueError as e:
# Catch and display output parsing errors
st.error(f"An error occurred while parsing the LLM's output: {str(e)}")
st.session_state.messages.append({'role': 'assistant', "content": "Sorry, I encountered an error processing your request."})
st.write("Sorry, I encountered an error processing your request.")
else:
st.error("Please enter your Groq API Key in the settings.")
|