Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,38 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
import anthropic
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
| 10 |
|
| 11 |
-
client = anthropic.Anthropic(
|
| 12 |
-
api_key=api_key,
|
| 13 |
-
)
|
| 14 |
-
if "ai_model" not in st.session_state:
|
| 15 |
-
st.session_state["ai_model"] = ai_model
|
| 16 |
|
| 17 |
if "messages" not in st.session_state:
|
| 18 |
-
st.session_state
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
st.markdown(message["content"])
|
| 23 |
|
| 24 |
-
if prompt := st.chat_input("What is
|
| 25 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with st.chat_message("assistant"):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
|
| 35 |
-
model=st.session_state["ai_model"],
|
| 36 |
-
) as stream:
|
| 37 |
-
for text in stream.text_stream:
|
| 38 |
-
full_response += str(text) if text is not None else ""
|
| 39 |
-
message_placeholder.markdown(full_response + "▌")
|
| 40 |
-
message_placeholder.markdown(full_response)
|
| 41 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
from langchain.agents import initialize_agent, AgentType
|
| 4 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
| 5 |
+
from langchain.chat_models import ChatOpenAI
|
| 6 |
+
from langchain.tools import DuckDuckGoSearchRun
|
| 7 |
+
|
| 8 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
+
st.title("🔎 PromptingAI.io - Chat with search")
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
if "messages" not in st.session_state:
|
| 14 |
+
st.session_state["messages"] = [
|
| 15 |
+
{"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
|
| 16 |
+
]
|
| 17 |
|
| 18 |
+
for msg in st.session_state.messages:
|
| 19 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
|
|
|
| 20 |
|
| 21 |
+
if prompt := st.chat_input(placeholder="What is PromptingAI.io"):
|
| 22 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 23 |
+
st.chat_message("user").write(prompt)
|
| 24 |
+
|
| 25 |
+
if not openai_api_key:
|
| 26 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 27 |
+
st.stop()
|
| 28 |
|
| 29 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, streaming=True)
|
| 30 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 31 |
+
search_agent = initialize_agent(
|
| 32 |
+
[search], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True
|
| 33 |
+
)
|
| 34 |
with st.chat_message("assistant"):
|
| 35 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 36 |
+
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 37 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 38 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|