Skip to content

Commit

Permalink
[Fix] bin_trim (open-compass#237)
Browse files Browse the repository at this point in the history
Co-authored-by: wangchonghua <wangchonghua@pjlab.org.cn>
  • Loading branch information
philipwangOvO and wangchonghua committed Aug 21, 2023
1 parent 57d63de commit 2f66bc7
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions opencompass/models/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,30 @@ def bin_trim(self, prompt: str, num_token: int) -> str:
pattern = re.compile(r'[\u4e00-\u9fa5]')
if pattern.search(prompt):
words = list(jieba.cut(prompt, cut_all=False))
sep = ''
else:
words = prompt.split(' ')
sep = ' '

l, r = 1, len(words)
while l + 2 < r:
mid = (l + r) // 2
if self.mode == 'front':
cur_prompt = ' '.join(words[-mid:])
cur_prompt = sep.join(words[-mid:])
elif self.mode == 'mid':
cur_prompt = ' '.join(words[:mid]) + ' '.join(words[-mid:])
cur_prompt = sep.join(words[:mid]) + sep.join(words[-mid:])
elif self.mode == 'rear':
cur_prompt = ' '.join(words[:mid])
cur_prompt = sep.join(words[:mid])

if self.get_token_len(cur_prompt) <= num_token:
l = mid # noqa: E741
else:
r = mid

if self.mode == 'front':
prompt = ' '.join(words[-l:])
prompt = sep.join(words[-l:])
elif self.mode == 'mid':
prompt = ' '.join(words[:l]) + ' '.join(words[-l:])
prompt = sep.join(words[:l]) + sep.join(words[-l:])
elif self.mode == 'rear':
prompt = ' '.join(words[:l])
prompt = sep.join(words[:l])
return prompt

0 comments on commit 2f66bc7

Please sign in to comment.