Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
# Streamlit app
|
| 5 |
+
st.title("Chat with OpenAI")
|
| 6 |
+
|
| 7 |
+
# Get OpenAI API key from user
|
| 8 |
+
api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
| 9 |
+
client = OpenAI(api_key=api_key)
|
| 10 |
+
|
| 11 |
+
if api_key:
|
| 12 |
+
# Configure OpenAI API key
|
| 13 |
+
try:
|
| 14 |
+
# Get user input
|
| 15 |
+
user_input = st.text_input("Ask a question")
|
| 16 |
+
|
| 17 |
+
if st.button("Submit"):
|
| 18 |
+
if user_input:
|
| 19 |
+
# Call OpenAI API
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model="gpt-3.5-turbo",
|
| 22 |
+
# response_format={ "type": "json_object" },
|
| 23 |
+
messages=[
|
| 24 |
+
{"role": "system", "content": "You are an AI that takes instructions from a human and produces an answer. Be concise in your output."},
|
| 25 |
+
{"role": "user", "content": f"{user_input}"}
|
| 26 |
+
]
|
| 27 |
+
)
|
| 28 |
+
answer = response.choices[0].message.content
|
| 29 |
+
st.write("AI Response:")
|
| 30 |
+
st.write(answer)
|
| 31 |
+
else:
|
| 32 |
+
st.write("Please enter a question.")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.error(f"Error: {e}")
|
| 35 |
+
else:
|
| 36 |
+
if not api_key:
|
| 37 |
+
st.write("Please enter your OpenAI API key.")
|