from openai import OpenAI import streamlit as st st.write("version 1.0.0") st.markdown(''' Hey there, I wrote a book titled "**:violet[Hands-On Retrieval-Augmented Generation]**," which includes this interactive example. If you\'re interested in exploring more practical applications of Retrieval-Augmented Generation, be sure to [check it out](https://retrieval-augmented-generation.com)! ''') st.header('Conversational Agent', divider='rainbow') client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) if "openai_model" not in st.session_state: st.session_state["openai_model"] = "gpt-3.5-turbo" if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if prompt := st.chat_input("What is up?"): st.session_state.messages = [] st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt) with st.chat_message("assistant"): stream = client.chat.completions.create( model=st.session_state["openai_model"], messages=[{"role": "user", "content": prompt}], stream=True, ) response = st.write_stream(stream) st.session_state.messages.append({"role": "assistant", "content": response})