Skip to content

Commit

Permalink
Fix handling of response options when n parameter is unsupported in…
Browse files Browse the repository at this point in the history
… OpenAI-compatible interfaces
  • Loading branch information
coolbeevip committed Nov 5, 2024
1 parent 059a674 commit 118474f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,16 @@ def _step_model_response(
# Obtain the model's response
response = self.model_backend.run(openai_messages)

# Some OpenAI compatible interfaces do not support the 'n' parameter.
# We will call the API multiple times until we accumulate the expected number of completion choices.
expected_completion_choices = self.model_backend.model_config_dict.get('n', 1)
while len(response.choices) < expected_completion_choices:
logger.warning(f"{self.role_type}[{self.role_name}] expected {expected_completion_choices} completion choices, "
f"but only {len(response.choices)} were returned. "
f"I will make another call until I accumulate {expected_completion_choices} choices")
additional_response = self.model_backend.run(openai_messages)
response.choices.extend(additional_response.choices)

if isinstance(response, ChatCompletion):
output_messages, finish_reasons, usage_dict, response_id = (
self.handle_batch_response(response)
Expand Down

0 comments on commit 118474f

Please sign in to comment.