mirror of
https://github.com/open-compass/opencompass.git
synced 2025-05-30 16:03:24 +08:00
[Update] OpenaiSDK handle empty content (#2096)
This commit is contained in:
parent
c492e49e79
commit
d590f557bb
@ -69,6 +69,8 @@ class OpenAI(BaseAPIModel):
|
|||||||
Defaults to None.
|
Defaults to None.
|
||||||
extra_body (Dict, optional): Add additional JSON properties to
|
extra_body (Dict, optional): Add additional JSON properties to
|
||||||
the request
|
the request
|
||||||
|
think_tag (str, optional): The tag to use for reasoning content.
|
||||||
|
Defaults to '</think>'.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
is_api: bool = True
|
is_api: bool = True
|
||||||
@ -92,6 +94,7 @@ class OpenAI(BaseAPIModel):
|
|||||||
tokenizer_path: Optional[str] = None,
|
tokenizer_path: Optional[str] = None,
|
||||||
extra_body: Optional[Dict] = None,
|
extra_body: Optional[Dict] = None,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
|
think_tag: str = '</think>',
|
||||||
):
|
):
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
@ -114,6 +117,7 @@ class OpenAI(BaseAPIModel):
|
|||||||
self.tokenizer_path = tokenizer_path
|
self.tokenizer_path = tokenizer_path
|
||||||
self.hf_tokenizer = None
|
self.hf_tokenizer = None
|
||||||
self.extra_body = extra_body
|
self.extra_body = extra_body
|
||||||
|
self.think_tag = think_tag
|
||||||
|
|
||||||
if isinstance(key, str):
|
if isinstance(key, str):
|
||||||
if key == 'ENV':
|
if key == 'ENV':
|
||||||
@ -319,7 +323,28 @@ class OpenAI(BaseAPIModel):
|
|||||||
if self.logprobs:
|
if self.logprobs:
|
||||||
return response['choices']
|
return response['choices']
|
||||||
else:
|
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:
|
except KeyError:
|
||||||
if 'error' in response:
|
if 'error' in response:
|
||||||
if response['error']['code'] == 'rate_limit_exceeded':
|
if response['error']['code'] == 'rate_limit_exceeded':
|
||||||
@ -658,7 +683,8 @@ class OpenAISDK(OpenAI):
|
|||||||
**query_data, timeout=timeout) # timeout in seconds
|
**query_data, timeout=timeout) # timeout in seconds
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'Successfully get response from OpenAI API')
|
'Successfully get response from OpenAI API '
|
||||||
|
'with query: %s', query_data)
|
||||||
try:
|
try:
|
||||||
self.logger.info(responses)
|
self.logger.info(responses)
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -672,6 +698,13 @@ class OpenAISDK(OpenAI):
|
|||||||
'reasoning_content',
|
'reasoning_content',
|
||||||
'',
|
'',
|
||||||
))): # noqa: E125
|
))): # 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 <stop>, '
|
||||||
|
'the input query is: %s', query_data)
|
||||||
|
return ''
|
||||||
self.logger.error(
|
self.logger.error(
|
||||||
'Failed to extract content from the responses. '
|
'Failed to extract content from the responses. '
|
||||||
'Please check the API response for detail information.'
|
'Please check the API response for detail information.'
|
||||||
|
Loading…
Reference in New Issue
Block a user