File size: 1,226 Bytes
6ca0fd8 6fb0c3d 6ca0fd8 56bab14 6ca0fd8 56bab14 6ca0fd8 56bab14 6ca0fd8 56bab14 6ca0fd8 91dfcdf 56bab14 91dfcdf 56bab14 91dfcdf 56bab14 91dfcdf 56bab14 |
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 |
import openai
import os
import streamlit as st
from streamlit_chat import message
import pandas as pd
from streamlit_chat import message as st_message # Ensure streamlit_chat is installed
# Ensure you have the OpenAI API key set in your environment variables
openai.api_key = st.secrets["OPENAI_API_KEY"]
# Function to generate a response from OpenAI's GPT model using the updated API
def ask_openai(prompt):
try:
response = openai.Completion.create(
model="text-davinci-003", # Example model, adjust as needed
prompt=prompt,
temperature=0.7,
max_tokens=150,
n=1,
stop=None,
)
return response.choices[0].text.strip()
except Exception as e:
st.error(f"Error in generating response: {e}")
return "I encountered an error. Please try again."
# UI for input and displaying the chat
user_input = st.text_input("Ask me anything:", key="chat_input")
if user_input:
# Display user input
with st.chat_message("user"):
st.write(user_input)
# Generate and display AI response
ai_response = ask_openai(user_input)
with st.chat_message("assistant"):
st.write(ai_response)
|