From 81e12d7cd7748530b1f4bf16da899668be83899b Mon Sep 17 00:00:00 2001 From: ClovertaTheTrilobita Date: Mon, 24 Feb 2025 17:53:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ai=5Fchat):=20=E9=87=87=E7=94=A8silico?= =?UTF-8?q?nflow=E7=9A=84API=EF=BC=8C=E5=B0=86=E8=AF=B7=E6=B1=82=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=BC=82=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 3 ++- src/clover_openai/ai_chat.py | 25 +++++++++++++++++++++++++ src/plugins/check.py | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 574768e..2a43779 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,4 +34,5 @@ lazy_object_proxy openai typing_extensions psutil -Beautifulsoup4 \ No newline at end of file +Beautifulsoup4 +aiohttp \ No newline at end of file diff --git a/src/clover_openai/ai_chat.py b/src/clover_openai/ai_chat.py index 9ba43ee..9a4a27f 100644 --- a/src/clover_openai/ai_chat.py +++ b/src/clover_openai/ai_chat.py @@ -2,6 +2,8 @@ import openai import requests from src.clover_sqlite.models.chat import GroupChatRole from src.configs.api_config import v3url, v3key, deepseek_url, deepseek_key +import aiohttp +import asyncio openai.api_key = deepseek_key openai.base_url = deepseek_url @@ -49,5 +51,28 @@ async def deepseek_chat(group_openid,content): await GroupChatRole.save_chat_history(group_openid, {"role": "assistant", "content": reply_content}) return reply_content +async def silicon_flow(group_openid, content): + await GroupChatRole.save_chat_history(group_openid, {"role": "user", "content": content}) + messages = await GroupChatRole.get_chat_history(group_openid) + url = "https://api.siliconflow.cn/v1/chat/completions" + payload = { + "model": "Pro/deepseek-ai/DeepSeek-V3", + "stream": False, + "messages": messages + } + headers = { + "Authorization": "Bearer sk-lcsbvcogybhzznggjsucscrcmveeuuksecxvdkhtrlmzjmqs", + "Content-Type": "application/json" + } + + async with aiohttp.ClientSession() as session: + async with session.post(url, json=payload, headers=headers) as response: + result = await response.json() + print(result) + reply_content = result["choices"][0]["message"]["content"] + + await GroupChatRole.save_chat_history(group_openid, {"role": "assistant", "content": reply_content}) + return reply_content + if __name__ == '__main__': print(deepseek_chat("你拽什么啊?")) diff --git a/src/plugins/check.py b/src/plugins/check.py index 6e07697..c59073b 100644 --- a/src/plugins/check.py +++ b/src/plugins/check.py @@ -40,7 +40,7 @@ async def handle_function(message: MessageEvent): member_openid, content = message.author.id, message.get_plaintext() status = await GroupChatRole.is_on(group_openid) if status: - msg = await ai_chat.deepseek_chat(group_openid,content) + msg = await ai_chat.silicon_flow(group_openid,content) await check.finish(msg) else: await check.finish(message=Message(random.choice(text_list)))