File size: 1,773 Bytes
712b371
75dd0b0
b18a03d
 
75dd0b0
a8b335b
995fa62
a8b335b
b18a03d
 
995fa62
a8b335b
 
b18a03d
 
 
 
 
9be7406
a8b335b
 
 
712b371
a8b335b
 
 
 
 
712b371
 
a8b335b
b18a03d
712b371
b18a03d
a8b335b
712b371
a8b335b
 
 
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
from openai import OpenAI
import streamlit as st
import os
import json  # Import the json module for parsing JSON files

# Fetch the API key from an environment variable
openai_api_key = os.getenv("OPENAI_API_KEY")

# Select model using a dropdown
model_choice = st.selectbox('Choose a model:', ['gpt-4-turbo','gpt-3.5-turbo', 'gpt-3.5-turbo-0125','gpt-3.5-turbo-1106','gpt-3.5-turbo-0613','gpt-3.5-turbo-16k-0613','gpt-3.5-turbo-16k','gpt-4-turbo-2024-04-09','gpt-4-turbo-preview', 'gpt-4-0125-preview','gpt-4-1106-preview','gpt-4-0613'])

# Initialize session state for storing messages if it doesn't already exist
if "messages" not in st.session_state:
    # Preload conversation history from a JSON file or define here
    try:
        with open("conversation_history.json", "r") as file:
            conversation_history = json.load(file)
            st.session_state["messages"] = conversation_history["history"]
   

# Input for new prompts
prompt = st.chat_input("Enter your question:")
if prompt:
    if not openai_api_key:
        st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
        st.stop()

    # Append the new user message to session state
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Use a spinner to indicate that the model is generating a response
    with st.spinner('PromptingAI is Thinking...'):
        client = OpenAI(api_key=openai_api_key)
        response = client.chat.completions.create(model=model_choice, messages=st.session_state.messages)
        msg = response.choices[0].message.content

    # Append and display the assistant's response
    st.session_state.messages.append({"role": "assistant", "content": msg})
    st.chat_message("assistant").write(msg)