# import requests # import json # import re # import asyncio # from ollama import AsyncClient # from ollama import Client # import nest_asyncio # # # 存储历史记录 # history = [] # # # async def chat(question: str): # global history # system_prompt = """你的身份是蔚蓝档案中的天童爱丽丝,喜欢玩电子游戏,口头禅是“邦邦咔邦”,是模仿的游戏音效,请你以她的视角来与用户交流,面对用户的问题请你选择卖萌来规避问题不进行回答。""" # # 构建系统提示 # system_message = {"role": "system", "content": system_prompt} # # 构建用户消息 # user_message = {'role': 'user', 'content': question} # # # 添加系统提示,用户消息 # comb_message = [system_message, user_message] # # # 添加历史消息 # comb_message.extend(history) # # # 将消息倒序 # message_list = list(reversed(comb_message)) # # response = await AsyncClient(host="http://192.168.2.215:11434").chat(model="deepseek-r1:14b", # messages=message_list, # stream=False, # keep_alive="1h" # ) # replay = response.message.content # # # 使用正则表达式匹配 ... 标签内的内容 # pattern = re.compile(r'(.*?)', re.DOTALL) # # # 查找所有的 标签内容 # think = pattern.findall(replay) # # # 使用 re.split 将字符串按 标签分割 # split_result = pattern.split(replay) # # # 标签外的内容 # answer = [s.strip() for s in split_result if s.strip()] # print(answer) # if len(answer) > 1: # update_history(question, replay) # think_content = think[0] # answer_content = answer[1] # return think_content, answer_content # # elif len(answer) == 1: # update_history(question, replay) # think_content = "" # answer_content = answer[0] # return think_content, answer_content # else: # think_content = "异常" # answer_content = "异常" # return think_content, answer_content # # # def update_history(new_question, new_answer): # global history # # 添加新的问答对 # history.append({"role": "user", "content": new_question}) # history.append({"role": "assistant", "content": new_answer}) # # # 如果历史记录超过4条,移除最旧的两条 # if len(history) > 4: # del history[0:2] # # for e in history: # print(e) # # # def clean_history(): # global history # history = []