Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,20 +3,64 @@ import pandas as pd
|
|
| 3 |
import numpy as np
|
| 4 |
from io import StringIO
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from io import StringIO
|
| 5 |
|
| 6 |
+
from langchain.agents import initialize_agent, AgentType
|
| 7 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
| 8 |
+
from langchain.chat_models import ChatOpenAI
|
| 9 |
+
from langchain.tools import DuckDuckGoSearchRun
|
| 10 |
+
|
| 11 |
+
# # Your CSV string
|
| 12 |
+
# csv_string = """question,answer
|
| 13 |
+
# What is your name?,My name is John
|
| 14 |
+
# How old are you?,I am 25 years old
|
| 15 |
+
# Where do you live?,I live in New York
|
| 16 |
+
# What is your favorite color?,My favorite color is blue
|
| 17 |
+
# What is your favorite food?,I love pizza"""
|
| 18 |
+
|
| 19 |
+
# # Convert the string to a StringIO object
|
| 20 |
+
# csv_data = StringIO(csv_string)
|
| 21 |
+
|
| 22 |
+
# # Read the CSV data into a pandas DataFrame
|
| 23 |
+
# df = pd.read_csv(csv_data)
|
| 24 |
+
# st.title("Hello Streamlit")
|
| 25 |
+
|
| 26 |
+
# st.write(df)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
with st.sidebar:
|
| 31 |
+
openai_api_key = st.text_input("OpenAI API Key", key="langchain_search_api_key_openai", type="password")
|
| 32 |
+
"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
|
| 33 |
+
"[View the source code](https://github.com/streamlit/llm-examples/blob/main/pages/2_Chat_with_search.py)"
|
| 34 |
+
"[](https://codespaces.new/streamlit/llm-examples?quickstart=1)"
|
| 35 |
+
|
| 36 |
+
st.title("🔎 LangChain - Chat with search")
|
| 37 |
+
|
| 38 |
+
"""
|
| 39 |
+
In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
|
| 40 |
+
Try more LangChain 🤝 Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
if "messages" not in st.session_state:
|
| 44 |
+
st.session_state["messages"] = [
|
| 45 |
+
{"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
for msg in st.session_state.messages:
|
| 49 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 50 |
+
|
| 51 |
+
if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"):
|
| 52 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 53 |
+
st.chat_message("user").write(prompt)
|
| 54 |
+
|
| 55 |
+
if not openai_api_key:
|
| 56 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 57 |
+
st.stop()
|
| 58 |
+
|
| 59 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, streaming=True)
|
| 60 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 61 |
+
search_agent = initialize_agent([search], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
|
| 62 |
+
with st.chat_message("assistant"):
|
| 63 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 64 |
+
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 65 |
+
st.session_state.messages.append({"role": "assistant
|
| 66 |
|