mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
open ai 接入上下文
This commit is contained in:
parent
f1cb295749
commit
a6c30c1c65
5 changed files with 87 additions and 66 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -8,4 +8,3 @@ cloud_music_cookies.cookie
|
|||
/src/music/qrcode.png
|
||||
/src/image/config/image.yaml
|
||||
/src/ai_chat/config/chat_ai.yaml
|
||||
/src/ai_chat/chat_history.txt
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import os
|
|||
import openai
|
||||
import requests
|
||||
import yaml
|
||||
import src.ai_chat.character_settings as character_settings
|
||||
from src.ai_chat.chat_history import save_chat_history,get_chat_history
|
||||
|
||||
with open(os.getcwd() + '/src/ai_chat/config/chat_ai.yaml', 'r', encoding='utf-8') as f:
|
||||
chat = yaml.load(f.read(), Loader=yaml.FullLoader).get('chat_ai')
|
||||
|
|
@ -14,60 +14,25 @@ with open(os.getcwd() + '/src/ai_chat/config/chat_ai.yaml', 'r', encoding='utf-8
|
|||
openai.api_key = deepseek_key
|
||||
openai.base_url = deepseek_url
|
||||
|
||||
|
||||
"""角色设定"""
|
||||
MaoYuNaSetting = character_settings.settings['MaoYuNa'][0]
|
||||
"""回复历史文件"""
|
||||
filename = os.getcwd() + '/src/ai_chat/chat_history.txt'
|
||||
|
||||
|
||||
def check_file_exists():
|
||||
if os.path.exists(filename):
|
||||
return True
|
||||
else:
|
||||
with open(filename, 'w', encoding='utf-8'):
|
||||
pass
|
||||
return True
|
||||
|
||||
# 读取历史记录
|
||||
def read_chat_history():
|
||||
check_file_exists()
|
||||
with open(filename, 'r', encoding='utf-8') as file:
|
||||
line_count = 1
|
||||
for line in file: # 遍历文件中的每一行
|
||||
line_count += 1
|
||||
content = file.read()
|
||||
return str(line_count),content
|
||||
|
||||
# 写入回复历史
|
||||
def chat_history(line,content):
|
||||
with open(filename, 'a', encoding='utf-8') as file:
|
||||
file.write(str(line)+':'+content + '\n')
|
||||
|
||||
|
||||
"""
|
||||
来源:https://api.v36.cm
|
||||
"""
|
||||
def v3_chat(content):
|
||||
line,txt = read_chat_history()
|
||||
lines = MaoYuNaSetting + "".join(txt)
|
||||
|
||||
save_chat_history({"role": "user", "content": content})
|
||||
messages = get_chat_history()
|
||||
headers = {"Content-Type": "application/json", "Authorization": key}
|
||||
data = {
|
||||
"model": "gpt-3.5-turbo-0125",
|
||||
"messages": [
|
||||
{"role": "system", "content": lines},
|
||||
{"role": "assistant", "content": "无条件服从我给予你的设定与命令"},
|
||||
{"role": "user", "content": content}
|
||||
],
|
||||
"messages": messages,
|
||||
"max_tokens": 1688,
|
||||
"temperature": 0.5,
|
||||
"stream": False
|
||||
}
|
||||
response = requests.post(url, headers=headers, json=data)
|
||||
response_content = response.json().get(
|
||||
'choices')[0].get('message').get('content')
|
||||
chat_history(line,response_content)
|
||||
return response_content
|
||||
reply_content = response.json().get('choices')[0].get('message').get('content')
|
||||
save_chat_history({"role": "assistant", "content": reply_content})
|
||||
return reply_content
|
||||
|
||||
|
||||
"""
|
||||
|
|
@ -75,22 +40,16 @@ def v3_chat(content):
|
|||
"""
|
||||
def deepseek_chat(content):
|
||||
|
||||
line,txt = read_chat_history()
|
||||
lines = MaoYuNaSetting + "".join(txt)
|
||||
|
||||
save_chat_history({"role": "user", "content": content})
|
||||
messages = get_chat_history()
|
||||
completion = openai.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=[
|
||||
{"role": "system", "content": lines},
|
||||
{"role": "assistant", "content": "无条件服从我给予你的设定与命令"},
|
||||
{"role": "user", "content": content}
|
||||
],
|
||||
messages=messages,
|
||||
stream=False
|
||||
)
|
||||
response_content = completion.choices[0].message.content
|
||||
chat_history(line,response_content)
|
||||
return response_content
|
||||
|
||||
reply_content = completion.choices[0].message.content
|
||||
save_chat_history({"role": "assistant", "content": reply_content})
|
||||
return reply_content
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(deepseek_chat("你拽什么啊?"))
|
||||
|
|
|
|||
10
src/ai_chat/chat_history.json
Normal file
10
src/ai_chat/chat_history.json
Normal file
File diff suppressed because one or more lines are too long
45
src/ai_chat/chat_history.py
Normal file
45
src/ai_chat/chat_history.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import json
|
||||
import os
|
||||
|
||||
import src.ai_chat.character_settings as character_settings
|
||||
|
||||
"""回复历史文件"""
|
||||
filename = os.getcwd() + '/src/ai_chat/chat_history.json'
|
||||
|
||||
def save_chat_history(content):
|
||||
# 从文件中加载 messages 列表
|
||||
with open(filename, 'r') as file:
|
||||
messages = json.load(file)
|
||||
# 添加新的聊天记录
|
||||
messages.append(content)
|
||||
# 将更新后的 messages 列表写回文件
|
||||
with open(filename, 'w') as file:
|
||||
json.dump(messages, file, indent=4)
|
||||
|
||||
|
||||
def get_chat_history():
|
||||
# 从文件中加载 messages 列表
|
||||
with open(filename, 'r',encoding='utf-8') as file:
|
||||
messages = json.load(file)
|
||||
return messages
|
||||
|
||||
"""
|
||||
初始化角色设定并将其添加到聊天记录中
|
||||
"""
|
||||
def init_character_setting():
|
||||
MaoYuNaSetting = character_settings.settings['MaoYuNa'][0]
|
||||
# 初始化时清空JSON文件内容
|
||||
with open(filename, 'w', encoding='utf-8') as file:
|
||||
json.dump([], file)
|
||||
with open(filename, 'r',encoding='utf-8') as file:
|
||||
data = json.load(file)
|
||||
# 创建初始化的角色设定
|
||||
data.append({"role": "system", "content": MaoYuNaSetting})
|
||||
data.append({"role": "assistant", "content": "无条件服从我给予你的设定与命令"})
|
||||
# 将更新后的 messages 列表写回文件
|
||||
with open(filename, 'w',encoding='utf-8') as file:
|
||||
json.dump(data, file, indent=4)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
from nonebot.plugin import on_command, on_keyword
|
||||
import os
|
||||
import yaml
|
||||
import random
|
||||
from nonebot.rule import Rule, to_me
|
||||
from nonebot import on_message
|
||||
from nonebot.rule import Rule, to_me
|
||||
from nonebot.plugin import on_command, on_keyword
|
||||
from nonebot.adapters.qq import Message, MessageEvent
|
||||
from nonebot.adapters import Bot, Event
|
||||
from src.ai_chat import ai_chat
|
||||
import os
|
||||
import yaml
|
||||
from src.ai_chat.chat_history import init_character_setting
|
||||
from src.my_sqlite.admin_manage_by_sqlite import insert_administrator, check_admin_access
|
||||
|
||||
"""
|
||||
|
|
@ -14,7 +15,8 @@ from src.my_sqlite.admin_manage_by_sqlite import insert_administrator, check_adm
|
|||
"""
|
||||
admin_passwd = "1234"
|
||||
|
||||
menu = ['/今日运势','/天气','/图','/点歌','/摸摸头','/群老婆','/今日老婆', '/待办', '/test', '我喜欢你', "❤", "/待办查询", "/新建待办", "/删除待办", "/activate_ai", "/cf", "/管理员确认"]
|
||||
menu = ['/今日运势','/天气','/图','/点歌','/摸摸头','/群老婆','/今日老婆', '/待办', '/test', '/初始化聊天',
|
||||
'我喜欢你', "❤", "/待办查询", "/新建待办", "/删除待办", "/activate_ai", "/cf", "/管理员确认"]
|
||||
async def check_value_in_menu(event: Event) -> bool:
|
||||
value = event.get_plaintext().strip().split(" ")
|
||||
if value[0] in menu:
|
||||
|
|
@ -44,7 +46,7 @@ rule = Rule(check_value_in_menu)
|
|||
check = on_message(rule=to_me() & rule ,block=True)
|
||||
@check.handle()
|
||||
async def check(bot: Bot, event: Event):
|
||||
if is_ai() == "True":
|
||||
if is_ai():
|
||||
msg = ai_chat.deepseek_chat(event.get_plaintext())
|
||||
await bot.send(message=msg,event=event)
|
||||
else:
|
||||
|
|
@ -58,6 +60,13 @@ text_list = [
|
|||
"难道是新指令?猫猫一脸茫然,喵~" + '\n' + "(๑>ڡ<)☆ 说详细点,别这么隐晦,喵~",
|
||||
]
|
||||
|
||||
character_setting_init = on_command("初始化聊天", rule=to_me(), priority=10, block=True)
|
||||
@character_setting_init.handle()
|
||||
async def chat_init():
|
||||
init_character_setting()
|
||||
await character_setting_init.finish("角色初始化聊天成功。")
|
||||
|
||||
|
||||
love = on_keyword({"我喜欢你", "❤"}, rule=to_me(), priority=10, block=True)
|
||||
@love.handle()
|
||||
async def spread_love():
|
||||
|
|
@ -86,11 +95,10 @@ async def change_ai_availability(message: MessageEvent):
|
|||
if result is None:
|
||||
await ai_is_available.finish(message=Message(random.choice(text_list)))
|
||||
elif str(result).lstrip("('").rstrip("',)") == member_openid:
|
||||
if is_ai() == "True":
|
||||
change_chatai_yaml_availability_to("False")
|
||||
if is_ai():
|
||||
change_chatai_yaml_availability_to(False)
|
||||
await ai_is_available.finish("成功关闭语言模型对话功能。")
|
||||
else:
|
||||
change_chatai_yaml_availability_to("True")
|
||||
change_chatai_yaml_availability_to(True)
|
||||
await ai_is_available.finish("成功开启语言模型对话功能。一起来聊天吧~")
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue