This commit is contained in:
MaiziXiao 2025-05-07 03:46:49 +00:00
parent 8c74e6a39e
commit 1673501a08

View File

@ -531,7 +531,8 @@ class OpenAI(BaseAPIModel):
class OpenAISDK(OpenAI): class OpenAISDK(OpenAI):
def __init__(self, def __init__(
self,
path: str = 'gpt-3.5-turbo', path: str = 'gpt-3.5-turbo',
max_seq_len: int = 16384, max_seq_len: int = 16384,
query_per_second: int = 1, query_per_second: int = 1,
@ -550,7 +551,8 @@ class OpenAISDK(OpenAI):
extra_body: Dict | None = None, extra_body: Dict | None = None,
verbose: bool = False, verbose: bool = False,
status_code_mappings: dict = {}, status_code_mappings: dict = {},
think_tag: str = '</think>'): think_tag: str = '</think>',
):
super().__init__( super().__init__(
path, path,
max_seq_len, max_seq_len,
@ -597,11 +599,13 @@ class OpenAISDK(OpenAI):
self.status_code_mappings = status_code_mappings self.status_code_mappings = status_code_mappings
self.think_tag = think_tag self.think_tag = think_tag
def _generate(self, def _generate(
self,
input: PromptList | str, input: PromptList | str,
max_out_len: int, max_out_len: int,
temperature: float, temperature: float,
timeout: int = 3600) -> str: timeout: int = 3600,
) -> str:
"""Generate results given a list of inputs. """Generate results given a list of inputs.
Args: Args:
@ -662,7 +666,12 @@ class OpenAISDK(OpenAI):
# Check if response is empty or content is empty # Check if response is empty or content is empty
if (not responses.choices or not responses.choices[0].message if (not responses.choices or not responses.choices[0].message
or not responses.choices[0].message.content): or
(not responses.choices[0].message.content and not getattr(
responses.choices[0].message,
'reasoning_content',
'',
))): # noqa: E125
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.'
@ -670,12 +679,13 @@ class OpenAISDK(OpenAI):
responses, responses,
) )
num_retries += 1 num_retries += 1
# Continue to retry instead of returning empty response
continue continue
reasoning_content = (getattr(responses.choices[0].message,
'reasoning_content', '') or '')
content = responses.choices[0].message.content or ''
# Concat Reasoning Content and tags to content # Concat Reasoning Content and tags to content
if (hasattr(responses.choices[0].message, 'reasoning_content') if reasoning_content:
and responses.choices[0].message.reasoning_content):
if self.verbose: if self.verbose:
self.logger.info( self.logger.info(
'Follow' 'Follow'
@ -684,14 +694,15 @@ class OpenAISDK(OpenAI):
'Reasoning Content: %s, \n' 'Reasoning Content: %s, \n'
'Tags: %s, \n' 'Tags: %s, \n'
'Content: %s', 'Content: %s',
responses.choices[0].message.reasoning_content, reasoning_content,
self.think_tag, self.think_tag,
responses.choices[0].message.content) content,
return (responses.choices[0].message.reasoning_content + )
self.think_tag + if content:
responses.choices[0].message.content) return reasoning_content + self.think_tag + content
return reasoning_content
return responses.choices[0].message.content return content
except (BadRequestError, APIStatusError) as e: except (BadRequestError, APIStatusError) as e:
# Handle BadRequest status # Handle BadRequest status