This commit is contained in:
zhangsongyang 2025-04-27 11:31:07 +00:00
parent 57e98f30bb
commit 8fc6343119

View File

@ -556,8 +556,7 @@ class OpenAI(BaseAPIModel):
class OpenAISDK(OpenAI): class OpenAISDK(OpenAI):
def __init__( def __init__(self,
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,
@ -575,9 +574,9 @@ class OpenAISDK(OpenAI):
tokenizer_path: str | None = None, tokenizer_path: str | None = None,
extra_body: Dict | None = None, extra_body: Dict | None = None,
verbose: bool = False, verbose: bool = False,
http_client_cfg: dict = {},
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,
@ -605,11 +604,9 @@ class OpenAISDK(OpenAI):
else: else:
self.openai_api_base = openai_api_base self.openai_api_base = openai_api_base
if self.proxy_url is None: if self.proxy_url or http_client_cfg:
self.openai_client = OpenAI(base_url=self.openai_api_base, if self.proxy_url:
api_key=key) http_client_cfg['proxies'] = {
else:
proxies = {
'http://': self.proxy_url, 'http://': self.proxy_url,
'https://': self.proxy_url, 'https://': self.proxy_url,
} }
@ -617,8 +614,10 @@ class OpenAISDK(OpenAI):
self.openai_client = OpenAI( self.openai_client = OpenAI(
base_url=self.openai_api_base, base_url=self.openai_api_base,
api_key=key, api_key=key,
http_client=httpx.Client(proxies=proxies), http_client=httpx.Client(
**http_client_cfg) if http_client_cfg else None,
) )
if self.verbose: if self.verbose:
self.logger.info(f'Used openai_client: {self.openai_client}') self.logger.info(f'Used openai_client: {self.openai_client}')
self.status_code_mappings = status_code_mappings self.status_code_mappings = status_code_mappings
@ -679,6 +678,7 @@ class OpenAISDK(OpenAI):
try: try:
if self.verbose: if self.verbose:
self.logger.info('Start calling OpenAI API') self.logger.info('Start calling OpenAI API')
responses = self.openai_client.chat.completions.create( responses = self.openai_client.chat.completions.create(
**query_data, timeout=timeout) # timeout in seconds **query_data, timeout=timeout) # timeout in seconds
if self.verbose: if self.verbose:
@ -689,7 +689,6 @@ class OpenAISDK(OpenAI):
self.logger.info(responses) self.logger.info(responses)
except Exception: except Exception:
pass # noqa F841 pass # noqa F841
# 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 or