From 118474f350f760dc84429b05b51436945f3503fd Mon Sep 17 00:00:00 2001 From: zhanglei Date: Tue, 5 Nov 2024 16:06:13 +0800 Subject: [PATCH] Fix handling of response options when `n` parameter is unsupported in OpenAI-compatible interfaces --- camel/agents/chat_agent.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py index 1c5f9a2c40..77da097f05 100644 --- a/camel/agents/chat_agent.py +++ b/camel/agents/chat_agent.py @@ -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)