Browse Source

feat: support scene without app code in linkai

master
lanvent 1 year ago
parent
commit
d1b867a7c0
5 changed files with 27 additions and 16 deletions
  1. +1
    -1
      bot/chatgpt/chat_gpt_bot.py
  2. +20
    -13
      bot/linkai/link_ai_bot.py
  3. +1
    -1
      bridge/bridge.py
  4. +4
    -1
      config-template.json
  5. +1
    -0
      config.py

+ 1
- 1
bot/chatgpt/chat_gpt_bot.py View File

@@ -36,7 +36,7 @@ class ChatGPTBot(Bot, OpenAIImage):
"model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称 "model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称
"temperature": conf().get("temperature", 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性 "temperature": conf().get("temperature", 0.9), # 值在[0,1]之间,越大表示回复越具有不确定性
# "max_tokens":4096, # 回复最大的字符数 # "max_tokens":4096, # 回复最大的字符数
"top_p": 1,
"top_p": conf().get("top_p", 1),
"frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 "frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
"presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容 "presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
"request_timeout": conf().get("request_timeout", None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间 "request_timeout": conf().get("request_timeout", None), # 请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间


+ 20
- 13
bot/linkai/link_ai_bot.py View File

@@ -1,18 +1,20 @@
# access LinkAI knowledge base platform # access LinkAI knowledge base platform
# docs: https://link-ai.tech/platform/link-app/wechat # docs: https://link-ai.tech/platform/link-app/wechat


import time

import requests

from bot.bot import Bot from bot.bot import Bot
from bridge.reply import Reply, ReplyType
from common.log import logger
from bridge.context import Context
from bot.chatgpt.chat_gpt_session import ChatGPTSession from bot.chatgpt.chat_gpt_session import ChatGPTSession
from bot.session_manager import SessionManager from bot.session_manager import SessionManager
from bridge.context import Context
from bridge.reply import Reply, ReplyType
from common.log import logger
from config import conf from config import conf
import requests
import time


class LinkAIBot(Bot):


class LinkAIBot(Bot):
# authentication failed # authentication failed
AUTH_FAILED_CODE = 401 AUTH_FAILED_CODE = 401
NO_QUOTA_CODE = 406 NO_QUOTA_CODE = 406
@@ -31,22 +33,28 @@ class LinkAIBot(Bot):
return Reply(ReplyType.ERROR, "请再问我一次吧") return Reply(ReplyType.ERROR, "请再问我一次吧")


try: try:
# load config
app_code = conf().get("linkai_app_code")
linkai_api_key = conf().get("linkai_api_key")

session_id = context["session_id"] session_id = context["session_id"]


session = self.sessions.session_query(query, session_id) session = self.sessions.session_query(query, session_id)


# remove system message # remove system message
if session.messages[0].get("role") == "system":
if app_code and session.messages[0].get("role") == "system":
session.messages.pop(0) session.messages.pop(0)


# load config
app_code = conf().get("linkai_app_code")
linkai_api_key = conf().get("linkai_api_key")
logger.info(f"[LINKAI] query={query}, app_code={app_code}") logger.info(f"[LINKAI] query={query}, app_code={app_code}")


body = { body = {
"appCode": app_code, "appCode": app_code,
"messages": session.messages
"messages": session.messages,
"model": conf().get("model") or "gpt-3.5-turbo", # 对话模型的名称
"temperature": conf().get("temperature"),
"top_p": conf().get("top_p", 1),
"frequency_penalty": conf().get("frequency_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
"presence_penalty": conf().get("presence_penalty", 0.0), # [-2,2]之间,该值越大则更倾向于产生不同的内容
} }
headers = {"Authorization": "Bearer " + linkai_api_key} headers = {"Authorization": "Bearer " + linkai_api_key}


@@ -54,13 +62,12 @@ class LinkAIBot(Bot):
res = requests.post(url=self.base_url + "/chat/completion", json=body, headers=headers).json() res = requests.post(url=self.base_url + "/chat/completion", json=body, headers=headers).json()


if not res or not res["success"]: if not res or not res["success"]:

if res.get("code") == self.AUTH_FAILED_CODE: if res.get("code") == self.AUTH_FAILED_CODE:
logger.exception(f"[LINKAI] please check your linkai_api_key, res={res}") logger.exception(f"[LINKAI] please check your linkai_api_key, res={res}")
return Reply(ReplyType.ERROR, "请再问我一次吧") return Reply(ReplyType.ERROR, "请再问我一次吧")


elif res.get("code") == self.NO_QUOTA_CODE: elif res.get("code") == self.NO_QUOTA_CODE:
logger.exception(f"[LINKAI] please check your account quota, https://link-ai.chat/console/account")
logger.exception(f"[LINKAI] please check your account quota, https://chat.link-ai.tech/console/account")
return Reply(ReplyType.ERROR, "提问太快啦,请休息一下再问我吧") return Reply(ReplyType.ERROR, "提问太快啦,请休息一下再问我吧")


else: else:


+ 1
- 1
bridge/bridge.py View File

@@ -23,7 +23,7 @@ class Bridge(object):
self.btype["chat"] = const.OPEN_AI self.btype["chat"] = const.OPEN_AI
if conf().get("use_azure_chatgpt", False): if conf().get("use_azure_chatgpt", False):
self.btype["chat"] = const.CHATGPTONAZURE self.btype["chat"] = const.CHATGPTONAZURE
if conf().get("linkai_api_key") and conf().get("linkai_app_code"):
if conf().get("use_linkai") and conf().get("linkai_api_key"):
self.btype["chat"] = const.LINKAI self.btype["chat"] = const.LINKAI
self.bots = {} self.bots = {}




+ 4
- 1
config-template.json View File

@@ -28,5 +28,8 @@
"conversation_max_tokens": 1000, "conversation_max_tokens": 1000,
"expires_in_seconds": 3600, "expires_in_seconds": 3600,
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。",
"subscribe_msg": "感谢您的关注!\n这里是ChatGPT,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。"
"subscribe_msg": "感谢您的关注!\n这里是ChatGPT,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
} }

+ 1
- 0
config.py View File

@@ -100,6 +100,7 @@ available_setting = {
# 插件配置 # 插件配置
"plugin_trigger_prefix": "$", # 规范插件提供聊天相关指令的前缀,建议不要和管理员指令前缀"#"冲突 "plugin_trigger_prefix": "$", # 规范插件提供聊天相关指令的前缀,建议不要和管理员指令前缀"#"冲突
# 知识库平台配置 # 知识库平台配置
"use_linkai": False,
"linkai_api_key": "", "linkai_api_key": "",
"linkai_app_code": "" "linkai_app_code": ""
} }


Loading…
Cancel
Save