|
|
import openai |
|
|
import streamlit as st |
|
|
|
|
|
|
|
|
with st.sidebar: |
|
|
openai_api_key = st.text_input("Clé API OpenAI", key="chatbot_api_key", type="password") |
|
|
st.markdown("[Obtenez une clé API OpenAI](https://platform.openai.com/account/api-keys)") |
|
|
st.markdown("[Voir le code source](https://github.com/streamlit/llm-examples/blob/main/Chatbot.py)") |
|
|
st.markdown("[](https://codespaces.new/streamlit/llm-examples?quickstart=1)") |
|
|
|
|
|
|
|
|
st.title("💬 Chatbot") |
|
|
st.caption("🚀 Un chatbot Streamlit propulsé par OpenAI") |
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state["messages"] = [{"role": "assistant", "content": "Comment puis-je vous aider ?"}] |
|
|
|
|
|
|
|
|
for msg in st.session_state.messages: |
|
|
st.chat_message(msg["role"]).write(msg["content"]) |
|
|
|
|
|
|
|
|
if prompt := st.chat_input(): |
|
|
if not openai_api_key: |
|
|
st.info("Veuillez ajouter votre clé API OpenAI pour continuer.") |
|
|
st.stop() |
|
|
|
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
st.chat_message("user").write(prompt) |
|
|
|
|
|
|
|
|
openai.api_key = openai_api_key |
|
|
response = openai.ChatCompletion.create( |
|
|
model="gpt-3.5-turbo", |
|
|
messages=st.session_state.messages |
|
|
) |
|
|
|
|
|
|
|
|
msg = response.choices[0].message["content"] |
|
|
st.session_state.messages.append({"role": "assistant", "content": msg}) |
|
|
st.chat_message("assistant").write(msg) |
|
|
|