Skip to content

Commit

Permalink
mapping to role that openai supports
Browse files Browse the repository at this point in the history
  • Loading branch information
liujiangning30 committed Jul 11, 2024
1 parent c96b2fe commit bb2c094
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lagent/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def __init__(self,
meta_template: Optional[Dict] = [
dict(role='system', api_role='system'),
dict(role='user', api_role='user'),
dict(role='assistant', api_role='assistant')
dict(role='assistant', api_role='assistant'),
dict(role='environment', api_role='system')
],
openai_api_base: str = OPENAI_API_BASE,
proxies: Optional[Dict] = None,
Expand Down Expand Up @@ -140,10 +141,20 @@ def stream_chat(
if 'max_tokens' in gen_params:
raise NotImplementedError('unsupported parameter: max_tokens')
gen_params = self.update_gen_params(**gen_params)
gen_params['stream'] = True

resp = ''
finished = False
stop_words = gen_params.get('stop_words')
if stop_words is None:
stop_words = []
# mapping to role that openai supports
messages = inputs.copy()
for item in messages:
for role_cfg in self.meta_template:
if item['role'] == role_cfg['role']:
item['role'] = role_cfg['api_role']
break
for text in self._stream_chat(inputs, **gen_params):
resp += text
if not resp:
Expand Down

0 comments on commit bb2c094

Please sign in to comment.