Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,19 @@
|
|
| 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
|
|
|
|
| 6 |
from langchain.callbacks import StreamlitCallbackHandler
|
| 7 |
-
import os
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
-
## Code
|
| 10 |
-
####
|
| 11 |
|
| 12 |
-
## Arxiv and
|
| 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 |
"""
|
|
@@ -27,27 +23,95 @@ Try more LangChain 🤝 Streamlit Agent examples at [github.com/langchain-ai/str
|
|
| 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":"
|
| 35 |
]
|
| 36 |
|
|
|
|
| 37 |
for msg in st.session_state.messages:
|
| 38 |
st.chat_message(msg["role"]).write(msg['content'])
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
st.chat_message("user").write(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
with st.chat_message("assistant"):
|
| 50 |
-
st_cb=StreamlitCallbackHandler(st.container(),expand_new_thoughts=False)
|
| 51 |
-
|
| 52 |
-
|
| 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 create_react_agent, AgentExecutor
|
| 6 |
+
from langchain import hub
|
| 7 |
from langchain.callbacks import StreamlitCallbackHandler
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
## Arxiv and Wikipedia Tools
|
| 10 |
+
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 11 |
+
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 12 |
|
| 13 |
+
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 14 |
+
wiki = WikipediaQueryRun(api_wrapper=api_wrapper)
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 17 |
|
| 18 |
st.title("🔎 LangChain - Chat with search")
|
| 19 |
"""
|
|
|
|
| 23 |
|
| 24 |
## Sidebar for settings
|
| 25 |
st.sidebar.title("Settings")
|
| 26 |
+
api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
|
| 27 |
+
|
| 28 |
+
# Add clear chat button
|
| 29 |
+
if st.sidebar.button("Clear Chat History"):
|
| 30 |
+
st.session_state.messages = [
|
| 31 |
+
{"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
|
| 32 |
+
]
|
| 33 |
+
st.rerun()
|
| 34 |
|
| 35 |
+
# Initialize session state
|
| 36 |
if "messages" not in st.session_state:
|
| 37 |
+
st.session_state["messages"] = [
|
| 38 |
+
{"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
|
| 39 |
]
|
| 40 |
|
| 41 |
+
# Display chat messages
|
| 42 |
for msg in st.session_state.messages:
|
| 43 |
st.chat_message(msg["role"]).write(msg['content'])
|
| 44 |
|
| 45 |
+
# Chat input
|
| 46 |
+
if prompt := st.chat_input(placeholder="What is machine learning?"):
|
| 47 |
+
# Check if API key is provided
|
| 48 |
+
if not api_key:
|
| 49 |
+
st.error("Please enter your Groq API Key in the sidebar.")
|
| 50 |
+
st.stop()
|
| 51 |
+
|
| 52 |
+
# Add user message to chat
|
| 53 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 54 |
st.chat_message("user").write(prompt)
|
| 55 |
+
|
| 56 |
+
# Initialize LLM and tools
|
| 57 |
+
llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
|
| 58 |
+
tools = [search, arxiv, wiki]
|
| 59 |
+
|
| 60 |
+
# Get the react prompt template
|
| 61 |
+
try:
|
| 62 |
+
react_prompt = hub.pull("hwchase17/react")
|
| 63 |
+
except:
|
| 64 |
+
# Fallback prompt if hub is not accessible
|
| 65 |
+
from langchain.prompts import PromptTemplate
|
| 66 |
+
template = """Answer the following questions as best you can. You have access to the following tools:
|
| 67 |
+
|
| 68 |
+
{tools}
|
| 69 |
+
|
| 70 |
+
Use the following format:
|
| 71 |
|
| 72 |
+
Question: the input question you must answer
|
| 73 |
+
Thought: you should always think about what to do
|
| 74 |
+
Action: the action to take, should be one of [{tool_names}]
|
| 75 |
+
Action Input: the input to the action
|
| 76 |
+
Observation: the result of the action
|
| 77 |
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 78 |
+
Thought: I now know the final answer
|
| 79 |
+
Final Answer: the final answer to the original input question
|
| 80 |
|
| 81 |
+
Begin!
|
| 82 |
|
| 83 |
+
Question: {input}
|
| 84 |
+
Thought:{agent_scratchpad}"""
|
| 85 |
+
|
| 86 |
+
react_prompt = PromptTemplate(
|
| 87 |
+
template=template,
|
| 88 |
+
input_variables=["input", "tools", "tool_names", "agent_scratchpad"]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Create agent
|
| 92 |
+
agent = create_react_agent(llm, tools, react_prompt)
|
| 93 |
+
agent_executor = AgentExecutor(
|
| 94 |
+
agent=agent,
|
| 95 |
+
tools=tools,
|
| 96 |
+
verbose=True,
|
| 97 |
+
handle_parsing_errors=True,
|
| 98 |
+
max_iterations=5
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Generate response
|
| 102 |
with st.chat_message("assistant"):
|
| 103 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 104 |
+
try:
|
| 105 |
+
response = agent_executor.invoke(
|
| 106 |
+
{"input": prompt},
|
| 107 |
+
{"callbacks": [st_cb]}
|
| 108 |
+
)
|
| 109 |
+
answer = response['output']
|
| 110 |
+
st.session_state.messages.append({'role': 'assistant', "content": answer})
|
| 111 |
+
st.write(answer)
|
| 112 |
+
except Exception as e:
|
| 113 |
+
st.error(f"An error occurred: {str(e)}")
|
| 114 |
+
st.session_state.messages.append({
|
| 115 |
+
'role': 'assistant',
|
| 116 |
+
"content": f"Sorry, I encountered an error: {str(e)}"
|
| 117 |
+
})
|