mirror of
https://github.com/FuQuan233/nonebot-plugin-llmchat.git
synced 2025-09-06 19:40:44 +00:00
♻️ fix lint problems
This commit is contained in:
parent
f2d1521158
commit
5e048c9472
1 changed files with 14 additions and 8 deletions
|
@ -1,13 +1,13 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import base64
|
||||||
from collections import defaultdict, deque
|
from collections import defaultdict, deque
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import time
|
|
||||||
import base64
|
|
||||||
import ssl
|
import ssl
|
||||||
|
import time
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import aiofiles
|
import aiofiles
|
||||||
|
@ -39,7 +39,11 @@ require("nonebot_plugin_apscheduler")
|
||||||
from nonebot_plugin_apscheduler import scheduler
|
from nonebot_plugin_apscheduler import scheduler
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from openai.types.chat import ChatCompletionMessageParam
|
from openai.types.chat import (
|
||||||
|
ChatCompletionContentPartImageParam,
|
||||||
|
ChatCompletionContentPartTextParam,
|
||||||
|
ChatCompletionMessageParam,
|
||||||
|
)
|
||||||
|
|
||||||
__plugin_meta__ = PluginMetadata(
|
__plugin_meta__ = PluginMetadata(
|
||||||
name="llmchat",
|
name="llmchat",
|
||||||
|
@ -210,7 +214,7 @@ async def process_images(event: GroupMessageEvent) -> list[str]:
|
||||||
ssl_context = ssl.create_default_context()
|
ssl_context = ssl.create_default_context()
|
||||||
ssl_context.check_hostname = False
|
ssl_context.check_hostname = False
|
||||||
ssl_context.verify_mode = ssl.CERT_NONE
|
ssl_context.verify_mode = ssl.CERT_NONE
|
||||||
ssl_context.set_ciphers('DEFAULT@SECLEVEL=2')
|
ssl_context.set_ciphers("DEFAULT@SECLEVEL=2")
|
||||||
|
|
||||||
# 下载图片并将图片转换为base64
|
# 下载图片并将图片转换为base64
|
||||||
async with httpx.AsyncClient(verify=ssl_context) as client:
|
async with httpx.AsyncClient(verify=ssl_context) as client:
|
||||||
|
@ -219,7 +223,7 @@ async def process_images(event: GroupMessageEvent) -> list[str]:
|
||||||
logger.error(f"下载图片失败: {image_url}, 状态码: {response.status_code}")
|
logger.error(f"下载图片失败: {image_url}, 状态码: {response.status_code}")
|
||||||
continue
|
continue
|
||||||
image_data = response.content
|
image_data = response.content
|
||||||
base64_data = base64.b64encode(image_data).decode('utf-8')
|
base64_data = base64.b64encode(image_data).decode("utf-8")
|
||||||
base64_images.append(base64_data)
|
base64_images.append(base64_data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"处理图片时出错: {e}")
|
logger.error(f"处理图片时出错: {e}")
|
||||||
|
@ -288,15 +292,17 @@ async def process_messages(group_id: int):
|
||||||
# 没有未处理的消息说明已经被处理了,跳过
|
# 没有未处理的消息说明已经被处理了,跳过
|
||||||
if state.past_events.__len__() < 1:
|
if state.past_events.__len__() < 1:
|
||||||
break
|
break
|
||||||
|
|
||||||
# 将消息中的图片转成 base64
|
# 将消息中的图片转成 base64
|
||||||
base64_images = []
|
base64_images = []
|
||||||
if preset.support_image:
|
if preset.support_image:
|
||||||
base64_images = await process_images(event)
|
base64_images = await process_images(event)
|
||||||
|
|
||||||
# 将机器人错过的消息推送给LLM
|
# 将机器人错过的消息推送给LLM
|
||||||
content = ",".join([format_message(ev) for ev in state.past_events])
|
text_content = ",".join([format_message(ev) for ev in state.past_events])
|
||||||
content = [{"type": "text", "text": content}]
|
content: list[ChatCompletionContentPartTextParam | ChatCompletionContentPartImageParam] = [
|
||||||
|
{"type": "text", "text": text_content}
|
||||||
|
]
|
||||||
for base64_image in base64_images:
|
for base64_image in base64_images:
|
||||||
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}})
|
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue