diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000000..a6a922a221b --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,17 @@ +version: 2 +updates: + - package-ecosystem: "pip" + directory: "/" + schedule: + interval: "weekly" + timezone: "Asia/Shanghai" + day: "friday" + target-branch: "v2" + groups: + python-dependencies: + patterns: + - "*" +# ignore: +# - dependency-name: "pymupdf" +# versions: ["*"] + diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml index 26d2b86d297..1e1daf2696c 100644 --- a/.github/workflows/build-and-push.yml +++ b/.github/workflows/build-and-push.yml @@ -7,7 +7,7 @@ on: inputs: dockerImageTag: description: 'Image Tag' - default: 'v1.10.3-dev' + default: 'v1.10.7-dev' required: true dockerImageTagWithLatest: description: '是否发布latest tag(正式发版时选择,测试版本切勿选择)' @@ -36,7 +36,7 @@ on: jobs: build-and-push-to-fit2cloud-registry: if: ${{ contains(github.event.inputs.registry, 'fit2cloud') }} - runs-on: ubuntu-22.04 + runs-on: ubuntu-latest steps: - name: Check Disk Space run: df -h @@ -52,10 +52,6 @@ jobs: swap-storage: true - name: Check Disk Space run: df -h - - name: Set Swap Space - uses: pierotofy/set-swap-space@master - with: - swap-size-gb: 8 - name: Checkout uses: actions/checkout@v4 with: @@ -68,24 +64,17 @@ jobs: TAG_NAME=${{ github.event.inputs.dockerImageTag }} TAG_NAME_WITH_LATEST=${{ github.event.inputs.dockerImageTagWithLatest }} if [[ ${TAG_NAME_WITH_LATEST} == 'true' ]]; then - DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME} --tag ${DOCKER_IMAGE}:latest" + DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME} --tag ${DOCKER_IMAGE}:${TAG_NAME%%.*}" else DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME}" fi echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} --memory-swap -1 \ - --build-arg DOCKER_IMAGE_TAG=${{ github.event.inputs.dockerImageTag }} --build-arg BUILD_AT=$(TZ=Asia/Shanghai date +'%Y-%m-%dT%H:%M') --build-arg GITHUB_COMMIT=${GITHUB_SHA::8} --no-cache \ + --build-arg DOCKER_IMAGE_TAG=${{ github.event.inputs.dockerImageTag }} --build-arg BUILD_AT=$(TZ=Asia/Shanghai date +'%Y-%m-%dT%H:%M') --build-arg GITHUB_COMMIT=`git rev-parse --short HEAD` --no-cache \ ${DOCKER_IMAGE_TAGS} . - name: Set up QEMU uses: docker/setup-qemu-action@v3 - with: - # Until https://github.com/tonistiigi/binfmt/issues/215 - image: tonistiigi/binfmt:qemu-v7.0.0-28 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - with: - buildkitd-config-inline: | - [worker.oci] - max-parallelism = 1 - name: Login to GitHub Container Registry uses: docker/login-action@v3 with: @@ -100,11 +89,12 @@ jobs: password: ${{ secrets.FIT2CLOUD_REGISTRY_PASSWORD }} - name: Docker Buildx (build-and-push) run: | + sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && free -m docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }} -f installer/Dockerfile build-and-push-to-dockerhub: if: ${{ contains(github.event.inputs.registry, 'dockerhub') }} - runs-on: ubuntu-22.04 + runs-on: ubuntu-latest steps: - name: Check Disk Space run: df -h @@ -120,10 +110,6 @@ jobs: swap-storage: true - name: Check Disk Space run: df -h - - name: Set Swap Space - uses: pierotofy/set-swap-space@master - with: - swap-size-gb: 8 - name: Checkout uses: actions/checkout@v4 with: @@ -136,24 +122,17 @@ jobs: TAG_NAME=${{ github.event.inputs.dockerImageTag }} TAG_NAME_WITH_LATEST=${{ github.event.inputs.dockerImageTagWithLatest }} if [[ ${TAG_NAME_WITH_LATEST} == 'true' ]]; then - DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME} --tag ${DOCKER_IMAGE}:latest" + DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME} --tag ${DOCKER_IMAGE}:${TAG_NAME%%.*}" else DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME}" fi echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} --memory-swap -1 \ - --build-arg DOCKER_IMAGE_TAG=${{ github.event.inputs.dockerImageTag }} --build-arg BUILD_AT=$(TZ=Asia/Shanghai date +'%Y-%m-%dT%H:%M') --build-arg GITHUB_COMMIT=${GITHUB_SHA::8} --no-cache \ + --build-arg DOCKER_IMAGE_TAG=${{ github.event.inputs.dockerImageTag }} --build-arg BUILD_AT=$(TZ=Asia/Shanghai date +'%Y-%m-%dT%H:%M') --build-arg GITHUB_COMMIT=`git rev-parse --short HEAD` --no-cache \ ${DOCKER_IMAGE_TAGS} . - name: Set up QEMU uses: docker/setup-qemu-action@v3 - with: - # Until https://github.com/tonistiigi/binfmt/issues/215 - image: tonistiigi/binfmt:qemu-v7.0.0-28 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - with: - buildkitd-config-inline: | - [worker.oci] - max-parallelism = 1 - name: Login to GitHub Container Registry uses: docker/login-action@v3 with: @@ -167,4 +146,5 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Docker Buildx (build-and-push) run: | + sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && free -m docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }} -f installer/Dockerfile diff --git a/README.md b/README.md index cfe819e56ff..b4a925edb64 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@

MaxKB

-

Ready-to-use AI Chatbot

+

Open-source platform for building enterprise-grade agents

+

强大易用的企业级智能体平台

1Panel-dev%2FMaxKB | Trendshift

License: GPL v3 @@ -10,10 +11,10 @@


-MaxKB = Max Knowledge Base, it is a ready-to-use AI chatbot that integrates Retrieval-Augmented Generation (RAG) pipelines, supports robust workflows, and provides advanced MCP tool-use capabilities. MaxKB is widely applied in scenarios such as intelligent customer service, corporate internal knowledge bases, academic research, and education. +MaxKB = Max Knowledge Brain, it is an open-source platform for building enterprise-grade agents. MaxKB integrates Retrieval-Augmented Generation (RAG) pipelines, supports robust workflows, and provides advanced MCP tool-use capabilities. MaxKB is widely applied in scenarios such as intelligent customer service, corporate internal knowledge bases, academic research, and education. -- **RAG Pipeline**: Supports direct uploading of documents / automatic crawling of online documents, with features for automatic text splitting, vectorization, and RAG (Retrieval-Augmented Generation). This effectively reduces hallucinations in large models, providing a superior smart Q&A interaction experience. -- **Flexible Orchestration**: Equipped with a powerful workflow engine, function library and MCP tool-use, enabling the orchestration of AI processes to meet the needs of complex business scenarios. +- **RAG Pipeline**: Supports direct uploading of documents / automatic crawling of online documents, with features for automatic text splitting, vectorization. This effectively reduces hallucinations in large models, providing a superior smart Q&A interaction experience. +- **Agentic Workflow**: Equipped with a powerful workflow engine, function library and MCP tool-use, enabling the orchestration of AI processes to meet the needs of complex business scenarios. - **Seamless Integration**: Facilitates zero-coding rapid integration into third-party business systems, quickly equipping existing systems with intelligent Q&A capabilities to enhance user satisfaction. - **Model-Agnostic**: Supports various large models, including private models (such as DeepSeek, Llama, Qwen, etc.) and public models (like OpenAI, Claude, Gemini, etc.). - **Multi Modal**: Native support for input and output text, image, audio and video. @@ -23,7 +24,7 @@ MaxKB = Max Knowledge Base, it is a ready-to-use AI chatbot that integrates Retr Execute the script below to start a MaxKB container using Docker: ```bash -docker run -d --name=maxkb --restart=always -p 8080:8080 -v ~/.maxkb:/var/lib/postgresql/data -v ~/.python-packages:/opt/maxkb/app/sandbox/python-packages 1panel/maxkb +docker run -d --name=maxkb --restart=always -p 8080:8080 -v ~/.maxkb:/var/lib/postgresql/data -v ~/.python-packages:/opt/maxkb/app/sandbox/python-packages 1panel/maxkb:v1 ``` Access MaxKB web interface at `http://your_server_ip:8080` with default admin credentials: @@ -31,7 +32,7 @@ Access MaxKB web interface at `http://your_server_ip:8080` with default admin cr - username: admin - password: MaxKB@123.. -中国用户如遇到 Docker 镜像 Pull 失败问题,请参照该 [离线安装文档](https://maxkb.cn/docs/installation/offline_installtion/) 进行安装。 +中国用户如遇到 Docker 镜像 Pull 失败问题,请参照该 [离线安装文档](https://maxkb.cn/docs/v1/installation/offline_installtion/) 进行安装。 ## Screenshots @@ -55,8 +56,6 @@ Access MaxKB web interface at `http://your_server_ip:8080` with default admin cr ## Feature Comparison -MaxKB is positioned as an Ready-to-use RAG (Retrieval-Augmented Generation) intelligent Q&A application, rather than a middleware platform for building large model applications. The following table is merely a comparison from a functional perspective. - diff --git a/README_CN.md b/README_CN.md index e55150902ea..07fa00ea4e6 100644 --- a/README_CN.md +++ b/README_CN.md @@ -1,25 +1,25 @@

MaxKB

-

基于大模型和 RAG 的知识库问答系统

-

Ready-to-use, flexible RAG Chatbot

+

强大易用的企业级智能体平台

1Panel-dev%2FMaxKB | Trendshift - 1Panel-dev%2FMaxKB | Aliyun

English README - License: GPL v3 + License: GPL v3 Latest release - Stars - Download + Stars + Download + Gitee Stars + GitCode Stars


-MaxKB = Max Knowledge Base,是一款开箱即用的 RAG Chatbot,具备强大的工作流和 MCP 工具调用能力。它支持对接各种主流大语言模型(LLMs),广泛应用于智能客服、企业内部知识库、学术研究与教育等场景。 +MaxKB = Max Knowledge Brain,是一个强大易用的企业级智能体平台,致力于解决企业 AI 落地面临的技术门槛高、部署成本高、迭代周期长等问题,助力企业在人工智能时代赢得先机。秉承“开箱即用,伴随成长”的设计理念,MaxKB 支持企业快速接入主流大模型,高效构建专属知识库,并提供从基础问答(RAG)、复杂流程自动化(工作流)到智能体(Agent)的渐进式升级路径,全面赋能智能客服、智能办公助手等多种应用场景。 -- **开箱即用**:支持直接上传文档 / 自动爬取在线文档,支持文本自动拆分、向量化和 RAG(检索增强生成),有效减少大模型幻觉,智能问答交互体验好; -- **模型中立**:支持对接各种大模型,包括本地私有大模型(DeepSeek R1 / Llama 3 / Qwen 2 等)、国内公共大模型(通义千问 / 腾讯混元 / 字节豆包 / 百度千帆 / 智谱 AI / Kimi 等)和国外公共大模型(OpenAI / Claude / Gemini 等); +- **RAG 检索增强生成**:高效搭建本地 AI 知识库,支持直接上传文档 / 自动爬取在线文档,支持文本自动拆分、向量化,有效减少大模型幻觉,提升问答效果; - **灵活编排**:内置强大的工作流引擎、函数库和 MCP 工具调用能力,支持编排 AI 工作过程,满足复杂业务场景下的需求; -- **无缝嵌入**:支持零编码快速嵌入到第三方业务系统,让已有系统快速拥有智能问答能力,提高用户满意度。 +- **无缝嵌入**:支持零编码快速嵌入到第三方业务系统,让已有系统快速拥有智能问答能力,提高用户满意度; +- **模型中立**:支持对接各种大模型,包括本地私有大模型(DeepSeek R1 / Qwen 3 等)、国内公共大模型(通义千问 / 腾讯混元 / 字节豆包 / 百度千帆 / 智谱 AI / Kimi 等)和国外公共大模型(OpenAI / Claude / Gemini 等)。 MaxKB 三分钟视频介绍:https://www.bilibili.com/video/BV18JypYeEkj/ @@ -27,10 +27,10 @@ MaxKB 三分钟视频介绍:https://www.bilibili.com/video/BV18JypYeEkj/ ``` # Linux 机器 -docker run -d --name=maxkb --restart=always -p 8080:8080 -v ~/.maxkb:/var/lib/postgresql/data -v ~/.python-packages:/opt/maxkb/app/sandbox/python-packages registry.fit2cloud.com/maxkb/maxkb +docker run -d --name=maxkb --restart=always -p 8080:8080 -v ~/.maxkb:/var/lib/postgresql/data -v ~/.python-packages:/opt/maxkb/app/sandbox/python-packages registry.fit2cloud.com/maxkb/maxkb:v1 # Windows 机器 -docker run -d --name=maxkb --restart=always -p 8080:8080 -v C:/maxkb:/var/lib/postgresql/data -v C:/python-packages:/opt/maxkb/app/sandbox/python-packages registry.fit2cloud.com/maxkb/maxkb +docker run -d --name=maxkb --restart=always -p 8080:8080 -v C:/maxkb:/var/lib/postgresql/data -v C:/python-packages:/opt/maxkb/app/sandbox/python-packages registry.fit2cloud.com/maxkb/maxkb:v1 # 用户名: admin # 密码: MaxKB@123.. @@ -38,8 +38,8 @@ docker run -d --name=maxkb --restart=always -p 8080:8080 -v C:/maxkb:/var/lib/po - 你也可以通过 [1Panel 应用商店](https://apps.fit2cloud.com/1panel) 快速部署 MaxKB; - 如果是内网环境,推荐使用 [离线安装包](https://community.fit2cloud.com/#/products/maxkb/downloads) 进行安装部署; -- MaxKB 产品版本分为社区版和专业版,详情请参见:[MaxKB 产品版本对比](https://maxkb.cn/pricing.html); -- 如果您需要向团队介绍 MaxKB,可以使用这个 [官方 PPT 材料](https://maxkb.cn/download/introduce-maxkb_202503.pdf)。 +- MaxKB 不同产品产品版本的对比请参见:[MaxKB 产品版本对比](https://maxkb.cn/price); +- 如果您需要向团队介绍 MaxKB,可以使用这个 [官方 PPT 材料](https://fit2cloud.com/maxkb/download/introduce-maxkb_202507.pdf)。 如你有更多问题,可以查看使用手册,或者通过论坛与我们交流。 diff --git a/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py b/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py index c5a0de1a152..00871a8dc4d 100644 --- a/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py +++ b/apps/application/flow/step_node/ai_chat_step_node/impl/base_chat_node.py @@ -40,6 +40,7 @@ """ + def _write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow, answer: str, reasoning_content: str): chat_model = node_variable.get('chat_model') @@ -102,7 +103,6 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo _write_context(node_variable, workflow_variable, node, workflow, answer, reasoning_content) - async def _yield_mcp_response(chat_model, message_list, mcp_servers): async with MultiServerMCPClient(json.loads(mcp_servers)) as client: agent = create_react_agent(chat_model, client.get_tools()) @@ -115,6 +115,7 @@ async def _yield_mcp_response(chat_model, message_list, mcp_servers): if isinstance(chunk[0], AIMessageChunk): yield chunk[0] + def mcp_response_generator(chat_model, message_list, mcp_servers): loop = asyncio.new_event_loop() try: @@ -130,6 +131,7 @@ def mcp_response_generator(chat_model, message_list, mcp_servers): finally: loop.close() + async def anext_async(agen): return await agen.__anext__() @@ -186,7 +188,9 @@ def save_context(self, details, workflow_manage): self.context['answer'] = details.get('answer') self.context['question'] = details.get('question') self.context['reasoning_content'] = details.get('reasoning_content') - self.answer_text = details.get('answer') + self.context['model_setting'] = details.get('model_setting') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') def execute(self, model_id, system, prompt, dialogue_number, history_chat_record, stream, chat_id, chat_record_id, model_params_setting=None, @@ -216,7 +220,7 @@ def execute(self, model_id, system, prompt, dialogue_number, history_chat_record message_list = self.generate_message_list(system, prompt, history_message) self.context['message_list'] = message_list - if mcp_enable and mcp_servers is not None: + if mcp_enable and mcp_servers is not None and '"stdio"' not in mcp_servers: r = mcp_response_generator(chat_model, message_list, mcp_servers) return NodeResult( {'result': r, 'chat_model': chat_model, 'message_list': message_list, @@ -271,6 +275,7 @@ def get_details(self, index: int, **kwargs): "index": index, 'run_time': self.context.get('run_time'), 'system': self.context.get('system'), + 'model_setting': self.context.get('model_setting'), 'history_message': [{'content': message.content, 'role': message.type} for message in (self.context.get('history_message') if self.context.get( 'history_message') is not None else [])], diff --git a/apps/application/flow/step_node/application_node/impl/base_application_node.py b/apps/application/flow/step_node/application_node/impl/base_application_node.py index d962f7163bb..95445f45612 100644 --- a/apps/application/flow/step_node/application_node/impl/base_application_node.py +++ b/apps/application/flow/step_node/application_node/impl/base_application_node.py @@ -168,7 +168,8 @@ def save_context(self, details, workflow_manage): self.context['question'] = details.get('question') self.context['type'] = details.get('type') self.context['reasoning_content'] = details.get('reasoning_content') - self.answer_text = details.get('answer') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') def execute(self, application_id, message, chat_id, chat_record_id, stream, re_chat, client_id, client_type, app_document_list=None, app_image_list=None, app_audio_list=None, child_node=None, node_data=None, @@ -178,7 +179,8 @@ def execute(self, application_id, message, chat_id, chat_record_id, stream, re_c current_chat_id = string_to_uuid(chat_id + application_id) Chat.objects.get_or_create(id=current_chat_id, defaults={ 'application_id': application_id, - 'abstract': message[0:1024] + 'abstract': message[0:1024], + 'client_id': client_id, }) if app_document_list is None: app_document_list = [] diff --git a/apps/application/flow/step_node/direct_reply_node/impl/base_reply_node.py b/apps/application/flow/step_node/direct_reply_node/impl/base_reply_node.py index 6a51edd6bae..1d3115e4c67 100644 --- a/apps/application/flow/step_node/direct_reply_node/impl/base_reply_node.py +++ b/apps/application/flow/step_node/direct_reply_node/impl/base_reply_node.py @@ -15,7 +15,9 @@ class BaseReplyNode(IReplyNode): def save_context(self, details, workflow_manage): self.context['answer'] = details.get('answer') - self.answer_text = details.get('answer') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') + def execute(self, reply_type, stream, fields=None, content=None, **kwargs) -> NodeResult: if reply_type == 'referencing': result = self.get_reference_content(fields) diff --git a/apps/application/flow/step_node/form_node/impl/base_form_node.py b/apps/application/flow/step_node/form_node/impl/base_form_node.py index 7cbbe9cc1d4..dcf35dd3cfd 100644 --- a/apps/application/flow/step_node/form_node/impl/base_form_node.py +++ b/apps/application/flow/step_node/form_node/impl/base_form_node.py @@ -38,7 +38,8 @@ def save_context(self, details, workflow_manage): self.context['start_time'] = details.get('start_time') self.context['form_data'] = form_data self.context['is_submit'] = details.get('is_submit') - self.answer_text = details.get('result') + if self.node_params.get('is_result', False): + self.answer_text = details.get('result') if form_data is not None: for key in form_data: self.context[key] = form_data[key] @@ -70,7 +71,7 @@ def get_answer_list(self) -> List[Answer] | None: "chat_record_id": self.flow_params_serializer.data.get("chat_record_id"), 'form_data': self.context.get('form_data', {}), "is_submit": self.context.get("is_submit", False)} - form = f'{json.dumps(form_setting,ensure_ascii=False)}' + form = f'{json.dumps(form_setting, ensure_ascii=False)}' context = self.workflow_manage.get_workflow_content() form_content_format = self.workflow_manage.reset_prompt(form_content_format) prompt_template = PromptTemplate.from_template(form_content_format, template_format='jinja2') @@ -85,7 +86,7 @@ def get_details(self, index: int, **kwargs): "chat_record_id": self.flow_params_serializer.data.get("chat_record_id"), 'form_data': self.context.get('form_data', {}), "is_submit": self.context.get("is_submit", False)} - form = f'{json.dumps(form_setting,ensure_ascii=False)}' + form = f'{json.dumps(form_setting, ensure_ascii=False)}' context = self.workflow_manage.get_workflow_content() form_content_format = self.workflow_manage.reset_prompt(form_content_format) prompt_template = PromptTemplate.from_template(form_content_format, template_format='jinja2') diff --git a/apps/application/flow/step_node/function_lib_node/impl/base_function_lib_node.py b/apps/application/flow/step_node/function_lib_node/impl/base_function_lib_node.py index d21424f750d..341bb91da63 100644 --- a/apps/application/flow/step_node/function_lib_node/impl/base_function_lib_node.py +++ b/apps/application/flow/step_node/function_lib_node/impl/base_function_lib_node.py @@ -65,7 +65,7 @@ def valid_reference_value(_type, value, name): def convert_value(name: str, value, _type, is_required, source, node): - if not is_required and value is None: + if not is_required and (value is None or (isinstance(value, str) and len(value) == 0)): return None if not is_required and source == 'reference' and (value is None or len(value) == 0): return None @@ -113,7 +113,8 @@ def valid_function(function_lib, user_id): class BaseFunctionLibNodeNode(IFunctionLibNode): def save_context(self, details, workflow_manage): self.context['result'] = details.get('result') - self.answer_text = str(details.get('result')) + if self.node_params.get('is_result'): + self.answer_text = str(details.get('result')) def execute(self, function_lib_id, input_field_list, **kwargs) -> NodeResult: function_lib = QuerySet(FunctionLib).filter(id=function_lib_id).first() diff --git a/apps/application/flow/step_node/function_node/impl/base_function_node.py b/apps/application/flow/step_node/function_node/impl/base_function_node.py index 4a5c75c8132..d659227f1ee 100644 --- a/apps/application/flow/step_node/function_node/impl/base_function_node.py +++ b/apps/application/flow/step_node/function_node/impl/base_function_node.py @@ -49,7 +49,7 @@ def valid_reference_value(_type, value, name): def convert_value(name: str, value, _type, is_required, source, node): - if not is_required and value is None: + if not is_required and (value is None or (isinstance(value, str) and len(value) == 0)): return None if source == 'reference': value = node.workflow_manage.get_reference_field( @@ -84,7 +84,8 @@ def convert_value(name: str, value, _type, is_required, source, node): class BaseFunctionNodeNode(IFunctionNode): def save_context(self, details, workflow_manage): self.context['result'] = details.get('result') - self.answer_text = str(details.get('result')) + if self.node_params.get('is_result', False): + self.answer_text = str(details.get('result')) def execute(self, input_field_list, code, **kwargs) -> NodeResult: params = {field.get('name'): convert_value(field.get('name'), field.get('value'), field.get('type'), diff --git a/apps/application/flow/step_node/image_generate_step_node/impl/base_image_generate_node.py b/apps/application/flow/step_node/image_generate_step_node/impl/base_image_generate_node.py index d5cc2c5a211..16423eafd61 100644 --- a/apps/application/flow/step_node/image_generate_step_node/impl/base_image_generate_node.py +++ b/apps/application/flow/step_node/image_generate_step_node/impl/base_image_generate_node.py @@ -16,7 +16,8 @@ class BaseImageGenerateNode(IImageGenerateNode): def save_context(self, details, workflow_manage): self.context['answer'] = details.get('answer') self.context['question'] = details.get('question') - self.answer_text = details.get('answer') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') def execute(self, model_id, prompt, negative_prompt, dialogue_number, dialogue_type, history_chat_record, chat_id, model_params_setting, @@ -24,7 +25,8 @@ def execute(self, model_id, prompt, negative_prompt, dialogue_number, dialogue_t **kwargs) -> NodeResult: print(model_params_setting) application = self.workflow_manage.work_flow_post_handler.chat_info.application - tti_model = get_model_instance_by_model_user_id(model_id, self.flow_params_serializer.data.get('user_id'), **model_params_setting) + tti_model = get_model_instance_by_model_user_id(model_id, self.flow_params_serializer.data.get('user_id'), + **model_params_setting) history_message = self.get_history_message(history_chat_record, dialogue_number) self.context['history_message'] = history_message question = self.generate_prompt_question(prompt) diff --git a/apps/application/flow/step_node/image_understand_step_node/impl/base_image_understand_node.py b/apps/application/flow/step_node/image_understand_step_node/impl/base_image_understand_node.py index 3b96f15cd6f..44765bc4f93 100644 --- a/apps/application/flow/step_node/image_understand_step_node/impl/base_image_understand_node.py +++ b/apps/application/flow/step_node/image_understand_step_node/impl/base_image_understand_node.py @@ -69,7 +69,8 @@ class BaseImageUnderstandNode(IImageUnderstandNode): def save_context(self, details, workflow_manage): self.context['answer'] = details.get('answer') self.context['question'] = details.get('question') - self.answer_text = details.get('answer') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') def execute(self, model_id, system, prompt, dialogue_number, dialogue_type, history_chat_record, stream, chat_id, model_params_setting, diff --git a/apps/application/flow/step_node/mcp_node/impl/base_mcp_node.py b/apps/application/flow/step_node/mcp_node/impl/base_mcp_node.py index 6c9fe97fc69..e49ef7019f6 100644 --- a/apps/application/flow/step_node/mcp_node/impl/base_mcp_node.py +++ b/apps/application/flow/step_node/mcp_node/impl/base_mcp_node.py @@ -14,7 +14,8 @@ def save_context(self, details, workflow_manage): self.context['result'] = details.get('result') self.context['tool_params'] = details.get('tool_params') self.context['mcp_tool'] = details.get('mcp_tool') - self.answer_text = details.get('result') + if self.node_params.get('is_result', False): + self.answer_text = details.get('result') def execute(self, mcp_servers, mcp_server, mcp_tool, tool_params, **kwargs) -> NodeResult: servers = json.loads(mcp_servers) @@ -27,7 +28,8 @@ async def call_tool(s, session, t, a): return s res = asyncio.run(call_tool(servers, mcp_server, mcp_tool, params)) - return NodeResult({'result': [content.text for content in res.content], 'tool_params': params, 'mcp_tool': mcp_tool}, {}) + return NodeResult( + {'result': [content.text for content in res.content], 'tool_params': params, 'mcp_tool': mcp_tool}, {}) def handle_variables(self, tool_params): # 处理参数中的变量 diff --git a/apps/application/flow/step_node/question_node/impl/base_question_node.py b/apps/application/flow/step_node/question_node/impl/base_question_node.py index 48a2639b782..e1fd5b86069 100644 --- a/apps/application/flow/step_node/question_node/impl/base_question_node.py +++ b/apps/application/flow/step_node/question_node/impl/base_question_node.py @@ -80,7 +80,8 @@ def save_context(self, details, workflow_manage): self.context['answer'] = details.get('answer') self.context['message_tokens'] = details.get('message_tokens') self.context['answer_tokens'] = details.get('answer_tokens') - self.answer_text = details.get('answer') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') def execute(self, model_id, system, prompt, dialogue_number, history_chat_record, stream, chat_id, chat_record_id, model_params_setting=None, diff --git a/apps/application/flow/step_node/speech_to_text_step_node/impl/base_speech_to_text_node.py b/apps/application/flow/step_node/speech_to_text_step_node/impl/base_speech_to_text_node.py index c85588cd4d2..13b954e4622 100644 --- a/apps/application/flow/step_node/speech_to_text_step_node/impl/base_speech_to_text_node.py +++ b/apps/application/flow/step_node/speech_to_text_step_node/impl/base_speech_to_text_node.py @@ -18,7 +18,8 @@ class BaseSpeechToTextNode(ISpeechToTextNode): def save_context(self, details, workflow_manage): self.context['answer'] = details.get('answer') - self.answer_text = details.get('answer') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') def execute(self, stt_model_id, chat_id, audio, **kwargs) -> NodeResult: stt_model = get_model_instance_by_model_user_id(stt_model_id, self.flow_params_serializer.data.get('user_id')) diff --git a/apps/application/flow/step_node/start_node/impl/base_start_node.py b/apps/application/flow/step_node/start_node/impl/base_start_node.py index bf5203274eb..24b9684714e 100644 --- a/apps/application/flow/step_node/start_node/impl/base_start_node.py +++ b/apps/application/flow/step_node/start_node/impl/base_start_node.py @@ -40,10 +40,13 @@ def save_context(self, details, workflow_manage): self.context['document'] = details.get('document_list') self.context['image'] = details.get('image_list') self.context['audio'] = details.get('audio_list') + self.context['other'] = details.get('other_list') self.status = details.get('status') self.err_message = details.get('err_message') for key, value in workflow_variable.items(): workflow_manage.context[key] = value + for item in details.get('global_fields', []): + workflow_manage.context[item.get('key')] = item.get('value') def get_node_params_serializer_class(self) -> Type[serializers.Serializer]: pass @@ -59,7 +62,8 @@ def execute(self, question, **kwargs) -> NodeResult: 'question': question, 'image': self.workflow_manage.image_list, 'document': self.workflow_manage.document_list, - 'audio': self.workflow_manage.audio_list + 'audio': self.workflow_manage.audio_list, + 'other': self.workflow_manage.other_list, } return NodeResult(node_variable, workflow_variable) @@ -83,5 +87,6 @@ def get_details(self, index: int, **kwargs): 'image_list': self.context.get('image'), 'document_list': self.context.get('document'), 'audio_list': self.context.get('audio'), + 'other_list': self.context.get('other'), 'global_fields': global_fields } diff --git a/apps/application/flow/step_node/text_to_speech_step_node/impl/base_text_to_speech_node.py b/apps/application/flow/step_node/text_to_speech_step_node/impl/base_text_to_speech_node.py index 72c4d3be514..97044729596 100644 --- a/apps/application/flow/step_node/text_to_speech_step_node/impl/base_text_to_speech_node.py +++ b/apps/application/flow/step_node/text_to_speech_step_node/impl/base_text_to_speech_node.py @@ -37,7 +37,8 @@ def bytes_to_uploaded_file(file_bytes, file_name="generated_audio.mp3"): class BaseTextToSpeechNode(ITextToSpeechNode): def save_context(self, details, workflow_manage): self.context['answer'] = details.get('answer') - self.answer_text = details.get('answer') + if self.node_params.get('is_result', False): + self.answer_text = details.get('answer') def execute(self, tts_model_id, chat_id, content, model_params_setting=None, diff --git a/apps/application/flow/workflow_manage.py b/apps/application/flow/workflow_manage.py index be91f69be9e..7496e01fe8b 100644 --- a/apps/application/flow/workflow_manage.py +++ b/apps/application/flow/workflow_manage.py @@ -238,6 +238,7 @@ def __init__(self, flow: Flow, params, work_flow_post_handler: WorkFlowPostHandl base_to_response: BaseToResponse = SystemToResponse(), form_data=None, image_list=None, document_list=None, audio_list=None, + other_list=None, start_node_id=None, start_node_data=None, chat_record=None, child_node=None): if form_data is None: @@ -248,12 +249,15 @@ def __init__(self, flow: Flow, params, work_flow_post_handler: WorkFlowPostHandl document_list = [] if audio_list is None: audio_list = [] + if other_list is None: + other_list = [] self.start_node_id = start_node_id self.start_node = None self.form_data = form_data self.image_list = image_list self.document_list = document_list self.audio_list = audio_list + self.other_list = other_list self.params = params self.flow = flow self.context = {} @@ -294,8 +298,8 @@ def init_fields(self): if global_fields is not None: for global_field in global_fields: global_field_list.append({**global_field, 'node_id': node_id, 'node_name': node_name}) - field_list.sort(key=lambda f: len(f.get('node_name')), reverse=True) - global_field_list.sort(key=lambda f: len(f.get('node_name')), reverse=True) + field_list.sort(key=lambda f: len(f.get('node_name') + f.get('value')), reverse=True) + global_field_list.sort(key=lambda f: len(f.get('node_name') + f.get('value')), reverse=True) self.field_list = field_list self.global_field_list = global_field_list diff --git a/apps/application/models/application.py b/apps/application/models/application.py index dfe9534e82b..0032271a70b 100644 --- a/apps/application/models/application.py +++ b/apps/application/models/application.py @@ -11,7 +11,7 @@ from django.contrib.postgres.fields import ArrayField from django.db import models from langchain.schema import HumanMessage, AIMessage - +from django.utils.translation import gettext as _ from common.encoder.encoder import SystemEncoder from common.mixins.app_model_mixin import AppModelMixin from dataset.models.data_set import DataSet @@ -167,7 +167,11 @@ def get_human_message(self): return HumanMessage(content=self.problem_text) def get_ai_message(self): - return AIMessage(content=self.answer_text) + answer_text = self.answer_text + if answer_text is None or len(str(answer_text).strip()) == 0: + answer_text = _( + 'Sorry, no relevant content was found. Please re-describe your problem or provide more information. ') + return AIMessage(content=answer_text) def get_node_details_runtime_node_id(self, runtime_node_id): return self.details.get(runtime_node_id, None) diff --git a/apps/application/serializers/application_serializers.py b/apps/application/serializers/application_serializers.py index 3792076be7c..d25a6c95271 100644 --- a/apps/application/serializers/application_serializers.py +++ b/apps/application/serializers/application_serializers.py @@ -16,6 +16,7 @@ import uuid from functools import reduce from typing import Dict, List + from django.contrib.postgres.fields import ArrayField from django.core import cache, validators from django.core import signing @@ -24,8 +25,8 @@ from django.db.models.expressions import RawSQL from django.http import HttpResponse from django.template import Template, Context +from django.utils.translation import gettext_lazy as _, get_language, to_locale from langchain_mcp_adapters.client import MultiServerMCPClient -from mcp.client.sse import sse_client from rest_framework import serializers, status from rest_framework.utils.formatting import lazy_format @@ -38,7 +39,7 @@ from common.constants.authentication_type import AuthenticationType from common.db.search import get_dynamics_model, native_search, native_page_search from common.db.sql_execute import select_list -from common.exception.app_exception import AppApiException, NotFound404, AppUnauthorizedFailed, ChatException +from common.exception.app_exception import AppApiException, NotFound404, AppUnauthorizedFailed from common.field.common import UploadedImageField, UploadedFileField from common.models.db_model_manage import DBModelManage from common.response import result @@ -57,7 +58,6 @@ from setting.serializers.provider_serializers import ModelSerializer from smartdoc.conf import PROJECT_DIR from users.models import User -from django.utils.translation import gettext_lazy as _, get_language, to_locale chat_cache = cache.caches['chat_cache'] @@ -148,10 +148,12 @@ class ModelSettingSerializer(serializers.Serializer): error_messages=ErrMessage.char(_("Thinking process switch"))) reasoning_content_start = serializers.CharField(required=False, allow_null=True, default="", allow_blank=True, max_length=256, + trim_whitespace=False, error_messages=ErrMessage.char( _("The thinking process begins to mark"))) reasoning_content_end = serializers.CharField(required=False, allow_null=True, allow_blank=True, default="", max_length=256, + trim_whitespace=False, error_messages=ErrMessage.char(_("End of thinking process marker"))) @@ -162,7 +164,7 @@ class ApplicationWorkflowSerializer(serializers.Serializer): max_length=256, min_length=1, error_messages=ErrMessage.char(_("Application Description"))) work_flow = serializers.DictField(required=False, error_messages=ErrMessage.dict(_("Workflow Objects"))) - prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=4096, + prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=102400, error_messages=ErrMessage.char(_("Opening remarks"))) @staticmethod @@ -225,7 +227,7 @@ class ApplicationSerializer(serializers.Serializer): min_value=0, max_value=1024, error_messages=ErrMessage.integer(_("Historical chat records"))) - prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=4096, + prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=102400, error_messages=ErrMessage.char(_("Opening remarks"))) dataset_id_list = serializers.ListSerializer(required=False, child=serializers.UUIDField(required=True), allow_null=True, @@ -493,7 +495,7 @@ class Edit(serializers.Serializer): min_value=0, max_value=1024, error_messages=ErrMessage.integer(_("Historical chat records"))) - prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=4096, + prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=102400, error_messages=ErrMessage.char(_("Opening remarks"))) dataset_id_list = serializers.ListSerializer(required=False, child=serializers.UUIDField(required=True), error_messages=ErrMessage.list(_("Related Knowledge Base")) @@ -1010,7 +1012,8 @@ def profile(self, with_valid=True): 'stt_autosend': application.stt_autosend, 'file_upload_enable': application.file_upload_enable, 'file_upload_setting': application.file_upload_setting, - 'work_flow': application.work_flow, + 'work_flow': {'nodes': [node for node in ((application.work_flow or {}).get('nodes', []) or []) if + node.get('id') == 'base-node']}, 'show_source': application_access_token.show_source, 'language': application_access_token.language, **application_setting_dict}) @@ -1071,6 +1074,7 @@ def edit(self, instance: Dict, with_valid=True): for update_key in update_keys: if update_key in instance and instance.get(update_key) is not None: application.__setattr__(update_key, instance.get(update_key)) + print(application.name) application.save() if 'dataset_id_list' in instance: @@ -1089,6 +1093,7 @@ def edit(self, instance: Dict, with_valid=True): chat_cache.clear_by_application_id(application_id) application_access_token = QuerySet(ApplicationAccessToken).filter(application_id=application_id).first() # 更新缓存数据 + print(application.name) get_application_access_token(application_access_token.access_token, False) return self.one(with_valid=False) @@ -1141,6 +1146,8 @@ def get_work_flow_model(instance): instance['file_upload_enable'] = node_data['file_upload_enable'] if 'file_upload_setting' in node_data: instance['file_upload_setting'] = node_data['file_upload_setting'] + if 'name' in node_data: + instance['name'] = node_data['name'] break def speech_to_text(self, file, with_valid=True): @@ -1318,7 +1325,12 @@ class McpServers(serializers.Serializer): def get_mcp_servers(self, with_valid=True): if with_valid: self.is_valid(raise_exception=True) + if '"stdio"' in self.data.get('mcp_servers'): + raise AppApiException(500, _('stdio is not supported')) servers = json.loads(self.data.get('mcp_servers')) + for server, config in servers.items(): + if config.get('transport') not in ['sse', 'streamable_http']: + raise AppApiException(500, _('Only support transport=sse or transport=streamable_http')) async def get_mcp_tools(servers): async with MultiServerMCPClient(servers) as client: diff --git a/apps/application/serializers/chat_message_serializers.py b/apps/application/serializers/chat_message_serializers.py index 2194028e6dd..2ce5273f73d 100644 --- a/apps/application/serializers/chat_message_serializers.py +++ b/apps/application/serializers/chat_message_serializers.py @@ -213,12 +213,21 @@ def get_message(instance): return instance.get('messages')[-1].get('content') @staticmethod - def generate_chat(chat_id, application_id, message, client_id): + def generate_chat(chat_id, application_id, message, client_id, asker=None): if chat_id is None: chat_id = str(uuid.uuid1()) chat = QuerySet(Chat).filter(id=chat_id).first() if chat is None: - Chat(id=chat_id, application_id=application_id, abstract=message[0:1024], client_id=client_id).save() + asker_dict = {'user_name': '游客'} + if asker is not None: + if isinstance(asker, str): + asker_dict = { + 'user_name': asker + } + elif isinstance(asker, dict): + asker_dict = asker + Chat(id=chat_id, application_id=application_id, abstract=message[0:1024], client_id=client_id, + asker=asker_dict).save() return chat_id def chat(self, instance: Dict, with_valid=True): @@ -232,7 +241,8 @@ def chat(self, instance: Dict, with_valid=True): application_id = self.data.get('application_id') client_id = self.data.get('client_id') client_type = self.data.get('client_type') - chat_id = self.generate_chat(chat_id, application_id, message, client_id) + chat_id = self.generate_chat(chat_id, application_id, message, client_id, + asker=instance.get('form_data', {}).get("asker")) return ChatMessageSerializer( data={ 'chat_id': chat_id, 'message': message, @@ -245,6 +255,7 @@ def chat(self, instance: Dict, with_valid=True): 'image_list': instance.get('image_list', []), 'document_list': instance.get('document_list', []), 'audio_list': instance.get('audio_list', []), + 'other_list': instance.get('other_list', []), } ).chat(base_to_response=OpenaiToResponse()) @@ -274,6 +285,7 @@ class ChatMessageSerializer(serializers.Serializer): image_list = serializers.ListField(required=False, error_messages=ErrMessage.list(_("picture"))) document_list = serializers.ListField(required=False, error_messages=ErrMessage.list(_("document"))) audio_list = serializers.ListField(required=False, error_messages=ErrMessage.list(_("Audio"))) + other_list = serializers.ListField(required=False, error_messages=ErrMessage.list(_("Other"))) child_node = serializers.DictField(required=False, allow_null=True, error_messages=ErrMessage.dict(_("Child Nodes"))) @@ -372,6 +384,7 @@ def chat_work_flow(self, chat_info: ChatInfo, base_to_response): image_list = self.data.get('image_list') document_list = self.data.get('document_list') audio_list = self.data.get('audio_list') + other_list = self.data.get('other_list') user_id = chat_info.application.user_id chat_record_id = self.data.get('chat_record_id') chat_record = None @@ -388,7 +401,7 @@ def chat_work_flow(self, chat_info: ChatInfo, base_to_response): 'client_id': client_id, 'client_type': client_type, 'user_id': user_id}, WorkFlowPostHandler(chat_info, client_id, client_type), - base_to_response, form_data, image_list, document_list, audio_list, + base_to_response, form_data, image_list, document_list, audio_list, other_list, self.data.get('runtime_node_id'), self.data.get('node_data'), chat_record, self.data.get('child_node')) r = work_flow_manage.run() diff --git a/apps/application/serializers/chat_serializers.py b/apps/application/serializers/chat_serializers.py index b90194d5ae2..6eb429ac07e 100644 --- a/apps/application/serializers/chat_serializers.py +++ b/apps/application/serializers/chat_serializers.py @@ -174,7 +174,14 @@ def get_query_set(self, select_ids=None): condition = base_condition & min_trample_query else: condition = base_condition - return query_set.filter(condition).order_by("-application_chat.update_time") + inner_queryset = QuerySet(Chat).filter(application_id=self.data.get("application_id")) + if 'abstract' in self.data and self.data.get('abstract') is not None: + inner_queryset = inner_queryset.filter(abstract__icontains=self.data.get('abstract')) + + return { + 'inner_queryset': inner_queryset, + 'default_queryset': query_set.filter(condition).order_by("-application_chat.update_time") + } def list(self, with_valid=True): if with_valid: @@ -215,7 +222,8 @@ def to_row(row: Dict): reference_paragraph, "\n".join([ f"{improve_paragraph_list[index].get('title')}\n{improve_paragraph_list[index].get('content')}" - for index in range(len(improve_paragraph_list))]), + for index in range(len(improve_paragraph_list)) + ]) if improve_paragraph_list is not None else "", row.get('asker').get('user_name'), row.get('message_tokens') + row.get('answer_tokens'), row.get('run_time'), str(row.get('create_time').astimezone(pytz.timezone(TIME_ZONE)).strftime('%Y-%m-%d %H:%M:%S') diff --git a/apps/application/sql/export_application_chat.sql b/apps/application/sql/export_application_chat.sql index bb265ea5b02..de9c7ee6ef7 100644 --- a/apps/application/sql/export_application_chat.sql +++ b/apps/application/sql/export_application_chat.sql @@ -23,6 +23,8 @@ FROM chat_id FROM application_chat_record + WHERE chat_id IN ( + SELECT id FROM application_chat ${inner_queryset}) GROUP BY application_chat_record.chat_id ) chat_record_temp ON application_chat."id" = chat_record_temp.chat_id @@ -35,4 +37,5 @@ FROM END as improve_paragraph_list FROM application_chat_record application_chat_record - ) application_chat_record_temp ON application_chat_record_temp.chat_id = application_chat."id" \ No newline at end of file + ) application_chat_record_temp ON application_chat_record_temp.chat_id = application_chat."id" + ${default_queryset} \ No newline at end of file diff --git a/apps/application/sql/list_application_chat.sql b/apps/application/sql/list_application_chat.sql index 7f3e1680c99..c9f83c6b7c3 100644 --- a/apps/application/sql/list_application_chat.sql +++ b/apps/application/sql/list_application_chat.sql @@ -11,6 +11,9 @@ FROM chat_id FROM application_chat_record + WHERE chat_id IN ( + SELECT id FROM application_chat ${inner_queryset}) GROUP BY application_chat_record.chat_id - ) chat_record_temp ON application_chat."id" = chat_record_temp.chat_id \ No newline at end of file + ) chat_record_temp ON application_chat."id" = chat_record_temp.chat_id +${default_queryset} \ No newline at end of file diff --git a/apps/application/swagger_api/application_api.py b/apps/application/swagger_api/application_api.py index 2c9cbd86bf4..4e47ab32619 100644 --- a/apps/application/swagger_api/application_api.py +++ b/apps/application/swagger_api/application_api.py @@ -302,7 +302,19 @@ def get_request_body_api(): 'no_references_prompt': openapi.Schema(type=openapi.TYPE_STRING, title=_("No citation segmentation prompt"), default="{question}", - description=_("No citation segmentation prompt")) + description=_("No citation segmentation prompt")), + 'reasoning_content_enable': openapi.Schema(type=openapi.TYPE_BOOLEAN, + title=_("Reasoning enable"), + default=False, + description=_("Reasoning enable")), + 'reasoning_content_end': openapi.Schema(type=openapi.TYPE_STRING, + title=_("Reasoning end tag"), + default="", + description=_("Reasoning end tag")), + "reasoning_content_start": openapi.Schema(type=openapi.TYPE_STRING, + title=_("Reasoning start tag"), + default="", + description=_("Reasoning start tag")) } ) diff --git a/apps/application/swagger_api/chat_api.py b/apps/application/swagger_api/chat_api.py index 54b5678f747..f27a19c200e 100644 --- a/apps/application/swagger_api/chat_api.py +++ b/apps/application/swagger_api/chat_api.py @@ -326,11 +326,6 @@ def get_request_params_api(): type=openapi.TYPE_STRING, required=True, description=_('Application ID')), - openapi.Parameter(name='history_day', - in_=openapi.IN_QUERY, - type=openapi.TYPE_NUMBER, - required=True, - description=_('Historical days')), openapi.Parameter(name='abstract', in_=openapi.IN_QUERY, type=openapi.TYPE_STRING, required=False, description=_("abstract")), openapi.Parameter(name='min_star', in_=openapi.IN_QUERY, type=openapi.TYPE_INTEGER, required=False, diff --git a/apps/application/views/application_views.py b/apps/application/views/application_views.py index f16041d1de3..8c3e8059bcb 100644 --- a/apps/application/views/application_views.py +++ b/apps/application/views/application_views.py @@ -7,16 +7,6 @@ @desc: """ -from django.core import cache -from django.http import HttpResponse -from django.utils.translation import gettext_lazy as _, gettext -from drf_yasg.utils import swagger_auto_schema -from langchain_core.prompts import PromptTemplate -from rest_framework.decorators import action -from rest_framework.parsers import MultiPartParser -from rest_framework.request import Request -from rest_framework.views import APIView - from application.serializers.application_serializers import ApplicationSerializer from application.serializers.application_statistics_serializers import ApplicationStatisticsSerializer from application.swagger_api.application_api import ApplicationApi @@ -31,6 +21,14 @@ from common.swagger_api.common_api import CommonApi from common.util.common import query_params_to_single_dict from dataset.serializers.dataset_serializers import DataSetSerializers +from django.core import cache +from django.http import HttpResponse +from django.utils.translation import gettext_lazy as _ +from drf_yasg.utils import swagger_auto_schema +from rest_framework.decorators import action +from rest_framework.parsers import MultiPartParser +from rest_framework.request import Request +from rest_framework.views import APIView chat_cache = cache.caches['chat_cache'] @@ -494,7 +492,7 @@ def get(self, request: Request): class HitTest(APIView): authentication_classes = [TokenAuth] - @action(methods="GET", detail=False) + @action(methods="PUT", detail=False) @swagger_auto_schema(operation_summary=_("Hit Test List"), operation_id=_("Hit Test List"), manual_parameters=CommonApi.HitTestApi.get_request_params_api(), responses=result.get_api_array_response(CommonApi.HitTestApi.get_response_body_api()), @@ -505,15 +503,15 @@ class HitTest(APIView): [lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.USE, dynamic_tag=keywords.get('application_id'))], compare=CompareConstants.AND)) - def get(self, request: Request, application_id: str): - return result.success( - ApplicationSerializer.HitTest(data={'id': application_id, 'user_id': request.user.id, - "query_text": request.query_params.get("query_text"), - "top_number": request.query_params.get("top_number"), - 'similarity': request.query_params.get('similarity'), - 'search_mode': request.query_params.get( - 'search_mode')}).hit_test( - )) + def put(self, request: Request, application_id: str): + return result.success(ApplicationSerializer.HitTest(data={ + 'id': application_id, + 'user_id': request.user.id, + "query_text": request.data.get("query_text"), + "top_number": request.data.get("top_number"), + 'similarity': request.data.get('similarity'), + 'search_mode': request.data.get('search_mode')} + ).hit_test()) class Publish(APIView): authentication_classes = [TokenAuth] diff --git a/apps/application/views/chat_views.py b/apps/application/views/chat_views.py index 0415f8208dc..77a087351ee 100644 --- a/apps/application/views/chat_views.py +++ b/apps/application/views/chat_views.py @@ -144,6 +144,8 @@ def post(self, request: Request, chat_id: str): 'document_list') if 'document_list' in request.data else [], 'audio_list': request.data.get( 'audio_list') if 'audio_list' in request.data else [], + 'other_list': request.data.get( + 'other_list') if 'other_list' in request.data else [], 'client_type': request.auth.client_type, 'node_id': request.data.get('node_id', None), 'runtime_node_id': request.data.get('runtime_node_id', None), diff --git a/apps/common/auth/handle/impl/user_token.py b/apps/common/auth/handle/impl/user_token.py index dbb6bd2b51a..bdb041f9f79 100644 --- a/apps/common/auth/handle/impl/user_token.py +++ b/apps/common/auth/handle/impl/user_token.py @@ -6,18 +6,18 @@ @date:2024/3/14 03:02 @desc: 用户认证 """ +from django.core import cache from django.db.models import QuerySet +from django.utils.translation import gettext_lazy as _ from common.auth.handle.auth_base_handle import AuthBaseHandle from common.constants.authentication_type import AuthenticationType from common.constants.permission_constants import RoleConstants, get_permission_list_by_role, Auth from common.exception.app_exception import AppAuthenticationFailed -from smartdoc.settings import JWT_AUTH +from smartdoc.const import CONFIG from users.models import User -from django.core import cache - from users.models.user import get_user_dynamics_permission -from django.utils.translation import gettext_lazy as _ + token_cache = cache.caches['token_cache'] @@ -35,7 +35,7 @@ def handle(self, request, token: str, get_token_details): auth_details = get_token_details() user = QuerySet(User).get(id=auth_details['id']) # 续期 - token_cache.touch(token, timeout=JWT_AUTH['JWT_EXPIRATION_DELTA'].total_seconds()) + token_cache.touch(token, timeout=CONFIG.get_session_timeout()) rule = RoleConstants[user.role] permission_list = get_permission_list_by_role(RoleConstants[user.role]) # 获取用户的应用和知识库的权限 diff --git a/apps/common/config/embedding_config.py b/apps/common/config/embedding_config.py index a6e9ab9aa9b..69081be055d 100644 --- a/apps/common/config/embedding_config.py +++ b/apps/common/config/embedding_config.py @@ -11,35 +11,50 @@ from common.cache.mem_cache import MemCache -lock = threading.Lock() +_lock = threading.Lock() +locks = {} class ModelManage: cache = MemCache('model', {}) up_clear_time = time.time() + @staticmethod + def _get_lock(_id): + lock = locks.get(_id) + if lock is None: + with _lock: + lock = locks.get(_id) + if lock is None: + lock = threading.Lock() + locks[_id] = lock + + return lock + @staticmethod def get_model(_id, get_model): - # 获取锁 - lock.acquire() - try: - model_instance = ModelManage.cache.get(_id) - if model_instance is None or not model_instance.is_cache_model(): + model_instance = ModelManage.cache.get(_id) + if model_instance is None: + lock = ModelManage._get_lock(_id) + with lock: + model_instance = ModelManage.cache.get(_id) + if model_instance is None: + model_instance = get_model(_id) + ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8) + else: + if model_instance.is_cache_model(): + ModelManage.cache.touch(_id, timeout=60 * 60 * 8) + else: model_instance = get_model(_id) - ModelManage.cache.set(_id, model_instance, timeout=60 * 30) - return model_instance - # 续期 - ModelManage.cache.touch(_id, timeout=60 * 30) - ModelManage.clear_timeout_cache() - return model_instance - finally: - # 释放锁 - lock.release() + ModelManage.cache.set(_id, model_instance, timeout=60 * 60 * 8) + ModelManage.clear_timeout_cache() + return model_instance @staticmethod def clear_timeout_cache(): - if time.time() - ModelManage.up_clear_time > 60: - ModelManage.cache.clear_timeout_data() + if time.time() - ModelManage.up_clear_time > 60 * 60: + threading.Thread(target=lambda: ModelManage.cache.clear_timeout_data()).start() + ModelManage.up_clear_time = time.time() @staticmethod def delete_key(_id): diff --git a/apps/common/event/listener_manage.py b/apps/common/event/listener_manage.py index 72d16ebb523..dd2a54a0c67 100644 --- a/apps/common/event/listener_manage.py +++ b/apps/common/event/listener_manage.py @@ -238,11 +238,8 @@ def update_status(query_set: QuerySet, taskType: TaskType, state: State): for key in params_dict: _value_ = params_dict[key] exec_sql = exec_sql.replace(key, str(_value_)) - lock.acquire() - try: + with lock: native_update(query_set, exec_sql) - finally: - lock.release() @staticmethod def embedding_by_document(document_id, embedding_model: Embeddings, state_list=None): @@ -272,7 +269,6 @@ def is_the_task_interrupted(): ListenerManagement.update_status(QuerySet(Document).filter(id=document_id), TaskType.EMBEDDING, State.STARTED) - # 根据段落进行向量化处理 page_desc(QuerySet(Paragraph) .annotate( diff --git a/apps/common/forms/__init__.py b/apps/common/forms/__init__.py index 6095421935b..251f01df092 100644 --- a/apps/common/forms/__init__.py +++ b/apps/common/forms/__init__.py @@ -22,3 +22,4 @@ from .radio_card_field import * from .label import * from .slider_field import * +from .switch_field import * diff --git a/apps/common/forms/switch_field.py b/apps/common/forms/switch_field.py index 9fa176beea0..ea119c3ecfb 100644 --- a/apps/common/forms/switch_field.py +++ b/apps/common/forms/switch_field.py @@ -28,6 +28,6 @@ def __init__(self, label: str or BaseLabel, @param props_info: """ - super().__init__('Switch', label, required, default_value, relation_show_field_dict, + super().__init__('SwitchInput', label, required, default_value, relation_show_field_dict, {}, TriggerType.OPTION_LIST, attrs, props_info) diff --git a/apps/common/handle/impl/doc_split_handle.py b/apps/common/handle/impl/doc_split_handle.py index 1df7b6a66e0..4161f13a19d 100644 --- a/apps/common/handle/impl/doc_split_handle.py +++ b/apps/common/handle/impl/doc_split_handle.py @@ -112,11 +112,7 @@ def get_image_id(image_id): title_font_list = [ [36, 100], - [26, 36], - [24, 26], - [22, 24], - [18, 22], - [16, 18] + [30, 36] ] @@ -130,7 +126,7 @@ def get_title_level(paragraph: Paragraph): if len(paragraph.runs) == 1: font_size = paragraph.runs[0].font.size pt = font_size.pt - if pt >= 16: + if pt >= 30: for _value, index in zip(title_font_list, range(len(title_font_list))): if pt >= _value[0] and pt < _value[1]: return index + 1 diff --git a/apps/common/handle/impl/table/xls_parse_table_handle.py b/apps/common/handle/impl/table/xls_parse_table_handle.py index 5609e3e8835..897e347e8a8 100644 --- a/apps/common/handle/impl/table/xls_parse_table_handle.py +++ b/apps/common/handle/impl/table/xls_parse_table_handle.py @@ -82,7 +82,10 @@ def get_content(self, file, save_image): for row in data: # 将每个单元格中的内容替换换行符为
以保留原始格式 md_table += '| ' + ' | '.join( - [str(cell).replace('\n', '
') if cell else '' for cell in row]) + ' |\n' + [str(cell) + .replace('\r\n', '
') + .replace('\n', '
') + if cell else '' for cell in row]) + ' |\n' md_tables += md_table + '\n\n' return md_tables diff --git a/apps/common/handle/impl/table/xlsx_parse_table_handle.py b/apps/common/handle/impl/table/xlsx_parse_table_handle.py index abaec05769a..a68eb14f1a1 100644 --- a/apps/common/handle/impl/table/xlsx_parse_table_handle.py +++ b/apps/common/handle/impl/table/xlsx_parse_table_handle.py @@ -19,36 +19,24 @@ def support(self, file, get_buffer): def fill_merged_cells(self, sheet, image_dict): data = [] - - # 获取第一行作为标题行 - headers = [] - for idx, cell in enumerate(sheet[1]): - if cell.value is None: - headers.append(' ' * (idx + 1)) - else: - headers.append(cell.value) - # 从第二行开始遍历每一行 - for row in sheet.iter_rows(min_row=2, values_only=False): - row_data = {} + for row in sheet.iter_rows(values_only=False): + row_data = [] for col_idx, cell in enumerate(row): cell_value = cell.value - - # 如果单元格为空,并且该单元格在合并单元格内,获取合并单元格的值 - if cell_value is None: - for merged_range in sheet.merged_cells.ranges: - if cell.coordinate in merged_range: - cell_value = sheet[merged_range.min_row][merged_range.min_col - 1].value - break - image = image_dict.get(cell_value, None) if image is not None: cell_value = f'![](/api/image/{image.id})' # 使用标题作为键,单元格的值作为值存入字典 - row_data[headers[col_idx]] = cell_value + row_data.insert(col_idx, cell_value) data.append(row_data) + for merged_range in sheet.merged_cells.ranges: + cell_value = data[merged_range.min_row - 1][merged_range.min_col - 1] + for row_index in range(merged_range.min_row, merged_range.max_row + 1): + for col_index in range(merged_range.min_col, merged_range.max_col + 1): + data[row_index - 1][col_index - 1] = cell_value return data def handle(self, file, get_buffer, save_image): @@ -65,11 +53,13 @@ def handle(self, file, get_buffer, save_image): paragraphs = [] ws = wb[sheetname] data = self.fill_merged_cells(ws, image_dict) - - for row in data: - row_output = "; ".join([f"{key}: {value}" for key, value in row.items()]) - # print(row_output) - paragraphs.append({'title': '', 'content': row_output}) + if len(data) >= 2: + head_list = data[0] + for row_index in range(1, len(data)): + row_output = "; ".join( + [f"{head_list[col_index]}: {data[row_index][col_index]}" for col_index in + range(0, len(data[row_index]))]) + paragraphs.append({'title': '', 'content': row_output}) result.append({'name': sheetname, 'paragraphs': paragraphs}) @@ -78,7 +68,6 @@ def handle(self, file, get_buffer, save_image): return [{'name': file.name, 'paragraphs': []}] return result - def get_content(self, file, save_image): try: # 加载 Excel 文件 @@ -94,18 +83,18 @@ def get_content(self, file, save_image): # 如果未指定 sheet_name,则使用第一个工作表 for sheetname in workbook.sheetnames: sheet = workbook[sheetname] if sheetname else workbook.active - rows = self.fill_merged_cells(sheet, image_dict) - if len(rows) == 0: + data = self.fill_merged_cells(sheet, image_dict) + if len(data) == 0: continue # 提取表头和内容 - headers = [f"{key}" for key, value in rows[0].items()] + headers = [f"{value}" for value in data[0]] # 构建 Markdown 表格 md_table = '| ' + ' | '.join(headers) + ' |\n' md_table += '| ' + ' | '.join(['---'] * len(headers)) + ' |\n' - for row in rows: - r = [f'{value}' for key, value in row.items()] + for row_index in range(1, len(data)): + r = [f'{value}' for value in data[row_index]] md_table += '| ' + ' | '.join( [str(cell).replace('\n', '
') if cell is not None else '' for cell in r]) + ' |\n' diff --git a/apps/common/handle/impl/xls_split_handle.py b/apps/common/handle/impl/xls_split_handle.py index 3d8afdf62de..dbdcc95506d 100644 --- a/apps/common/handle/impl/xls_split_handle.py +++ b/apps/common/handle/impl/xls_split_handle.py @@ -14,7 +14,7 @@ def post_cell(cell_value): - return cell_value.replace('\n', '
').replace('|', '|') + return cell_value.replace('\r\n', '
').replace('\n', '
').replace('|', '|') def row_to_md(row): diff --git a/apps/common/management/commands/services/services/local_model.py b/apps/common/management/commands/services/services/local_model.py index 4511f8f5fee..05f4f561009 100644 --- a/apps/common/management/commands/services/services/local_model.py +++ b/apps/common/management/commands/services/services/local_model.py @@ -24,12 +24,13 @@ def cmd(self): os.environ.setdefault('SERVER_NAME', 'local_model') log_format = '%(h)s %(t)s %(L)ss "%(r)s" %(s)s %(b)s ' bind = f'{CONFIG.get("LOCAL_MODEL_HOST")}:{CONFIG.get("LOCAL_MODEL_PORT")}' + worker = CONFIG.get("LOCAL_MODEL_HOST_WORKER", 1) cmd = [ 'gunicorn', 'smartdoc.wsgi:application', '-b', bind, '-k', 'gthread', '--threads', '200', - '-w', "1", + '-w', str(worker), '--max-requests', '10240', '--max-requests-jitter', '2048', '--access-logformat', log_format, diff --git a/apps/common/util/common.py b/apps/common/util/common.py index b0111029af9..8583a1c989f 100644 --- a/apps/common/util/common.py +++ b/apps/common/util/common.py @@ -11,6 +11,7 @@ import io import mimetypes import pickle +import random import re import shutil from functools import reduce @@ -297,3 +298,14 @@ def markdown_to_plain_text(md: str) -> str: # 去除首尾空格 text = text.strip() return text + + +SAFE_CHAR_SET = ( + [chr(i) for i in range(65, 91) if chr(i) not in {'I', 'O'}] + # 大写字母 A-H, J-N, P-Z + [chr(i) for i in range(97, 123) if chr(i) not in {'i', 'l', 'o'}] + # 小写字母 a-h, j-n, p-z + [str(i) for i in range(10) if str(i) not in {'0', '1', '7'}] # 数字 2-6, 8-9 +) + + +def get_random_chars(number=4): + return ''.join(random.choices(SAFE_CHAR_SET, k=number)) diff --git a/apps/common/util/function_code.py b/apps/common/util/function_code.py index 30ce3a33d20..3a877a62367 100644 --- a/apps/common/util/function_code.py +++ b/apps/common/util/function_code.py @@ -7,13 +7,12 @@ @desc: """ import os +import pickle import subprocess import sys import uuid from textwrap import dedent -from diskcache import Cache - from smartdoc.const import BASE_DIR from smartdoc.const import PROJECT_DIR @@ -37,6 +36,8 @@ def _createdir(self): old_mask = os.umask(0o077) try: os.makedirs(self.sandbox_path, 0o700, exist_ok=True) + os.makedirs(os.path.join(self.sandbox_path, 'execute'), 0o700, exist_ok=True) + os.makedirs(os.path.join(self.sandbox_path, 'result'), 0o700, exist_ok=True) finally: os.umask(old_mask) @@ -44,10 +45,11 @@ def exec_code(self, code_str, keywords): _id = str(uuid.uuid1()) success = '{"code":200,"msg":"成功","data":exec_result}' err = '{"code":500,"msg":str(e),"data":None}' - path = r'' + self.sandbox_path + '' + result_path = f'{self.sandbox_path}/result/{_id}.result' _exec_code = f""" try: import os + import pickle env = dict(os.environ) for key in list(env.keys()): if key in os.environ and (key.startswith('MAXKB') or key.startswith('POSTGRES') or key.startswith('PG')): @@ -60,13 +62,11 @@ def exec_code(self, code_str, keywords): for local in locals_v: globals_v[local] = locals_v[local] exec_result=f(**keywords) - from diskcache import Cache - cache = Cache({path!a}) - cache.set({_id!a},{success}) + with open({result_path!a}, 'wb') as file: + file.write(pickle.dumps({success})) except Exception as e: - from diskcache import Cache - cache = Cache({path!a}) - cache.set({_id!a},{err}) + with open({result_path!a}, 'wb') as file: + file.write(pickle.dumps({err})) """ if self.sandbox: subprocess_result = self._exec_sandbox(_exec_code, _id) @@ -74,18 +74,18 @@ def exec_code(self, code_str, keywords): subprocess_result = self._exec(_exec_code) if subprocess_result.returncode == 1: raise Exception(subprocess_result.stderr) - cache = Cache(self.sandbox_path) - result = cache.get(_id) - cache.delete(_id) + with open(result_path, 'rb') as file: + result = pickle.loads(file.read()) + os.remove(result_path) if result.get('code') == 200: return result.get('data') raise Exception(result.get('msg')) def _exec_sandbox(self, _code, _id): - exec_python_file = f'{self.sandbox_path}/{_id}.py' + exec_python_file = f'{self.sandbox_path}/execute/{_id}.py' with open(exec_python_file, 'w') as file: file.write(_code) - os.system(f"chown {self.user}:{self.user} {exec_python_file}") + os.system(f"chown {self.user}:root {exec_python_file}") kwargs = {'cwd': BASE_DIR} subprocess_result = subprocess.run( ['su', '-s', python_directory, '-c', "exec(open('" + exec_python_file + "').read())", self.user], diff --git a/apps/common/util/rsa_util.py b/apps/common/util/rsa_util.py index 00301867208..452ca678d9e 100644 --- a/apps/common/util/rsa_util.py +++ b/apps/common/util/rsa_util.py @@ -40,15 +40,12 @@ def generate(): def get_key_pair(): rsa_value = rsa_cache.get(cache_key) if rsa_value is None: - lock.acquire() - rsa_value = rsa_cache.get(cache_key) - if rsa_value is not None: - return rsa_value - try: + with lock: + rsa_value = rsa_cache.get(cache_key) + if rsa_value is not None: + return rsa_value rsa_value = get_key_pair_by_sql() rsa_cache.set(cache_key, rsa_value) - finally: - lock.release() return rsa_value diff --git a/apps/dataset/serializers/document_serializers.py b/apps/dataset/serializers/document_serializers.py index 5915877fc7c..caae7a78dae 100644 --- a/apps/dataset/serializers/document_serializers.py +++ b/apps/dataset/serializers/document_serializers.py @@ -23,6 +23,8 @@ from django.db.models import QuerySet, Count from django.db.models.functions import Substr, Reverse from django.http import HttpResponse +from django.utils.translation import get_language +from django.utils.translation import gettext_lazy as _, gettext, to_locale from drf_yasg import openapi from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE from rest_framework import serializers @@ -64,8 +66,6 @@ embedding_by_document_list from setting.models import Model from smartdoc.conf import PROJECT_DIR -from django.utils.translation import gettext_lazy as _, gettext, to_locale -from django.utils.translation import get_language parse_qa_handle_list = [XlsParseQAHandle(), CsvParseQAHandle(), XlsxParseQAHandle(), ZipParseQAHandle()] parse_table_handle_list = [CsvSplitTableHandle(), XlsSplitTableHandle(), XlsxSplitTableHandle()] @@ -141,7 +141,8 @@ def is_valid(self, *, document: Document = None): if 'meta' in self.data and self.data.get('meta') is not None: dataset_meta_valid_map = self.get_meta_valid_map() valid_class = dataset_meta_valid_map.get(document.type) - valid_class(data=self.data.get('meta')).is_valid(raise_exception=True) + if valid_class is not None: + valid_class(data=self.data.get('meta')).is_valid(raise_exception=True) class DocumentWebInstanceSerializer(ApiMixin, serializers.Serializer): @@ -661,6 +662,8 @@ def get_workbook(data_dict, document_dict): cell = worksheet.cell(row=row_idx + 1, column=col_idx + 1) if isinstance(col, str): col = re.sub(ILLEGAL_CHARACTERS_RE, '', col) + if col.startswith(('=', '+', '-', '@')): + col = '\ufeff' + col cell.value = col # 创建HttpResponse对象返回Excel文件 return workbook @@ -853,7 +856,7 @@ def get_request_body_api(): class Create(ApiMixin, serializers.Serializer): dataset_id = serializers.UUIDField(required=True, error_messages=ErrMessage.char( - _('document id'))) + _('dataset id'))) def is_valid(self, *, raise_exception=False): super().is_valid(raise_exception=True) diff --git a/apps/dataset/serializers/file_serializers.py b/apps/dataset/serializers/file_serializers.py index 37f72fc8429..899c8a088de 100644 --- a/apps/dataset/serializers/file_serializers.py +++ b/apps/dataset/serializers/file_serializers.py @@ -28,6 +28,9 @@ "woff2": "font/woff2", "jar": "application/java-archive", "war": "application/java-archive", "ear": "application/java-archive", "json": "application/json", "hqx": "application/mac-binhex40", "doc": "application/msword", "pdf": "application/pdf", "ps": "application/postscript", + "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", "eps": "application/postscript", "ai": "application/postscript", "rtf": "application/rtf", "m3u8": "application/vnd.apple.mpegurl", "kml": "application/vnd.google-earth.kml+xml", "kmz": "application/vnd.google-earth.kmz", "xls": "application/vnd.ms-excel", @@ -87,4 +90,4 @@ def get(self, with_valid=True): 'Content-Disposition': 'attachment; filename="{}"'.format( file.file_name)}) return HttpResponse(file.get_byte(), status=200, - headers={'Content-Type': mime_types.get(file.file_name.split(".")[-1], 'text/plain')}) + headers={'Content-Type': mime_types.get(file_type, 'text/plain')}) diff --git a/apps/dataset/sql/update_document_char_length.sql b/apps/dataset/sql/update_document_char_length.sql index 4a4060cd9d4..2781809b23d 100644 --- a/apps/dataset/sql/update_document_char_length.sql +++ b/apps/dataset/sql/update_document_char_length.sql @@ -2,6 +2,7 @@ UPDATE "document" SET "char_length" = ( SELECT CASE WHEN "sum" ( "char_length" ( "content" ) ) IS NULL THEN 0 ELSE "sum" ( "char_length" ( "content" ) ) - END FROM paragraph WHERE "document_id" = %s ) + END FROM paragraph WHERE "document_id" = %s ), + "update_time" = CURRENT_TIMESTAMP WHERE "id" = %s \ No newline at end of file diff --git a/apps/dataset/views/dataset.py b/apps/dataset/views/dataset.py index bbb9e033980..ad28bc1984e 100644 --- a/apps/dataset/views/dataset.py +++ b/apps/dataset/views/dataset.py @@ -7,13 +7,13 @@ @desc: """ +from django.utils.translation import gettext_lazy as _ from drf_yasg.utils import swagger_auto_schema from rest_framework.decorators import action from rest_framework.parsers import MultiPartParser from rest_framework.views import APIView from rest_framework.views import Request -import dataset.models from common.auth import TokenAuth, has_permissions from common.constants.permission_constants import PermissionConstants, CompareConstants, Permission, Group, Operate, \ ViewPermission, RoleConstants @@ -25,7 +25,6 @@ from dataset.serializers.dataset_serializers import DataSetSerializers from dataset.views.common import get_dataset_operation_object from setting.serializers.provider_serializers import ModelSerializer -from django.utils.translation import gettext_lazy as _ class Dataset(APIView): @@ -141,21 +140,22 @@ def post(self, request: Request): class HitTest(APIView): authentication_classes = [TokenAuth] - @action(methods="GET", detail=False) + @action(methods="PUT", detail=False) @swagger_auto_schema(operation_summary=_('Hit test list'), operation_id=_('Hit test list'), manual_parameters=CommonApi.HitTestApi.get_request_params_api(), responses=result.get_api_array_response(CommonApi.HitTestApi.get_response_body_api()), tags=[_('Knowledge Base')]) @has_permissions(lambda r, keywords: Permission(group=Group.DATASET, operate=Operate.USE, dynamic_tag=keywords.get('dataset_id'))) - def get(self, request: Request, dataset_id: str): - return result.success( - DataSetSerializers.HitTest(data={'id': dataset_id, 'user_id': request.user.id, - "query_text": request.query_params.get("query_text"), - "top_number": request.query_params.get("top_number"), - 'similarity': request.query_params.get('similarity'), - 'search_mode': request.query_params.get('search_mode')}).hit_test( - )) + def put(self, request: Request, dataset_id: str): + return result.success(DataSetSerializers.HitTest(data={ + 'id': dataset_id, + 'user_id': request.user.id, + "query_text": request.data.get("query_text"), + "top_number": request.data.get("top_number"), + 'similarity': request.data.get('similarity'), + 'search_mode': request.data.get('search_mode')} + ).hit_test()) class Embedding(APIView): authentication_classes = [TokenAuth] diff --git a/apps/function_lib/migrations/0004_functionlib_decimal_date.py b/apps/function_lib/migrations/0004_functionlib_decimal_date.py new file mode 100644 index 00000000000..82e4a6d029a --- /dev/null +++ b/apps/function_lib/migrations/0004_functionlib_decimal_date.py @@ -0,0 +1,127 @@ +# Generated by Django 4.2.15 on 2025-03-13 07:21 + +from django.db import migrations +from django.db.models import Q + +mysql_template = """ +def query_mysql(host,port, user, password, database, sql): + import pymysql + import json + from pymysql.cursors import DictCursor + from datetime import datetime, date + + def default_serializer(obj): + from decimal import Decimal + if isinstance(obj, (datetime, date)): + return obj.isoformat() # 将 datetime/date 转换为 ISO 格式字符串 + elif isinstance(obj, Decimal): + return float(obj) # 将 Decimal 转换为 float + raise TypeError(f"Type {type(obj)} not serializable") + + try: + # 创建连接 + db = pymysql.connect( + host=host, + port=int(port), + user=user, + password=password, + database=database, + cursorclass=DictCursor # 使用字典游标 + ) + + # 使用 cursor() 方法创建一个游标对象 cursor + cursor = db.cursor() + + # 使用 execute() 方法执行 SQL 查询 + cursor.execute(sql) + + # 使用 fetchall() 方法获取所有数据 + data = cursor.fetchall() + + # 处理 bytes 类型的数据 + for row in data: + for key, value in row.items(): + if isinstance(value, bytes): + row[key] = value.decode("utf-8") # 转换为字符串 + + # 将数据序列化为 JSON + json_data = json.dumps(data, default=default_serializer, ensure_ascii=False) + return json_data + + # 关闭数据库连接 + db.close() + + except Exception as e: + print(f"Error while connecting to MySQL: {e}") + raise e +""" + +pgsql_template = """ +def queryPgSQL(database, user, password, host, port, query): + import psycopg2 + import json + from datetime import datetime + + # 自定义 JSON 序列化函数 + def default_serializer(obj): + from decimal import Decimal + if isinstance(obj, datetime): + return obj.isoformat() # 将 datetime 转换为 ISO 格式字符串 + elif isinstance(obj, Decimal): + return float(obj) # 将 Decimal 转换为 float + raise TypeError(f"Type {type(obj)} not serializable") + + # 数据库连接信息 + conn_params = { + "dbname": database, + "user": user, + "password": password, + "host": host, + "port": port + } + try: + # 建立连接 + conn = psycopg2.connect(**conn_params) + print("连接成功!") + # 创建游标对象 + cursor = conn.cursor() + # 执行查询语句 + cursor.execute(query) + # 获取查询结果 + rows = cursor.fetchall() + # 处理 bytes 类型的数据 + columns = [desc[0] for desc in cursor.description] + result = [dict(zip(columns, row)) for row in rows] + # 转换为 JSON 格式 + json_result = json.dumps(result, default=default_serializer, ensure_ascii=False) + return json_result + except Exception as e: + print(f"发生错误:{e}") + raise e + finally: + # 关闭游标和连接 + if cursor: + cursor.close() + if conn: + conn.close() +""" + + +def fix_type(apps, schema_editor): + FunctionLib = apps.get_model('function_lib', 'FunctionLib') + FunctionLib.objects.filter( + Q(id='22c21b76-0308-11f0-9694-5618c4394482') | Q(template_id='22c21b76-0308-11f0-9694-5618c4394482') + ).update(code=mysql_template) + FunctionLib.objects.filter( + Q(id='bd1e8b88-0302-11f0-87bb-5618c4394482') | Q(template_id='bd1e8b88-0302-11f0-87bb-5618c4394482') + ).update(code=pgsql_template) + + +class Migration(migrations.Migration): + dependencies = [ + ('function_lib', '0003_functionlib_function_type_functionlib_icon_and_more'), + ] + + operations = [ + migrations.RunPython(fix_type) + ] diff --git a/apps/function_lib/serializers/function_lib_serializer.py b/apps/function_lib/serializers/function_lib_serializer.py index 440eb22c786..ad7ff3cce61 100644 --- a/apps/function_lib/serializers/function_lib_serializer.py +++ b/apps/function_lib/serializers/function_lib_serializer.py @@ -33,11 +33,13 @@ function_executor = FunctionExecutor(CONFIG.get('SANDBOX')) + class FlibInstance: def __init__(self, function_lib: dict, version: str): self.function_lib = function_lib self.version = version + def encryption(message: str): """ 加密敏感字段数据 加密方式是 如果密码是 1234567890 那么给前端则是 123******890 @@ -68,7 +70,8 @@ def encryption(message: str): class FunctionLibModelSerializer(serializers.ModelSerializer): class Meta: model = FunctionLib - fields = ['id', 'name', 'icon', 'desc', 'code', 'input_field_list','init_field_list', 'init_params', 'permission_type', 'is_active', 'user_id', 'template_id', + fields = ['id', 'name', 'icon', 'desc', 'code', 'input_field_list', 'init_field_list', 'init_params', + 'permission_type', 'is_active', 'user_id', 'template_id', 'create_time', 'update_time'] @@ -148,7 +151,6 @@ class Query(serializers.Serializer): select_user_id = serializers.CharField(required=False, allow_null=True, allow_blank=True) function_type = serializers.CharField(required=False, allow_null=True, allow_blank=True) - def get_query_set(self): query_set = QuerySet(FunctionLib).filter( (Q(user_id=self.data.get('user_id')) | Q(permission_type='PUBLIC'))) @@ -269,7 +271,7 @@ class Operate(serializers.Serializer): def is_valid(self, *, raise_exception=False): super().is_valid(raise_exception=True) - if not QuerySet(FunctionLib).filter(id=self.data.get('id')).exists(): + if not QuerySet(FunctionLib).filter(user_id=self.data.get('user_id'), id=self.data.get('id')).exists(): raise AppApiException(500, _('Function does not exist')) def delete(self, with_valid=True): @@ -285,7 +287,8 @@ def edit(self, instance, with_valid=True): if with_valid: self.is_valid(raise_exception=True) EditFunctionLib(data=instance).is_valid(raise_exception=True) - edit_field_list = ['name', 'desc', 'code', 'icon', 'input_field_list', 'init_field_list', 'init_params', 'permission_type', 'is_active'] + edit_field_list = ['name', 'desc', 'code', 'icon', 'input_field_list', 'init_field_list', 'init_params', + 'permission_type', 'is_active'] edit_dict = {field: instance.get(field) for field in edit_field_list if ( field in instance and instance.get(field) is not None)} @@ -317,7 +320,8 @@ def one(self, with_valid=True): if function_lib.init_params: function_lib.init_params = json.loads(rsa_long_decrypt(function_lib.init_params)) if function_lib.init_field_list: - password_fields = [i["field"] for i in function_lib.init_field_list if i.get("input_type") == "PasswordInput"] + password_fields = [i["field"] for i in function_lib.init_field_list if + i.get("input_type") == "PasswordInput"] if function_lib.init_params: for k in function_lib.init_params: if k in password_fields and function_lib.init_params[k]: diff --git a/apps/locales/en_US/LC_MESSAGES/django.po b/apps/locales/en_US/LC_MESSAGES/django.po index d13912928b9..9b83be9686d 100644 --- a/apps/locales/en_US/LC_MESSAGES/django.po +++ b/apps/locales/en_US/LC_MESSAGES/django.po @@ -7238,7 +7238,7 @@ msgstr "" msgid "" "The confirmation password must be 6-20 characters long and must be a " "combination of letters, numbers, and special characters." -msgstr "" +msgstr "The confirmation password must be 6-20 characters long and must be a combination of letters, numbers, and special characters.(Special character support:_、!、@、#、$、(、) ……)" #: community/apps/users/serializers/user_serializers.py:380 #, python-brace-format @@ -7490,4 +7490,22 @@ msgid "Field: {name} No value set" msgstr "" msgid "Generate related" +msgstr "" + +msgid "Obtain graphical captcha" +msgstr "" + +msgid "Captcha code error or expiration" +msgstr "" + +msgid "captcha" +msgstr "" + +msgid "Reasoning enable" +msgstr "" + +msgid "Reasoning start tag" +msgstr "" + +msgid "Reasoning end tag" msgstr "" \ No newline at end of file diff --git a/apps/locales/zh_CN/LC_MESSAGES/django.po b/apps/locales/zh_CN/LC_MESSAGES/django.po index b0ab7871bf6..9500103c702 100644 --- a/apps/locales/zh_CN/LC_MESSAGES/django.po +++ b/apps/locales/zh_CN/LC_MESSAGES/django.po @@ -4536,7 +4536,7 @@ msgstr "修改知识库信息" #: community/apps/dataset/views/document.py:463 #: community/apps/dataset/views/document.py:464 msgid "Get the knowledge base paginated list" -msgstr "获取知识库分页列表" +msgstr "获取知识库文档分页列表" #: community/apps/dataset/views/document.py:31 #: community/apps/dataset/views/document.py:32 @@ -7395,7 +7395,7 @@ msgstr "语言只支持:" msgid "" "The confirmation password must be 6-20 characters long and must be a " "combination of letters, numbers, and special characters." -msgstr "确认密码长度6-20个字符,必须字母、数字、特殊字符组合" +msgstr "确认密码长度6-20个字符,必须字母、数字、特殊字符组合(特殊字符支持:_、!、@、#、$、(、) ……)" #: community/apps/users/serializers/user_serializers.py:380 #, python-brace-format @@ -7653,4 +7653,22 @@ msgid "Field: {name} No value set" msgstr "字段: {name} 未设置值" msgid "Generate related" -msgstr "生成问题" \ No newline at end of file +msgstr "生成问题" + +msgid "Obtain graphical captcha" +msgstr "获取图形验证码" + +msgid "Captcha code error or expiration" +msgstr "验证码错误或过期" + +msgid "captcha" +msgstr "验证码" + +msgid "Reasoning enable" +msgstr "开启思考过程" + +msgid "Reasoning start tag" +msgstr "思考过程开始标签" + +msgid "Reasoning end tag" +msgstr "思考过程结束标签" \ No newline at end of file diff --git a/apps/locales/zh_Hant/LC_MESSAGES/django.po b/apps/locales/zh_Hant/LC_MESSAGES/django.po index dab1d176c26..6993cdd2161 100644 --- a/apps/locales/zh_Hant/LC_MESSAGES/django.po +++ b/apps/locales/zh_Hant/LC_MESSAGES/django.po @@ -4545,7 +4545,7 @@ msgstr "修改知識庫信息" #: community/apps/dataset/views/document.py:463 #: community/apps/dataset/views/document.py:464 msgid "Get the knowledge base paginated list" -msgstr "獲取知識庫分頁列表" +msgstr "獲取知識庫文档分頁列表" #: community/apps/dataset/views/document.py:31 #: community/apps/dataset/views/document.py:32 @@ -7405,7 +7405,7 @@ msgstr "語言只支持:" msgid "" "The confirmation password must be 6-20 characters long and must be a " "combination of letters, numbers, and special characters." -msgstr "確認密碼長度6-20個字符,必須字母、數字、特殊字符組合" +msgstr "確認密碼長度6-20個字符,必須字母、數字、特殊字符組合(特殊字元支持:_、!、@、#、$、(、) ……)" #: community/apps/users/serializers/user_serializers.py:380 #, python-brace-format @@ -7663,4 +7663,22 @@ msgid "Field: {name} No value set" msgstr "欄位: {name} 未設定值" msgid "Generate related" -msgstr "生成問題" \ No newline at end of file +msgstr "生成問題" + +msgid "Obtain graphical captcha" +msgstr "獲取圖形驗證碼" + +msgid "Captcha code error or expiration" +msgstr "驗證碼錯誤或過期" + +msgid "captcha" +msgstr "驗證碼" + +msgid "Reasoning enable" +msgstr "開啟思考過程" + +msgid "Reasoning start tag" +msgstr "思考過程開始標籤" + +msgid "Reasoning end tag" +msgstr "思考過程結束標籤" \ No newline at end of file diff --git a/apps/setting/models_provider/base_model_provider.py b/apps/setting/models_provider/base_model_provider.py index 622be703dad..2b02bdc1fb1 100644 --- a/apps/setting/models_provider/base_model_provider.py +++ b/apps/setting/models_provider/base_model_provider.py @@ -106,7 +106,10 @@ def filter_optional_params(model_kwargs): optional_params = {} for key, value in model_kwargs.items(): if key not in ['model_id', 'use_local', 'streaming', 'show_ref_label']: - optional_params[key] = value + if key == 'extra_body' and isinstance(value, dict): + optional_params = {**optional_params, **value} + else: + optional_params[key] = value return optional_params diff --git a/apps/setting/models_provider/constants/model_provider_constants.py b/apps/setting/models_provider/constants/model_provider_constants.py index e6bf698b01a..e68b9361f0b 100644 --- a/apps/setting/models_provider/constants/model_provider_constants.py +++ b/apps/setting/models_provider/constants/model_provider_constants.py @@ -19,6 +19,8 @@ from setting.models_provider.impl.ollama_model_provider.ollama_model_provider import OllamaModelProvider from setting.models_provider.impl.openai_model_provider.openai_model_provider import OpenAIModelProvider from setting.models_provider.impl.qwen_model_provider.qwen_model_provider import QwenModelProvider +from setting.models_provider.impl.regolo_model_provider.regolo_model_provider import \ + RegoloModelProvider from setting.models_provider.impl.siliconCloud_model_provider.siliconCloud_model_provider import \ SiliconCloudModelProvider from setting.models_provider.impl.tencent_cloud_model_provider.tencent_cloud_model_provider import \ @@ -55,3 +57,4 @@ class ModelProvideConstants(Enum): aliyun_bai_lian_model_provider = AliyunBaiLianModelProvider() model_anthropic_provider = AnthropicModelProvider() model_siliconCloud_provider = SiliconCloudModelProvider() + model_regolo_provider = RegoloModelProvider() diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/aliyun_bai_lian_model_provider.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/aliyun_bai_lian_model_provider.py index 8c5031f08f2..b1d72f0869a 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/aliyun_bai_lian_model_provider.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/aliyun_bai_lian_model_provider.py @@ -51,6 +51,23 @@ _("Universal text vector is Tongyi Lab's multi-language text unified vector model based on the LLM base. It provides high-level vector services for multiple mainstream languages around the world and helps developers quickly convert text data into high-quality vector data."), ModelTypeConst.EMBEDDING, aliyun_bai_lian_embedding_model_credential, AliyunBaiLianEmbedding), + ModelInfo('qwen3-0.6b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen3-1.7b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen3-4b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen3-8b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen3-14b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen3-32b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen3-30b-a3b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen3-235b-a22b', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, + BaiLianChatModel), + ModelInfo('qwen-turbo', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, BaiLianChatModel), ModelInfo('qwen-plus', '', ModelTypeConst.LLM, aliyun_bai_lian_llm_model_credential, diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py index f316a0c6d1c..9da30b72796 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py @@ -30,6 +30,29 @@ class BaiLianLLMModelParams(BaseForm): precision=0) +class BaiLianLLMStreamModelParams(BaseForm): + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), + required=True, default_value=0.7, + _min=0.1, + _max=1.0, + _step=0.01, + precision=2) + + max_tokens = forms.SliderField( + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), + required=True, default_value=800, + _min=1, + _max=100000, + _step=1, + precision=0) + + stream = forms.SwitchField(label=TooltipLabel(_('Is the answer in streaming mode'), + _('Is the answer in streaming mode')), + required=True, default_value=True) + + class BaiLianLLMModelCredential(BaseForm, BaseModelCredential): def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, @@ -47,7 +70,11 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=gettext('Hello'))]) + if model_params.get('stream'): + for res in model.stream([HumanMessage(content=gettext('Hello'))]): + pass + else: + model.invoke([HumanMessage(content=gettext('Hello'))]) except Exception as e: traceback.print_exc() if isinstance(e, AppApiException): @@ -68,4 +95,6 @@ def encryption_dict(self, model: Dict[str, object]): api_key = forms.PasswordInputField('API Key', required=True) def get_model_params_setting_form(self, model_name): + if 'qwen3' in model_name: + return BaiLianLLMStreamModelParams() return BaiLianLLMModelParams() diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/image.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/image.py index 2b1fe31f228..7cda97f2388 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/image.py @@ -15,9 +15,8 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model_name=model_name, openai_api_key=model_credential.get('api_key'), openai_api_base='https://dashscope.aliyuncs.com/compatible-mode/v1', - # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) return chat_tong_yi diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/llm.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/llm.py index d914f7c8ad6..ee3ee6488c2 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/llm.py @@ -20,5 +20,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model=model_name, openai_api_base=model_credential.get('api_base'), openai_api_key=model_credential.get('api_key'), - **optional_params + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/aws_bedrock_model_provider/model/llm.py b/apps/setting/models_provider/impl/aws_bedrock_model_provider/model/llm.py index ef1c133378e..7b0088a4ab4 100644 --- a/apps/setting/models_provider/impl/aws_bedrock_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/aws_bedrock_model_provider/model/llm.py @@ -1,10 +1,12 @@ import os import re -from typing import Dict +from typing import Dict, List from botocore.config import Config from langchain_community.chat_models import BedrockChat +from langchain_core.messages import BaseMessage, get_buffer_string +from common.config.tokenizer_manage_config import TokenizerManage from setting.models_provider.base_model_provider import MaxKBBaseModel @@ -72,6 +74,20 @@ def new_instance(cls, model_type: str, model_name: str, model_credential: Dict[s config=config ) + def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: + try: + return super().get_num_tokens_from_messages(messages) + except Exception as e: + tokenizer = TokenizerManage.get_tokenizer() + return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages]) + + def get_num_tokens(self, text: str) -> int: + try: + return super().get_num_tokens(text) + except Exception as e: + tokenizer = TokenizerManage.get_tokenizer() + return len(tokenizer.encode(text)) + def _update_aws_credentials(profile_name, access_key_id, secret_access_key): credentials_path = os.path.join(os.path.expanduser("~"), ".aws", "credentials") diff --git a/apps/setting/models_provider/impl/base_chat_open_ai.py b/apps/setting/models_provider/impl/base_chat_open_ai.py index 54076b7efda..626a751f740 100644 --- a/apps/setting/models_provider/impl/base_chat_open_ai.py +++ b/apps/setting/models_provider/impl/base_chat_open_ai.py @@ -1,15 +1,16 @@ # coding=utf-8 -import warnings -from typing import List, Dict, Optional, Any, Iterator, cast, Type, Union +from typing import Dict, Optional, Any, Iterator, cast, Union, Sequence, Callable, Mapping -import openai -from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import LanguageModelInput -from langchain_core.messages import BaseMessage, get_buffer_string, BaseMessageChunk, AIMessageChunk -from langchain_core.outputs import ChatGenerationChunk, ChatGeneration +from langchain_core.messages import BaseMessage, get_buffer_string, BaseMessageChunk, HumanMessageChunk, AIMessageChunk, \ + SystemMessageChunk, FunctionMessageChunk, ChatMessageChunk +from langchain_core.messages.ai import UsageMetadata +from langchain_core.messages.tool import tool_call_chunk, ToolMessageChunk +from langchain_core.outputs import ChatGenerationChunk from langchain_core.runnables import RunnableConfig, ensure_config -from langchain_core.utils.pydantic import is_basemodel_subclass +from langchain_core.tools import BaseTool from langchain_openai import ChatOpenAI +from langchain_openai.chat_models.base import _create_usage_metadata from common.config.tokenizer_manage_config import TokenizerManage @@ -19,6 +20,65 @@ def custom_get_token_ids(text: str): return tokenizer.encode(text) +def _convert_delta_to_message_chunk( + _dict: Mapping[str, Any], default_class: type[BaseMessageChunk] +) -> BaseMessageChunk: + id_ = _dict.get("id") + role = cast(str, _dict.get("role")) + content = cast(str, _dict.get("content") or "") + additional_kwargs: dict = {} + if 'reasoning_content' in _dict: + additional_kwargs['reasoning_content'] = _dict.get('reasoning_content') + if _dict.get("function_call"): + function_call = dict(_dict["function_call"]) + if "name" in function_call and function_call["name"] is None: + function_call["name"] = "" + additional_kwargs["function_call"] = function_call + tool_call_chunks = [] + if raw_tool_calls := _dict.get("tool_calls"): + additional_kwargs["tool_calls"] = raw_tool_calls + try: + tool_call_chunks = [ + tool_call_chunk( + name=rtc["function"].get("name"), + args=rtc["function"].get("arguments"), + id=rtc.get("id"), + index=rtc["index"], + ) + for rtc in raw_tool_calls + ] + except KeyError: + pass + + if role == "user" or default_class == HumanMessageChunk: + return HumanMessageChunk(content=content, id=id_) + elif role == "assistant" or default_class == AIMessageChunk: + return AIMessageChunk( + content=content, + additional_kwargs=additional_kwargs, + id=id_, + tool_call_chunks=tool_call_chunks, # type: ignore[arg-type] + ) + elif role in ("system", "developer") or default_class == SystemMessageChunk: + if role == "developer": + additional_kwargs = {"__openai_role__": "developer"} + else: + additional_kwargs = {} + return SystemMessageChunk( + content=content, id=id_, additional_kwargs=additional_kwargs + ) + elif role == "function" or default_class == FunctionMessageChunk: + return FunctionMessageChunk(content=content, name=_dict["name"], id=id_) + elif role == "tool" or default_class == ToolMessageChunk: + return ToolMessageChunk( + content=content, tool_call_id=_dict["tool_call_id"], id=id_ + ) + elif role or default_class == ChatMessageChunk: + return ChatMessageChunk(content=content, role=role, id=id_) + else: + return default_class(content=content, id=id_) # type: ignore + + class BaseChatOpenAI(ChatOpenAI): usage_metadata: dict = {} custom_get_token_ids = custom_get_token_ids @@ -26,14 +86,20 @@ class BaseChatOpenAI(ChatOpenAI): def get_last_generation_info(self) -> Optional[Dict[str, Any]]: return self.usage_metadata - def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: + def get_num_tokens_from_messages( + self, + messages: list[BaseMessage], + tools: Optional[ + Sequence[Union[dict[str, Any], type, Callable, BaseTool]] + ] = None, + ) -> int: if self.usage_metadata is None or self.usage_metadata == {}: try: return super().get_num_tokens_from_messages(messages) except Exception as e: tokenizer = TokenizerManage.get_tokenizer() return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages]) - return self.usage_metadata.get('input_tokens', 0) + return self.usage_metadata.get('input_tokens', self.usage_metadata.get('prompt_tokens', 0)) def get_num_tokens(self, text: str) -> int: if self.usage_metadata is None or self.usage_metadata == {}: @@ -42,116 +108,80 @@ def get_num_tokens(self, text: str) -> int: except Exception as e: tokenizer = TokenizerManage.get_tokenizer() return len(tokenizer.encode(text)) - return self.get_last_generation_info().get('output_tokens', 0) + return self.get_last_generation_info().get('output_tokens', + self.get_last_generation_info().get('completion_tokens', 0)) + + def _stream(self, *args: Any, **kwargs: Any) -> Iterator[ChatGenerationChunk]: + kwargs['stream_usage'] = True + for chunk in super()._stream(*args, **kwargs): + if chunk.message.usage_metadata is not None: + self.usage_metadata = chunk.message.usage_metadata + yield chunk - def _stream( + def _convert_chunk_to_generation_chunk( self, - messages: List[BaseMessage], - stop: Optional[List[str]] = None, - run_manager: Optional[CallbackManagerForLLMRun] = None, - **kwargs: Any, - ) -> Iterator[ChatGenerationChunk]: - kwargs["stream"] = True - kwargs["stream_options"] = {"include_usage": True} - """Set default stream_options.""" - stream_usage = self._should_stream_usage(kwargs.get('stream_usage'), **kwargs) - # Note: stream_options is not a valid parameter for Azure OpenAI. - # To support users proxying Azure through ChatOpenAI, here we only specify - # stream_options if include_usage is set to True. - # See https://learn.microsoft.com/en-us/azure/ai-services/openai/whats-new - # for release notes. - if stream_usage: - kwargs["stream_options"] = {"include_usage": stream_usage} - - payload = self._get_request_payload(messages, stop=stop, **kwargs) - default_chunk_class: Type[BaseMessageChunk] = AIMessageChunk - base_generation_info = {} - - if "response_format" in payload and is_basemodel_subclass( - payload["response_format"] - ): - # TODO: Add support for streaming with Pydantic response_format. - warnings.warn("Streaming with Pydantic response_format not yet supported.") - chat_result = self._generate( - messages, stop, run_manager=run_manager, **kwargs - ) - msg = chat_result.generations[0].message - yield ChatGenerationChunk( - message=AIMessageChunk( - **msg.dict(exclude={"type", "additional_kwargs"}), - # preserve the "parsed" Pydantic object without converting to dict - additional_kwargs=msg.additional_kwargs, - ), - generation_info=chat_result.generations[0].generation_info, + chunk: dict, + default_chunk_class: type, + base_generation_info: Optional[dict], + ) -> Optional[ChatGenerationChunk]: + if chunk.get("type") == "content.delta": # from beta.chat.completions.stream + return None + token_usage = chunk.get("usage") + choices = ( + chunk.get("choices", []) + # from beta.chat.completions.stream + or chunk.get("chunk", {}).get("choices", []) + ) + + usage_metadata: Optional[UsageMetadata] = ( + _create_usage_metadata(token_usage) if token_usage and token_usage.get("prompt_tokens") else None + ) + if len(choices) == 0: + # logprobs is implicitly None + generation_chunk = ChatGenerationChunk( + message=default_chunk_class(content="", usage_metadata=usage_metadata) ) - return - if self.include_response_headers: - raw_response = self.client.with_raw_response.create(**payload) - response = raw_response.parse() - base_generation_info = {"headers": dict(raw_response.headers)} - else: - response = self.client.create(**payload) - with response: - is_first_chunk = True - for chunk in response: - if not isinstance(chunk, dict): - chunk = chunk.model_dump() - - generation_chunk = super()._convert_chunk_to_generation_chunk( - chunk, - default_chunk_class, - base_generation_info if is_first_chunk else {}, - ) - if generation_chunk is None: - continue - - # custom code - if len(chunk['choices']) > 0 and 'reasoning_content' in chunk['choices'][0]['delta']: - generation_chunk.message.additional_kwargs["reasoning_content"] = chunk['choices'][0]['delta'][ - 'reasoning_content'] - - default_chunk_class = generation_chunk.message.__class__ - logprobs = (generation_chunk.generation_info or {}).get("logprobs") - if run_manager: - run_manager.on_llm_new_token( - generation_chunk.text, chunk=generation_chunk, logprobs=logprobs - ) - is_first_chunk = False - # custom code - if generation_chunk.message.usage_metadata is not None: - self.usage_metadata = generation_chunk.message.usage_metadata - yield generation_chunk - - def _create_chat_result(self, - response: Union[dict, openai.BaseModel], - generation_info: Optional[Dict] = None): - result = super()._create_chat_result(response, generation_info) - try: - reasoning_content = '' - reasoning_content_enable = False - for res in response.choices: - if 'reasoning_content' in res.message.model_extra: - reasoning_content_enable = True - _reasoning_content = res.message.model_extra.get('reasoning_content') - if _reasoning_content is not None: - reasoning_content += _reasoning_content - if reasoning_content_enable: - result.llm_output['reasoning_content'] = reasoning_content - except Exception as e: - pass - return result + return generation_chunk + + choice = choices[0] + if choice["delta"] is None: + return None + + message_chunk = _convert_delta_to_message_chunk( + choice["delta"], default_chunk_class + ) + generation_info = {**base_generation_info} if base_generation_info else {} + + if finish_reason := choice.get("finish_reason"): + generation_info["finish_reason"] = finish_reason + if model_name := chunk.get("model"): + generation_info["model_name"] = model_name + if system_fingerprint := chunk.get("system_fingerprint"): + generation_info["system_fingerprint"] = system_fingerprint + + logprobs = choice.get("logprobs") + if logprobs: + generation_info["logprobs"] = logprobs + + if usage_metadata and isinstance(message_chunk, AIMessageChunk): + message_chunk.usage_metadata = usage_metadata + + generation_chunk = ChatGenerationChunk( + message=message_chunk, generation_info=generation_info or None + ) + return generation_chunk def invoke( self, input: LanguageModelInput, config: Optional[RunnableConfig] = None, *, - stop: Optional[List[str]] = None, + stop: Optional[list[str]] = None, **kwargs: Any, ) -> BaseMessage: config = ensure_config(config) chat_result = cast( - ChatGeneration, + "ChatGeneration", self.generate_prompt( [self._convert_input(input)], stop=stop, @@ -162,7 +192,9 @@ def invoke( run_id=config.pop("run_id", None), **kwargs, ).generations[0][0], + ).message + self.usage_metadata = chat_result.response_metadata[ 'token_usage'] if 'token_usage' in chat_result.response_metadata else chat_result.usage_metadata return chat_result diff --git a/apps/setting/models_provider/impl/deepseek_model_provider/model/llm.py b/apps/setting/models_provider/impl/deepseek_model_provider/model/llm.py index 9db4faca7cc..081d648a716 100644 --- a/apps/setting/models_provider/impl/deepseek_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/deepseek_model_provider/model/llm.py @@ -26,6 +26,6 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model=model_name, openai_api_base='https://api.deepseek.com', openai_api_key=model_credential.get('api_key'), - **optional_params + extra_body=optional_params ) return deepseek_chat_open_ai diff --git a/apps/setting/models_provider/impl/gemini_model_provider/model/llm.py b/apps/setting/models_provider/impl/gemini_model_provider/model/llm.py index 4106cc1d6e3..af23d0341a4 100644 --- a/apps/setting/models_provider/impl/gemini_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/gemini_model_provider/model/llm.py @@ -13,7 +13,7 @@ Tool as GoogleTool, ) from langchain_core.callbacks import CallbackManagerForLLMRun -from langchain_core.messages import BaseMessage +from langchain_core.messages import BaseMessage, get_buffer_string from langchain_core.outputs import ChatGenerationChunk from langchain_google_genai import ChatGoogleGenerativeAI from langchain_google_genai._function_utils import _ToolConfigDict, _ToolDict @@ -22,6 +22,8 @@ from langchain_google_genai._common import ( SafetySettingDict, ) + +from common.config.tokenizer_manage_config import TokenizerManage from setting.models_provider.base_model_provider import MaxKBBaseModel @@ -46,10 +48,18 @@ def get_last_generation_info(self) -> Optional[Dict[str, Any]]: return self.__dict__.get('_last_generation_info') def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: - return self.get_last_generation_info().get('input_tokens', 0) + try: + return self.get_last_generation_info().get('input_tokens', 0) + except Exception as e: + tokenizer = TokenizerManage.get_tokenizer() + return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages]) def get_num_tokens(self, text: str) -> int: - return self.get_last_generation_info().get('output_tokens', 0) + try: + return self.get_last_generation_info().get('output_tokens', 0) + except Exception as e: + tokenizer = TokenizerManage.get_tokenizer() + return len(tokenizer.encode(text)) def _stream( self, diff --git a/apps/setting/models_provider/impl/kimi_model_provider/model/llm.py b/apps/setting/models_provider/impl/kimi_model_provider/model/llm.py index c389c177e4e..c0ce2ec029a 100644 --- a/apps/setting/models_provider/impl/kimi_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/kimi_model_provider/model/llm.py @@ -21,11 +21,10 @@ def is_cache_model(): @staticmethod def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs) - kimi_chat_open_ai = KimiChatModel( openai_api_base=model_credential['api_base'], openai_api_key=model_credential['api_key'], model_name=model_name, - **optional_params + extra_body=optional_params, ) return kimi_chat_open_ai diff --git a/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py b/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py index 0194d1f0d27..add06621937 100644 --- a/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py @@ -25,7 +25,7 @@ class OllamaLLMModelParams(BaseForm): _step=0.01, precision=2) - max_tokens = forms.SliderField( + num_predict = forms.SliderField( TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, diff --git a/apps/setting/models_provider/impl/ollama_model_provider/model/image.py b/apps/setting/models_provider/impl/ollama_model_provider/model/image.py index 4cf0f1d56fc..215ce0130d7 100644 --- a/apps/setting/models_provider/impl/ollama_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/ollama_model_provider/model/image.py @@ -28,5 +28,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/openai_model_provider/model/image.py b/apps/setting/models_provider/impl/openai_model_provider/model/image.py index 731f476c45f..7ac0906a786 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/openai_model_provider/model/image.py @@ -16,5 +16,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/openai_model_provider/model/llm.py b/apps/setting/models_provider/impl/openai_model_provider/model/llm.py index 2e6dd89ac93..1893852100b 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/openai_model_provider/model/llm.py @@ -9,7 +9,6 @@ from typing import List, Dict from langchain_core.messages import BaseMessage, get_buffer_string -from langchain_openai.chat_models import ChatOpenAI from common.config.tokenizer_manage_config import TokenizerManage from setting.models_provider.base_model_provider import MaxKBBaseModel @@ -35,9 +34,9 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** streaming = False azure_chat_open_ai = OpenAIChatModel( model=model_name, - openai_api_base=model_credential.get('api_base'), - openai_api_key=model_credential.get('api_key'), - **optional_params, + base_url=model_credential.get('api_base'), + api_key=model_credential.get('api_key'), + extra_body=optional_params, streaming=streaming, custom_get_token_ids=custom_get_token_ids ) diff --git a/apps/setting/models_provider/impl/qwen_model_provider/model/image.py b/apps/setting/models_provider/impl/qwen_model_provider/model/image.py index 97166757e67..bf3af0e3484 100644 --- a/apps/setting/models_provider/impl/qwen_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/qwen_model_provider/model/image.py @@ -18,9 +18,8 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model_name=model_name, openai_api_key=model_credential.get('api_key'), openai_api_base='https://dashscope.aliyuncs.com/compatible-mode/v1', - # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) return chat_tong_yi diff --git a/apps/setting/models_provider/impl/qwen_model_provider/model/llm.py b/apps/setting/models_provider/impl/qwen_model_provider/model/llm.py index 3b66ddfd62a..c4df28af9bb 100644 --- a/apps/setting/models_provider/impl/qwen_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/qwen_model_provider/model/llm.py @@ -26,6 +26,6 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** openai_api_base='https://dashscope.aliyuncs.com/compatible-mode/v1', streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) return chat_tong_yi diff --git a/apps/setting/models_provider/impl/regolo_model_provider/__init__.py b/apps/setting/models_provider/impl/regolo_model_provider/__init__.py new file mode 100644 index 00000000000..2dc4ab10db4 --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/__init__.py @@ -0,0 +1,8 @@ +# coding=utf-8 +""" + @project: maxkb + @Author:虎 + @file: __init__.py.py + @date:2024/3/28 16:25 + @desc: +""" diff --git a/apps/setting/models_provider/impl/regolo_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/regolo_model_provider/credential/embedding.py new file mode 100644 index 00000000000..ddea7fed52d --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/credential/embedding.py @@ -0,0 +1,52 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: embedding.py + @date:2024/7/12 16:45 + @desc: +""" +import traceback +from typing import Dict + +from django.utils.translation import gettext as _ + +from common import forms +from common.exception.app_exception import AppApiException +from common.forms import BaseForm +from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode + + +class RegoloEmbeddingCredential(BaseForm, BaseModelCredential): + def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, + raise_exception=True): + model_type_list = provider.get_model_type_list() + if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))): + raise AppApiException(ValidCode.valid_error.value, + _('{model_type} Model type is not supported').format(model_type=model_type)) + + for key in ['api_key']: + if key not in model_credential: + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + else: + return False + try: + model = provider.get_model(model_type, model_name, model_credential) + model.embed_query(_('Hello')) + except Exception as e: + traceback.print_exc() + if isinstance(e, AppApiException): + raise e + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, + _('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) + else: + return False + return True + + def encryption_dict(self, model: Dict[str, object]): + return {**model, 'api_key': super().encryption(model.get('api_key', ''))} + + api_key = forms.PasswordInputField('API Key', required=True) diff --git a/apps/setting/models_provider/impl/regolo_model_provider/credential/image.py b/apps/setting/models_provider/impl/regolo_model_provider/credential/image.py new file mode 100644 index 00000000000..5975c774806 --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/credential/image.py @@ -0,0 +1,74 @@ +# coding=utf-8 +import base64 +import os +import traceback +from typing import Dict + +from langchain_core.messages import HumanMessage + +from common import forms +from common.exception.app_exception import AppApiException +from common.forms import BaseForm, TooltipLabel +from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode +from django.utils.translation import gettext_lazy as _, gettext + + +class RegoloImageModelParams(BaseForm): + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), + required=True, default_value=0.7, + _min=0.1, + _max=1.0, + _step=0.01, + precision=2) + + max_tokens = forms.SliderField( + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), + required=True, default_value=800, + _min=1, + _max=100000, + _step=1, + precision=0) + + +class RegoloImageModelCredential(BaseForm, BaseModelCredential): + api_base = forms.TextInputField('API URL', required=True) + api_key = forms.PasswordInputField('API Key', required=True) + + def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, + raise_exception=False): + model_type_list = provider.get_model_type_list() + if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))): + raise AppApiException(ValidCode.valid_error.value, + gettext('{model_type} Model type is not supported').format(model_type=model_type)) + + for key in ['api_key']: + if key not in model_credential: + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key)) + else: + return False + try: + model = provider.get_model(model_type, model_name, model_credential, **model_params) + res = model.stream([HumanMessage(content=[{"type": "text", "text": gettext('Hello')}])]) + for chunk in res: + print(chunk) + except Exception as e: + traceback.print_exc() + if isinstance(e, AppApiException): + raise e + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, + gettext( + 'Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) + else: + return False + return True + + def encryption_dict(self, model: Dict[str, object]): + return {**model, 'api_key': super().encryption(model.get('api_key', ''))} + + def get_model_params_setting_form(self, model_name): + return RegoloImageModelParams() diff --git a/apps/setting/models_provider/impl/regolo_model_provider/credential/llm.py b/apps/setting/models_provider/impl/regolo_model_provider/credential/llm.py new file mode 100644 index 00000000000..60eb4ff0abf --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/credential/llm.py @@ -0,0 +1,78 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: llm.py + @date:2024/7/11 18:32 + @desc: +""" +import traceback +from typing import Dict + +from django.utils.translation import gettext_lazy as _, gettext +from langchain_core.messages import HumanMessage + +from common import forms +from common.exception.app_exception import AppApiException +from common.forms import BaseForm, TooltipLabel +from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode + + +class RegoloLLMModelParams(BaseForm): + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), + required=True, default_value=0.7, + _min=0.1, + _max=1.0, + _step=0.01, + precision=2) + + max_tokens = forms.SliderField( + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), + required=True, default_value=800, + _min=1, + _max=100000, + _step=1, + precision=0) + + +class RegoloLLMModelCredential(BaseForm, BaseModelCredential): + + def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, + raise_exception=False): + model_type_list = provider.get_model_type_list() + if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))): + raise AppApiException(ValidCode.valid_error.value, + gettext('{model_type} Model type is not supported').format(model_type=model_type)) + + for key in ['api_key']: + if key not in model_credential: + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key)) + else: + return False + try: + + model = provider.get_model(model_type, model_name, model_credential, **model_params) + model.invoke([HumanMessage(content=gettext('Hello'))]) + except Exception as e: + traceback.print_exc() + if isinstance(e, AppApiException): + raise e + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, + gettext( + 'Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) + else: + return False + return True + + def encryption_dict(self, model: Dict[str, object]): + return {**model, 'api_key': super().encryption(model.get('api_key', ''))} + + api_key = forms.PasswordInputField('API Key', required=True) + + def get_model_params_setting_form(self, model_name): + return RegoloLLMModelParams() diff --git a/apps/setting/models_provider/impl/regolo_model_provider/credential/tti.py b/apps/setting/models_provider/impl/regolo_model_provider/credential/tti.py new file mode 100644 index 00000000000..88f46ce4143 --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/credential/tti.py @@ -0,0 +1,89 @@ +# coding=utf-8 +import traceback +from typing import Dict + +from django.utils.translation import gettext_lazy as _, gettext + +from common import forms +from common.exception.app_exception import AppApiException +from common.forms import BaseForm, TooltipLabel +from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode + + +class RegoloTTIModelParams(BaseForm): + size = forms.SingleSelect( + TooltipLabel(_('Image size'), + _('The image generation endpoint allows you to create raw images based on text prompts. ')), + required=True, + default_value='1024x1024', + option_list=[ + {'value': '1024x1024', 'label': '1024x1024'}, + {'value': '1024x1792', 'label': '1024x1792'}, + {'value': '1792x1024', 'label': '1792x1024'}, + ], + text_field='label', + value_field='value' + ) + + quality = forms.SingleSelect( + TooltipLabel(_('Picture quality'), _(''' +By default, images are produced in standard quality. + ''')), + required=True, + default_value='standard', + option_list=[ + {'value': 'standard', 'label': 'standard'}, + {'value': 'hd', 'label': 'hd'}, + ], + text_field='label', + value_field='value' + ) + + n = forms.SliderField( + TooltipLabel(_('Number of pictures'), + _('1 as default')), + required=True, default_value=1, + _min=1, + _max=10, + _step=1, + precision=0) + + +class RegoloTextToImageModelCredential(BaseForm, BaseModelCredential): + api_key = forms.PasswordInputField('API Key', required=True) + + def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, + raise_exception=False): + model_type_list = provider.get_model_type_list() + if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))): + raise AppApiException(ValidCode.valid_error.value, + gettext('{model_type} Model type is not supported').format(model_type=model_type)) + + for key in ['api_key']: + if key not in model_credential: + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key)) + else: + return False + try: + model = provider.get_model(model_type, model_name, model_credential, **model_params) + res = model.check_auth() + print(res) + except Exception as e: + traceback.print_exc() + if isinstance(e, AppApiException): + raise e + if raise_exception: + raise AppApiException(ValidCode.valid_error.value, + gettext( + 'Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) + else: + return False + return True + + def encryption_dict(self, model: Dict[str, object]): + return {**model, 'api_key': super().encryption(model.get('api_key', ''))} + + def get_model_params_setting_form(self, model_name): + return RegoloTTIModelParams() diff --git a/apps/setting/models_provider/impl/regolo_model_provider/icon/regolo_icon_svg b/apps/setting/models_provider/impl/regolo_model_provider/icon/regolo_icon_svg new file mode 100644 index 00000000000..b69154451ad --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/icon/regolo_icon_svg @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + diff --git a/apps/setting/models_provider/impl/regolo_model_provider/model/embedding.py b/apps/setting/models_provider/impl/regolo_model_provider/model/embedding.py new file mode 100644 index 00000000000..b067b8eff29 --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/model/embedding.py @@ -0,0 +1,23 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: embedding.py + @date:2024/7/12 17:44 + @desc: +""" +from typing import Dict + +from langchain_community.embeddings import OpenAIEmbeddings + +from setting.models_provider.base_model_provider import MaxKBBaseModel + + +class RegoloEmbeddingModel(MaxKBBaseModel, OpenAIEmbeddings): + @staticmethod + def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): + return RegoloEmbeddingModel( + api_key=model_credential.get('api_key'), + model=model_name, + openai_api_base="https://api.regolo.ai/v1", + ) diff --git a/apps/setting/models_provider/impl/regolo_model_provider/model/image.py b/apps/setting/models_provider/impl/regolo_model_provider/model/image.py new file mode 100644 index 00000000000..f16768fad1e --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/model/image.py @@ -0,0 +1,19 @@ +from typing import Dict + +from setting.models_provider.base_model_provider import MaxKBBaseModel +from setting.models_provider.impl.base_chat_open_ai import BaseChatOpenAI + + +class RegoloImage(MaxKBBaseModel, BaseChatOpenAI): + + @staticmethod + def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): + optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs) + return RegoloImage( + model_name=model_name, + openai_api_base="https://api.regolo.ai/v1", + openai_api_key=model_credential.get('api_key'), + streaming=True, + stream_usage=True, + extra_body=optional_params + ) diff --git a/apps/setting/models_provider/impl/regolo_model_provider/model/llm.py b/apps/setting/models_provider/impl/regolo_model_provider/model/llm.py new file mode 100644 index 00000000000..126a756a20d --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/model/llm.py @@ -0,0 +1,38 @@ +# coding=utf-8 +""" + @project: maxkb + @Author:虎 + @file: llm.py + @date:2024/4/18 15:28 + @desc: +""" +from typing import List, Dict + +from langchain_core.messages import BaseMessage, get_buffer_string +from langchain_openai.chat_models import ChatOpenAI + +from common.config.tokenizer_manage_config import TokenizerManage +from setting.models_provider.base_model_provider import MaxKBBaseModel +from setting.models_provider.impl.base_chat_open_ai import BaseChatOpenAI + + +def custom_get_token_ids(text: str): + tokenizer = TokenizerManage.get_tokenizer() + return tokenizer.encode(text) + + +class RegoloChatModel(MaxKBBaseModel, BaseChatOpenAI): + + @staticmethod + def is_cache_model(): + return False + + @staticmethod + def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): + optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs) + return RegoloChatModel( + model=model_name, + openai_api_base="https://api.regolo.ai/v1", + openai_api_key=model_credential.get('api_key'), + extra_body=optional_params + ) diff --git a/apps/setting/models_provider/impl/regolo_model_provider/model/tti.py b/apps/setting/models_provider/impl/regolo_model_provider/model/tti.py new file mode 100644 index 00000000000..a92527295ac --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/model/tti.py @@ -0,0 +1,58 @@ +from typing import Dict + +from openai import OpenAI + +from common.config.tokenizer_manage_config import TokenizerManage +from setting.models_provider.base_model_provider import MaxKBBaseModel +from setting.models_provider.impl.base_tti import BaseTextToImage + + +def custom_get_token_ids(text: str): + tokenizer = TokenizerManage.get_tokenizer() + return tokenizer.encode(text) + + +class RegoloTextToImage(MaxKBBaseModel, BaseTextToImage): + api_base: str + api_key: str + model: str + params: dict + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.api_key = kwargs.get('api_key') + self.api_base = "https://api.regolo.ai/v1" + self.model = kwargs.get('model') + self.params = kwargs.get('params') + + @staticmethod + def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): + optional_params = {'params': {'size': '1024x1024', 'quality': 'standard', 'n': 1}} + for key, value in model_kwargs.items(): + if key not in ['model_id', 'use_local', 'streaming']: + optional_params['params'][key] = value + return RegoloTextToImage( + model=model_name, + api_base="https://api.regolo.ai/v1", + api_key=model_credential.get('api_key'), + **optional_params, + ) + + def is_cache_model(self): + return False + + def check_auth(self): + chat = OpenAI(api_key=self.api_key, base_url=self.api_base) + response_list = chat.models.with_raw_response.list() + + # self.generate_image('生成一个小猫图片') + + def generate_image(self, prompt: str, negative_prompt: str = None): + chat = OpenAI(api_key=self.api_key, base_url=self.api_base) + res = chat.images.generate(model=self.model, prompt=prompt, **self.params) + file_urls = [] + for content in res.data: + url = content.url + file_urls.append(url) + + return file_urls diff --git a/apps/setting/models_provider/impl/regolo_model_provider/regolo_model_provider.py b/apps/setting/models_provider/impl/regolo_model_provider/regolo_model_provider.py new file mode 100644 index 00000000000..a5e7dc36550 --- /dev/null +++ b/apps/setting/models_provider/impl/regolo_model_provider/regolo_model_provider.py @@ -0,0 +1,89 @@ +# coding=utf-8 +""" + @project: maxkb + @Author:虎 + @file: openai_model_provider.py + @date:2024/3/28 16:26 + @desc: +""" +import os + +from common.util.file_util import get_file_content +from setting.models_provider.base_model_provider import IModelProvider, ModelProvideInfo, ModelInfo, \ + ModelTypeConst, ModelInfoManage +from setting.models_provider.impl.regolo_model_provider.credential.embedding import \ + RegoloEmbeddingCredential +from setting.models_provider.impl.regolo_model_provider.credential.llm import RegoloLLMModelCredential +from setting.models_provider.impl.regolo_model_provider.credential.tti import \ + RegoloTextToImageModelCredential +from setting.models_provider.impl.regolo_model_provider.model.embedding import RegoloEmbeddingModel +from setting.models_provider.impl.regolo_model_provider.model.llm import RegoloChatModel +from setting.models_provider.impl.regolo_model_provider.model.tti import RegoloTextToImage +from smartdoc.conf import PROJECT_DIR +from django.utils.translation import gettext as _ + +openai_llm_model_credential = RegoloLLMModelCredential() +openai_tti_model_credential = RegoloTextToImageModelCredential() +model_info_list = [ + ModelInfo('Phi-4', '', ModelTypeConst.LLM, + openai_llm_model_credential, RegoloChatModel + ), + ModelInfo('DeepSeek-R1-Distill-Qwen-32B', '', ModelTypeConst.LLM, + openai_llm_model_credential, + RegoloChatModel), + ModelInfo('maestrale-chat-v0.4-beta', '', + ModelTypeConst.LLM, openai_llm_model_credential, + RegoloChatModel), + ModelInfo('Llama-3.3-70B-Instruct', + '', + ModelTypeConst.LLM, openai_llm_model_credential, + RegoloChatModel), + ModelInfo('Llama-3.1-8B-Instruct', + '', + ModelTypeConst.LLM, openai_llm_model_credential, + RegoloChatModel), + ModelInfo('DeepSeek-Coder-6.7B-Instruct', '', + ModelTypeConst.LLM, openai_llm_model_credential, + RegoloChatModel) +] +open_ai_embedding_credential = RegoloEmbeddingCredential() +model_info_embedding_list = [ + ModelInfo('gte-Qwen2', '', + ModelTypeConst.EMBEDDING, open_ai_embedding_credential, + RegoloEmbeddingModel), +] + +model_info_tti_list = [ + ModelInfo('FLUX.1-dev', '', + ModelTypeConst.TTI, openai_tti_model_credential, + RegoloTextToImage), + ModelInfo('sdxl-turbo', '', + ModelTypeConst.TTI, openai_tti_model_credential, + RegoloTextToImage), +] +model_info_manage = ( + ModelInfoManage.builder() + .append_model_info_list(model_info_list) + .append_default_model_info( + ModelInfo('gpt-3.5-turbo', _('The latest gpt-3.5-turbo, updated with OpenAI adjustments'), ModelTypeConst.LLM, + openai_llm_model_credential, RegoloChatModel + )) + .append_model_info_list(model_info_embedding_list) + .append_default_model_info(model_info_embedding_list[0]) + .append_model_info_list(model_info_tti_list) + .append_default_model_info(model_info_tti_list[0]) + + .build() +) + + +class RegoloModelProvider(IModelProvider): + + def get_model_info_manage(self): + return model_info_manage + + def get_model_provide_info(self): + return ModelProvideInfo(provider='model_regolo_provider', name='Regolo', icon=get_file_content( + os.path.join(PROJECT_DIR, "apps", "setting", 'models_provider', 'impl', 'regolo_model_provider', + 'icon', + 'regolo_icon_svg'))) diff --git a/apps/setting/models_provider/impl/siliconCloud_model_provider/model/image.py b/apps/setting/models_provider/impl/siliconCloud_model_provider/model/image.py index bb840f8c6dc..2ec0689d4d2 100644 --- a/apps/setting/models_provider/impl/siliconCloud_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/siliconCloud_model_provider/model/image.py @@ -16,5 +16,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/siliconCloud_model_provider/model/llm.py b/apps/setting/models_provider/impl/siliconCloud_model_provider/model/llm.py index 9d79c6e0761..6fb0c7816fa 100644 --- a/apps/setting/models_provider/impl/siliconCloud_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/siliconCloud_model_provider/model/llm.py @@ -34,5 +34,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model=model_name, openai_api_base=model_credential.get('api_base'), openai_api_key=model_credential.get('api_key'), - **optional_params + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/tencent_cloud_model_provider/model/llm.py b/apps/setting/models_provider/impl/tencent_cloud_model_provider/model/llm.py index 7653cfc2f1f..cfcdf7aca21 100644 --- a/apps/setting/models_provider/impl/tencent_cloud_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/tencent_cloud_model_provider/model/llm.py @@ -33,21 +33,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model=model_name, openai_api_base=model_credential.get('api_base'), openai_api_key=model_credential.get('api_key'), - **optional_params, + extra_body=optional_params, custom_get_token_ids=custom_get_token_ids ) return azure_chat_open_ai - - def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: - try: - return super().get_num_tokens_from_messages(messages) - except Exception as e: - tokenizer = TokenizerManage.get_tokenizer() - return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages]) - - def get_num_tokens(self, text: str) -> int: - try: - return super().get_num_tokens(text) - except Exception as e: - tokenizer = TokenizerManage.get_tokenizer() - return len(tokenizer.encode(text)) diff --git a/apps/setting/models_provider/impl/tencent_model_provider/model/image.py b/apps/setting/models_provider/impl/tencent_model_provider/model/image.py index 1b66ab6d23f..6800cdd567c 100644 --- a/apps/setting/models_provider/impl/tencent_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/tencent_model_provider/model/image.py @@ -16,5 +16,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/vllm_model_provider/model/image.py b/apps/setting/models_provider/impl/vllm_model_provider/model/image.py index 4d5dda29dd7..c8cb0a84db9 100644 --- a/apps/setting/models_provider/impl/vllm_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/vllm_model_provider/model/image.py @@ -19,7 +19,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) def is_cache_model(self): diff --git a/apps/setting/models_provider/impl/vllm_model_provider/model/llm.py b/apps/setting/models_provider/impl/vllm_model_provider/model/llm.py index 7d2a63acd08..4662a616965 100644 --- a/apps/setting/models_provider/impl/vllm_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/vllm_model_provider/model/llm.py @@ -1,9 +1,10 @@ # coding=utf-8 -from typing import Dict, List +from typing import Dict, Optional, Sequence, Union, Any, Callable from urllib.parse import urlparse, ParseResult from langchain_core.messages import BaseMessage, get_buffer_string +from langchain_core.tools import BaseTool from common.config.tokenizer_manage_config import TokenizerManage from setting.models_provider.base_model_provider import MaxKBBaseModel @@ -31,13 +32,19 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model=model_name, openai_api_base=model_credential.get('api_base'), openai_api_key=model_credential.get('api_key'), - **optional_params, streaming=True, stream_usage=True, + extra_body=optional_params ) return vllm_chat_open_ai - def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: + def get_num_tokens_from_messages( + self, + messages: list[BaseMessage], + tools: Optional[ + Sequence[Union[dict[str, Any], type, Callable, BaseTool]] + ] = None, + ) -> int: if self.usage_metadata is None or self.usage_metadata == {}: tokenizer = TokenizerManage.get_tokenizer() return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages]) diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/image.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/image.py index 39446b4e19c..6e2517bd4ad 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/image.py @@ -16,5 +16,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/llm.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/llm.py index 181ad2971db..8f089f26988 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/llm.py @@ -17,5 +17,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model=model_name, openai_api_base=model_credential.get('api_base'), openai_api_key=model_credential.get('api_key'), - **optional_params + extra_body=optional_params ) diff --git a/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py b/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py index 06ec94aae34..d4d379db3d5 100644 --- a/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py @@ -27,7 +27,7 @@ class WenxinLLMModelParams(BaseForm): _step=0.01, precision=2) - max_tokens = forms.SliderField( + max_output_tokens = forms.SliderField( TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, diff --git a/apps/setting/models_provider/impl/xinference_model_provider/model/image.py b/apps/setting/models_provider/impl/xinference_model_provider/model/image.py index a195b86491b..66a766ba8c0 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/model/image.py @@ -19,7 +19,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: diff --git a/apps/setting/models_provider/impl/xinference_model_provider/model/llm.py b/apps/setting/models_provider/impl/xinference_model_provider/model/llm.py index d76979bd3a3..9c0316ad20a 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/model/llm.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/model/llm.py @@ -34,7 +34,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** model=model_name, openai_api_base=base_url, openai_api_key=model_credential.get('api_key'), - **optional_params + extra_body=optional_params ) def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: diff --git a/apps/setting/models_provider/impl/xinference_model_provider/model/reranker.py b/apps/setting/models_provider/impl/xinference_model_provider/model/reranker.py index 8820a198607..28c8d267839 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/model/reranker.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/model/reranker.py @@ -22,6 +22,9 @@ class XInferenceReranker(MaxKBBaseModel, BaseDocumentCompressor): """UID of the launched model""" api_key: Optional[str] + @staticmethod + def is_cache_model(): + return False @staticmethod def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): return XInferenceReranker(server_url=model_credential.get('server_url'), model_uid=model_name, diff --git a/apps/setting/models_provider/impl/zhipu_model_provider/model/image.py b/apps/setting/models_provider/impl/zhipu_model_provider/model/image.py index f13c7153803..6ac7830d8ff 100644 --- a/apps/setting/models_provider/impl/zhipu_model_provider/model/image.py +++ b/apps/setting/models_provider/impl/zhipu_model_provider/model/image.py @@ -16,5 +16,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], ** # stream_options={"include_usage": True}, streaming=True, stream_usage=True, - **optional_params, + extra_body=optional_params ) diff --git a/apps/smartdoc/conf.py b/apps/smartdoc/conf.py index de61cb8e339..21deebf55cf 100644 --- a/apps/smartdoc/conf.py +++ b/apps/smartdoc/conf.py @@ -7,6 +7,7 @@ 2. 程序需要, 用户不需要更改的写到settings中 3. 程序需要, 用户需要更改的写到本config中 """ +import datetime import errno import logging import os @@ -93,7 +94,8 @@ class Config(dict): 'SANDBOX': False, 'LOCAL_MODEL_HOST': '127.0.0.1', 'LOCAL_MODEL_PORT': '11636', - 'LOCAL_MODEL_PROTOCOL': "http" + 'LOCAL_MODEL_PROTOCOL': "http", + 'LOCAL_MODEL_HOST_WORKER': 1 } @@ -113,10 +115,14 @@ def get_db_setting(self) -> dict: "ENGINE": self.get('DB_ENGINE'), "POOL_OPTIONS": { "POOL_SIZE": 20, - "MAX_OVERFLOW": int(self.get('DB_MAX_OVERFLOW')) + "MAX_OVERFLOW": int(self.get('DB_MAX_OVERFLOW')), + 'RECYCLE': 30 * 60 } } + def get_session_timeout(self): + return datetime.timedelta(seconds=self.get('SESSION_TIMEOUT', 28800)) + def get_language_code(self): return self.get('LANGUAGE_CODE', 'zh-CN') diff --git a/apps/smartdoc/settings/base.py b/apps/smartdoc/settings/base.py index edf4586629d..de81420798a 100644 --- a/apps/smartdoc/settings/base.py +++ b/apps/smartdoc/settings/base.py @@ -126,6 +126,10 @@ "token_cache": { 'BACKEND': 'common.cache.file_cache.FileCache', 'LOCATION': os.path.join(PROJECT_DIR, 'data', 'cache', "token_cache") # 文件夹路径 + }, + 'captcha_cache': { + 'BACKEND': 'common.cache.file_cache.FileCache', + 'LOCATION': os.path.join(PROJECT_DIR, 'data', 'cache', "captcha_cache") # 文件夹路径 } } diff --git a/apps/users/serializers/user_serializers.py b/apps/users/serializers/user_serializers.py index 6093819a46a..96a4bb390a0 100644 --- a/apps/users/serializers/user_serializers.py +++ b/apps/users/serializers/user_serializers.py @@ -6,18 +6,22 @@ @date:2023/9/5 16:32 @desc: """ +import base64 import datetime import os import random import re import uuid +from captcha.image import ImageCaptcha from django.conf import settings from django.core import validators, signing, cache from django.core.mail import send_mail from django.core.mail.backends.smtp import EmailBackend from django.db import transaction from django.db.models import Q, QuerySet, Prefetch +from django.utils.translation import get_language +from django.utils.translation import gettext_lazy as _, to_locale from drf_yasg import openapi from rest_framework import serializers @@ -30,7 +34,7 @@ from common.mixins.api_mixin import ApiMixin from common.models.db_model_manage import DBModelManage from common.response.result import get_api_response -from common.util.common import valid_license +from common.util.common import valid_license, get_random_chars from common.util.field_message import ErrMessage from common.util.lock import lock from dataset.models import DataSet, Document, Paragraph, Problem, ProblemParagraphMapping @@ -39,9 +43,29 @@ from setting.models import Team, SystemSetting, SettingType, Model, TeamMember, TeamMemberPermission from smartdoc.conf import PROJECT_DIR from users.models.user import User, password_encrypt, get_user_dynamics_permission -from django.utils.translation import gettext_lazy as _, gettext, to_locale -from django.utils.translation import get_language + user_cache = cache.caches['user_cache'] +captcha_cache = cache.caches['captcha_cache'] + + +class CaptchaSerializer(ApiMixin, serializers.Serializer): + @staticmethod + def get_response_body_api(): + return get_api_response(openapi.Schema( + type=openapi.TYPE_STRING, + title="captcha", + default="xxxx", + description="captcha" + )) + + @staticmethod + def generate(): + chars = get_random_chars() + image = ImageCaptcha() + data = image.generate(chars) + captcha = base64.b64encode(data.getbuffer()) + captcha_cache.set(f"LOGIN:{chars.lower()}", chars, timeout=5 * 60) + return 'data:image/png;base64,' + captcha.decode() class SystemSerializer(ApiMixin, serializers.Serializer): @@ -71,6 +95,8 @@ class LoginSerializer(ApiMixin, serializers.Serializer): password = serializers.CharField(required=True, error_messages=ErrMessage.char(_("Password"))) + captcha = serializers.CharField(required=True, error_messages=ErrMessage.char(_("captcha"))) + def is_valid(self, *, raise_exception=False): """ 校验参数 @@ -78,6 +104,10 @@ def is_valid(self, *, raise_exception=False): :return: User information """ super().is_valid(raise_exception=True) + captcha = self.data.get('captcha') + captcha_value = captcha_cache.get(f"LOGIN:{captcha.lower()}") + if captcha_value is None: + raise AppApiException(1005, _("Captcha code error or expiration")) username = self.data.get("username") password = password_encrypt(self.data.get("password")) user = QuerySet(User).filter(Q(username=username, @@ -109,7 +139,8 @@ def get_request_body_api(self): required=['username', 'password'], properties={ 'username': openapi.Schema(type=openapi.TYPE_STRING, title=_("Username"), description=_("Username")), - 'password': openapi.Schema(type=openapi.TYPE_STRING, title=_("Password"), description=_("Password")) + 'password': openapi.Schema(type=openapi.TYPE_STRING, title=_("Password"), description=_("Password")), + 'captcha': openapi.Schema(type=openapi.TYPE_STRING, title=_("captcha"), description=_("captcha")) } ) diff --git a/apps/users/urls.py b/apps/users/urls.py index e5e2fe0dfb2..a9d1e134c90 100644 --- a/apps/users/urls.py +++ b/apps/users/urls.py @@ -6,6 +6,7 @@ urlpatterns = [ path('profile', views.Profile.as_view()), path('user', views.User.as_view(), name="profile"), + path('user/captcha', views.CaptchaView.as_view(), name='captcha'), path('user/language', views.SwitchUserLanguageView.as_view(), name='language'), path('user/list', views.User.Query.as_view()), path('user/login', views.Login.as_view(), name='login'), diff --git a/apps/users/views/user.py b/apps/users/views/user.py index 55d4b6b9ad9..3ca8b395f91 100644 --- a/apps/users/views/user.py +++ b/apps/users/views/user.py @@ -22,11 +22,11 @@ from common.log.log import log from common.response import result from common.util.common import encryption -from smartdoc.settings import JWT_AUTH +from smartdoc.const import CONFIG from users.serializers.user_serializers import RegisterSerializer, LoginSerializer, CheckCodeSerializer, \ RePasswordSerializer, \ SendEmailSerializer, UserProfile, UserSerializer, UserManageSerializer, UserInstanceSerializer, SystemSerializer, \ - SwitchLanguageSerializer + SwitchLanguageSerializer, CaptchaSerializer from users.views.common import get_user_operation_object, get_re_password_details user_cache = cache.caches['user_cache'] @@ -170,6 +170,18 @@ def _get_details(request): } +class CaptchaView(APIView): + + @action(methods=['GET'], detail=False) + @swagger_auto_schema(operation_summary=_("Obtain graphical captcha"), + operation_id=_("Obtain graphical captcha"), + responses=CaptchaSerializer().get_response_body_api(), + security=[], + tags=[_("User management")]) + def get(self, request: Request): + return result.success(CaptchaSerializer().generate()) + + class Login(APIView): @action(methods=['POST'], detail=False) @@ -187,7 +199,7 @@ def post(self, request: Request): # 校验请求参数 user = login_request.is_valid(raise_exception=True) token = login_request.get_user_token() - token_cache.set(token, user, timeout=JWT_AUTH['JWT_EXPIRATION_DELTA']) + token_cache.set(token, user, timeout=CONFIG.get_session_timeout()) return result.success(token) diff --git a/installer/Dockerfile b/installer/Dockerfile index d2c1eefb6fa..796e8535ef3 100644 --- a/installer/Dockerfile +++ b/installer/Dockerfile @@ -25,7 +25,7 @@ RUN python3 -m venv /opt/py3 && \ pip install poetry==1.8.5 --break-system-packages && \ poetry config virtualenvs.create false && \ . /opt/py3/bin/activate && \ - if [ "$(uname -m)" = "x86_64" ]; then sed -i 's/^torch.*/torch = {version = "^2.6.0+cpu", source = "pytorch"}/g' pyproject.toml; fi && \ + if [ "$(uname -m)" = "x86_64" ]; then sed -i 's/^torch.*/torch = {version = "2.6.0+cpu", source = "pytorch"}/g' pyproject.toml; fi && \ poetry install && \ export MAXKB_CONFIG_TYPE=ENV && python3 /opt/maxkb/app/apps/manage.py compilemessages @@ -70,7 +70,8 @@ RUN chmod 755 /opt/maxkb/app/installer/run-maxkb.sh && \ useradd --no-create-home --home /opt/maxkb/app/sandbox sandbox -g root && \ chown -R sandbox:root /opt/maxkb/app/sandbox && \ chmod g-x /usr/local/bin/* /usr/bin/* /bin/* /usr/sbin/* /sbin/* /usr/lib/postgresql/15/bin/* && \ - chmod g+x /usr/local/bin/python* + chmod g+x /usr/local/bin/python* && \ + find /etc/ -type f ! -path '/etc/resolv.conf' ! -path '/etc/hosts' | xargs chmod g-rx EXPOSE 8080 diff --git a/pyproject.toml b/pyproject.toml index 35d74a52e95..ea87b807d8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,65 +8,67 @@ package-mode = false [tool.poetry.dependencies] python = ">=3.11,<3.12" -django = "4.2.18" -djangorestframework = "^3.15.2" +django = "4.2.20" +djangorestframework = "3.16.0" drf-yasg = "1.21.7" django-filter = "23.2" -langchain-openai = "^0.3.0" -langchain-anthropic = "^0.3.0" -langchain-community = "^0.3.0" -langchain-deepseek = "^0.1.0" -langchain-google-genai = "^2.0.9" -langchain-mcp-adapters = "^0.0.5" -langchain-huggingface = "^0.1.2" -langchain-ollama = "^0.3.0" -langgraph = "^0.3.0" -mcp = "^1.4.1" +langchain = "0.3.23" +langchain-openai = "0.3.12" +langchain-anthropic = "0.3.12" +langchain-community = "0.3.21" +langchain-deepseek = "0.1.3" +langchain-google-genai = "2.1.2" +langchain-mcp-adapters = "0.0.11" +langchain-huggingface = "0.1.2" +langchain-ollama = "0.3.2" +langgraph = "0.3.27" +mcp = "1.8.0" psycopg2-binary = "2.9.10" -jieba = "^0.42.1" -diskcache = "^5.6.3" -pillow = "^10.2.0" -filetype = "^1.2.0" +jieba = "0.42.1" +diskcache = "5.6.3" +pillow = "10.4.0" +filetype = "1.2.0" torch = "2.6.0" -sentence-transformers = "^4.0.2" -openai = "^1.13.3" -tiktoken = "^0.7.0" -qianfan = "^0.3.6.1" -pycryptodome = "^3.19.0" -beautifulsoup4 = "^4.12.2" -html2text = "^2024.2.26" -django-ipware = "^6.0.4" -django-apscheduler = "^0.6.2" +sentence-transformers = "4.0.2" +openai = "1.72.0" +tiktoken = "0.7.0" +qianfan = "0.3.18" +pycryptodome = "3.22.0" +beautifulsoup4 = "4.13.3" +html2text = "2024.2.26" +django-ipware = "6.0.5" +django-apscheduler = "0.6.2" pymupdf = "1.24.9" pypdf = "4.3.1" rapidocr-onnxruntime = "1.3.24" -python-docx = "^1.1.0" -xlwt = "^1.3.0" -dashscope = "^1.17.0" -zhipuai = "^2.0.1" -httpx = "^0.27.0" -httpx-sse = "^0.4.0" -websockets = "^13.0" -openpyxl = "^3.1.2" -xlrd = "^2.0.1" -gunicorn = "^23.0.0" +python-docx = "1.1.2" +xlwt = "1.3.0" +dashscope = "1.23.1" +zhipuai = "2.1.5.20250410" +httpx = "0.27.2" +httpx-sse = "0.4.0" +websockets = "13.1" +openpyxl = "3.1.5" +xlrd = "2.0.1" +gunicorn = "23.0.0" python-daemon = "3.0.1" -boto3 = "^1.34.160" -tencentcloud-sdk-python = "^3.0.1209" -xinference-client = "^1.3.0" -psutil = "^6.0.0" -celery = { extras = ["sqlalchemy"], version = "^5.4.0" } -django-celery-beat = "^2.6.0" -celery-once = "^3.0.1" -anthropic = "^0.49.0" -pylint = "3.1.0" -pydub = "^0.25.1" -cffi = "^1.17.1" -pysilk = "^0.0.1" -django-db-connection-pool = "^1.2.5" -opencv-python-headless = "^4.11.0.86" -pymysql = "^1.1.1" -accelerate = "^1.6.0" +boto3 = "1.37.31" +tencentcloud-sdk-python = "3.0.1357" +xinference-client = "1.4.1" +psutil = "6.1.1" +celery = { extras = ["sqlalchemy"], version = "5.5.1" } +django-celery-beat = "2.7.0" +celery-once = "3.0.1" +anthropic = "0.49.0" +pylint = "3.3.6" +pydub = "0.25.1" +cffi = "1.17.1" +pysilk = "0.0.1" +django-db-connection-pool = "1.2.5" +opencv-python-headless = "4.11.0.86" +pymysql = "1.1.1" +accelerate = "1.6.0" +captcha = "0.7.1" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" diff --git a/ui/src/api/application.ts b/ui/src/api/application.ts index efd4a4985a8..bc903c957eb 100644 --- a/ui/src/api/application.ts +++ b/ui/src/api/application.ts @@ -227,7 +227,7 @@ const getApplicationHitTest: ( data: any, loading?: Ref ) => Promise>> = (application_id, data, loading) => { - return get(`${prefix}/${application_id}/hit_test`, data, loading) + return put(`${prefix}/${application_id}/hit_test`, data, undefined, loading) } /** diff --git a/ui/src/api/dataset.ts b/ui/src/api/dataset.ts index a5a663b03c7..83de865b3bc 100644 --- a/ui/src/api/dataset.ts +++ b/ui/src/api/dataset.ts @@ -186,7 +186,7 @@ const getDatasetHitTest: ( data: any, loading?: Ref ) => Promise>> = (dataset_id, data, loading) => { - return get(`${prefix}/${dataset_id}/hit_test`, data, loading) + return put(`${prefix}/${dataset_id}/hit_test`, data, undefined, loading) } /** diff --git a/ui/src/api/team.ts b/ui/src/api/team.ts index 82e8f986e46..462534b0eba 100644 --- a/ui/src/api/team.ts +++ b/ui/src/api/team.ts @@ -36,7 +36,7 @@ const getMemberPermissions: (member_id: String) => Promise> = (membe } /** - * 获取成员权限 + * 修改成员权限 * @param 参数 member_id * @param 参数 { "team_member_permission_list": [ diff --git a/ui/src/api/type/application.ts b/ui/src/api/type/application.ts index 077e230973e..c423f11105a 100644 --- a/ui/src/api/type/application.ts +++ b/ui/src/api/type/application.ts @@ -72,6 +72,7 @@ interface chatType { document_list: Array image_list: Array audio_list: Array + other_list: Array } } diff --git a/ui/src/api/type/user.ts b/ui/src/api/type/user.ts index a452673546a..ef22b55d982 100644 --- a/ui/src/api/type/user.ts +++ b/ui/src/api/type/user.ts @@ -37,6 +37,10 @@ interface LoginRequest { * 密码 */ password: string + /** + * 验证码 + */ + captcha: string } interface RegisterRequest { diff --git a/ui/src/api/user.ts b/ui/src/api/user.ts index eb12fd2ebf8..d14a65e2f07 100644 --- a/ui/src/api/user.ts +++ b/ui/src/api/user.ts @@ -27,6 +27,13 @@ const login: ( } return post('/user/login', request, undefined, loading) } +/** + * 获取图形验证码 + * @returns + */ +const getCaptcha: () => Promise> = () => { + return get('user/captcha') +} /** * 登出 * @param loading 接口加载器 @@ -226,5 +233,6 @@ export default { postLanguage, getDingOauth2Callback, getlarkCallback, - getQrSource + getQrSource, + getCaptcha } diff --git a/ui/src/components/ai-chat/ExecutionDetailDialog.vue b/ui/src/components/ai-chat/ExecutionDetailDialog.vue index 0f2296439ae..98ec1e6fa3e 100644 --- a/ui/src/components/ai-chat/ExecutionDetailDialog.vue +++ b/ui/src/components/ai-chat/ExecutionDetailDialog.vue @@ -125,6 +125,28 @@ +
+

+ {{ $t('common.fileUpload.document') }}: +

+ + + + +
diff --git a/ui/src/components/ai-chat/component/answer-content/index.vue b/ui/src/components/ai-chat/component/answer-content/index.vue index 7f09fa04c68..26cd8a0d06f 100644 --- a/ui/src/components/ai-chat/component/answer-content/index.vue +++ b/ui/src/components/ai-chat/component/answer-content/index.vue @@ -80,7 +80,7 @@ const props = defineProps<{ chatRecord: chatType application: any loading: boolean - sendMessage: (question: string, other_params_data?: any, chat?: chatType) => void + sendMessage: (question: string, other_params_data?: any, chat?: chatType) => Promise chatManagement: any type: 'log' | 'ai-chat' | 'debug-ai-chat' }>() @@ -98,9 +98,10 @@ const showUserAvatar = computed(() => { const chatMessage = (question: string, type: 'old' | 'new', other_params_data?: any) => { if (type === 'old') { add_answer_text_list(props.chatRecord.answer_text_list) - props.sendMessage(question, other_params_data, props.chatRecord) - props.chatManagement.open(props.chatRecord.id) - props.chatManagement.write(props.chatRecord.id) + props.sendMessage(question, other_params_data, props.chatRecord).then(() => { + props.chatManagement.open(props.chatRecord.id) + props.chatManagement.write(props.chatRecord.id) + }) } else { props.sendMessage(question, other_params_data) } diff --git a/ui/src/components/ai-chat/component/chat-input-operate/index.vue b/ui/src/components/ai-chat/component/chat-input-operate/index.vue index acf3085ed97..de3c86b8849 100644 --- a/ui/src/components/ai-chat/component/chat-input-operate/index.vue +++ b/ui/src/components/ai-chat/component/chat-input-operate/index.vue @@ -10,7 +10,8 @@ uploadDocumentList.length || uploadImageList.length || uploadAudioList.length || - uploadVideoList.length + uploadVideoList.length || + uploadOtherList.length " > @@ -30,22 +31,62 @@ class="file cursor" >
+
+ +
+ {{ item && item?.name }} +
+
- +
- -
- {{ item && item?.name }} +
+ + + + +
+
+ +
+ {{ item && item?.name }} +
+
+
+ + +
@@ -63,23 +104,25 @@ >
+
+ +
+ {{ item && item?.name }} +
+
- +
- -
- {{ item && item?.name }} -
@@ -87,7 +130,7 @@ - + diff --git a/ui/src/views/application/component/CreateApplicationDialog.vue b/ui/src/views/application/component/CreateApplicationDialog.vue index 438bfe211a9..7415753c1af 100644 --- a/ui/src/views/application/component/CreateApplicationDialog.vue +++ b/ui/src/views/application/component/CreateApplicationDialog.vue @@ -242,6 +242,7 @@ const submitHandle = async (formEl: FormInstance | undefined) => { } applicationApi.postApplication(applicationForm.value, loading).then((res) => { MsgSuccess(t('common.createSuccess')) + emit('refresh') if (isWorkFlow(applicationForm.value.type)) { router.push({ path: `/application/${res.data.id}/workflow` }) } else { diff --git a/ui/src/views/application/component/ParamSettingDialog.vue b/ui/src/views/application/component/ParamSettingDialog.vue index cdae5bf6e85..bd0cb5545c3 100644 --- a/ui/src/views/application/component/ParamSettingDialog.vue +++ b/ui/src/views/application/component/ParamSettingDialog.vue @@ -11,7 +11,7 @@ >
- + { if (!bool) { - form.value = { - dataset_setting: { - search_mode: 'embedding', - top_n: 3, - similarity: 0.6, - max_paragraph_char_number: 5000, - no_references_setting: { - status: 'ai_questioning', - value: '{question}' - } - }, - problem_optimization: false, - problem_optimization_prompt: '' - } + // form.value = { + // dataset_setting: { + // search_mode: 'embedding', + // top_n: 3, + // similarity: 0.6, + // max_paragraph_char_number: 5000, + // no_references_setting: { + // status: 'ai_questioning', + // value: '{question}' + // } + // }, + // problem_optimization: false, + // problem_optimization_prompt: '' + // } noReferencesform.value = { ai_questioning: defaultValue['ai_questioning'], designated_answer: defaultValue['designated_answer'] diff --git a/ui/src/views/authentication/component/OIDC.vue b/ui/src/views/authentication/component/OIDC.vue index 2666bc6479d..d71158b9a8e 100644 --- a/ui/src/views/authentication/component/OIDC.vue +++ b/ui/src/views/authentication/component/OIDC.vue @@ -61,6 +61,15 @@ show-password /> + + + ({ state: '', clientId: '', clientSecret: '', + fieldMapping: '{"username": "preferred_username", "email": "email"}', redirectUrl: '' }, is_active: true @@ -156,6 +166,13 @@ const rules = reactive>({ trigger: 'blur' } ], + 'config_data.fieldMapping': [ + { + required: true, + message: t('views.system.authentication.oauth2.filedMappingPlaceholder'), + trigger: 'blur' + } + ], 'config_data.redirectUrl': [ { required: true, @@ -187,6 +204,12 @@ function getDetail() { authApi.getAuthSetting(form.value.auth_type, loading).then((res: any) => { if (res.data && JSON.stringify(res.data) !== '{}') { form.value = res.data + if ( + form.value.config_data.fieldMapping === '' || + form.value.config_data.fieldMapping === undefined + ) { + form.value.config_data.fieldMapping = '{"username": "preferred_username", "email": "email"}' + } } }) } diff --git a/ui/src/views/chat/base/index.vue b/ui/src/views/chat/base/index.vue index 27be286f25a..7156f7d894a 100644 --- a/ui/src/views/chat/base/index.vue +++ b/ui/src/views/chat/base/index.vue @@ -42,7 +42,6 @@
-
diff --git a/ui/src/views/login/index.vue b/ui/src/views/login/index.vue index 714c439c6bb..cb1684ac47a 100644 --- a/ui/src/views/login/index.vue +++ b/ui/src/views/login/index.vue @@ -34,6 +34,21 @@ +
+ +
+ + + + +
+
+
({ username: '', - password: '' + password: '', + captcha: '' }) - +const identifyCode = ref('') +function makeCode() { + useApi.getCaptcha().then((res: any) => { + identifyCode.value = res.data + }) +} const rules = ref>({ username: [ { @@ -137,6 +159,13 @@ const rules = ref>({ message: t('views.user.userForm.form.password.requiredMessage'), trigger: 'blur' } + ], + captcha: [ + { + required: true, + message: t('views.user.userForm.form.captcha.placeholder'), + trigger: 'blur' + } ] }) const loginFormRef = ref() @@ -222,7 +251,8 @@ function changeMode(val: string) { showQrCodeTab.value = false loginForm.value = { username: '', - password: '' + password: '', + captcha: '' } redirectAuth(val) loginFormRef.value?.clearValidate() @@ -232,7 +262,12 @@ const login = () => { loginFormRef.value?.validate().then(() => { loading.value = true user - .login(loginMode.value, loginForm.value.username, loginForm.value.password) + .login( + loginMode.value, + loginForm.value.username, + loginForm.value.password, + loginForm.value.captcha + ) .then(() => { locale.value = localStorage.getItem('MaxKB-locale') || getBrowserLang() || 'en-US' router.push({ name: 'home' }) @@ -285,6 +320,7 @@ onBeforeMount(() => { declare const window: any onMounted(() => { + makeCode() const route = useRoute() const currentUrl = ref(route.fullPath) const params = new URLSearchParams(currentUrl.value.split('?')[1]) diff --git a/ui/src/views/login/reset-password/index.vue b/ui/src/views/login/reset-password/index.vue index 2c2ff02576e..876afde1470 100644 --- a/ui/src/views/login/reset-password/index.vue +++ b/ui/src/views/login/reset-password/index.vue @@ -1,6 +1,10 @@ - + diff --git a/ui/src/workflow/nodes/application-node/index.vue b/ui/src/workflow/nodes/application-node/index.vue index 77bff4ac0ca..4fc9fba5483 100644 --- a/ui/src/workflow/nodes/application-node/index.vue +++ b/ui/src/workflow/nodes/application-node/index.vue @@ -238,7 +238,8 @@ const update_field = () => { const new_user_input_field_list = cloneDeep( ok.data.work_flow.nodes[0].properties.user_input_field_list ) - const merge_api_input_field_list = new_api_input_field_list.map((item: any) => { + + const merge_api_input_field_list = (new_api_input_field_list || []).map((item: any) => { const find_field = old_api_input_field_list.find( (old_item: any) => old_item.variable == item.variable ) @@ -258,7 +259,7 @@ const update_field = () => { 'api_input_field_list', merge_api_input_field_list ) - const merge_user_input_field_list = new_user_input_field_list.map((item: any) => { + const merge_user_input_field_list = (new_user_input_field_list || []).map((item: any) => { const find_field = old_user_input_field_list.find( (old_item: any) => old_item.field == item.field ) @@ -294,6 +295,7 @@ const update_field = () => { } }) .catch((err) => { + console.log(err) set(props.nodeModel.properties, 'status', 500) }) } diff --git a/ui/src/workflow/nodes/base-node/component/FileUploadSettingDialog.vue b/ui/src/workflow/nodes/base-node/component/FileUploadSettingDialog.vue index ec4f9bebd21..0f55fa1ddaa 100644 --- a/ui/src/workflow/nodes/base-node/component/FileUploadSettingDialog.vue +++ b/ui/src/workflow/nodes/base-node/component/FileUploadSettingDialog.vue @@ -7,7 +7,7 @@ :destroy-on-close="true" :before-close="close" append-to-body - width="600" + width="800" >

- {{ $t('common.fileUpload.document') }}(TXT、MD、DOCX、HTML、CSV、XLSX、XLS、PDF) + {{ $t('common.fileUpload.document') }} + {{ + $t( + 'views.applicationWorkflow.nodes.baseNode.FileUploadSetting.fileUploadType.documentText' + ) + }} +

- {{ - $t( - 'views.applicationWorkflow.nodes.baseNode.FileUploadSetting.fileUploadType.documentText' - ) - }} +

{{ documentExtensions.join('、') }}

- {{ $t('common.fileUpload.image') }}(JPG、JPEG、PNG、GIF) + {{ $t('common.fileUpload.image') }} + {{ + $t( + 'views.applicationWorkflow.nodes.baseNode.FileUploadSetting.fileUploadType.imageText' + ) + }} +

- {{ - $t( - 'views.applicationWorkflow.nodes.baseNode.FileUploadSetting.fileUploadType.imageText' - ) - }} +

{{ imageExtensions.join('、') }}

@@ -106,18 +112,76 @@

- {{ $t('common.fileUpload.audio') }}(MP3、WAV、OGG、ACC、M4A) + {{ $t('common.fileUpload.audio') }} + {{ + $t( + 'views.applicationWorkflow.nodes.baseNode.FileUploadSetting.fileUploadType.audioText' + ) + }} +

- {{ - $t( - 'views.applicationWorkflow.nodes.baseNode.FileUploadSetting.fileUploadType.audioText' - ) - }} +

{{ audioExtensions.join('、') }}

+ +
+
+ +
+

+ {{ $t('common.fileUpload.other') }} + {{ + $t( + 'views.applicationWorkflow.nodes.baseNode.FileUploadSetting.fileUploadType.otherText' + ) + }} + +

+ + + {{ tag }} + + + + + {{ $t('common.fileUpload.addExtensions') }} + + +
+
+ +
+
Feature