ambrosfitz commited on
Commit
2b8a170
1 Parent(s): 006cc46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  import time
5
 
6
  # Initialize the OpenAI Client with your API key and endpoint
7
- api_key = os.environ.get("RUNPOD_API_KEY") # Make sure to set your environment variable correctly
8
  client = OpenAI(
9
  api_key=api_key,
10
  base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
@@ -14,9 +14,9 @@ def get_response(user_message, history):
14
  # Format the history for OpenAI
15
  history_openai_format = []
16
  for human, assistant in history:
17
- if human: # Ensure there's a human message
18
  history_openai_format.append({"role": "user", "content": human})
19
- if assistant: # Ensure there's an assistant message
20
  history_openai_format.append({"role": "assistant", "content": assistant})
21
  history_openai_format.append({"role": "user", "content": user_message})
22
 
@@ -28,8 +28,11 @@ def get_response(user_message, history):
28
  max_tokens=150
29
  )
30
 
31
- # Get the text response
32
- bot_message = response.choices[0].message['content'].strip() if response.choices else "No response generated."
 
 
 
33
  return bot_message
34
 
35
  with gr.Blocks() as demo:
 
4
  import time
5
 
6
  # Initialize the OpenAI Client with your API key and endpoint
7
+ api_key = os.environ.get("RUNPOD_API_KEY") # Ensure your API key is correctly loaded from environment variables
8
  client = OpenAI(
9
  api_key=api_key,
10
  base_url="https://api.runpod.ai/v2/vllm-k0g4c60zor9xuu/openai/v1",
 
14
  # Format the history for OpenAI
15
  history_openai_format = []
16
  for human, assistant in history:
17
+ if human:
18
  history_openai_format.append({"role": "user", "content": human})
19
+ if assistant:
20
  history_openai_format.append({"role": "assistant", "content": assistant})
21
  history_openai_format.append({"role": "user", "content": user_message})
22
 
 
28
  max_tokens=150
29
  )
30
 
31
+ # Properly access the text response
32
+ if response.choices:
33
+ bot_message = response.choices[0].text.strip() if response.choices[0].text.strip() else "No response generated."
34
+ else:
35
+ bot_message = "No response generated."
36
  return bot_message
37
 
38
  with gr.Blocks() as demo: