olivebot/llm/nlp_lingju.py
guo zebin 4cfad5ae0f 年翻更新
- 全新ui
- 全面优化websocket逻辑,提高数字人和ui连接的稳定性及资源开销
- 全面优化唤醒逻辑,提供稳定的普通唤醒模式和前置词唤醒模式
- 优化拾音质量,支持多声道麦克风拾音
- 优化自动播放服务器的对接机制,提供稳定和兼容旧版ue工程的对接模式
- 数字人接口输出机器人表情,以适应新fay ui及单片机的数字人表情输出
- 使用更高级的音频时长计算方式,可以更精准控制音频播放完成后的逻辑
- 修复点击关闭按钮会导致程序退出的bug
- 修复没有麦克风的设备开启麦克风会出错的问题
- 为服务器主机地址提供配置项,以方便服务器部署
2024-10-26 11:34:55 +08:00

101 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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, "北京"