cjfla784 commited on
Commit
f67a802
·
verified ·
1 Parent(s): 8f8b4c6

update with oai chatbot

Browse files
Files changed (1) hide show
  1. app.py +35 -2
app.py CHANGED
@@ -1,4 +1,37 @@
1
  import streamlit as st
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")