feat: support gpt-4-vision-preview

This commit is contained in:
Soulter
2023-11-09 20:53:02 +08:00
parent 89fc7b0553
commit 510c8b4236
3 changed files with 50 additions and 27 deletions
+11 -1
View File
@@ -628,11 +628,21 @@ async def oper_msg(message: Union[GroupMessage, FriendMessage, GuildMessage, Nak
await send_message(platform, message, f"管理员未启动任何语言模型或者语言模型初始化时失败。", session_id=session_id)
return
try:
image_url = None
for comp in message.message:
if isinstance(comp, Image):
if comp.url is None:
image_url = comp.file
break
else:
image_url = comp.url
break
if chosen_provider == REV_CHATGPT or chosen_provider == OPENAI_OFFICIAL:
if _global_object.web_search:
chatgpt_res = gplugin.web_search(qq_msg, llm_instance[chosen_provider], session_id)
else:
chatgpt_res = str(llm_instance[chosen_provider].text_chat(qq_msg, session_id))
chatgpt_res = str(llm_instance[chosen_provider].text_chat(qq_msg, session_id, image_url))
elif chosen_provider == REV_EDGEGPT:
res, res_code = await llm_instance[chosen_provider].text_chat(qq_msg, platform)
if res_code == 0: # bing不想继续话题,重置会话后重试。
+38 -25
View File
@@ -95,7 +95,7 @@ class ProviderOpenAIOfficial(Provider):
# 每隔10分钟转储一次
time.sleep(10*self.history_dump_interval)
def text_chat(self, prompt, session_id = None):
def text_chat(self, prompt, session_id = None, image_url = None):
if session_id is None:
session_id = "unknown"
if "unknown" in self.session_dict:
@@ -118,7 +118,7 @@ class ProviderOpenAIOfficial(Provider):
f.flush()
f.close()
cache_data_list, new_record, req = self.wrap(prompt, session_id)
cache_data_list, new_record, req = self.wrap(prompt, session_id, image_url)
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
@@ -306,7 +306,20 @@ class ProviderOpenAIOfficial(Provider):
return -1, -1, -1, -1
# 包装信息
def wrap(self, prompt, session_id):
def wrap(self, prompt, session_id, image_url = None):
if image_url is not None:
prompt = [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": image_url
}
}
]
# 获得缓存信息
context = self.session_dict[session_id]
new_record = {
@@ -332,30 +345,30 @@ class ProviderOpenAIOfficial(Provider):
while True:
is_all_exceed = True
for key in self.key_stat:
if key == None:
if key == None or self.key_stat[key]['exceed']:
continue
if not self.key_stat[key]['exceed']:
is_all_exceed = False
openai.api_key = key
gu.log(f"切换到Key: {key}, 已使用token: {self.key_stat[key]['used']}", level=gu.LEVEL_INFO)
if len(req) > 0:
try:
response = openai.ChatCompletion.create(
messages=req,
**self.chatGPT_configs
)
return response, True
except Exception as e:
if 'You exceeded' in str(e):
gu.log("当前Key已超额, 正在切换")
self.key_stat[openai.api_key]['exceed'] = True
self.save_key_record()
time.sleep(1)
continue
else:
gu.log(str(e), level=gu.LEVEL_ERROR)
is_all_exceed = False
openai.api_key = key
gu.log(f"切换到Key: {key}, 已使用token: {self.key_stat[key]['used']}", level=gu.LEVEL_INFO)
if len(req) == 0:
return None, False
try:
response = openai.ChatCompletion.create(
messages=req,
**self.chatGPT_configs
)
return response, True
except Exception as e:
if 'You exceeded' in str(e):
gu.log("当前Key已超额, 正在切换")
self.key_stat[openai.api_key]['exceed'] = True
self.save_key_record()
time.sleep(1)
continue
else:
return True
gu.log(str(e), level=gu.LEVEL_ERROR)
else:
return True
if is_all_exceed:
gu.log("所有Key已超额", level=gu.LEVEL_CRITICAL)
return None, False
+1 -1
View File
@@ -101,7 +101,7 @@ class ProviderRevChatGPT(Provider):
# print("[RevChatGPT] "+str(resp))
return resp
def text_chat(self, prompt, session_id = None) -> str:
def text_chat(self, prompt, session_id = None, image_url = None) -> str:
# 选择一个人少的账号。
selected_revstat = None