From db79e40896e041e77b0ffd3062fe792f54ea7095 Mon Sep 17 00:00:00 2001 From: Yanlong Wang Date: Fri, 14 Feb 2025 20:35:03 +0800 Subject: [PATCH] jina-ai: store promptContext for future use --- jina-ai/package-lock.json | 142 ++++++++++++++++++++++++++++++++++- jina-ai/package.json | 1 + jina-ai/src/lib/firestore.ts | 3 + jina-ai/src/patch-express.ts | 14 +++- 4 files changed, 155 insertions(+), 5 deletions(-) diff --git a/jina-ai/package-lock.json b/jina-ai/package-lock.json index 149e33e..a70a7b8 100644 --- a/jina-ai/package-lock.json +++ b/jina-ai/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@ai-sdk/google-vertex": "^2.1.12", "@google-cloud/firestore": "^7.11.0", + "@google-cloud/storage": "^7.15.1", "civkit": "^0.8.3-15926cb", "dayjs": "^1.11.13", "lodash": "^4.17.21", @@ -124,6 +125,72 @@ "node": ">=14.0.0" } }, + "node_modules/@google-cloud/paginator": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@google-cloud/paginator/-/paginator-5.0.2.tgz", + "integrity": "sha512-DJS3s0OVH4zFDB1PzjxAsHqJT6sKVbRwwML0ZBP9PbU7Yebtu/7SWMRzvO2J3nUi9pRNITCfu4LJeooM2w4pjg==", + "license": "Apache-2.0", + "dependencies": { + "arrify": "^2.0.0", + "extend": "^3.0.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@google-cloud/projectify": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@google-cloud/projectify/-/projectify-4.0.0.tgz", + "integrity": "sha512-MmaX6HeSvyPbWGwFq7mXdo0uQZLGBYCwziiLIGq5JVX+/bdI3SAq6bP98trV5eTWfLuvsMcIC1YJOF2vfteLFA==", + "license": "Apache-2.0", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@google-cloud/promisify": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@google-cloud/promisify/-/promisify-4.0.0.tgz", + "integrity": "sha512-Orxzlfb9c67A15cq2JQEyVc7wEsmFBmHjZWZYQMUyJ1qivXyMwdyNOs9odi79hze+2zqdTtu1E19IM/FtqZ10g==", + "license": "Apache-2.0", + "engines": { + "node": ">=14" + } + }, + "node_modules/@google-cloud/storage": { + "version": "7.15.1", + "resolved": "https://registry.npmjs.org/@google-cloud/storage/-/storage-7.15.1.tgz", + "integrity": "sha512-2bOD6d2D8b0FCV/By/VVRSvtagTllXFcRAv6F/9XUDY3r54VMQ0gM/B1emMdqnuVDX5mWJrrxlelHBZ9u6r6CA==", + "license": "Apache-2.0", + "dependencies": { + "@google-cloud/paginator": "^5.0.0", + "@google-cloud/projectify": "^4.0.0", + "@google-cloud/promisify": "^4.0.0", + "abort-controller": "^3.0.0", + "async-retry": "^1.3.3", + "duplexify": "^4.1.3", + "fast-xml-parser": "^4.4.1", + "gaxios": "^6.0.2", + "google-auth-library": "^9.6.3", + "html-entities": "^2.5.2", + "mime": "^3.0.0", + "p-limit": "^3.0.1", + "retry-request": "^7.0.0", + "teeny-request": "^9.0.0", + "uuid": "^8.0.0" + }, + "engines": { + "node": ">=14" + } + }, + "node_modules/@google-cloud/storage/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "license": "MIT", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/@grpc/grpc-js": { "version": "1.12.6", "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.12.6.tgz", @@ -618,6 +685,15 @@ "license": "MIT", "optional": true }, + "node_modules/arrify": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/arrify/-/arrify-2.0.1.tgz", + "integrity": "sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/asn1js": { "version": "3.0.5", "resolved": "https://registry.npmjs.org/asn1js/-/asn1js-3.0.5.tgz", @@ -640,6 +716,15 @@ "license": "MIT", "optional": true }, + "node_modules/async-retry": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/async-retry/-/async-retry-1.3.3.tgz", + "integrity": "sha512-wfr/jstw9xNi/0teMHrRW7dsz3Lt5ARhYNZ2ewpadnhaIp5mbALhOAP+EAdsC7t4Z6wqsDVv9+W6gm1Dk9mEyw==", + "license": "MIT", + "dependencies": { + "retry": "0.13.1" + } + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -1522,7 +1607,6 @@ } ], "license": "MIT", - "optional": true, "dependencies": { "strnum": "^1.0.5" }, @@ -1939,6 +2023,22 @@ "dev": true, "license": "MIT" }, + "node_modules/html-entities": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.5.2.tgz", + "integrity": "sha512-K//PSRMQk4FZ78Kyau+mZurHn3FH0Vwr+H36eE0rPbeYkRRi9YxceYPhuN60UwWorxyKHhqoAJl2OFKa4BVtaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/mdevils" + }, + { + "type": "patreon", + "url": "https://patreon.com/mdevils" + } + ], + "license": "MIT" + }, "node_modules/http-assert": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/http-assert/-/http-assert-1.5.0.tgz", @@ -2532,7 +2632,6 @@ "resolved": "https://registry.npmjs.org/mime/-/mime-3.0.0.tgz", "integrity": "sha512-jSCU7/VB1loIWBZe14aEYHU/+1UMEHoaO7qxCOVJOw9GgH72VAWppxNcjU+x9a2k3GSIBXNKxXQFqRvvZ7vr3A==", "license": "MIT", - "optional": true, "bin": { "mime": "cli.js" }, @@ -2843,6 +2942,21 @@ "integrity": "sha512-Fvw+Jemq5fjjyWz6CpKx6w9s7xxqo3+JCyM0WXWeCSOboZ8ABkyvP8ID4CZuChA/wxSx+XSJmdOm8rGVyJ1hdQ==", "optional": true }, + "node_modules/p-limit": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", + "license": "MIT", + "dependencies": { + "yocto-queue": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -3242,6 +3356,15 @@ "node": ">=0.10.0" } }, + "node_modules/retry": { + "version": "0.13.1", + "resolved": "https://registry.npmjs.org/retry/-/retry-0.13.1.tgz", + "integrity": "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==", + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, "node_modules/retry-request": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/retry-request/-/retry-request-7.0.2.tgz", @@ -3710,8 +3833,7 @@ "version": "1.0.5", "resolved": "https://registry.npmjs.org/strnum/-/strnum-1.0.5.tgz", "integrity": "sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==", - "license": "MIT", - "optional": true + "license": "MIT" }, "node_modules/stubs": { "version": "3.0.0", @@ -4085,6 +4207,18 @@ "node": ">= 4.0.0" } }, + "node_modules/yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/zod": { "version": "3.24.1", "resolved": "https://registry.npmjs.org/zod/-/zod-3.24.1.tgz", diff --git a/jina-ai/package.json b/jina-ai/package.json index e9720f0..9051bfb 100644 --- a/jina-ai/package.json +++ b/jina-ai/package.json @@ -26,6 +26,7 @@ "dependencies": { "@ai-sdk/google-vertex": "^2.1.12", "@google-cloud/firestore": "^7.11.0", + "@google-cloud/storage": "^7.15.1", "civkit": "^0.8.3-15926cb", "dayjs": "^1.11.13", "lodash": "^4.17.21", diff --git a/jina-ai/src/lib/firestore.ts b/jina-ai/src/lib/firestore.ts index ecaabaa..81024f2 100644 --- a/jina-ai/src/lib/firestore.ts +++ b/jina-ai/src/lib/firestore.ts @@ -4,6 +4,9 @@ import { Firestore, FieldValue, DocumentReference, Query, Timestamp, SetOptions, DocumentSnapshot, } from '@google-cloud/firestore'; +import { Storage } from '@google-cloud/storage'; + +export const firebaseDefaultBucket = new Storage().bucket(`${process.env.GCLOUD_PROJECT}.appspot.com`); // Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the \"new\" operator) function patchFireStoreArrogance(func: Function) { diff --git a/jina-ai/src/patch-express.ts b/jina-ai/src/patch-express.ts index e18e124..e3b017e 100644 --- a/jina-ai/src/patch-express.ts +++ b/jina-ai/src/patch-express.ts @@ -9,7 +9,7 @@ import rateLimitControl, { API_CALL_STATUS, RateLimitDesc } from "./rate-limit"; import asyncLocalContext from "./lib/async-context"; import globalLogger from "./lib/logger"; import { InsufficientBalanceError } from "./lib/errors"; -import { FirestoreRecord } from "./lib/firestore"; +import { firebaseDefaultBucket, FirestoreRecord } from "./lib/firestore"; import cors from "cors"; globalLogger.serviceReady(); @@ -139,6 +139,18 @@ export const jinaAiMiddleware = (req: Request, res: Response, next: NextFunction logger.warn(`Failed to save knowledge`, { err: marshalErrorLike(err) }); }); } + if (ctx.promptContext) { + firebaseDefaultBucket.file(`promptContext/${ctx.traceId}.json`).save( + JSON.stringify(ctx.promptContext), + { + metadata: { + contentType: 'application/json', + }, + } + ).catch((err: any) => { + logger.warn(`Failed to save promptContext`, { err: marshalErrorLike(err) }); + }); + } } catch (err: any) { if (!res.headersSent) {