From 24b63bc5bd2e677f7c30831a0e945f9504f29f35 Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Tue, 25 Jul 2023 11:11:02 +0800 Subject: [PATCH] Add Baidu access token validation --- bot/baidu/baidu_wenxin.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bot/baidu/baidu_wenxin.py b/bot/baidu/baidu_wenxin.py index 8835f7a..589e249 100644 --- a/bot/baidu/baidu_wenxin.py +++ b/bot/baidu/baidu_wenxin.py @@ -1,7 +1,6 @@ # encoding:utf-8 import requests, json -import pdb from bot.bot import Bot from bridge.reply import Reply, ReplyType from bot.session_manager import SessionManager @@ -12,7 +11,7 @@ from config import conf from bot.baidu.baidu_wenxin_session import BaiduWenxinSession BAIDU_API_KEY = conf().get("baidu_wenxin_api_key") -BAIDU_SECRET_KEY = conf().get("baidu_wenxin_api_key") +BAIDU_SECRET_KEY = conf().get("baidu_wenxin_secret_key") class BaiduWenxinBot(Bot): @@ -62,7 +61,15 @@ class BaiduWenxinBot(Bot): def reply_text(self, session: BaiduWenxinSession, retry_count=0): try: + logger.info("[BAIDU] model={}".format(session.model)) access_token = self.get_access_token() + if access_token == 'None': + logger.warn("[BAIDU] access token 获取失败") + return { + "total_tokens": 0, + "completion_tokens": 0, + "content": 0, + } url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/" + session.model + "?access_token=" + access_token headers = { 'Content-Type': 'application/json'