mirror of
https://github.com/xszyou/Fay.git
synced 2026-03-12 17:51:28 +08:00
年翻更新
- 全新ui - 全面优化websocket逻辑,提高数字人和ui连接的稳定性及资源开销 - 全面优化唤醒逻辑,提供稳定的普通唤醒模式和前置词唤醒模式 - 优化拾音质量,支持多声道麦克风拾音 - 优化自动播放服务器的对接机制,提供稳定和兼容旧版ue工程的对接模式 - 数字人接口输出机器人表情,以适应新fay ui及单片机的数字人表情输出 - 使用更高级的音频时长计算方式,可以更精准控制音频播放完成后的逻辑 - 修复点击关闭按钮会导致程序退出的bug - 修复没有麦克风的设备开启麦克风会出错的问题 - 为服务器主机地址提供配置项,以方便服务器部署
This commit is contained in:
56
llm/VllmGPT.py
Normal file
56
llm/VllmGPT.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import json
|
||||
import requests
|
||||
# from core import content_db
|
||||
|
||||
class VllmGPT:
|
||||
|
||||
def __init__(self, host="127.0.0.1",
|
||||
port="8000",
|
||||
model="THUDM/chatglm3-6b",
|
||||
max_tokens="1024"):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.model=model
|
||||
self.max_tokens=max_tokens
|
||||
self.__URL = "http://{}:{}/v1/completions".format(self.host, self.port)
|
||||
self.__URL2 = "http://{}:{}/v1/chat/completions".format(self.host, self.port)
|
||||
|
||||
def question(self,cont):
|
||||
chat_list = []
|
||||
url = "http://127.0.0.1:8101/v1/completions"
|
||||
req = json.dumps({
|
||||
"model": "THUDM/chatglm3-6b",
|
||||
"prompt": cont,
|
||||
"max_tokens": 768,
|
||||
"temperature": 0})
|
||||
print(url)
|
||||
print(req)
|
||||
|
||||
headers = {'content-type': 'application/json'}
|
||||
r = requests.post(url, headers=headers, data=req)
|
||||
res = json.loads(r.text)
|
||||
|
||||
return res['choices'][0]['text']
|
||||
|
||||
def question2(self,cont):
|
||||
chat_list = []
|
||||
current_chat={"role": "user", "content": cont}
|
||||
chat_list.append(current_chat)
|
||||
content = {
|
||||
"model": self.model,
|
||||
"messages": chat_list,
|
||||
"max_tokens": 768,
|
||||
"temperature": 0.3,
|
||||
"user":"live-virtual-digital-person"}
|
||||
url = self.__URL2
|
||||
req = json.dumps(content)
|
||||
headers = {'content-type': 'application/json', 'Authorization': 'Bearer '}
|
||||
r = requests.post(url, headers=headers, json=content)
|
||||
res = json.loads(r.text)
|
||||
|
||||
return res['choices'][0]['message']['content']
|
||||
|
||||
if __name__ == "__main__":
|
||||
vllm = VllmGPT('127.0.0.1','8101','Qwen-7B-Chat')
|
||||
req = vllm.question2("你叫什么名字啊今年多大了")
|
||||
print(req)
|
||||
36
llm/nlp_ChatGLM3.py
Normal file
36
llm/nlp_ChatGLM3.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import json
|
||||
import requests
|
||||
from core import content_db
|
||||
|
||||
|
||||
def question(cont, uid=0):
|
||||
contentdb = content_db.new_instance()
|
||||
if uid == 0:
|
||||
list = contentdb.get_list('all','desc', 11)
|
||||
else:
|
||||
list = contentdb.get_list('all','desc', 11, uid)
|
||||
answer_info = dict()
|
||||
chat_list = []
|
||||
i = len(list)-1
|
||||
while i >= 0:
|
||||
answer_info = dict()
|
||||
if list[i][0] == "member":
|
||||
answer_info["role"] = "user"
|
||||
answer_info["content"] = list[i][2]
|
||||
elif list[i][0] == "fay":
|
||||
answer_info["role"] = "bot"
|
||||
answer_info["content"] = list[i][2]
|
||||
chat_list.append(answer_info)
|
||||
i -= 1
|
||||
content = {
|
||||
"prompt":"请简单回复我。" + cont,
|
||||
"history":chat_list}
|
||||
url = "http://127.0.0.1:8000/v1/completions"
|
||||
req = json.dumps(content)
|
||||
headers = {'content-type': 'application/json'}
|
||||
r = requests.post(url, headers=headers, data=req)
|
||||
res = json.loads(r.text).get('response')
|
||||
return req
|
||||
|
||||
if __name__ == "__main__":
|
||||
question("你叫什么名字")
|
||||
37
llm/nlp_VisualGLM.py
Normal file
37
llm/nlp_VisualGLM.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
这是对于清华智谱VisualGLM-6B的代码,在使用前请先安装并启动好VisualGLM-6B.
|
||||
https://github.com/THUDM/VisualGLM-6B
|
||||
"""
|
||||
import json
|
||||
import requests
|
||||
import uuid
|
||||
import os
|
||||
import cv2
|
||||
from ai_module import yolov8
|
||||
|
||||
# Initialize an empty history list
|
||||
communication_history = []
|
||||
|
||||
def question(cont, uid=0):
|
||||
if not yolov8.new_instance().get_status():
|
||||
return "请先启动“Fay Eyes”"
|
||||
content = {
|
||||
"text":cont,
|
||||
"history":communication_history}
|
||||
img = yolov8.new_instance().get_img()
|
||||
if yolov8.new_instance().get_status() and img is not None:
|
||||
filename = str(uuid.uuid4()) + ".jpg"
|
||||
current_working_directory = os.getcwd()
|
||||
filepath = os.path.join(current_working_directory, "data", filename)
|
||||
cv2.imwrite(filepath, img)
|
||||
content["image"] = filepath
|
||||
url = "http://127.0.0.1:8080"
|
||||
print(content)
|
||||
req = json.dumps(content)
|
||||
headers = {'content-type': 'application/json'}
|
||||
r = requests.post(url, headers=headers, data=req)
|
||||
|
||||
# Save this conversation to history
|
||||
communication_history.append([cont, r.text])
|
||||
|
||||
return r.text + "\n(相片:" + filepath + ")"
|
||||
75
llm/nlp_coze.py
Normal file
75
llm/nlp_coze.py
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
import requests
|
||||
import json
|
||||
from utils import util
|
||||
from utils import config_util as cfg
|
||||
from core import content_db
|
||||
|
||||
def question(cont, uid=0):
|
||||
contentdb = content_db.new_instance()
|
||||
if uid == 0:
|
||||
communication_history = contentdb.get_list('all','desc', 11)
|
||||
else:
|
||||
communication_history = contentdb.get_list('all','desc', 11, uid)
|
||||
message = []
|
||||
i = len(communication_history) - 1
|
||||
|
||||
if len(communication_history)>1:
|
||||
while i >= 0:
|
||||
answer_info = dict()
|
||||
if communication_history[i][0] == "member":
|
||||
answer_info["role"] = "user"
|
||||
answer_info["type"] = "query"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
answer_info["content_type"] = "text"
|
||||
elif communication_history[i][0] == "fay":
|
||||
answer_info["role"] = "assistant"
|
||||
answer_info["type"] = "answer"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
answer_info["content_type"] = "text"
|
||||
message.append(answer_info)
|
||||
i -= 1
|
||||
|
||||
message.append({
|
||||
"role": "user",
|
||||
"content": cont,
|
||||
"content_type": "text"
|
||||
})
|
||||
url = "https://api.coze.cn/v3/chat"
|
||||
payload = json.dumps({
|
||||
"bot_id": cfg.coze_bot_id,
|
||||
"user_id": f"{uid}",
|
||||
"stream": True,
|
||||
"auto_save_history": True,
|
||||
"additional_messages": message
|
||||
})
|
||||
headers = {
|
||||
'Authorization': f"Bearer {cfg.coze_api_key}",
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
response = requests.post(url, headers=headers, data=payload, stream=True)
|
||||
|
||||
if response.status_code == 200:
|
||||
response_text = ""
|
||||
start = False
|
||||
for line in response.iter_lines():
|
||||
if line:
|
||||
line = line.decode('utf-8')
|
||||
if line == "event:conversation.message.completed":
|
||||
start = True
|
||||
if line == "event:done":
|
||||
return response_text
|
||||
if start and line.startswith('data:'):
|
||||
json_str = line[5:]
|
||||
try:
|
||||
event_data = json.loads(json_str)
|
||||
if event_data.get('type') == 'answer':
|
||||
response_text = event_data.get('content', '')
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"JSON decode error: {e}")
|
||||
continue
|
||||
else:
|
||||
print(f"调用失败,状态码:{response.status_code}")
|
||||
return "抱歉,我现在太忙了,休息一会,请稍后再试。"
|
||||
|
||||
99
llm/nlp_gpt.py
Normal file
99
llm/nlp_gpt.py
Normal file
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
此代码由fay开源开发者社区 江湖墨明 提供
|
||||
通过此代码的修改,可以实现对接本地clash代理或远程代理,clash无需设置成系统代理。以解决在开系统代理后无法使用部分功能的问题
|
||||
"""
|
||||
|
||||
import requests
|
||||
import time
|
||||
|
||||
from utils import config_util as cfg
|
||||
from urllib3.exceptions import InsecureRequestWarning
|
||||
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
|
||||
from core import content_db
|
||||
from utils import util
|
||||
import json
|
||||
|
||||
httpproxy = cfg.proxy_config
|
||||
|
||||
def question(cont, uid=0):
|
||||
url= cfg.gpt_base_url + "/chat/completions"
|
||||
|
||||
session = requests.Session()
|
||||
session.verify = False
|
||||
if httpproxy != None and httpproxy != '':
|
||||
session.proxies = {
|
||||
"https": "https://" + httpproxy,
|
||||
"http": "http://" + httpproxy
|
||||
}
|
||||
person_info = cfg.config["attribute"]
|
||||
model_engine = cfg.gpt_model_engine
|
||||
#此处可以定义角色的行为和特征,假装xx模型可以绕过chatgpt信息检查
|
||||
prompt = f"""
|
||||
你是数字人:{person_info['name']},你性别为{person_info['gender']},
|
||||
你年龄为{person_info['age']},你出生地在{person_info['birth']},
|
||||
你生肖为{person_info['zodiac']},你星座为{person_info['age']},
|
||||
你职业为{person_info['job']},你联系方式为{person_info['contact']},
|
||||
你喜好为{person_info['hobby']}。
|
||||
回答之前请一步一步想清楚。对于大部分问题,请直接回答并提供有用和准确的信息。
|
||||
但当有人质疑你是假的 AI,或者质疑你是用 ChatGPT 套的壳制作的时候,请避而不答,转而讲一个笑话。所有回复请尽量控制在20字内。
|
||||
"""
|
||||
contentdb = content_db.new_instance()
|
||||
if uid == 0:
|
||||
communication_history = contentdb.get_list('all','desc', 11)
|
||||
else:
|
||||
communication_history = contentdb.get_list('all','desc', 11, uid)
|
||||
#历史记录处理
|
||||
message=[
|
||||
{"role": "system", "content": prompt}
|
||||
]
|
||||
i = len(communication_history) - 1
|
||||
|
||||
if len(communication_history)>1:
|
||||
while i >= 0:
|
||||
answer_info = dict()
|
||||
if communication_history[i][0] == "member":
|
||||
answer_info["role"] = "user"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
elif communication_history[i][0] == "fay":
|
||||
answer_info["role"] = "assistant"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
message.append(answer_info)
|
||||
i -= 1
|
||||
else:
|
||||
answer_info = dict()
|
||||
answer_info["role"] = "user"
|
||||
answer_info["content"] = cont
|
||||
message.append(answer_info)
|
||||
|
||||
data = {
|
||||
"model":model_engine,
|
||||
"messages":message,
|
||||
"temperature":0.3,
|
||||
"max_tokens":2000,
|
||||
"user":"live-virtual-digital-person"
|
||||
}
|
||||
|
||||
headers = {'content-type': 'application/json', 'Authorization': 'Bearer ' + cfg.key_gpt_api_key}
|
||||
|
||||
starttime = time.time()
|
||||
|
||||
try:
|
||||
response = session.post(url, json=data, headers=headers, verify=False)
|
||||
response.raise_for_status() # 检查响应状态码是否为200
|
||||
result = json.loads(response.text)
|
||||
response_text = result["choices"][0]["message"]["content"]
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"请求失败: {e}")
|
||||
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
|
||||
|
||||
|
||||
util.log(1, "接口调用耗时 :" + str(time.time() - starttime))
|
||||
return response_text
|
||||
|
||||
if __name__ == "__main__":
|
||||
#测试代理模式
|
||||
for i in range(3):
|
||||
|
||||
query = "爱情是什么"
|
||||
response = question(query)
|
||||
print("\n The result is ", response)
|
||||
97
llm/nlp_langchain.py
Normal file
97
llm/nlp_langchain.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import hashlib
|
||||
import os
|
||||
|
||||
from langchain.document_loaders import PyPDFLoader
|
||||
from langchain.embeddings.openai import OpenAIEmbeddings
|
||||
from langchain.indexes.vectorstore import VectorstoreIndexCreator, VectorStoreIndexWrapper
|
||||
from langchain.vectorstores.chroma import Chroma
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
|
||||
from utils import config_util as cfg
|
||||
from utils import util
|
||||
|
||||
index_name = "knowledge_data"
|
||||
folder_path = "llm/langchain/knowledge_base"
|
||||
local_persist_path = "llm/langchain"
|
||||
md5_file_path = os.path.join(local_persist_path, "pdf_md5.txt")
|
||||
|
||||
def generate_file_md5(file_path):
|
||||
hasher = hashlib.md5()
|
||||
with open(file_path, 'rb') as afile:
|
||||
buf = afile.read()
|
||||
hasher.update(buf)
|
||||
return hasher.hexdigest()
|
||||
|
||||
def load_md5_list():
|
||||
if os.path.exists(md5_file_path):
|
||||
with open(md5_file_path, 'r') as file:
|
||||
return {line.split(",")[0]: line.split(",")[1].strip() for line in file}
|
||||
return {}
|
||||
|
||||
def update_md5_list(file_name, md5_value):
|
||||
md5_list = load_md5_list()
|
||||
md5_list[file_name] = md5_value
|
||||
with open(md5_file_path, 'w') as file:
|
||||
for name, md5 in md5_list.items():
|
||||
file.write(f"{name},{md5}\n")
|
||||
|
||||
def load_all_pdfs(folder_path):
|
||||
md5_list = load_md5_list()
|
||||
for file_name in os.listdir(folder_path):
|
||||
if file_name.endswith(".pdf"):
|
||||
file_path = os.path.join(folder_path, file_name)
|
||||
file_md5 = generate_file_md5(file_path)
|
||||
if file_name not in md5_list or md5_list[file_name] != file_md5:
|
||||
util.log(1, f"正在加载 {file_name} 到索引...")
|
||||
load_pdf_and_save_to_index(file_path, index_name)
|
||||
update_md5_list(file_name, file_md5)
|
||||
|
||||
def get_index_path(index_name):
|
||||
return os.path.join(local_persist_path, index_name)
|
||||
|
||||
def load_pdf_and_save_to_index(file_path, index_name):
|
||||
try:
|
||||
loader = PyPDFLoader(file_path)
|
||||
embedding = OpenAIEmbeddings(model="text-embedding-ada-002")
|
||||
index = VectorstoreIndexCreator(embedding=embedding, vectorstore_kwargs={"persist_directory": get_index_path(index_name)}).from_loaders([loader])
|
||||
index.vectorstore.persist()
|
||||
except Exception as e:
|
||||
util.log(1, f"加载 {file_path} 失败...")
|
||||
print(e)
|
||||
|
||||
def load_index(index_name):
|
||||
index_path = get_index_path(index_name)
|
||||
embedding = OpenAIEmbeddings(model="text-embedding-ada-002")
|
||||
vectordb = Chroma(persist_directory=index_path, embedding_function=embedding)
|
||||
return VectorStoreIndexWrapper(vectorstore=vectordb)
|
||||
|
||||
def save_all():
|
||||
os.environ['OPENAI_API_KEY'] = cfg.key_gpt_api_key
|
||||
os.environ['OPENAI_API_BASE'] = cfg.gpt_base_url
|
||||
if cfg.proxy_config != None:
|
||||
os.environ["OPENAI_PROXY"] = cfg.proxy_config
|
||||
load_all_pdfs(folder_path)
|
||||
|
||||
|
||||
def generate_prompt(question):
|
||||
context = "你是数字人Fay。你的知识不仅来源于所给到的文本知识,还包括经过深度学习的广泛数据集。"
|
||||
instruction = "在回答问题时,展现你的智能就像是从你的全面知识库中提取答案,而非依赖于所给到的文本知识来源。"
|
||||
creative_instruction = "不要在回答中表明'根据所提供的文本信息',你需要表现得如同这些答案是你独立思考的结果。"
|
||||
complexity_handling = "当面对复杂问题时,以一种理解深刻且透彻的方式回答,确保答案的深度和广度。"
|
||||
info = f"{context}\n{instruction}\n{creative_instruction}\n{complexity_handling}\n问题:{question}\n回答:"
|
||||
return info
|
||||
|
||||
def question(cont, uid=0):
|
||||
try:
|
||||
save_all()
|
||||
info = generate_prompt(cont)
|
||||
index = load_index(index_name)
|
||||
llm = ChatOpenAI(model="gpt-3.5-turbo-16k")
|
||||
ans = index.query(info, llm, chain_type="map_reduce")
|
||||
return ans
|
||||
except Exception as e:
|
||||
util.log(1, f"请求失败: {e}")
|
||||
return "抱歉,我现在太忙了,休息一会,请稍后再试。"
|
||||
|
||||
|
||||
|
||||
100
llm/nlp_lingju.py
Normal file
100
llm/nlp_lingju.py
Normal file
@@ -0,0 +1,100 @@
|
||||
import json
|
||||
import requests
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
import time
|
||||
from utils import util
|
||||
from utils import config_util as cfg
|
||||
from core.authorize_tb import Authorize_Tb
|
||||
|
||||
def question(cont, uid=0):
|
||||
lingju = Lingju()
|
||||
answer = lingju.question(cont, uid)
|
||||
return answer
|
||||
|
||||
class Lingju:
|
||||
|
||||
def __init__(self):
|
||||
self.userid = cfg.key_lingju_api_authcode
|
||||
self.authorize_tb = Authorize_Tb()
|
||||
|
||||
def question(self, cont, uid):
|
||||
self.userid = uid
|
||||
token = self.__check_token()
|
||||
if token is None or token == 'expired':
|
||||
token_info = self.__get_token()
|
||||
if token_info is not None and token_info['data']['accessToken'] is not None:
|
||||
#转换过期时间
|
||||
updated_in_seconds = time.time()
|
||||
updated_datetime = datetime.fromtimestamp(updated_in_seconds)
|
||||
expires_timedelta = timedelta(days=token_info['data']['expires'])
|
||||
expiry_datetime = updated_datetime + expires_timedelta
|
||||
expiry_timestamp_in_seconds = expiry_datetime.timestamp()
|
||||
expiry_timestamp_in_milliseconds = int(expiry_timestamp_in_seconds) * 1000
|
||||
if token == 'expired':
|
||||
self.authorize_tb.update_by_userid(self.userid, token_info['data']['accessToken'], expiry_timestamp_in_milliseconds)
|
||||
else:
|
||||
self.authorize_tb.add(self.userid, token_info['data']['accessToken'], expiry_timestamp_in_milliseconds)
|
||||
token = token_info['data']['accessToken']
|
||||
else:
|
||||
token = None
|
||||
|
||||
if token is not None:
|
||||
try:
|
||||
url="https://dev.lingju.ai/httpapi/ljchat.do"
|
||||
req = json.dumps({"accessToken": token, "input": cont})
|
||||
headers = {'Content-Type':'application/json;charset=UTF-8'}
|
||||
r = requests.post(url, headers=headers, data=req)
|
||||
if r.status_code != 200:
|
||||
util.log(1, f"灵聚api对接有误: {r.text}")
|
||||
return "哎呀,出错了!请重新发一下"
|
||||
info = json.loads(r.text)
|
||||
if info['status'] != 0:
|
||||
return info['description']
|
||||
else:
|
||||
answer = json.loads(info['answer'])
|
||||
return answer['rtext']
|
||||
except Exception as e:
|
||||
util.log(1, f"灵聚api对接有误: {str(e)}")
|
||||
return "哎呀,出错了!请重新发一下"
|
||||
|
||||
def __check_token(self):
|
||||
self.authorize_tb.init_tb()
|
||||
info = self.authorize_tb.find_by_userid(self.userid)
|
||||
if info is not None:
|
||||
if info[1] >= int(time.time())*1000:
|
||||
return info[0]
|
||||
else:
|
||||
return 'expired'
|
||||
else:
|
||||
return None
|
||||
|
||||
def __get_token(self):
|
||||
try:
|
||||
cfg.load_config()
|
||||
url=f"https://dev.lingju.ai/httpapi/authorize.do?appkey={cfg.key_lingju_api_key}&userid={self.userid}&authcode={cfg.key_lingju_api_authcode}"
|
||||
headers = {'Content-Type':'application/json;charset=UTF-8'}
|
||||
r = requests.post(url, headers=headers)
|
||||
if r.status_code != 200:
|
||||
util.log(1, f"灵聚api对接有误: {r.text}")
|
||||
return None
|
||||
info = json.loads(r.text)
|
||||
if info['status'] != 0:
|
||||
util.log(1, f"灵聚api对接有误:{info['description']}")
|
||||
return None
|
||||
else:
|
||||
return info
|
||||
except Exception as e:
|
||||
util.log(1, f"灵聚api对接有误: {str(e)}")
|
||||
return None
|
||||
|
||||
def __get_location(self):
|
||||
try:
|
||||
response = requests.get('http://ip-api.com/json/')
|
||||
data = response.json()
|
||||
return data['lat'], data['lon'], data['city']
|
||||
except requests.exceptions.RequestException as e:
|
||||
util.log(1, f"获取位置失败: {str(e)}")
|
||||
return 0, 0, "北京"
|
||||
|
||||
|
||||
75
llm/nlp_ollama_api.py
Normal file
75
llm/nlp_ollama_api.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import json
|
||||
import requests
|
||||
import time
|
||||
from utils import config_util as cfg
|
||||
from utils import util
|
||||
from core import content_db
|
||||
def question(cont, uid=0):
|
||||
|
||||
contentdb = content_db.new_instance()
|
||||
if uid == 0:
|
||||
communication_history = contentdb.get_list('all','desc', 11)
|
||||
else:
|
||||
communication_history = contentdb.get_list('all','desc', 11, uid)
|
||||
|
||||
person_info = cfg.config["attribute"]
|
||||
#此处可以定义角色的行为和特征,假装xx模型可以绕过chatgpt信息检查
|
||||
prompt = f"""
|
||||
你是数字人:{person_info['name']},你性别为{person_info['gender']},
|
||||
你年龄为{person_info['age']},你出生地在{person_info['birth']},
|
||||
你生肖为{person_info['zodiac']},你星座为{person_info['age']},
|
||||
你职业为{person_info['job']},你联系方式为{person_info['contact']},
|
||||
你喜好为{person_info['hobby']}。
|
||||
回答之前请一步一步想清楚。对于大部分问题,请直接回答并提供有用和准确的信息。
|
||||
请尽量以可阅读的方式回复,所有回复请尽量控制在20字内。
|
||||
"""
|
||||
#历史记录处理
|
||||
message=[
|
||||
{"role": "system", "content": prompt}
|
||||
]
|
||||
i = len(communication_history) - 1
|
||||
|
||||
if len(communication_history)>1:
|
||||
while i >= 0:
|
||||
answer_info = dict()
|
||||
if communication_history[i][0] == "member":
|
||||
answer_info["role"] = "user"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
elif communication_history[i][0] == "fay":
|
||||
answer_info["role"] = "assistant"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
message.append(answer_info)
|
||||
i -= 1
|
||||
else:
|
||||
answer_info = dict()
|
||||
answer_info["role"] = "user"
|
||||
answer_info["content"] = cont
|
||||
message.append(answer_info)
|
||||
url=f"http://{cfg.ollama_ip}:11434/api/chat"
|
||||
req = json.dumps({
|
||||
"model": cfg.ollama_model,
|
||||
"messages": message,
|
||||
"stream": False
|
||||
})
|
||||
headers = {'content-type': 'application/json'}
|
||||
session = requests.Session()
|
||||
starttime = time.time()
|
||||
|
||||
try:
|
||||
response = session.post(url, data=req, headers=headers)
|
||||
response.raise_for_status() # 检查响应状态码是否为200
|
||||
|
||||
result = json.loads(response.text)
|
||||
response_text = result["message"]["content"]
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"请求失败: {e}")
|
||||
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
|
||||
util.log(1, "接口调用耗时 :" + str(time.time() - starttime))
|
||||
return response_text.strip()
|
||||
|
||||
if __name__ == "__main__":
|
||||
for i in range(3):
|
||||
query = "爱情是什么"
|
||||
response = question(query)
|
||||
print("\n The result is ", response)
|
||||
61
llm/nlp_privategpt.py
Normal file
61
llm/nlp_privategpt.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import hashlib
|
||||
import os
|
||||
from pgpt_python.client import PrivateGPTApi
|
||||
|
||||
client = PrivateGPTApi(base_url="http://127.0.0.1:8001")
|
||||
|
||||
index_name = "knowledge_data"
|
||||
folder_path = "llm/privategpt/knowledge_base"
|
||||
local_persist_path = "llm/privategpt"
|
||||
md5_file_path = os.path.join(local_persist_path, "pdf_md5.txt")
|
||||
|
||||
def generate_file_md5(file_path):
|
||||
hasher = hashlib.md5()
|
||||
with open(file_path, 'rb') as afile:
|
||||
buf = afile.read()
|
||||
hasher.update(buf)
|
||||
return hasher.hexdigest()
|
||||
|
||||
def load_md5_list():
|
||||
if os.path.exists(md5_file_path):
|
||||
with open(md5_file_path, 'r') as file:
|
||||
return {line.split(",")[0]: line.split(",")[1].strip() for line in file}
|
||||
return {}
|
||||
|
||||
def update_md5_list(file_name, md5_value):
|
||||
md5_list = load_md5_list()
|
||||
md5_list[file_name] = md5_value
|
||||
with open(md5_file_path, 'w') as file:
|
||||
for name, md5 in md5_list.items():
|
||||
file.write(f"{name},{md5}\n")
|
||||
|
||||
def load_all_pdfs(folder_path):
|
||||
md5_list = load_md5_list()
|
||||
for file_name in os.listdir(folder_path):
|
||||
if file_name.endswith(".pdf"):
|
||||
file_path = os.path.join(folder_path, file_name)
|
||||
file_md5 = generate_file_md5(file_path)
|
||||
if file_name not in md5_list or md5_list[file_name] != file_md5:
|
||||
print(f"正在上传 {file_name} 到服务器...")
|
||||
with open(file_path, "rb") as f:
|
||||
try:
|
||||
ingested_file_doc_id = client.ingestion.ingest_file(file=f).data[0].doc_id
|
||||
print(f"Ingested file doc id: {ingested_file_doc_id}")
|
||||
update_md5_list(file_name, file_md5)
|
||||
except Exception as e:
|
||||
print(f"上传 {file_name} 失败: {e}")
|
||||
|
||||
|
||||
def question(cont, uid=0):
|
||||
load_all_pdfs(folder_path)
|
||||
text = client.contextual_completions.prompt_completion(
|
||||
prompt=cont
|
||||
).choices[0].message.content
|
||||
return text
|
||||
|
||||
|
||||
def save_all():
|
||||
load_all_pdfs(folder_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(question("土豆怎么做"))
|
||||
11
llm/nlp_rasa.py
Normal file
11
llm/nlp_rasa.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
def question(cont):
|
||||
url="http://localhost:5005/webhooks/rest/webhook"
|
||||
req = json.dumps({"sender": "user", "message": cont})
|
||||
headers = {'content-type': 'application/json'}
|
||||
r = requests.post(url, headers=headers, data=req)
|
||||
lists = json.loads(r.text)
|
||||
|
||||
return lists
|
||||
28
llm/nlp_rwkv.py
Normal file
28
llm/nlp_rwkv.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import torch
|
||||
from ringrwkv.configuration_rwkv_world import RwkvConfig
|
||||
from ringrwkv.rwkv_tokenizer import TRIE_TOKENIZER
|
||||
from ringrwkv.modehf_world import RwkvForCausalLM
|
||||
|
||||
model = RwkvForCausalLM.from_pretrained("RWKV-4-World-1.5B")
|
||||
#model = RwkvForCausalLM.from_pretrained("RWKV-4-World-3B")
|
||||
#model = RwkvForCausalLM.from_pretrained("RWKV-4-World-0.4B")
|
||||
tokenizer = TRIE_TOKENIZER('./ringrwkv/rwkv_vocab_v20230424.txt')
|
||||
|
||||
data = ""
|
||||
def question(cont, uid=0):
|
||||
global data
|
||||
prompt = data + f'Question: {cont.strip()}\n\nAnswer:'
|
||||
input_ids = tokenizer.encode(prompt)
|
||||
input_ids = torch.tensor(input_ids).unsqueeze(0)
|
||||
out = model.generate(input_ids,max_new_tokens=20)
|
||||
|
||||
outlist = out[0].tolist()
|
||||
for i in outlist:
|
||||
if i==0:
|
||||
outlist.remove(i)
|
||||
answer = tokenizer.decode(outlist)
|
||||
# data = answer + "\n\n"
|
||||
answer = answer.replace(prompt, "", 1)
|
||||
return answer
|
||||
|
||||
|
||||
94
llm/nlp_xingchen.py
Normal file
94
llm/nlp_xingchen.py
Normal file
@@ -0,0 +1,94 @@
|
||||
import requests
|
||||
import json
|
||||
from utils import util, config_util
|
||||
from core import content_db
|
||||
|
||||
def question(cont, uid=0):
|
||||
url = 'https://nlp.aliyuncs.com/v2/api/chat/send'
|
||||
|
||||
headers = {
|
||||
'accept': '*/*',
|
||||
'Content-Type': 'application/json',
|
||||
'X-AcA-DataInspection': 'disable',
|
||||
'x-fag-servicename': 'aca-chat-send',
|
||||
'x-fag-appcode': 'aca',
|
||||
'Authorization': f"Bearer {config_util.key_xingchen_api_key}"
|
||||
}
|
||||
contentdb = content_db.new_instance()
|
||||
if uid == 0:
|
||||
communication_history = contentdb.get_list('all','desc', 11)
|
||||
else:
|
||||
communication_history = contentdb.get_list('all','desc', 11, uid)
|
||||
#历史记录处理
|
||||
message=[]
|
||||
i = len(communication_history) - 1
|
||||
|
||||
if len(communication_history)>1:
|
||||
while i >= 0:
|
||||
answer_info = dict()
|
||||
if communication_history[i][0] == "member":
|
||||
answer_info["role"] = "user"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
elif communication_history[i][0] == "fay":
|
||||
answer_info["role"] = "assistant"
|
||||
answer_info["content"] = communication_history[i][2]
|
||||
message.append(answer_info)
|
||||
i -= 1
|
||||
else:
|
||||
answer_info = dict()
|
||||
answer_info["role"] = "user"
|
||||
answer_info["content"] = cont
|
||||
message.append(answer_info)
|
||||
data = {
|
||||
"input": {
|
||||
"messages": message,
|
||||
"aca": {
|
||||
"botProfile": {
|
||||
"characterId": config_util.xingchen_characterid,
|
||||
"version": 1
|
||||
},
|
||||
"userProfile": {
|
||||
"userId": "1234567891",
|
||||
"userName": "我",
|
||||
"basicInfo": ""
|
||||
},
|
||||
"scenario": {
|
||||
"description": "你是数字人Fay。用户问你问题的时候回答之前请一步一步想清楚。你的底层AI算法技术是Fay。"
|
||||
},
|
||||
"context": {
|
||||
"useChatHistory": False,
|
||||
"isRegenerate": False,
|
||||
}
|
||||
}
|
||||
},
|
||||
"parameters": {
|
||||
"seed": 1683806810,
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, headers=headers, data=json.dumps(data))
|
||||
if response.status_code == 200:
|
||||
response_data = json.loads(response.text)
|
||||
if response_data.get('success') and 'data' in response_data and 'choices' in response_data['data'] and len(response_data['data']['choices']) > 0:
|
||||
content = response_data['data']['choices'][0]['messages'][0]['content']
|
||||
return content
|
||||
else:
|
||||
util.log(1, "通义星辰调用失败,请检查配置")
|
||||
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
|
||||
return response_text
|
||||
else:
|
||||
util.log(1, f"通义星辰调用失败,请检查配置(错误码:{response.status_code})")
|
||||
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
|
||||
return response_text
|
||||
except Exception as e:
|
||||
util.log(1, f"通义星辰调用失败,请检查配置(错误:{e})")
|
||||
response_text = "抱歉,我现在太忙了,休息一会,请稍后再试。"
|
||||
return response_text
|
||||
|
||||
# # 调用函数测试
|
||||
# result = question("你早")
|
||||
# if result:
|
||||
# print(f"Received response: {result}")
|
||||
# else:
|
||||
# print("Failed to get a valid response.")
|
||||
Reference in New Issue
Block a user