From d590f557bb017f13d2457fd0db16c25b6d791dee Mon Sep 17 00:00:00 2001 From: Linchen Xiao Date: Mon, 12 May 2025 19:38:30 +0800 Subject: [PATCH] [Update] OpenaiSDK handle empty content (#2096) --- opencompass/models/openai_api.py | 37 ++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/opencompass/models/openai_api.py b/opencompass/models/openai_api.py index 9a67c87d..f48869c5 100644 --- a/opencompass/models/openai_api.py +++ b/opencompass/models/openai_api.py @@ -69,6 +69,8 @@ class OpenAI(BaseAPIModel): Defaults to None. extra_body (Dict, optional): Add additional JSON properties to the request + think_tag (str, optional): The tag to use for reasoning content. + Defaults to ''. """ is_api: bool = True @@ -92,6 +94,7 @@ class OpenAI(BaseAPIModel): tokenizer_path: Optional[str] = None, extra_body: Optional[Dict] = None, verbose: bool = False, + think_tag: str = '', ): super().__init__( @@ -114,6 +117,7 @@ class OpenAI(BaseAPIModel): self.tokenizer_path = tokenizer_path self.hf_tokenizer = None self.extra_body = extra_body + self.think_tag = think_tag if isinstance(key, str): if key == 'ENV': @@ -319,7 +323,28 @@ class OpenAI(BaseAPIModel): if self.logprobs: return response['choices'] else: - return response['choices'][0]['message']['content'].strip() + # Extract content and reasoning_content from response + message = response['choices'][0]['message'] + content = message.get('content', '') or '' + reasoning_content = message.get('reasoning_content', + '') or '' + + # Handle reasoning_content similar to OpenAISDK + if reasoning_content: + if self.verbose: + self.logger.info( + 'Extracting reasoning content and tags.' + 'Reasoning Content: %s, \n' + 'Tags: %s, \n' + 'Content: %s', reasoning_content, + self.think_tag, content) + + if content: + return reasoning_content + self.think_tag + content + else: + return reasoning_content + else: + return content.strip() except KeyError: if 'error' in response: if response['error']['code'] == 'rate_limit_exceeded': @@ -658,7 +683,8 @@ class OpenAISDK(OpenAI): **query_data, timeout=timeout) # timeout in seconds if self.verbose: self.logger.info( - 'Successfully get response from OpenAI API') + 'Successfully get response from OpenAI API ' + 'with query: %s', query_data) try: self.logger.info(responses) except Exception: @@ -672,6 +698,13 @@ class OpenAISDK(OpenAI): 'reasoning_content', '', ))): # noqa: E125 + # There is case that server does not return any content + if responses.choices[0].finish_reason == 'stop': + self.logger.info( + 'Server does not return any content ' + 'and stop reason is , ' + 'the input query is: %s', query_data) + return '' self.logger.error( 'Failed to extract content from the responses. ' 'Please check the API response for detail information.'