SanYeCao-Nonebot/src/clover_openai/ai_chat.py
SlyAimer 7968114e12 fix(ai_chat): 移除测试代码
feat(jm): 新增邮件发送功能及配置管理
feat(config): 新增jm配置文件
feat(email): 新增邮件发送模块
chore(.gitignore): 添加jm图片目录忽略
chore(requirements): 添加aiosmtplib依赖
refactor(jm_comic): 重构下载逻辑改为邮件发送
refactor(path_config): 更新jm配置路径
refactor(jm_download): 适配新下载逻辑并添加邮箱验证
2025-04-02 16:37:11 +08:00

72 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from openai import AsyncOpenAI as openai
import requests
from src.clover_sqlite.models.chat import GroupChatRole
from src.configs.api_config import v3url, v3key, deepseek_url, deepseek_key,silicon_flow_key
import aiohttp
"""
来源https://api.v36.cm
"""
async def v3_chat(group_openid,content):
await GroupChatRole.save_chat_history(group_openid, {"role": "user", "content": content})
messages = await GroupChatRole.get_chat_history(group_openid)
headers = {"Content-Type": "application/json", "Authorization": v3key}
data = {
"model": "gpt-3.5-turbo-0125",
"messages": messages,
"max_tokens": 1688,
"temperature": 0.5,
"stream": False
}
response = requests.post(v3url, headers=headers, json=data)
reply_content = response.json().get('choices')[0].get('message').get('content')
await GroupChatRole.save_chat_history(group_openid, {"role": "assistant", "content": reply_content})
return reply_content
"""
来源:https://api.deepseek.com
"""
async def deepseek_chat(group_openid,content):
"""
ai 角色扮演聊天
:param group_openid:
:param content:
:return:
"""
openai.api_key = deepseek_key
openai.base_url = deepseek_url
await GroupChatRole.save_chat_history(group_openid, {"role": "user", "content": content})
messages = await GroupChatRole.get_chat_history(group_openid)
completion = await openai.chat.completions.create(
model="deepseek-chat",
messages=messages,
stream=False
)
reply_content = completion.choices[0].message.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": f"Bearer {silicon_flow_key}",
"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