From 1af80ca83abcc97a2d3b90c500c0447cbc722d90 Mon Sep 17 00:00:00 2001 From: Yanyutin753 <132346501+Yanyutin753@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:04:23 +0800 Subject: [PATCH] fix logger info update files --- .github/workflows/sync.yml | 48 ++++++++++++++++++++++++++++++ src/api/controllers/chat.ts | 58 ++++++++++++++++++++----------------- 2 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/sync.yml diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml new file mode 100644 index 0000000..4bc0e92 --- /dev/null +++ b/.github/workflows/sync.yml @@ -0,0 +1,48 @@ +name: Upstream Sync + +permissions: + contents: write + issues: write + actions: write + +on: + schedule: + - cron: '0 * * * *' # every hour + workflow_dispatch: + +jobs: + sync_latest_from_upstream: + name: Sync latest commits from upstream repo + runs-on: ubuntu-latest + if: ${{ github.event.repository.fork }} + + steps: + - uses: actions/checkout@v4 + + - name: Clean issue notice + uses: actions-cool/issues-helper@v3 + with: + actions: 'close-issues' + labels: '🚨 Sync Fail' + + - name: Sync upstream changes + id: sync + uses: aormsby/Fork-Sync-With-Upstream-action@v3.4 + with: + upstream_sync_repo: LLM-Red-Team/glm-free-api + upstream_sync_branch: master + target_sync_branch: master + target_repo_token: ${{ secrets.GITHUB_TOKEN }} # automatically generated, no need to set + test_mode: false + + - name: Sync check + if: failure() + uses: actions-cool/issues-helper@v3 + with: + actions: 'create-issue' + title: '🚨 同步失败 | Sync Fail' + labels: '🚨 Sync Fail' + body: | + Due to a change in the workflow file of the LLM-Red-Team/glm-free-api upstream repository, GitHub has automatically suspended the scheduled automatic update. You need to manually sync your fork. Please refer to the detailed [Tutorial][tutorial-en-US] for instructions. + + 由于 LLM-Red-Team/glm-free-api 上游仓库的 workflow 文件变更,导致 GitHub 自动暂停了本次自动更新,你需要手动 Sync Fork 一次, \ No newline at end of file diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index eefd0d4..b0b0486 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -429,7 +429,7 @@ async function generateImages( console.error(err) ); - if(imageUrls.length == 0) + if (imageUrls.length == 0) throw new APIException(EX.API_IMAGE_GENERATION_FAILED); return imageUrls; @@ -452,29 +452,27 @@ async function generateImages( * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文 */ function extractRefFileUrls(messages: any[]) { - return messages.reduce((urls, message) => { - if (_.isArray(message.content)) { - message.content.forEach((v) => { - if (!_.isObject(v) || !["file", "image_url"].includes(v["type"])) - return; - // glm-free-api支持格式 - if ( - v["type"] == "file" && - _.isObject(v["file_url"]) && - _.isString(v["file_url"]["url"]) - ) - urls.push(v["file_url"]["url"]); - // 兼容gpt-4-vision-preview API格式 - else if ( - v["type"] == "image_url" && - _.isObject(v["image_url"]) && - _.isString(v["image_url"]["url"]) - ) - urls.push(v["image_url"]["url"]); - }); - } + const urls = []; + // 如果没有消息,则返回[] + if (!messages.length) { return urls; - }, []); + } + // 只获取最新的消息 + const lastMessage = messages[messages.length - 1]; + if (_.isArray(lastMessage.content)) { + lastMessage.content.forEach(v => { + if (!_.isObject(v) || !['file', 'image_url'].includes(v['type'])) + return; + // glm-free-api支持格式 + if (v['type'] == 'file' && _.isObject(v['file_url']) && _.isString(v['file_url']['url'])) + urls.push(v['file_url']['url']); + // 兼容gpt-4-vision-preview API格式 + else if (v['type'] == 'image_url' && _.isObject(v['image_url']) && _.isString(v['image_url']['url'])) + urls.push(v['image_url']['url']); + }); + } + logger.info("本次请求上传:" + urls.length + "个文件"); + return urls; } /** @@ -489,8 +487,16 @@ function extractRefFileUrls(messages: any[]) { * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文 */ function messagesPrepare(messages: any[], refs: any[]) { + // 只保留最新消息以及不包含"type": "image_url"或"type": "file"的消息 + let validMessages = messages.filter((message, index) => { + if (index === messages.length - 1) return true; + if (!Array.isArray(message.content)) return true; + // 不含"type": "image_url"或"type": "file"的消息保留 + return !message.content.some(v => (typeof v === 'object' && ['file', 'image_url'].includes(v['type']))); + }); + const content = - messages.reduce((content, message) => { + validMessages.reduce((content, message) => { if (_.isArray(message.content)) { return ( message.content.reduce((_content, v) => { @@ -1002,8 +1008,8 @@ async function receiveImages( throw new Error(`Stream response invalid: ${event.data}`); if (!convId && result.conversation_id) convId = result.conversation_id; - if(result.status == "intervene") - throw new APIException(EX.API_CONTENT_FILTERED); + if (result.status == "intervene") + throw new APIException(EX.API_CONTENT_FILTERED); if (result.status != "finish") { result.parts.forEach(part => { const { content } = part;