cool-radio commited on
Commit
b959451
1 Parent(s): 71b563a

Update app.py

Browse files

openai.api_key

Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -5,16 +5,22 @@ import os
5
  # Setup and initialization
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
- def chatbot_response(prompt):
9
- response = openai.ChatCompletion.create(
10
- model="gpt-3.5-turbo",
11
- messages=[
12
- {"role": "system", "content": "You are a helpful assistant."},
13
- {"role": "user", "content": prompt}
14
- ],
15
- max_tokens=150
16
- )
17
- return response.choices[0].message['content']
 
 
 
 
 
 
18
 
19
  iface = gr.Interface(
20
  fn=chatbot_response,
 
5
  # Setup and initialization
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
+ from openai import OpenAI
9
+
10
+ client = OpenAI(openai.api_key=os.getenv("OPENAI_API_KEY"))
11
+
12
+ def openai_chat(prompt, chat_history):
13
+ """Generic function to handle chatting with OpenAI's GPT model."""
14
+ try:
15
+ response = client.engines.gpt_3_5_turbo.completions.create(
16
+ prompt=prompt,
17
+ max_tokens=150
18
+ )
19
+ bot_message = response.choices[0].text.strip()
20
+ chat_history.append({"role": "assistant", "content": bot_message})
21
+ return '', chat_history
22
+ except Exception as e:
23
+ return f"An error occurred: {str(e)}", chat_history
24
 
25
  iface = gr.Interface(
26
  fn=chatbot_response,