Skip to content

[Fix]: optimize OpenAI model implementation and resolve parameter issues #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lagent/llms/lmdeploy_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def __init__(self,
'`do_sample` parameter is not supported by lmdeploy until '
f'v0.6.0, but currently using lmdeloy {self.str_version}')
super().__init__(path=path, **kwargs)
backend_config = copy.deepcopy(pipeline_cfg)
backend_config = dict(copy.deepcopy(pipeline_cfg))
backend_config.update(tp=tp)
backend_config = {
k: v
Expand Down
29 changes: 9 additions & 20 deletions lagent/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def generate_request_data(self, model_type, messages, gen_params, json_mode=Fals
gen_params = gen_params.copy()

# Hold out 100 tokens due to potential errors in token calculation
max_tokens = min(gen_params.pop('max_new_tokens'), 4096)
max_tokens = gen_params.pop('max_new_tokens')
if max_tokens <= 0:
return '', ''

Expand All @@ -374,23 +374,19 @@ def generate_request_data(self, model_type, messages, gen_params, json_mode=Fals
if 'repetition_penalty' in gen_params:
gen_params['frequency_penalty'] = gen_params.pop('repetition_penalty')

# Model-specific processing
data = {}
if model_type.lower().startswith('gpt') or model_type.lower().startswith('qwen'):
if any(x in model_type.lower() for x in ['o1', 'o3', 'o4']):
data = {'model': model_type, 'messages': messages, 'n': 1}
else:
if 'top_k' in gen_params:
warnings.warn('`top_k` parameter is deprecated in OpenAI APIs.', DeprecationWarning)
gen_params.pop('top_k')
gen_params.pop('skip_special_tokens', None)
gen_params.pop('session_id', None)

data = {'model': model_type, 'messages': messages, 'n': 1, **gen_params}
if json_mode:
data['response_format'] = {'type': 'json_object'}
elif model_type.lower().startswith('internlm'):
data = {'model': model_type, 'messages': messages, 'n': 1, **gen_params}
if json_mode:
data['response_format'] = {'type': 'json_object'}
else:
raise NotImplementedError(f'Model type {model_type} is not supported')

return header, data

Expand Down Expand Up @@ -756,7 +752,7 @@ def generate_request_data(self, model_type, messages, gen_params, json_mode=Fals
gen_params = gen_params.copy()

# Hold out 100 tokens due to potential errors in token calculation
max_tokens = min(gen_params.pop('max_new_tokens'), 4096)
max_tokens = gen_params.pop('max_new_tokens')
if max_tokens <= 0:
return '', ''

Expand All @@ -772,9 +768,10 @@ def generate_request_data(self, model_type, messages, gen_params, json_mode=Fals
if 'repetition_penalty' in gen_params:
gen_params['frequency_penalty'] = gen_params.pop('repetition_penalty')

# Model-specific processing
data = {}
if model_type.lower().startswith('gpt') or model_type.lower().startswith('qwen'):
if any(x in model_type.lower() for x in ['o1', 'o3', 'o4']):
data = {'model': model_type, 'messages': messages, 'n': 1}
else:
if 'top_k' in gen_params:
warnings.warn('`top_k` parameter is deprecated in OpenAI APIs.', DeprecationWarning)
gen_params.pop('top_k')
Expand All @@ -784,14 +781,6 @@ def generate_request_data(self, model_type, messages, gen_params, json_mode=Fals
data = {'model': model_type, 'messages': messages, 'n': 1, **gen_params}
if json_mode:
data['response_format'] = {'type': 'json_object'}
elif model_type.lower().startswith('internlm'):
data = {'model': model_type, 'messages': messages, 'n': 1, **gen_params}
if json_mode:
data['response_format'] = {'type': 'json_object'}
elif model_type.lower().startswith('o1'):
data = {'model': model_type, 'messages': messages, 'n': 1}
else:
raise NotImplementedError(f'Model type {model_type} is not supported')

return header, data

Expand Down