Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
+
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)
|
|
|