AUXteam commited on
Commit
fc4e65c
·
verified ·
1 Parent(s): 233d71d

Fix: enforce system message to be the first in openai_utils requests

Browse files
Files changed (1) hide show
  1. tinytroupe/openai_utils.py +8 -0
tinytroupe/openai_utils.py CHANGED
@@ -224,6 +224,14 @@ class OpenAIClient:
224
  return None
225
 
226
  def _raw_model_call(self, model, chat_api_params):
 
 
 
 
 
 
 
 
227
  """
228
  Calls the OpenAI API with the given parameters. Subclasses should
229
  override this method to implement their own API calls.
 
224
  return None
225
 
226
  def _raw_model_call(self, model, chat_api_params):
227
+
228
+ # Ensure system message is strictly first
229
+ if "messages" in chat_api_params:
230
+ system_msgs = [m for m in chat_api_params["messages"] if m.get("role") == "system"]
231
+ other_msgs = [m for m in chat_api_params["messages"] if m.get("role") != "system"]
232
+ chat_api_params["messages"] = system_msgs + other_msgs
233
+
234
+
235
  """
236
  Calls the OpenAI API with the given parameters. Subclasses should
237
  override this method to implement their own API calls.