2025-01-17 10:43:00 +00:00
|
|
|
|
import openai
|
2025-01-16 17:15:10 +00:00
|
|
|
|
import requests
|
2025-02-06 08:00:21 +00:00
|
|
|
|
from src.clover_sqlite.models.chat import GroupChatRole
|
2025-02-06 06:38:00 +00:00
|
|
|
|
from src.configs.api_config import v3url, v3key, deepseek_url, deepseek_key
|
2025-01-17 10:43:00 +00:00
|
|
|
|
|
|
|
|
|
|
openai.api_key = deepseek_key
|
|
|
|
|
|
openai.base_url = deepseek_url
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
来源:https://api.v36.cm
|
|
|
|
|
|
"""
|
2025-01-22 03:25:47 +00:00
|
|
|
|
async def v3_chat(group_openid,content):
|
2025-01-17 20:06:31 +00:00
|
|
|
|
|
2025-01-22 03:25:47 +00:00
|
|
|
|
await GroupChatRole.save_chat_history(group_openid, {"role": "user", "content": content})
|
|
|
|
|
|
messages = await GroupChatRole.get_chat_history(group_openid)
|
2025-02-06 06:38:00 +00:00
|
|
|
|
headers = {"Content-Type": "application/json", "Authorization": v3key}
|
2025-01-16 17:15:10 +00:00
|
|
|
|
data = {
|
2025-01-17 10:43:00 +00:00
|
|
|
|
"model": "gpt-3.5-turbo-0125",
|
2025-01-17 20:06:31 +00:00
|
|
|
|
"messages": messages,
|
2025-01-17 10:43:00 +00:00
|
|
|
|
"max_tokens": 1688,
|
|
|
|
|
|
"temperature": 0.5,
|
|
|
|
|
|
"stream": False
|
|
|
|
|
|
}
|
2025-02-06 06:38:00 +00:00
|
|
|
|
response = requests.post(v3url, headers=headers, json=data)
|
2025-01-17 20:06:31 +00:00
|
|
|
|
reply_content = response.json().get('choices')[0].get('message').get('content')
|
2025-01-22 03:25:47 +00:00
|
|
|
|
await GroupChatRole.save_chat_history(group_openid, {"role": "assistant", "content": reply_content})
|
2025-01-17 20:06:31 +00:00
|
|
|
|
return reply_content
|
2025-01-17 10:43:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
来源:https://api.deepseek.com
|
|
|
|
|
|
"""
|
2025-01-22 03:25:47 +00:00
|
|
|
|
async def deepseek_chat(group_openid,content):
|
2025-02-06 06:38:00 +00:00
|
|
|
|
"""
|
|
|
|
|
|
ai 角色扮演聊天
|
|
|
|
|
|
:param group_openid:
|
|
|
|
|
|
:param content:
|
|
|
|
|
|
:return:
|
|
|
|
|
|
"""
|
2025-01-17 10:43:00 +00:00
|
|
|
|
|
2025-01-22 03:25:47 +00:00
|
|
|
|
await GroupChatRole.save_chat_history(group_openid, {"role": "user", "content": content})
|
|
|
|
|
|
messages = await GroupChatRole.get_chat_history(group_openid)
|
2025-01-17 10:43:00 +00:00
|
|
|
|
completion = openai.chat.completions.create(
|
|
|
|
|
|
model="deepseek-chat",
|
2025-01-17 20:06:31 +00:00
|
|
|
|
messages=messages,
|
2025-01-17 10:43:00 +00:00
|
|
|
|
stream=False
|
|
|
|
|
|
)
|
2025-01-17 20:06:31 +00:00
|
|
|
|
reply_content = completion.choices[0].message.content
|
2025-01-22 03:25:47 +00:00
|
|
|
|
await GroupChatRole.save_chat_history(group_openid, {"role": "assistant", "content": reply_content})
|
2025-01-17 20:06:31 +00:00
|
|
|
|
return reply_content
|
2025-01-16 14:40:22 +00:00
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2025-01-17 10:43:00 +00:00
|
|
|
|
print(deepseek_chat("你拽什么啊?"))
|