michaelmc1618 commited on
Commit
0f62f38
·
verified ·
1 Parent(s): d7ca4a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -2
app.py CHANGED
@@ -1,4 +1,44 @@
1
  import streamlit as st
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ with st.sidebar:
9
+ openai_api_key = st.text_input("OpenAI API Key", key="langchain_search_api_key_openai", type="password")
10
+ "[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
11
+ "[View the source code](https://github.com/streamlit/llm-examples/blob/main/pages/2_Chat_with_search.py)"
12
+ "[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/streamlit/llm-examples?quickstart=1)"
13
+
14
+ st.title("🔎 LangChain - Chat with search")
15
+
16
+ """
17
+ In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
18
+ Try more LangChain 🤝 Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
19
+ """
20
+
21
+ if "messages" not in st.session_state:
22
+ st.session_state["messages"] = [
23
+ {"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
24
+ ]
25
+
26
+ for msg in st.session_state.messages:
27
+ st.chat_message(msg["role"]).write(msg["content"])
28
+
29
+ if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"):
30
+ st.session_state.messages.append({"role": "user", "content": prompt})
31
+ st.chat_message("user").write(prompt)
32
+
33
+ if not openai_api_key:
34
+ st.info("Please add your OpenAI API key to continue.")
35
+ st.stop()
36
+
37
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, streaming=True)
38
+ search = DuckDuckGoSearchRun(name="Search")
39
+ search_agent = initialize_agent([search], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
40
+ with st.chat_message("assistant"):
41
+ st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
42
+ response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
43
+ st.session_state.messages.append({"role": "assistant", "content": response})
44
+ st.write(response)