2025-01-16 14:40:22 +00:00
|
|
|
|
import os
|
2025-01-17 10:43:00 +00:00
|
|
|
|
import openai
|
2025-01-16 17:15:10 +00:00
|
|
|
|
import requests
|
2025-01-16 14:40:22 +00:00
|
|
|
|
import yaml
|
2025-01-22 03:25:47 +00:00
|
|
|
|
from src.my_sqlite.models.chat import GroupChatRole
|
2025-01-16 14:40:22 +00:00
|
|
|
|
|
2025-01-17 10:43:00 +00:00
|
|
|
|
with open(os.getcwd() + '/src/ai_chat/config/chat_ai.yaml', 'r', encoding='utf-8') as f:
|
2025-01-16 17:15:10 +00:00
|
|
|
|
chat = yaml.load(f.read(), Loader=yaml.FullLoader).get('chat_ai')
|
2025-01-17 10:43:00 +00:00
|
|
|
|
url = chat.get('v3url')
|
|
|
|
|
|
key = chat.get('v3key')
|
|
|
|
|
|
deepseek_url = chat.get('deepseek_url')
|
|
|
|
|
|
deepseek_key = chat.get('deepseek_key')
|
|
|
|
|
|
|
|
|
|
|
|
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-01-17 10:43:00 +00:00
|
|
|
|
headers = {"Content-Type": "application/json", "Authorization": key}
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
response = requests.post(url, 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-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("你拽什么啊?"))
|