File size: 2,786 Bytes
e4e51bf 25522b7 e4e51bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import os
import openai
import streamlit as st
from utils import load_base_prompt
import configparser
# Create a ConfigParser object
config = configparser.ConfigParser()
# Read the config.ini file
config.read('config.ini')
# Access the password
open_ai_key = config.get('access', 'openai_key')
print(f"Openai key: {open_ai_key}")
# hack from: https://discuss.streamlit.io/t/remove-ui-top-bar-forehead/22071/3
hide_streamlit_style = """
<style>
#root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 1.3rem;}
</style>
"""
# remove some padding bottom
st.markdown(
"""
<style>
.stChatFloatingInputContainer {padding-bottom: 1rem;}
</style>
""",
unsafe_allow_html=True,
)
# remove some padding in between
# st.markdown(
# """
# <style>
# .block-container.css-1y4p8pa.ea3mdgi4 {padding-bottom: .3rem;}
# </style>
# """,
# unsafe_allow_html=True,
# )
# st.markdown(
# """
# <style>
# .block-container.st-emotion-cache-1y4p8pa.ea3mdgi4 {padding-bottom: .5rem;}
# </style>
# """,
# unsafe_allow_html=True,
# )
st.title("Forher AI Genie")
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
#openai.api_key = os.environ.get("open_ai_key")
openai.api_key =open_ai_key
base_prompt = load_base_prompt()
if "openai_model" not in st.session_state:
#st.session_state["openai_model"] = "gpt-3.5-turbo"
#st.session_state["openai_model"] = "gpt-4"
st.session_state["openai_model"] = "gpt-4-1106-preview"
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
avatar = "π€" if message["role"] == "user" else "π€"
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
if prompt := st.chat_input("Ask your question here", ):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar="π§βπ»"):
st.markdown(prompt)
with st.chat_message("assistant", avatar="π€"):
message_placeholder = st.empty()
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=
[{"role": "system", "content": base_prompt}] +
[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
):
full_response += response.choices[0].delta.get("content", "")
message_placeholder.markdown(full_response + "β")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
|