From 10d8b853a8d0a79cdc77f684b7982aeb39c88e72 Mon Sep 17 00:00:00 2001 From: Jazee6 Date: Tue, 4 Jun 2024 10:54:19 +0800 Subject: [PATCH] fix: Gemini 500 --- Dockerfile | 2 ++ package.json | 9 +++-- patches/@google__generative-ai@0.12.0.patch | 26 ++++++++++++++ pnpm-lock.yaml | 38 ++++++++------------- server/api/auth/gemini.post.ts | 10 +----- 5 files changed, 50 insertions(+), 35 deletions(-) create mode 100644 patches/@google__generative-ai@0.12.0.patch diff --git a/Dockerfile b/Dockerfile index 5a84a1f..f4dfa9d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,8 @@ RUN npm i -g pnpm@9.1.4 COPY package.json ./ +COPY patches ./patches + RUN pnpm install COPY . . diff --git a/package.json b/package.json index 932a51c..2403557 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cloudflare-ai-web", - "version": "3.0.0", + "version": "3.0.1", "private": true, "type": "module", "scripts": { @@ -27,5 +27,10 @@ "highlight.js": "^11.9.0", "markdown-it": "^14.1.0" }, - "packageManager": "pnpm@9.1.4+sha512.9df9cf27c91715646c7d675d1c9c8e41f6fce88246f1318c1aa6a1ed1aeb3c4f032fcdf4ba63cc69c4fe6d634279176b5358727d8f2cc1e65b65f43ce2f8bfb0" + "packageManager": "pnpm@9.1.4+sha512.9df9cf27c91715646c7d675d1c9c8e41f6fce88246f1318c1aa6a1ed1aeb3c4f032fcdf4ba63cc69c4fe6d634279176b5358727d8f2cc1e65b65f43ce2f8bfb0", + "pnpm": { + "patchedDependencies": { + "@google/generative-ai@0.12.0": "patches/@google__generative-ai@0.12.0.patch" + } + } } diff --git a/patches/@google__generative-ai@0.12.0.patch b/patches/@google__generative-ai@0.12.0.patch new file mode 100644 index 0000000..6db2294 --- /dev/null +++ b/patches/@google__generative-ai@0.12.0.patch @@ -0,0 +1,26 @@ +diff --git a/dist/index.js b/dist/index.js +index 4290f816871d5e88e44e264fa47b2960cde864a7..64647480b2d600d8f163a15ef7ce49920b04bae5 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -241,7 +241,7 @@ class GoogleGenerativeAIRequestInputError extends GoogleGenerativeAIError { + * See the License for the specific language governing permissions and + * limitations under the License. + */ +-const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com"; ++const DEFAULT_BASE_URL = process.env.G_API_URL || "https://generativelanguage.googleapis.com"; + const DEFAULT_API_VERSION = "v1beta"; + /** + * We can't `require` package.json if this runs on web. We will use rollup to +diff --git a/dist/index.mjs b/dist/index.mjs +index aabb1c97ad6ec5af1e8e736da9d3dc23c478cb09..b02db137b30e5f77e7a5d68657b1fbc19581c8f2 100644 +--- a/dist/index.mjs ++++ b/dist/index.mjs +@@ -239,7 +239,7 @@ class GoogleGenerativeAIRequestInputError extends GoogleGenerativeAIError { + * See the License for the specific language governing permissions and + * limitations under the License. + */ +-const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com"; ++const DEFAULT_BASE_URL = process.env.G_API_URL || "https://generativelanguage.googleapis.com"; + const DEFAULT_API_VERSION = "v1beta"; + /** + * We can't `require` package.json if this runs on web. We will use rollup to diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 01d2176..6def608 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,13 +4,18 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +patchedDependencies: + '@google/generative-ai@0.12.0': + hash: 45ycpsbrvhx65zamv2tslnpetq + path: patches/@google__generative-ai@0.12.0.patch + importers: .: dependencies: '@google/generative-ai': specifier: ^0.12.0 - version: 0.12.0 + version: 0.12.0(patch_hash=45ycpsbrvhx65zamv2tslnpetq) '@nuxt/ui': specifier: ^2.16.0 version: 2.16.0(focus-trap@7.5.4)(nuxt@3.11.2(@parcel/watcher@2.4.1)(@types/node@20.11.30)(@unocss/reset@0.58.6)(encoding@0.1.13)(floating-vue@5.2.2(@nuxt/kit@3.11.2(rollup@4.18.0))(vue@3.4.27))(fuse.js@6.6.2)(ioredis@5.3.2)(rollup@4.18.0)(terser@5.29.2)(unocss@0.58.6(postcss@8.4.38)(rollup@4.18.0)(vite@5.2.12(@types/node@20.11.30)(terser@5.29.2)))(vite@5.2.12(@types/node@20.11.30)(terser@5.29.2)))(rollup@4.18.0)(vite@5.2.12(@types/node@20.11.30)(terser@5.29.2))(vue@3.4.27) @@ -450,9 +455,6 @@ packages: '@iconify/types@2.0.0': resolution: {integrity: sha512-+wluvCrRhXrhyOmRDJ3q8mux9JkKy5SJ/v8ol2tu4FVjyYvtEzkc/3pK15ET6RKg4b4w4BmTk1+gsCUhf21Ykg==} - '@iconify/utils@2.1.22': - resolution: {integrity: sha512-6UHVzTVXmvO8uS6xFF+L/QTSpTzA/JZxtgU+KYGFyDYMEObZ1bu/b5l+zNJjHy+0leWjHI+C0pXlzGvv3oXZMA==} - '@iconify/utils@2.1.24': resolution: {integrity: sha512-H8r2KpL5uKyrkb3z9/3HD/22JcxqW3BJyjEWZhX2T7DehnYVZthEap1cNsEl/UtCDC3TlpNmwiPX8wg3y8E4dg==} @@ -4627,7 +4629,7 @@ snapshots: '@floating-ui/utils@0.2.1': {} - '@google/generative-ai@0.12.0': {} + '@google/generative-ai@0.12.0(patch_hash=45ycpsbrvhx65zamv2tslnpetq)': {} '@headlessui/tailwindcss@0.2.0(tailwindcss@3.4.3)': dependencies: @@ -4648,18 +4650,6 @@ snapshots: '@iconify/types@2.0.0': {} - '@iconify/utils@2.1.22': - dependencies: - '@antfu/install-pkg': 0.1.1 - '@antfu/utils': 0.7.7 - '@iconify/types': 2.0.0 - debug: 4.3.4 - kolorist: 1.8.0 - local-pkg: 0.5.0 - mlly: 1.6.1 - transitivePeerDependencies: - - supports-color - '@iconify/utils@2.1.24': dependencies: '@antfu/install-pkg': 0.1.1 @@ -5646,7 +5636,7 @@ snapshots: colorette: 2.0.20 consola: 3.2.3 fast-glob: 3.3.2 - magic-string: 0.30.8 + magic-string: 0.30.10 pathe: 1.1.2 perfect-debounce: 1.0.0 transitivePeerDependencies: @@ -5677,7 +5667,7 @@ snapshots: '@unocss/rule-utils': 0.58.6 css-tree: 2.3.1 fast-glob: 3.3.2 - magic-string: 0.30.8 + magic-string: 0.30.10 postcss: 8.4.38 '@unocss/preset-attributify@0.58.6': @@ -5686,7 +5676,7 @@ snapshots: '@unocss/preset-icons@0.58.6': dependencies: - '@iconify/utils': 2.1.22 + '@iconify/utils': 2.1.24 '@unocss/core': 0.58.6 ofetch: 1.3.4 transitivePeerDependencies: @@ -5730,7 +5720,7 @@ snapshots: '@unocss/rule-utils@0.58.6': dependencies: '@unocss/core': 0.58.6 - magic-string: 0.30.8 + magic-string: 0.30.10 '@unocss/scope@0.58.6': {} @@ -5772,7 +5762,7 @@ snapshots: '@unocss/transformer-directives': 0.58.6 chokidar: 3.6.0 fast-glob: 3.3.2 - magic-string: 0.30.8 + magic-string: 0.30.10 vite: 5.2.12(@types/node@20.11.30)(terser@5.29.2) transitivePeerDependencies: - rollup @@ -8807,10 +8797,10 @@ snapshots: unconfig@0.3.11: dependencies: - '@antfu/utils': 0.7.7 + '@antfu/utils': 0.7.8 defu: 6.1.4 jiti: 1.21.0 - mlly: 1.6.1 + mlly: 1.7.0 uncrypto@0.1.3: {} diff --git a/server/api/auth/gemini.post.ts b/server/api/auth/gemini.post.ts index 7d8ab98..11ef318 100644 --- a/server/api/auth/gemini.post.ts +++ b/server/api/auth/gemini.post.ts @@ -1,4 +1,4 @@ -import {GoogleGenerativeAI, HarmBlockThreshold, HarmCategory} from '@google/generative-ai' +import {GoogleGenerativeAI} from '@google/generative-ai' import {headers} from '~/utils/helper'; import {OpenAIMessage} from "~/utils/types"; @@ -12,14 +12,6 @@ export default defineEventHandler(async (event) => { const m = genAI.getGenerativeModel({model}) let msg = messages.slice(1) - // let flag = ['user', 'assistant'] - // for (let i = 0; i < msg.length; i++) { - // if (msg[i].role !== flag[i % 2]) { - // flag = [] - // break - // } - // } - // if (!flag.length) return new Response('对话失效,请重新开始对话', {status: 400}) let res if (files.length) {