Spaces:
Runtime error
Runtime error
Upload 2 files
Browse filesFix ChatCompletion call: use correct 'temperature' and 'max_tokens' parameters for legacy OpenAI SDK; update planner.py accordingly
- app.py +33 -14
- planner.py +29 -13
app.py
CHANGED
|
@@ -28,8 +28,8 @@ def chat(user_message, chat_history, chat_pairs, sources, plan):
|
|
| 28 |
# Call OpenAI's ChatCompletion to get assistant's reply
|
| 29 |
try:
|
| 30 |
model = os.getenv("OPENAI_MODEL", "gpt-5-mini")
|
| 31 |
-
temperature = float(os.getenv("TEMPERATURE", "
|
| 32 |
-
|
| 33 |
# Support alternative secret name COURSECREATOR_API_KEY as a fallback for the OpenAI API key
|
| 34 |
api_key = os.getenv("OPENAI_API_KEY") or os.getenv("COURSECREATOR_API_KEY")
|
| 35 |
if not api_key:
|
|
@@ -37,22 +37,41 @@ def chat(user_message, chat_history, chat_pairs, sources, plan):
|
|
| 37 |
# Prefer the new OpenAI SDK (>=1.0) if available
|
| 38 |
if hasattr(openai, "OpenAI"):
|
| 39 |
client = openai.OpenAI(api_key=api_key)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
assistant_reply = response.choices[0].message.content
|
| 47 |
else:
|
| 48 |
# Legacy OpenAI SDK (<1.0)
|
| 49 |
openai.api_key = api_key
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
assistant_reply = response["choices"][0]["message"]["content"]
|
| 57 |
except Exception as e:
|
| 58 |
# When the API call fails (e.g. missing API key), return an error message
|
|
|
|
| 28 |
# Call OpenAI's ChatCompletion to get assistant's reply
|
| 29 |
try:
|
| 30 |
model = os.getenv("OPENAI_MODEL", "gpt-5-mini")
|
| 31 |
+
temperature = float(os.getenv("TEMPERATURE", "0.7"))
|
| 32 |
+
max_tokens = int(os.getenv("MAX_OUTPUT_TOKENS", "1024"))
|
| 33 |
# Support alternative secret name COURSECREATOR_API_KEY as a fallback for the OpenAI API key
|
| 34 |
api_key = os.getenv("OPENAI_API_KEY") or os.getenv("COURSECREATOR_API_KEY")
|
| 35 |
if not api_key:
|
|
|
|
| 37 |
# Prefer the new OpenAI SDK (>=1.0) if available
|
| 38 |
if hasattr(openai, "OpenAI"):
|
| 39 |
client = openai.OpenAI(api_key=api_key)
|
| 40 |
+
# Try sending max_tokens; if unsupported, retry with max_completion_tokens
|
| 41 |
+
try:
|
| 42 |
+
response = client.chat.completions.create(
|
| 43 |
+
model=model,
|
| 44 |
+
messages=messages,
|
| 45 |
+
temperature=temperature,
|
| 46 |
+
max_tokens=max_tokens,
|
| 47 |
+
)
|
| 48 |
+
except Exception:
|
| 49 |
+
# Some newer models (e.g. o1 series) do not support max_tokens
|
| 50 |
+
response = client.chat.completions.create(
|
| 51 |
+
model=model,
|
| 52 |
+
messages=messages,
|
| 53 |
+
temperature=temperature,
|
| 54 |
+
max_completion_tokens=max_tokens,
|
| 55 |
+
)
|
| 56 |
assistant_reply = response.choices[0].message.content
|
| 57 |
else:
|
| 58 |
# Legacy OpenAI SDK (<1.0)
|
| 59 |
openai.api_key = api_key
|
| 60 |
+
try:
|
| 61 |
+
response = openai.ChatCompletion.create(
|
| 62 |
+
model=model,
|
| 63 |
+
messages=messages,
|
| 64 |
+
temperature=temperature,
|
| 65 |
+
max_tokens=max_tokens,
|
| 66 |
+
)
|
| 67 |
+
except Exception:
|
| 68 |
+
# Fallback for models that require max_completion_tokens
|
| 69 |
+
response = openai.ChatCompletion.create(
|
| 70 |
+
model=model,
|
| 71 |
+
messages=messages,
|
| 72 |
+
temperature=temperature,
|
| 73 |
+
max_completion_tokens=max_tokens,
|
| 74 |
+
)
|
| 75 |
assistant_reply = response["choices"][0]["message"]["content"]
|
| 76 |
except Exception as e:
|
| 77 |
# When the API call fails (e.g. missing API key), return an error message
|
planner.py
CHANGED
|
@@ -25,23 +25,39 @@ def plan_course(messages, sources):
|
|
| 25 |
# Newer OpenAI Python SDK (>=1.0) exposes `OpenAI` client
|
| 26 |
if hasattr(openai, "OpenAI"):
|
| 27 |
client = openai.OpenAI(api_key=api_key)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
plan_text = response.choices[0].message.content
|
| 36 |
else:
|
| 37 |
# Legacy OpenAI SDK (<1.0)
|
| 38 |
openai.api_key = api_key
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
plan_text = response["choices"][0]["message"]["content"]
|
| 46 |
except Exception as e:
|
| 47 |
# Propagate error for caller to handle
|
|
|
|
| 25 |
# Newer OpenAI Python SDK (>=1.0) exposes `OpenAI` client
|
| 26 |
if hasattr(openai, "OpenAI"):
|
| 27 |
client = openai.OpenAI(api_key=api_key)
|
| 28 |
+
# Try with max_tokens; fall back to max_completion_tokens if unsupported
|
| 29 |
+
try:
|
| 30 |
+
response = client.chat.completions.create(
|
| 31 |
+
model=model,
|
| 32 |
+
messages=formatted_messages,
|
| 33 |
+
temperature=temperature,
|
| 34 |
+
max_tokens=max_tokens,
|
| 35 |
+
)
|
| 36 |
+
except Exception:
|
| 37 |
+
response = client.chat.completions.create(
|
| 38 |
+
model=model,
|
| 39 |
+
messages=formatted_messages,
|
| 40 |
+
temperature=temperature,
|
| 41 |
+
max_completion_tokens=max_tokens,
|
| 42 |
+
)
|
| 43 |
plan_text = response.choices[0].message.content
|
| 44 |
else:
|
| 45 |
# Legacy OpenAI SDK (<1.0)
|
| 46 |
openai.api_key = api_key
|
| 47 |
+
try:
|
| 48 |
+
response = openai.ChatCompletion.create(
|
| 49 |
+
model=model,
|
| 50 |
+
messages=formatted_messages,
|
| 51 |
+
temperature=temperature,
|
| 52 |
+
max_tokens=max_tokens,
|
| 53 |
+
)
|
| 54 |
+
except Exception:
|
| 55 |
+
response = openai.ChatCompletion.create(
|
| 56 |
+
model=model,
|
| 57 |
+
messages=formatted_messages,
|
| 58 |
+
temperature=temperature,
|
| 59 |
+
max_completion_tokens=max_tokens,
|
| 60 |
+
)
|
| 61 |
plan_text = response["choices"][0]["message"]["content"]
|
| 62 |
except Exception as e:
|
| 63 |
# Propagate error for caller to handle
|