From 2f4e0bb4f257c297dda793568799563107ed444e Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 25 Sep 2023 15:55:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=BA=E6=A0=BC?= =?UTF-8?q?=E4=B8=80=E6=AE=B5=E6=97=B6=E9=97=B4=E5=90=8E=E6=B6=88=E5=A4=B1?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/command/command_openai_official.py | 14 ++++++++++++-- model/provider/provider_openai_official.py | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/model/command/command_openai_official.py b/model/command/command_openai_official.py index 02c96888a..877c68dac 100644 --- a/model/command/command_openai_official.py +++ b/model/command/command_openai_official.py @@ -235,9 +235,14 @@ class CommandOpenAIOfficial(Command): self.provider.session_dict[session_id] = [] new_record = { "user": { - "role": "system", + "role": "user", "content": personalities[ps], }, + "AI": { + "role": "assistant", + "content": "好的,接下来我会扮演这个角色。" + }, + 'type': "personality", 'usage_tokens': 0, 'single-tokens': 0 } @@ -251,9 +256,14 @@ class CommandOpenAIOfficial(Command): } new_record = { "user": { - "role": "system", + "role": "user", "content": ps, }, + "AI": { + "role": "assistant", + "content": "好的,接下来我会扮演这个角色。" + }, + 'type': "personality", 'usage_tokens': 0, 'single-tokens': 0 } diff --git a/model/provider/provider_openai_official.py b/model/provider/provider_openai_official.py index 2f920769d..7779b5bc4 100644 --- a/model/provider/provider_openai_official.py +++ b/model/provider/provider_openai_official.py @@ -113,6 +113,7 @@ class ProviderOpenAIOfficial(Provider): f.close() cache_data_list, new_record, req = self.wrap(prompt, session_id) + gu.log(f"CACHE_DATA_: {str(cache_data_list)}", level=gu.LEVEL_DEBUG, max_len=99999) gu.log(f"OPENAI REQUEST: {str(req)}", level=gu.LEVEL_DEBUG, max_len=9999) retry = 0 response = None @@ -156,6 +157,8 @@ class ProviderOpenAIOfficial(Provider): chatgpt_res = str(response["choices"][0]["message"]["content"]).strip() current_usage_tokens = response['usage']['total_tokens'] + gu.log(f"OPENAI RESPONSE: {response['usage']}", level=gu.LEVEL_DEBUG, max_len=9999) + # 超过指定tokens, 尽可能的保留最多的条目,直到小于max_tokens if current_usage_tokens > self.max_tokens: t = current_usage_tokens @@ -164,7 +167,7 @@ class ProviderOpenAIOfficial(Provider): if index >= len(cache_data_list): break # 保留人格信息 - if 'user' in cache_data_list[index] and cache_data_list[index]['user']['role'] != 'system': + if cache_data_list[index]['type'] != 'personality': t -= int(cache_data_list[index]['single_tokens']) del cache_data_list[index] else: @@ -297,6 +300,7 @@ class ProviderOpenAIOfficial(Provider): "content": prompt, }, "AI": {}, + 'type': "common", 'usage_tokens': 0, } req_list = []