refactor(ai_chat): 采用siliconflow的API,将请求改为异步

This commit is contained in:
ClovertaTheTrilobita 2025-02-24 17:53:24 +08:00
parent 573f06b8f8
commit 81e12d7cd7
3 changed files with 28 additions and 2 deletions

View file

@ -35,3 +35,4 @@ openai
typing_extensions
psutil
Beautifulsoup4
aiohttp

View file

@ -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("你拽什么啊?"))

View file

@ -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)))