mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 20:14:36 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b8c5a3499 | ||
|
|
53dde0607d | ||
|
|
7f034b4ffa | ||
|
|
599ab83100 | ||
|
|
f4a3508ec2 | ||
|
|
44b92909eb | ||
|
|
8ed07b8d1a | ||
|
|
2ff9ced15e | ||
|
|
641b8d71ed |
28
README.md
28
README.md
@@ -77,3 +77,31 @@
|
||||
- WebUI Demo: https://demo.langbot.dev/
|
||||
- 登录信息:邮箱:`demo@langbot.app` 密码:`langbot123456`
|
||||
- 注意:仅展示webui效果,公开环境,请不要在其中填入您的任何敏感信息。
|
||||
|
||||
## 🔌 组件兼容性
|
||||
|
||||
### 消息平台
|
||||
|
||||
| 平台 | 状态 | 备注 |
|
||||
| --- | --- | --- |
|
||||
| OneBot v11 | ✅ | QQ 个人号私聊、群聊 |
|
||||
| go-cqhttp | ✅ | QQ 个人号私聊、群聊 |
|
||||
| QQ 官方 API | ✅ | QQ 频道机器人,支持频道、私聊、群聊 |
|
||||
| 企业微信 | 🚧 | |
|
||||
| 钉钉 | 🚧 | |
|
||||
|
||||
🚧: 正在开发中
|
||||
|
||||
### 大模型
|
||||
|
||||
| 模型 | 状态 | 备注 |
|
||||
| --- | --- | --- |
|
||||
| [OpenAI](https://platform.openai.com/) | ✅ | 可接入任何 OpenAI 接口格式模型 |
|
||||
| [DeepSeek](https://www.deepseek.com/) | ✅ | |
|
||||
| [Moonshot](https://www.moonshot.cn/) | ✅ | |
|
||||
| [Anthropic](https://www.anthropic.com/) | ✅ | |
|
||||
| [xAI](https://x.ai/) | ✅ | |
|
||||
| [智谱AI](https://open.bigmodel.cn/) | ✅ | |
|
||||
| [Dify](https://dify.ai) | ✅ | LLMOps 平台 |
|
||||
| [Ollama](https://ollama.com/) | ✅ | 本地大模型管理平台 |
|
||||
| [GiteeAI](https://ai.gitee.com/) | ✅ | 大模型接口聚合平台 |
|
||||
|
||||
25
pkg/core/migrations/m019_zhipuai_config.py
Normal file
25
pkg/core/migrations/m019_zhipuai_config.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .. import migration
|
||||
|
||||
|
||||
@migration.migration_class("zhipuai-config", 19)
|
||||
class ZhipuaiConfigMigration(migration.Migration):
|
||||
"""迁移"""
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
return 'zhipuai-chat-completions' not in self.ap.provider_cfg.data['requester']
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
self.ap.provider_cfg.data['requester']['zhipuai-chat-completions'] = {
|
||||
"base-url": "https://open.bigmodel.cn/api/paas/v4",
|
||||
"args": {},
|
||||
"timeout": 120
|
||||
}
|
||||
self.ap.provider_cfg.data['keys']['zhipuai'] = [
|
||||
"xxxxxxx"
|
||||
]
|
||||
|
||||
await self.ap.provider_cfg.dump_config()
|
||||
@@ -7,7 +7,7 @@ from .. import migration
|
||||
from ..migrations import m001_sensitive_word_migration, m002_openai_config_migration, m003_anthropic_requester_cfg_completion, m004_moonshot_cfg_completion
|
||||
from ..migrations import m005_deepseek_cfg_completion, m006_vision_config, m007_qcg_center_url, m008_ad_fixwin_config_migrate, m009_msg_truncator_cfg
|
||||
from ..migrations import m010_ollama_requester_config, m011_command_prefix_config, m012_runner_config, m013_http_api_config, m014_force_delay_config
|
||||
from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_api_timeout_params, m018_xai_config
|
||||
from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_api_timeout_params, m018_xai_config, m019_zhipuai_config
|
||||
|
||||
|
||||
@stage.stage_class("MigrationStage")
|
||||
|
||||
@@ -9,6 +9,7 @@ from . import events
|
||||
from ..provider.tools import entities as tools_entities
|
||||
from ..core import app
|
||||
from ..platform.types import message as platform_message
|
||||
from ..platform import adapter as platform_adapter
|
||||
|
||||
|
||||
def register(
|
||||
@@ -113,6 +114,37 @@ class APIHost:
|
||||
async def initialize(self):
|
||||
pass
|
||||
|
||||
# ========== 插件可调用的 API(主程序API) ==========
|
||||
|
||||
def get_platform_adapters(self) -> list[platform_adapter.MessageSourceAdapter]:
|
||||
"""获取已启用的消息平台适配器列表
|
||||
|
||||
Returns:
|
||||
list[platform.adapter.MessageSourceAdapter]: 已启用的消息平台适配器列表
|
||||
"""
|
||||
return self.ap.platform_mgr.adapters
|
||||
|
||||
async def send_active_message(
|
||||
self,
|
||||
adapter: platform_adapter.MessageSourceAdapter,
|
||||
target_type: str,
|
||||
target_id: str,
|
||||
message: platform_message.MessageChain,
|
||||
):
|
||||
"""发送主动消息
|
||||
|
||||
Args:
|
||||
adapter (platform.adapter.MessageSourceAdapter): 消息平台适配器对象,调用 host.get_platform_adapters() 获取并取用其中某个
|
||||
target_type (str): 目标类型,`person`或`group`
|
||||
target_id (str): 目标ID
|
||||
message (platform.types.MessageChain): 消息链
|
||||
"""
|
||||
await adapter.send_message(
|
||||
target_type=target_type,
|
||||
target_id=target_id,
|
||||
message=message,
|
||||
)
|
||||
|
||||
def require_ver(
|
||||
self,
|
||||
ge: str,
|
||||
|
||||
@@ -6,7 +6,7 @@ from . import entities, requester
|
||||
from ...core import app
|
||||
|
||||
from . import token
|
||||
from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl
|
||||
from .requesters import chatcmpl, anthropicmsgs, moonshotchatcmpl, deepseekchatcmpl, ollamachat, giteeaichatcmpl, xaichatcmpl, zhipuaichatcmpl
|
||||
|
||||
FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list"
|
||||
|
||||
|
||||
@@ -1,23 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import typing
|
||||
import json
|
||||
import base64
|
||||
from typing import AsyncGenerator
|
||||
|
||||
import openai
|
||||
import openai.types.chat.chat_completion as chat_completion
|
||||
import httpx
|
||||
import aiohttp
|
||||
import async_lru
|
||||
|
||||
from . import chatcmpl
|
||||
from .. import entities, errors, requester
|
||||
from ....core import entities as core_entities, app
|
||||
from ... import entities as llm_entities
|
||||
from ...tools import entities as tools_entities
|
||||
from ....utils import image
|
||||
from .. import requester
|
||||
from ....core import app
|
||||
|
||||
|
||||
@requester.requester_class("xai-chat-completions")
|
||||
@@ -32,114 +19,3 @@ class XaiChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
self.ap = ap
|
||||
|
||||
self.requester_cfg = self.ap.provider_cfg.data['requester']['xai-chat-completions']
|
||||
|
||||
# async def initialize(self):
|
||||
|
||||
# self.client = openai.AsyncClient(
|
||||
# api_key="",
|
||||
# base_url=self.requester_cfg['base-url'],
|
||||
# timeout=self.requester_cfg['timeout'],
|
||||
# http_client=httpx.AsyncClient(
|
||||
# proxies=self.ap.proxy_mgr.get_forward_proxies()
|
||||
# )
|
||||
# )
|
||||
|
||||
# async def _req(
|
||||
# self,
|
||||
# args: dict,
|
||||
# ) -> chat_completion.ChatCompletion:
|
||||
# return await self.client.chat.completions.create(**args)
|
||||
|
||||
# async def _make_msg(
|
||||
# self,
|
||||
# chat_completion: chat_completion.ChatCompletion,
|
||||
# ) -> llm_entities.Message:
|
||||
# chatcmpl_message = chat_completion.choices[0].message.dict()
|
||||
|
||||
# # 确保 role 字段存在且不为 None
|
||||
# if 'role' not in chatcmpl_message or chatcmpl_message['role'] is None:
|
||||
# chatcmpl_message['role'] = 'assistant'
|
||||
|
||||
# message = llm_entities.Message(**chatcmpl_message)
|
||||
|
||||
# return message
|
||||
|
||||
# async def _closure(
|
||||
# self,
|
||||
# req_messages: list[dict],
|
||||
# use_model: entities.LLMModelInfo,
|
||||
# use_funcs: list[tools_entities.LLMFunction] = None,
|
||||
# ) -> llm_entities.Message:
|
||||
# self.client.api_key = use_model.token_mgr.get_token()
|
||||
|
||||
# args = self.requester_cfg['args'].copy()
|
||||
# args["model"] = use_model.name if use_model.model_name is None else use_model.model_name
|
||||
|
||||
# if use_funcs:
|
||||
# tools = await self.ap.tool_mgr.generate_tools_for_openai(use_funcs)
|
||||
|
||||
# if tools:
|
||||
# args["tools"] = tools
|
||||
|
||||
# # 设置此次请求中的messages
|
||||
# messages = req_messages.copy()
|
||||
|
||||
# # 检查vision
|
||||
# for msg in messages:
|
||||
# if 'content' in msg and isinstance(msg["content"], list):
|
||||
# for me in msg["content"]:
|
||||
# if me["type"] == "image_url":
|
||||
# me["image_url"]['url'] = await self.get_base64_str(me["image_url"]['url'])
|
||||
|
||||
# args["messages"] = messages
|
||||
|
||||
# # 发送请求
|
||||
# resp = await self._req(args)
|
||||
|
||||
# # 处理请求结果
|
||||
# message = await self._make_msg(resp)
|
||||
|
||||
# return message
|
||||
|
||||
# async def call(
|
||||
# self,
|
||||
# model: entities.LLMModelInfo,
|
||||
# messages: typing.List[llm_entities.Message],
|
||||
# funcs: typing.List[tools_entities.LLMFunction] = None,
|
||||
# ) -> llm_entities.Message:
|
||||
# req_messages = [] # req_messages 仅用于类内,外部同步由 query.messages 进行
|
||||
# for m in messages:
|
||||
# msg_dict = m.dict(exclude_none=True)
|
||||
# content = msg_dict.get("content")
|
||||
# if isinstance(content, list):
|
||||
# # 检查 content 列表中是否每个部分都是文本
|
||||
# if all(isinstance(part, dict) and part.get("type") == "text" for part in content):
|
||||
# # 将所有文本部分合并为一个字符串
|
||||
# msg_dict["content"] = "\n".join(part["text"] for part in content)
|
||||
# req_messages.append(msg_dict)
|
||||
|
||||
# try:
|
||||
# return await self._closure(req_messages, model, funcs)
|
||||
# except asyncio.TimeoutError:
|
||||
# raise errors.RequesterError('请求超时')
|
||||
# except openai.BadRequestError as e:
|
||||
# if 'context_length_exceeded' in e.message:
|
||||
# raise errors.RequesterError(f'上文过长,请重置会话: {e.message}')
|
||||
# else:
|
||||
# raise errors.RequesterError(f'请求参数错误: {e.message}')
|
||||
# except openai.AuthenticationError as e:
|
||||
# raise errors.RequesterError(f'无效的 api-key: {e.message}')
|
||||
# except openai.NotFoundError as e:
|
||||
# raise errors.RequesterError(f'请求路径错误: {e.message}')
|
||||
# except openai.RateLimitError as e:
|
||||
# raise errors.RequesterError(f'请求过于频繁或余额不足: {e.message}')
|
||||
# except openai.APIError as e:
|
||||
# raise errors.RequesterError(f'请求错误: {e.message}')
|
||||
|
||||
# @async_lru.alru_cache(maxsize=128)
|
||||
# async def get_base64_str(
|
||||
# self,
|
||||
# original_url: str,
|
||||
# ) -> str:
|
||||
# base64_image, image_format = await image.qq_image_url_to_base64(original_url)
|
||||
# return f"data:image/{image_format};base64,{base64_image}"
|
||||
|
||||
21
pkg/provider/modelmgr/requesters/zhipuaichatcmpl.py
Normal file
21
pkg/provider/modelmgr/requesters/zhipuaichatcmpl.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import openai
|
||||
|
||||
from ....core import app
|
||||
from . import chatcmpl
|
||||
from .. import requester
|
||||
|
||||
|
||||
@requester.requester_class("zhipuai-chat-completions")
|
||||
class ZhipuAIChatCompletions(chatcmpl.OpenAIChatCompletions):
|
||||
"""智谱AI ChatCompletion API 请求器"""
|
||||
|
||||
client: openai.AsyncClient
|
||||
|
||||
requester_cfg: dict
|
||||
|
||||
def __init__(self, ap: app.Application):
|
||||
self.ap = ap
|
||||
|
||||
self.requester_cfg = self.ap.provider_cfg.data['requester']['zhipuai-chat-completions']
|
||||
@@ -5,6 +5,8 @@ import json
|
||||
import uuid
|
||||
import base64
|
||||
|
||||
import aiohttp
|
||||
|
||||
from .. import runner
|
||||
from ...core import entities as core_entities
|
||||
from .. import entities as llm_entities
|
||||
@@ -97,7 +99,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
files=files,
|
||||
timeout=self.ap.provider_cfg.data["dify-service-api"]["chat"]["timeout"],
|
||||
):
|
||||
self.ap.logger.debug("dify-chat-chunk: ", chunk)
|
||||
self.ap.logger.debug("dify-chat-chunk: " + str(chunk))
|
||||
|
||||
if chunk['event'] == 'workflow_started':
|
||||
mode = "workflow"
|
||||
@@ -149,7 +151,8 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
files=files,
|
||||
timeout=self.ap.provider_cfg.data["dify-service-api"]["chat"]["timeout"],
|
||||
):
|
||||
self.ap.logger.debug("dify-agent-chunk: ", chunk)
|
||||
self.ap.logger.debug("dify-agent-chunk: " + str(chunk))
|
||||
|
||||
if chunk["event"] in ignored_events:
|
||||
continue
|
||||
if chunk["event"] == "agent_thought":
|
||||
@@ -179,6 +182,21 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
],
|
||||
)
|
||||
yield msg
|
||||
if chunk['event'] == 'message_file':
|
||||
|
||||
if chunk['type'] == 'image' and chunk['belongs_to'] == 'assistant':
|
||||
|
||||
base_url = self.dify_client.base_url
|
||||
|
||||
if base_url.endswith('/v1'):
|
||||
base_url = base_url[:-3]
|
||||
|
||||
image_url = base_url + chunk['url']
|
||||
|
||||
yield llm_entities.Message(
|
||||
role="assistant",
|
||||
content=[llm_entities.ContentElement.from_image_url(image_url)],
|
||||
)
|
||||
|
||||
query.session.using_conversation.uuid = chunk["conversation_id"]
|
||||
|
||||
@@ -215,7 +233,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
|
||||
files=files,
|
||||
timeout=self.ap.provider_cfg.data["dify-service-api"]["workflow"]["timeout"],
|
||||
):
|
||||
self.ap.logger.debug("dify-workflow-chunk: ", chunk)
|
||||
self.ap.logger.debug("dify-workflow-chunk: " + str(chunk))
|
||||
if chunk["event"] in ignored_events:
|
||||
continue
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
semantic_version = "v3.4.2"
|
||||
semantic_version = "v3.4.2.1"
|
||||
|
||||
debug_mode = False
|
||||
|
||||
|
||||
@@ -147,6 +147,65 @@
|
||||
"name": "grok-beta",
|
||||
"requester": "xai-chat-completions",
|
||||
"token_mgr": "xai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4-plus",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4-0520",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4-air",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4-airx",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4-long",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4-flashx",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4-flash",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai"
|
||||
},
|
||||
{
|
||||
"name": "glm-4v-plus",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4v",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-4v-flash",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
},
|
||||
{
|
||||
"name": "glm-zero-preview",
|
||||
"requester": "zhipuai-chat-completions",
|
||||
"token_mgr": "zhipuai",
|
||||
"vision_supported": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -19,6 +19,9 @@
|
||||
],
|
||||
"xai": [
|
||||
"xai-1234567890"
|
||||
],
|
||||
"zhipuai": [
|
||||
"xxxxxxx"
|
||||
]
|
||||
},
|
||||
"requester": {
|
||||
@@ -58,6 +61,11 @@
|
||||
"base-url": "https://api.x.ai/v1",
|
||||
"args": {},
|
||||
"timeout": 120
|
||||
},
|
||||
"zhipuai-chat-completions": {
|
||||
"base-url": "https://open.bigmodel.cn/api/paas/v4",
|
||||
"args": {},
|
||||
"timeout": 120
|
||||
}
|
||||
},
|
||||
"model": "gpt-4o",
|
||||
|
||||
@@ -66,6 +66,14 @@
|
||||
"type": "string"
|
||||
},
|
||||
"default": []
|
||||
},
|
||||
"zhipuai": {
|
||||
"type": "array",
|
||||
"title": "智谱AI API 密钥",
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"default": []
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -210,6 +218,23 @@
|
||||
"default": 120
|
||||
}
|
||||
}
|
||||
},
|
||||
"zhipuai-chat-completions": {
|
||||
"type": "object",
|
||||
"title": "智谱AI API 请求配置",
|
||||
"description": "仅可编辑 URL 和 超时时间,额外请求参数不支持可视化编辑,请到编辑器编辑",
|
||||
"properties": {
|
||||
"base-url": {
|
||||
"type": "string",
|
||||
"title": "API URL"
|
||||
},
|
||||
"args": {
|
||||
"type": "object"
|
||||
},
|
||||
"timeout": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user