Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接入 在线版qanything;audio修改成员变量到类变量(后期应用使用 #645

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,12 @@
"history_max_len": 4096
},
"qanything": {
"type": "online",
"app_key": "",
"app_secret": "",
"api_ip_port": "http://127.0.0.1:8777",
"user_id": "zzp",
"kb_ids": ["KB2435554f1fb348ad84a1eb60eaa1c466"],
"kb_ids": ["KB938fb10c8a924530abf1754ea620b7cd"],
"history_enable": true,
"history_max_len": 300
},
Expand Down
5 changes: 4 additions & 1 deletion config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,12 @@
"history_max_len": 4096
},
"qanything": {
"type": "online",
"app_key": "",
"app_secret": "",
"api_ip_port": "http://127.0.0.1:8777",
"user_id": "zzp",
"kb_ids": ["KB2435554f1fb348ad84a1eb60eaa1c466"],
"kb_ids": ["KB938fb10c8a924530abf1754ea620b7cd"],
"history_enable": true,
"history_max_len": 300
},
Expand Down
235 changes: 207 additions & 28 deletions tests/test_qanything/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import requests
from urllib.parse import urljoin

import hashlib
import time
import uuid

# from utils.common import Common
# from utils.logger import Configure_logger

Expand Down Expand Up @@ -38,6 +42,170 @@ def get_list_knowledge_base(self):
return None


# 官方在线API
'''
添加鉴权相关参数 -
appKey : 应用ID
salt : 随机值
curtime : 当前时间戳(秒)
signType : 签名版本
sign : 请求签名
@param appKey 您的应用ID
@param appSecret 您的应用密钥
@param paramsMap 请求参数表
'''
def addAuthParams(self, appKey, appSecret, params):
def returnAuthMap(appKey, appSecret, q):
salt = str(uuid.uuid1())
curtime = str(int(time.time()))
sign = calculateSign(appKey, appSecret, q, salt, curtime)
params = {'appKey': appKey,
'salt': salt,
'curtime': curtime,
'signType': 'v3',
'sign': sign}
return params


'''
计算鉴权签名 -
计算方式 : sign = sha256(appKey + input(q) + salt + curtime + appSecret)
@param appKey 您的应用ID
@param appSecret 您的应用密钥
@param q 请求内容
@param salt 随机值
@param curtime 当前时间戳(秒)
@return 鉴权签名sign
'''
def calculateSign(appKey, appSecret, q, salt, curtime):
strSrc = appKey + getInput(q) + salt + curtime + appSecret
return encrypt(strSrc)


def encrypt(strSrc):
hash_algorithm = hashlib.sha256()
hash_algorithm.update(strSrc.encode('utf-8'))
return hash_algorithm.hexdigest()


def getInput(input):
if input is None:
return input
inputLen = len(input)
return input if inputLen <= 20 else input[0:10] + str(inputLen) + input[inputLen - 10:inputLen]



q = params.get('q')
if q is None:
q = params.get('img')
q = "".join(q)
salt = str(uuid.uuid1())
curtime = str(int(time.time()))
sign = calculateSign(appKey, appSecret, q, salt, curtime)
params['appKey'] = appKey
params['salt'] = salt
params['curtime'] = curtime
params['signType'] = 'v3'
params['sign'] = sign

return params



def createKB(self, kbName):
data = {'q': kbName}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.info('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/create_kb', header, json.dumps(data), 'post')
logging.info(str(res.content, 'utf-8'))


def deleteKB(self, kbId):
data = {'q': kbId}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.info('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/delete_kb', header, json.dumps(data), 'post')
logging.info(str(res.content, 'utf-8'))


def uploadDoc(self, kbId, file):
data = {'q': kbId}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
res = requests.post('https://openapi.youdao.com/q_anything/paas/upload_file', data=data, files={'file': file})
logging.info(str(res.content, 'utf-8'))


def uploadUrl(self, kbId, url):
data = {'q': kbId, 'url': url}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.info('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/upload_url', header, json.dumps(data), 'post')
logging.info(str(res.content, 'utf-8'))


def deleteFile(self, kbId, fileId):
data = {'q': kbId, 'fileIds': [fileId]}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.info('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/delete_file', header, json.dumps(data), 'post')
logging.info(str(res.content, 'utf-8'))


def kbList(self):
data = {'q': ''}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.debug('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/kb_list', header, json.dumps(data), 'post')
logging.info(str(res.content, 'utf-8'))


def fileList(self, kbId):
data = {'q': kbId}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.info('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/file_list', header, json.dumps(data), 'post')
logging.info(str(res.content, 'utf-8'))


def chat(self, kbId, q):
try:
data = {'q': q, 'kbIds': [kbId]}
logging.debug(f"data={data}")
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.debug('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/chat', header, json.dumps(data), 'post')
logging.debug(str(res.content, 'utf-8'))

return json.loads(str(res.content, 'utf-8'))
except Exception as e:
logging.error(e)
return None

def chatStream(self, kbId, q):
data = {'q': q, 'kbIds': [kbId]}
data = self.addAuthParams(self.config_data["app_key"], self.config_data["app_secret"], data)
header = {'Content-Type': 'application/json'}
logging.debug('请求参数:' + json.dumps(data))
res = self.doCall('https://openapi.youdao.com/q_anything/paas/chat_stream', header, json.dumps(data), 'post')
logging.debug(str(res.content, 'utf-8'))


def doCall(self, url, header, params, method):
if 'get' == method:
return requests.get(url, params)
elif 'post' == method:
return requests.post(url, params, headers=header)


def get_resp(self, data):
"""请求对应接口,获取返回值
Expand All @@ -48,40 +216,45 @@ def get_resp(self, data):
str: 返回的文本回答
"""
try:
url = self.api_ip_port + "/api/local_doc_qa/local_doc_chat"
if self.config_data["type"] == "online":
resp_json = self.chat(self.config_data["kb_ids"][0], data["prompt"])

data_json = {
"user_id": self.config_data["user_id"],
"kb_ids": self.config_data["kb_ids"],
"question": data["prompt"],
"history": self.history
}
return resp_json["result"]["response"]
elif self.config_data["type"] == "local":
url = self.api_ip_port + "/api/local_doc_qa/local_doc_chat"

response = requests.post(url=url, json=data_json)
response.raise_for_status() # 检查响应的状态码
data_json = {
"user_id": self.config_data["user_id"],
"kb_ids": self.config_data["kb_ids"],
"question": data["prompt"],
"history": self.history
}

result = response.content
ret = json.loads(result)
response = requests.post(url=url, json=data_json)
response.raise_for_status() # 检查响应的状态码

result = response.content
ret = json.loads(result)

logging.info(ret)
logging.info(ret)

resp_content = ret["response"]
resp_content = ret["response"]

# 启用历史就给我记住!
if self.config_data["history_enable"]:
self.history = ret["history"]
# 启用历史就给我记住!
if self.config_data["history_enable"]:
self.history = ret["history"]

while True:
# 计算所有字符数
total_chars = sum(len(item) for sublist in self.history for item in sublist)
while True:
# 计算所有字符数
total_chars = sum(len(item) for sublist in self.history for item in sublist)

# 如果大于限定最大历史数,就剔除第一个元素
if total_chars > self.config_data["history_max_len"]:
self.history.pop(0)
else:
break
# 如果大于限定最大历史数,就剔除第一个元素
if total_chars > self.config_data["history_max_len"]:
self.history.pop(0)
else:
break

return resp_content
return resp_content
except Exception as e:
logging.error(e)
return None
Expand All @@ -96,16 +269,22 @@ def get_resp(self, data):
)

data = {
"type": "online",
"app_key": "",
"app_secret": "",
"api_ip_port": "http://127.0.0.1:8777",
"user_id": "zzp",
"kb_ids": ["KB2435554f1fb348ad84a1eb60eaa1c466"],
"kb_ids": ["KBace3c3bb8c204432b4dfb0ba77a8552e", "KB2435554f1fb348ad84a1eb60eaa1c466"],
"history_enable": True,
"history_max_len": 300
}
qanything = QAnything(data)

if data["type"] == "online":
qanything.kbList()
elif data["type"] == "local":
qanything.get_list_knowledge_base()

qanything.get_list_knowledge_base()
logging.info(qanything.get_resp({"prompt": "伊卡洛斯和妮姆芙的关系"}))
logging.info(qanything.get_resp({"prompt": "伊卡洛斯的英文名"}))
# logging.info(qanything.get_resp({"prompt": "伊卡洛斯的英文名"}))

Loading
Loading