|
import streamlit as st |
|
from openai import OpenAI |
|
|
|
|
|
st.title("Chat with OpenAI") |
|
|
|
|
|
api_key = st.text_input("Enter your OpenAI API key:", type="password") |
|
client = OpenAI(api_key=api_key) |
|
|
|
if api_key: |
|
|
|
try: |
|
|
|
user_input = st.text_input("Ask a question") |
|
|
|
if st.button("Submit"): |
|
if user_input: |
|
|
|
response = client.chat.completions.create( |
|
model="gpt-3.5-turbo", |
|
|
|
messages=[ |
|
{"role": "system", "content": "You are an AI that takes instructions from a human and produces an answer. Be concise in your output."}, |
|
{"role": "user", "content": f"{user_input}"} |
|
] |
|
) |
|
answer = response.choices[0].message.content |
|
st.write("AI Response:") |
|
st.write(answer) |
|
else: |
|
st.write("Please enter a question.") |
|
except Exception as e: |
|
st.error(f"Error: {e}") |
|
else: |
|
if not api_key: |
|
st.write("Please enter your OpenAI API key.") |