Promotingai commited on
Commit
141b771
·
verified ·
1 Parent(s): 0b1f389

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os # Import pour accéder aux variables d'environnement
3
+
4
+ from langchain.agents import initialize_agent, AgentType
5
+ from langchain.callbacks import StreamlitCallbackHandler
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.tools import DuckDuckGoSearchRun
8
+
9
+ # Récupérer la clé API de l'environnement
10
+ openai_api_key = os.getenv("OPENAI_API_KEY")
11
+
12
+ st.title("🔎 LangChain - Chat with search")
13
+
14
+ """
15
+ In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
16
+ Try more LangChain 🤝 Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
17
+ """
18
+
19
+ if "messages" not in st.session_state:
20
+ st.session_state["messages"] = [
21
+ {"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
22
+ ]
23
+
24
+ for msg in st.session_state.messages:
25
+ st.chat_message(msg["role"]).write(msg["content"])
26
+
27
+ if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"):
28
+ st.session_state.messages.append({"role": "user", "content": prompt})
29
+ st.chat_message("user").write(prompt)
30
+
31
+ if not openai_api_key:
32
+ st.error("Please set the OPENAI_API_KEY environment variable.")
33
+ st.stop()
34
+
35
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, streaming=True)
36
+ search = DuckDuckGoSearchRun(name="Search")
37
+ search_agent = initialize_agent(
38
+ [search], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True
39
+ )
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)