@langchain/google-common 2.1.24 → 2.1.26-dev-1773698445534
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/auth.cjs +2 -3
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.js +1 -2
- package/dist/auth.js.map +1 -1
- package/dist/chat_models.cjs +9 -10
- package/dist/chat_models.cjs.map +1 -1
- package/dist/chat_models.js +2 -3
- package/dist/chat_models.js.map +1 -1
- package/dist/connection.cjs +5 -6
- package/dist/connection.cjs.map +1 -1
- package/dist/connection.js +1 -2
- package/dist/connection.js.map +1 -1
- package/dist/embeddings.cjs +3 -4
- package/dist/embeddings.cjs.map +1 -1
- package/dist/embeddings.js +1 -2
- package/dist/embeddings.js.map +1 -1
- package/dist/experimental/media.cjs +5 -6
- package/dist/experimental/media.cjs.map +1 -1
- package/dist/experimental/media.js +1 -2
- package/dist/experimental/media.js.map +1 -1
- package/dist/experimental/utils/media_core.cjs +2 -3
- package/dist/experimental/utils/media_core.cjs.map +1 -1
- package/dist/experimental/utils/media_core.js +1 -2
- package/dist/experimental/utils/media_core.js.map +1 -1
- package/dist/index.cjs +13 -14
- package/dist/index.js +1 -2
- package/dist/llms.cjs +8 -9
- package/dist/llms.cjs.map +1 -1
- package/dist/llms.js +2 -3
- package/dist/llms.js.map +1 -1
- package/dist/output_parsers.cjs +1 -2
- package/dist/output_parsers.cjs.map +1 -1
- package/dist/output_parsers.js +1 -2
- package/dist/output_parsers.js.map +1 -1
- package/dist/profiles.cjs +1 -2
- package/dist/profiles.cjs.map +1 -1
- package/dist/profiles.js +1 -1
- package/dist/types.cjs +2 -3
- package/dist/types.cjs.map +1 -1
- package/dist/types.js +1 -1
- package/dist/utils/anthropic.cjs +1 -2
- package/dist/utils/anthropic.cjs.map +1 -1
- package/dist/utils/anthropic.js +1 -2
- package/dist/utils/anthropic.js.map +1 -1
- package/dist/utils/common.cjs +5 -6
- package/dist/utils/common.cjs.map +1 -1
- package/dist/utils/common.js +1 -2
- package/dist/utils/common.js.map +1 -1
- package/dist/utils/failed_handler.cjs +1 -2
- package/dist/utils/failed_handler.cjs.map +1 -1
- package/dist/utils/failed_handler.js +1 -1
- package/dist/utils/gemini.cjs +43 -5
- package/dist/utils/gemini.cjs.map +1 -1
- package/dist/utils/gemini.d.cts.map +1 -1
- package/dist/utils/gemini.d.ts.map +1 -1
- package/dist/utils/gemini.js +40 -2
- package/dist/utils/gemini.js.map +1 -1
- package/dist/utils/index.cjs +8 -9
- package/dist/utils/index.js +1 -2
- package/dist/utils/safety.cjs +1 -2
- package/dist/utils/safety.cjs.map +1 -1
- package/dist/utils/safety.js +1 -1
- package/dist/utils/stream.cjs +1 -2
- package/dist/utils/stream.cjs.map +1 -1
- package/dist/utils/stream.js +1 -1
- package/dist/utils/stream.js.map +1 -1
- package/dist/utils/zod_to_gemini_parameters.cjs +1 -2
- package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -1
- package/dist/utils/zod_to_gemini_parameters.js +1 -2
- package/dist/utils/zod_to_gemini_parameters.js.map +1 -1
- package/package.json +9 -14
- package/experimental/media.cjs +0 -1
- package/experimental/media.d.cts +0 -1
- package/experimental/media.d.ts +0 -1
- package/experimental/media.js +0 -1
- package/experimental/utils/media_core.cjs +0 -1
- package/experimental/utils/media_core.d.cts +0 -1
- package/experimental/utils/media_core.d.ts +0 -1
- package/experimental/utils/media_core.js +0 -1
- package/types.cjs +0 -1
- package/types.d.cts +0 -1
- package/types.d.ts +0 -1
- package/types.js +0 -1
- package/utils.cjs +0 -1
- package/utils.d.cts +0 -1
- package/utils.d.ts +0 -1
- package/utils.js +0 -1
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value:
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let uuid = require("uuid");
|
|
3
3
|
let _langchain_core_stores = require("@langchain/core/stores");
|
|
4
4
|
let _langchain_core_load_serializable = require("@langchain/core/load/serializable");
|
|
5
|
-
|
|
6
5
|
//#region src/experimental/utils/media_core.ts
|
|
7
6
|
function bytesToString(dataArray) {
|
|
8
7
|
let ret = "";
|
|
@@ -381,7 +380,6 @@ var MediaManager = class {
|
|
|
381
380
|
return await this._isInvalid(aliasBlob) ? await this._resolveAndSave(uri) : aliasBlob;
|
|
382
381
|
}
|
|
383
382
|
};
|
|
384
|
-
|
|
385
383
|
//#endregion
|
|
386
384
|
exports.BackedBlobStore = BackedBlobStore;
|
|
387
385
|
exports.BlobStore = BlobStore;
|
|
@@ -390,4 +388,5 @@ exports.MediaBlob = MediaBlob;
|
|
|
390
388
|
exports.MediaManager = MediaManager;
|
|
391
389
|
exports.ReadThroughBlobStore = ReadThroughBlobStore;
|
|
392
390
|
exports.SimpleWebBlobStore = SimpleWebBlobStore;
|
|
391
|
+
|
|
393
392
|
//# sourceMappingURL=media_core.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media_core.cjs","names":["Serializable","BaseStore"],"sources":["../../../src/experimental/utils/media_core.ts"],"sourcesContent":["import { v1, v4 } from \"uuid\"; // FIXME - it is importing the wrong uuid, so v6 and v7 aren't implemented\nimport { BaseStore } from \"@langchain/core/stores\";\nimport { Serializable } from \"@langchain/core/load/serializable\";\n\nexport type MediaBlobData = {\n value: string; // In Base64 encoding\n type: string; // The mime type and possibly encoding\n};\n\nexport interface MediaBlobParameters {\n data?: MediaBlobData;\n\n metadata?: Record<string, unknown>;\n\n path?: string;\n}\n\nfunction bytesToString(dataArray: Uint8Array): string {\n // Need to handle the array in smaller chunks to deal with stack size limits\n let ret = \"\";\n const chunkSize = 102400;\n for (let i = 0; i < dataArray.length; i += chunkSize) {\n const chunk = dataArray.subarray(i, i + chunkSize);\n ret += String.fromCharCode(...chunk);\n }\n\n return ret;\n}\n\n/**\n * Represents a chunk of data that can be identified by the path where the\n * data is (or will be) located, along with optional metadata about the data.\n */\nexport class MediaBlob extends Serializable implements MediaBlobParameters {\n lc_serializable = true;\n\n lc_namespace = [\n \"langchain\",\n \"google_common\",\n \"experimental\",\n \"utils\",\n \"media_core\",\n ];\n\n data: MediaBlobData = {\n value: \"\",\n type: \"text/plain\",\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>;\n\n path?: string;\n\n constructor(params: MediaBlobParameters) {\n super(params);\n\n this.data = params.data ?? this.data;\n this.metadata = params.metadata;\n this.path = params.path;\n }\n\n get size(): number {\n return this.asBytes.length;\n }\n\n get dataType(): string {\n return this.data?.type ?? \"\";\n }\n\n get encoding(): string {\n const charsetEquals = this.dataType.indexOf(\"charset=\");\n return charsetEquals === -1\n ? \"utf-8\"\n : this.dataType.substring(charsetEquals + 8);\n }\n\n get mimetype(): string {\n const semicolon = this.dataType.indexOf(\";\");\n return semicolon === -1\n ? this.dataType\n : this.dataType.substring(0, semicolon);\n }\n\n get asBytes(): Uint8Array {\n if (!this.data) {\n return Uint8Array.from([]);\n }\n const binString = atob(this.data?.value);\n const ret = new Uint8Array(binString.length);\n for (let co = 0; co < binString.length; co += 1) {\n ret[co] = binString.charCodeAt(co);\n }\n return ret;\n }\n\n async asString(): Promise<string> {\n return bytesToString(this.asBytes);\n }\n\n async asBase64(): Promise<string> {\n return this.data?.value ?? \"\";\n }\n\n async asDataUrl(): Promise<string> {\n return `data:${this.mimetype};base64,${await this.asBase64()}`;\n }\n\n async asUri(): Promise<string> {\n return this.path ?? (await this.asDataUrl());\n }\n\n async encode(): Promise<{ encoded: string; encoding: string }> {\n const dataUrl = await this.asDataUrl();\n const comma = dataUrl.indexOf(\",\");\n const encoded = dataUrl.substring(comma + 1);\n const encoding: string = dataUrl.indexOf(\"base64\") > -1 ? \"base64\" : \"8bit\";\n return {\n encoded,\n encoding,\n };\n }\n\n static fromDataUrl(url: string): MediaBlob {\n if (!url.startsWith(\"data:\")) {\n throw new Error(\"Not a data: URL\");\n }\n const colon = url.indexOf(\":\");\n const semicolon = url.indexOf(\";\");\n const mimeType = url.substring(colon + 1, semicolon);\n\n const comma = url.indexOf(\",\");\n const base64Data = url.substring(comma + 1);\n\n const data: MediaBlobData = {\n type: mimeType,\n value: base64Data,\n };\n\n return new MediaBlob({\n data,\n path: url,\n });\n }\n\n static async fromBlob(\n blob: Blob,\n other?: Omit<MediaBlobParameters, \"data\">\n ): Promise<MediaBlob> {\n const valueBuffer = await blob.arrayBuffer();\n const valueArray = new Uint8Array(valueBuffer);\n const valueStr = bytesToString(valueArray);\n const value = btoa(valueStr);\n\n return new MediaBlob({\n ...other,\n data: {\n value,\n type: blob.type,\n },\n });\n }\n}\n\nexport type ActionIfInvalidAction =\n | \"ignore\"\n | \"prefixPath\"\n | \"prefixUuid1\"\n | \"prefixUuid4\"\n | \"prefixUuid6\"\n | \"prefixUuid7\"\n | \"removePath\";\n\nexport interface BlobStoreStoreOptions {\n /**\n * If the path is missing or invalid in the blob, how should we create\n * a new path?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an emtpy string: Reject the blob\n * - \"ignore\": Attempt to store it anyway (but this may fail)\n * - \"prefixPath\": Use the default prefix for the BlobStore and get the\n * unique portion from the URL. The original path is stored in the metadata\n * - \"prefixUuid\": Use the default prefix for the BlobStore and get the\n * unique portion from a generated UUID. The original path is stored\n * in the metadata\n */\n actionIfInvalid?: ActionIfInvalidAction;\n\n /**\n * The expected prefix for URIs that are stored.\n * This may be used to test if a MediaBlob is valid and used to create a new\n * path if \"prefixPath\" or \"prefixUuid\" is set for actionIfInvalid.\n */\n pathPrefix?: string;\n}\n\nexport type ActionIfBlobMissingAction = \"emptyBlob\";\n\nexport interface BlobStoreFetchOptions {\n /**\n * If the blob is not found when fetching, what should we do?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an empty string: return undefined\n * - \"emptyBlob\": return a new MediaBlob that has the path set, but nothing else.\n */\n actionIfBlobMissing?: ActionIfBlobMissingAction;\n}\n\nexport interface BlobStoreOptions {\n defaultStoreOptions?: BlobStoreStoreOptions;\n\n defaultFetchOptions?: BlobStoreFetchOptions;\n}\n\n/**\n * A specialized Store that is designed to handle MediaBlobs and use the\n * key that is included in the blob to determine exactly how it is stored.\n *\n * The full details of a MediaBlob may be changed when it is stored.\n * For example, it may get additional or different Metadata. This should be\n * what is returned when the store() method is called.\n *\n * Although BlobStore extends BaseStore, not all of the methods from\n * BaseStore may be implemented (or even possible). Those that are not\n * implemented should be documented and throw an Error if called.\n */\nexport abstract class BlobStore extends BaseStore<string, MediaBlob> {\n lc_namespace = [\"langchain\", \"google-common\"]; // FIXME - What should this be? And why?\n\n defaultStoreOptions: BlobStoreStoreOptions;\n\n defaultFetchOptions: BlobStoreFetchOptions;\n\n constructor(opts?: BlobStoreOptions) {\n super(opts);\n this.defaultStoreOptions = opts?.defaultStoreOptions ?? {};\n this.defaultFetchOptions = opts?.defaultFetchOptions ?? {};\n }\n\n protected async _realKey(key: string | MediaBlob): Promise<string> {\n return typeof key === \"string\" ? key : await key.asUri();\n }\n\n /**\n * Is the path supported by this BlobStore?\n *\n * Although this is async, this is expected to be a relatively fast operation\n * (ie - you shouldn't make network calls).\n *\n * @param path The path to check\n * @param opts Any options (if needed) that may be used to determine if it is valid\n * @return If the path is supported\n */\n hasValidPath(\n path: string | undefined,\n opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n const prefix = opts?.pathPrefix ?? \"\";\n const isPrefixed = typeof path !== \"undefined\" && path.startsWith(prefix);\n return Promise.resolve(isPrefixed);\n }\n\n protected _blobPathSuffix(blob: MediaBlob): string {\n // Get the path currently set and make sure we treat it as a string\n const blobPath = `${blob.path}`;\n\n // Advance past the first set of /\n let pathStart = blobPath.indexOf(\"/\") + 1;\n while (blobPath.charAt(pathStart) === \"/\") {\n pathStart += 1;\n }\n\n // We will use the rest as the path for a replacement\n return blobPath.substring(pathStart);\n }\n\n protected async _newBlob(\n oldBlob: MediaBlob,\n newPath?: string\n ): Promise<MediaBlob> {\n const oldPath = oldBlob.path;\n const metadata = oldBlob?.metadata ?? {};\n metadata.langchainOldPath = oldPath;\n const newBlob = new MediaBlob({\n ...oldBlob,\n metadata,\n });\n if (newPath) {\n newBlob.path = newPath;\n } else if (newBlob.path) {\n delete newBlob.path;\n }\n return newBlob;\n }\n\n protected async _validBlobPrefixPath(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._blobPathSuffix(blob);\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected _validBlobPrefixUuidFunction(\n name: ActionIfInvalidAction | string\n ): string {\n switch (name) {\n case \"prefixUuid1\":\n return v1();\n case \"prefixUuid4\":\n return v4();\n // case \"prefixUuid6\": return v6();\n // case \"prefixUuid7\": return v7();\n default:\n throw new Error(`Unknown uuid function: ${name}`);\n }\n }\n\n protected async _validBlobPrefixUuid(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._validBlobPrefixUuidFunction(\n opts?.actionIfInvalid ?? \"prefixUuid4\"\n );\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected async _validBlobRemovePath(\n blob: MediaBlob,\n _opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n return this._newBlob(blob, undefined);\n }\n\n /**\n * Based on the blob and options, return a blob that has a valid path\n * that can be saved.\n * @param blob\n * @param opts\n */\n protected async _validStoreBlob(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob | undefined> {\n if (await this.hasValidPath(blob.path, opts)) {\n return blob;\n }\n switch (opts?.actionIfInvalid) {\n case \"ignore\":\n return blob;\n case \"prefixPath\":\n return this._validBlobPrefixPath(blob, opts);\n case \"prefixUuid1\":\n case \"prefixUuid4\":\n case \"prefixUuid6\":\n case \"prefixUuid7\":\n return this._validBlobPrefixUuid(blob, opts);\n case \"removePath\":\n return this._validBlobRemovePath(blob, opts);\n default:\n return undefined;\n }\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreStoreOptions = {\n ...this.defaultStoreOptions,\n ...opts,\n };\n const validBlob = await this._validStoreBlob(blob, allOpts);\n if (typeof validBlob !== \"undefined\") {\n const validKey = await validBlob.asUri();\n await this.mset([[validKey, validBlob]]);\n const savedKey = await validBlob.asUri();\n return await this.fetch(savedKey);\n }\n return undefined;\n }\n\n protected async _missingFetchBlobEmpty(\n path: string,\n _opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob> {\n return new MediaBlob({ path });\n }\n\n protected async _missingFetchBlob(\n path: string,\n opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob | undefined> {\n switch (opts?.actionIfBlobMissing) {\n case \"emptyBlob\":\n return this._missingFetchBlobEmpty(path, opts);\n default:\n return undefined;\n }\n }\n\n async fetch(\n key: string | MediaBlob,\n opts: BlobStoreFetchOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreFetchOptions = {\n ...this.defaultFetchOptions,\n ...opts,\n };\n const realKey = await this._realKey(key);\n const ret = await this.mget([realKey]);\n return ret?.[0] ?? (await this._missingFetchBlob(realKey, allOpts));\n }\n}\n\nexport interface BackedBlobStoreOptions extends BlobStoreOptions {\n backingStore: BaseStore<string, MediaBlob>;\n}\n\nexport class BackedBlobStore extends BlobStore {\n backingStore: BaseStore<string, MediaBlob>;\n\n constructor(opts: BackedBlobStoreOptions) {\n super(opts);\n this.backingStore = opts.backingStore;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.backingStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.backingStore.mget(keys);\n }\n\n mset(keyValuePairs: [string, MediaBlob][]): Promise<void> {\n return this.backingStore.mset(keyValuePairs);\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.backingStore.yieldKeys(prefix);\n }\n}\n\nexport interface ReadThroughBlobStoreOptions extends BlobStoreOptions {\n baseStore: BlobStore;\n backingStore: BlobStore;\n}\n\nexport class ReadThroughBlobStore extends BlobStore {\n baseStore: BlobStore;\n\n backingStore: BlobStore;\n\n constructor(opts: ReadThroughBlobStoreOptions) {\n super(opts);\n this.baseStore = opts.baseStore;\n this.backingStore = opts.backingStore;\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const originalUri = await blob.asUri();\n const newBlob = await this.backingStore.store(blob, opts);\n if (newBlob) {\n await this.baseStore.mset([[originalUri, newBlob]]);\n }\n return newBlob;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.baseStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.baseStore.mget(keys);\n }\n\n mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n throw new Error(\"Do not call ReadThroughBlobStore.mset directly\");\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.baseStore.yieldKeys(prefix);\n }\n}\n\nexport class SimpleWebBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for SimpleWebBlobStore\");\n }\n\n async hasValidPath(\n path: string | undefined,\n _opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n return (\n (await super.hasValidPath(path, { pathPrefix: \"https://\" })) ||\n (await super.hasValidPath(path, { pathPrefix: \"http://\" }))\n );\n }\n\n async _fetch(url: string): Promise<MediaBlob | undefined> {\n const ret = new MediaBlob({\n path: url,\n });\n const metadata: Record<string, unknown> = {};\n const fetchOptions = {\n method: \"GET\",\n };\n const res = await fetch(url, fetchOptions);\n metadata.status = res.status;\n\n const headers: Record<string, string> = {};\n for (const [key, value] of res.headers.entries()) {\n headers[key] = value;\n }\n metadata.headers = headers;\n\n metadata.ok = res.ok;\n if (res.ok) {\n const resMediaBlob = await MediaBlob.fromBlob(await res.blob());\n ret.data = resMediaBlob.data;\n }\n\n ret.metadata = metadata;\n return ret;\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return await Promise.all(blobMap);\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\n/**\n * A blob \"store\" that works with data: URLs that will turn the URL into\n * a blob.\n */\nexport class DataBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for DataBlobStore\");\n }\n\n hasValidPath(path: string, _opts?: BlobStoreStoreOptions): Promise<boolean> {\n return super.hasValidPath(path, { pathPrefix: \"data:\" });\n }\n\n _fetch(url: string): MediaBlob {\n return MediaBlob.fromDataUrl(url);\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return blobMap;\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\nexport interface MediaManagerConfiguration {\n /**\n * A store that, given a common URI, returns the corresponding MediaBlob.\n * The returned MediaBlob may have a different URI.\n * In many cases, this will be a ReadThroughStore or something similar\n * that has a cached version of the MediaBlob, but also a way to get\n * a new (or refreshed) version.\n */\n store: BlobStore;\n\n /**\n * BlobStores that can resolve a URL into the MediaBlob to save\n * in the canonical store. This list is evaluated in order.\n * If not provided, a default list (which involves a DataBlobStore\n * and a SimpleWebBlobStore) will be used.\n */\n resolvers?: BlobStore[];\n}\n\n/**\n * Responsible for converting a URI (typically a web URL) into a MediaBlob.\n * Allows for aliasing / caching of the requested URI and what it resolves to.\n * This MediaBlob is expected to be usable to provide to an LLM, either\n * through the Base64 of the media or through a canonical URI that the LLM\n * supports.\n */\nexport class MediaManager {\n store: BlobStore;\n\n resolvers: BlobStore[] | undefined;\n\n constructor(config: MediaManagerConfiguration) {\n this.store = config.store;\n this.resolvers = config.resolvers;\n }\n\n defaultResolvers(): BlobStore[] {\n return [new DataBlobStore({}), new SimpleWebBlobStore({})];\n }\n\n async _isInvalid(blob: MediaBlob | undefined): Promise<boolean> {\n return typeof blob === \"undefined\";\n }\n\n /**\n * Given the public URI, load what is at this URI and save it\n * in the store.\n * @param uri The URI to resolve using the resolver\n * @return A canonical MediaBlob for this URI\n */\n async _resolveAndSave(uri: string): Promise<MediaBlob | undefined> {\n let resolvedBlob: MediaBlob | undefined;\n\n const resolvers = this.resolvers || this.defaultResolvers();\n for (let co = 0; co < resolvers.length; co += 1) {\n const resolver = resolvers[co];\n if (await resolver.hasValidPath(uri)) {\n resolvedBlob = await resolver.fetch(uri);\n }\n }\n\n if (resolvedBlob) {\n return await this.store.store(resolvedBlob);\n } else {\n return new MediaBlob({});\n }\n }\n\n async getMediaBlob(uri: string): Promise<MediaBlob | undefined> {\n const aliasBlob = await this.store.fetch(uri);\n const ret = (await this._isInvalid(aliasBlob))\n ? await this._resolveAndSave(uri)\n : (aliasBlob as MediaBlob);\n return ret;\n }\n}\n"],"mappings":";;;;;;AAiBA,SAAS,cAAc,WAA+B;CAEpD,IAAI,MAAM;CACV,MAAM,YAAY;AAClB,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK,WAAW;EACpD,MAAM,QAAQ,UAAU,SAAS,GAAG,IAAI,UAAU;AAClD,SAAO,OAAO,aAAa,GAAG,MAAM;;AAGtC,QAAO;;;;;;AAOT,IAAa,YAAb,MAAa,kBAAkBA,+CAA4C;CACzE,kBAAkB;CAElB,eAAe;EACb;EACA;EACA;EACA;EACA;EACD;CAED,OAAsB;EACpB,OAAO;EAC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K,WAAW,UAAU,GACzC,MAAM,KAAK,gBAAgB,IAAI,GAC9B"}
|
|
1
|
+
{"version":3,"file":"media_core.cjs","names":["Serializable","BaseStore"],"sources":["../../../src/experimental/utils/media_core.ts"],"sourcesContent":["import { v1, v4 } from \"uuid\"; // FIXME - it is importing the wrong uuid, so v6 and v7 aren't implemented\nimport { BaseStore } from \"@langchain/core/stores\";\nimport { Serializable } from \"@langchain/core/load/serializable\";\n\nexport type MediaBlobData = {\n value: string; // In Base64 encoding\n type: string; // The mime type and possibly encoding\n};\n\nexport interface MediaBlobParameters {\n data?: MediaBlobData;\n\n metadata?: Record<string, unknown>;\n\n path?: string;\n}\n\nfunction bytesToString(dataArray: Uint8Array): string {\n // Need to handle the array in smaller chunks to deal with stack size limits\n let ret = \"\";\n const chunkSize = 102400;\n for (let i = 0; i < dataArray.length; i += chunkSize) {\n const chunk = dataArray.subarray(i, i + chunkSize);\n ret += String.fromCharCode(...chunk);\n }\n\n return ret;\n}\n\n/**\n * Represents a chunk of data that can be identified by the path where the\n * data is (or will be) located, along with optional metadata about the data.\n */\nexport class MediaBlob extends Serializable implements MediaBlobParameters {\n lc_serializable = true;\n\n lc_namespace = [\n \"langchain\",\n \"google_common\",\n \"experimental\",\n \"utils\",\n \"media_core\",\n ];\n\n data: MediaBlobData = {\n value: \"\",\n type: \"text/plain\",\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>;\n\n path?: string;\n\n constructor(params: MediaBlobParameters) {\n super(params);\n\n this.data = params.data ?? this.data;\n this.metadata = params.metadata;\n this.path = params.path;\n }\n\n get size(): number {\n return this.asBytes.length;\n }\n\n get dataType(): string {\n return this.data?.type ?? \"\";\n }\n\n get encoding(): string {\n const charsetEquals = this.dataType.indexOf(\"charset=\");\n return charsetEquals === -1\n ? \"utf-8\"\n : this.dataType.substring(charsetEquals + 8);\n }\n\n get mimetype(): string {\n const semicolon = this.dataType.indexOf(\";\");\n return semicolon === -1\n ? this.dataType\n : this.dataType.substring(0, semicolon);\n }\n\n get asBytes(): Uint8Array {\n if (!this.data) {\n return Uint8Array.from([]);\n }\n const binString = atob(this.data?.value);\n const ret = new Uint8Array(binString.length);\n for (let co = 0; co < binString.length; co += 1) {\n ret[co] = binString.charCodeAt(co);\n }\n return ret;\n }\n\n async asString(): Promise<string> {\n return bytesToString(this.asBytes);\n }\n\n async asBase64(): Promise<string> {\n return this.data?.value ?? \"\";\n }\n\n async asDataUrl(): Promise<string> {\n return `data:${this.mimetype};base64,${await this.asBase64()}`;\n }\n\n async asUri(): Promise<string> {\n return this.path ?? (await this.asDataUrl());\n }\n\n async encode(): Promise<{ encoded: string; encoding: string }> {\n const dataUrl = await this.asDataUrl();\n const comma = dataUrl.indexOf(\",\");\n const encoded = dataUrl.substring(comma + 1);\n const encoding: string = dataUrl.indexOf(\"base64\") > -1 ? \"base64\" : \"8bit\";\n return {\n encoded,\n encoding,\n };\n }\n\n static fromDataUrl(url: string): MediaBlob {\n if (!url.startsWith(\"data:\")) {\n throw new Error(\"Not a data: URL\");\n }\n const colon = url.indexOf(\":\");\n const semicolon = url.indexOf(\";\");\n const mimeType = url.substring(colon + 1, semicolon);\n\n const comma = url.indexOf(\",\");\n const base64Data = url.substring(comma + 1);\n\n const data: MediaBlobData = {\n type: mimeType,\n value: base64Data,\n };\n\n return new MediaBlob({\n data,\n path: url,\n });\n }\n\n static async fromBlob(\n blob: Blob,\n other?: Omit<MediaBlobParameters, \"data\">\n ): Promise<MediaBlob> {\n const valueBuffer = await blob.arrayBuffer();\n const valueArray = new Uint8Array(valueBuffer);\n const valueStr = bytesToString(valueArray);\n const value = btoa(valueStr);\n\n return new MediaBlob({\n ...other,\n data: {\n value,\n type: blob.type,\n },\n });\n }\n}\n\nexport type ActionIfInvalidAction =\n | \"ignore\"\n | \"prefixPath\"\n | \"prefixUuid1\"\n | \"prefixUuid4\"\n | \"prefixUuid6\"\n | \"prefixUuid7\"\n | \"removePath\";\n\nexport interface BlobStoreStoreOptions {\n /**\n * If the path is missing or invalid in the blob, how should we create\n * a new path?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an emtpy string: Reject the blob\n * - \"ignore\": Attempt to store it anyway (but this may fail)\n * - \"prefixPath\": Use the default prefix for the BlobStore and get the\n * unique portion from the URL. The original path is stored in the metadata\n * - \"prefixUuid\": Use the default prefix for the BlobStore and get the\n * unique portion from a generated UUID. The original path is stored\n * in the metadata\n */\n actionIfInvalid?: ActionIfInvalidAction;\n\n /**\n * The expected prefix for URIs that are stored.\n * This may be used to test if a MediaBlob is valid and used to create a new\n * path if \"prefixPath\" or \"prefixUuid\" is set for actionIfInvalid.\n */\n pathPrefix?: string;\n}\n\nexport type ActionIfBlobMissingAction = \"emptyBlob\";\n\nexport interface BlobStoreFetchOptions {\n /**\n * If the blob is not found when fetching, what should we do?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an empty string: return undefined\n * - \"emptyBlob\": return a new MediaBlob that has the path set, but nothing else.\n */\n actionIfBlobMissing?: ActionIfBlobMissingAction;\n}\n\nexport interface BlobStoreOptions {\n defaultStoreOptions?: BlobStoreStoreOptions;\n\n defaultFetchOptions?: BlobStoreFetchOptions;\n}\n\n/**\n * A specialized Store that is designed to handle MediaBlobs and use the\n * key that is included in the blob to determine exactly how it is stored.\n *\n * The full details of a MediaBlob may be changed when it is stored.\n * For example, it may get additional or different Metadata. This should be\n * what is returned when the store() method is called.\n *\n * Although BlobStore extends BaseStore, not all of the methods from\n * BaseStore may be implemented (or even possible). Those that are not\n * implemented should be documented and throw an Error if called.\n */\nexport abstract class BlobStore extends BaseStore<string, MediaBlob> {\n lc_namespace = [\"langchain\", \"google-common\"]; // FIXME - What should this be? And why?\n\n defaultStoreOptions: BlobStoreStoreOptions;\n\n defaultFetchOptions: BlobStoreFetchOptions;\n\n constructor(opts?: BlobStoreOptions) {\n super(opts);\n this.defaultStoreOptions = opts?.defaultStoreOptions ?? {};\n this.defaultFetchOptions = opts?.defaultFetchOptions ?? {};\n }\n\n protected async _realKey(key: string | MediaBlob): Promise<string> {\n return typeof key === \"string\" ? key : await key.asUri();\n }\n\n /**\n * Is the path supported by this BlobStore?\n *\n * Although this is async, this is expected to be a relatively fast operation\n * (ie - you shouldn't make network calls).\n *\n * @param path The path to check\n * @param opts Any options (if needed) that may be used to determine if it is valid\n * @return If the path is supported\n */\n hasValidPath(\n path: string | undefined,\n opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n const prefix = opts?.pathPrefix ?? \"\";\n const isPrefixed = typeof path !== \"undefined\" && path.startsWith(prefix);\n return Promise.resolve(isPrefixed);\n }\n\n protected _blobPathSuffix(blob: MediaBlob): string {\n // Get the path currently set and make sure we treat it as a string\n const blobPath = `${blob.path}`;\n\n // Advance past the first set of /\n let pathStart = blobPath.indexOf(\"/\") + 1;\n while (blobPath.charAt(pathStart) === \"/\") {\n pathStart += 1;\n }\n\n // We will use the rest as the path for a replacement\n return blobPath.substring(pathStart);\n }\n\n protected async _newBlob(\n oldBlob: MediaBlob,\n newPath?: string\n ): Promise<MediaBlob> {\n const oldPath = oldBlob.path;\n const metadata = oldBlob?.metadata ?? {};\n metadata.langchainOldPath = oldPath;\n const newBlob = new MediaBlob({\n ...oldBlob,\n metadata,\n });\n if (newPath) {\n newBlob.path = newPath;\n } else if (newBlob.path) {\n delete newBlob.path;\n }\n return newBlob;\n }\n\n protected async _validBlobPrefixPath(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._blobPathSuffix(blob);\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected _validBlobPrefixUuidFunction(\n name: ActionIfInvalidAction | string\n ): string {\n switch (name) {\n case \"prefixUuid1\":\n return v1();\n case \"prefixUuid4\":\n return v4();\n // case \"prefixUuid6\": return v6();\n // case \"prefixUuid7\": return v7();\n default:\n throw new Error(`Unknown uuid function: ${name}`);\n }\n }\n\n protected async _validBlobPrefixUuid(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._validBlobPrefixUuidFunction(\n opts?.actionIfInvalid ?? \"prefixUuid4\"\n );\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected async _validBlobRemovePath(\n blob: MediaBlob,\n _opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n return this._newBlob(blob, undefined);\n }\n\n /**\n * Based on the blob and options, return a blob that has a valid path\n * that can be saved.\n * @param blob\n * @param opts\n */\n protected async _validStoreBlob(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob | undefined> {\n if (await this.hasValidPath(blob.path, opts)) {\n return blob;\n }\n switch (opts?.actionIfInvalid) {\n case \"ignore\":\n return blob;\n case \"prefixPath\":\n return this._validBlobPrefixPath(blob, opts);\n case \"prefixUuid1\":\n case \"prefixUuid4\":\n case \"prefixUuid6\":\n case \"prefixUuid7\":\n return this._validBlobPrefixUuid(blob, opts);\n case \"removePath\":\n return this._validBlobRemovePath(blob, opts);\n default:\n return undefined;\n }\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreStoreOptions = {\n ...this.defaultStoreOptions,\n ...opts,\n };\n const validBlob = await this._validStoreBlob(blob, allOpts);\n if (typeof validBlob !== \"undefined\") {\n const validKey = await validBlob.asUri();\n await this.mset([[validKey, validBlob]]);\n const savedKey = await validBlob.asUri();\n return await this.fetch(savedKey);\n }\n return undefined;\n }\n\n protected async _missingFetchBlobEmpty(\n path: string,\n _opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob> {\n return new MediaBlob({ path });\n }\n\n protected async _missingFetchBlob(\n path: string,\n opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob | undefined> {\n switch (opts?.actionIfBlobMissing) {\n case \"emptyBlob\":\n return this._missingFetchBlobEmpty(path, opts);\n default:\n return undefined;\n }\n }\n\n async fetch(\n key: string | MediaBlob,\n opts: BlobStoreFetchOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreFetchOptions = {\n ...this.defaultFetchOptions,\n ...opts,\n };\n const realKey = await this._realKey(key);\n const ret = await this.mget([realKey]);\n return ret?.[0] ?? (await this._missingFetchBlob(realKey, allOpts));\n }\n}\n\nexport interface BackedBlobStoreOptions extends BlobStoreOptions {\n backingStore: BaseStore<string, MediaBlob>;\n}\n\nexport class BackedBlobStore extends BlobStore {\n backingStore: BaseStore<string, MediaBlob>;\n\n constructor(opts: BackedBlobStoreOptions) {\n super(opts);\n this.backingStore = opts.backingStore;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.backingStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.backingStore.mget(keys);\n }\n\n mset(keyValuePairs: [string, MediaBlob][]): Promise<void> {\n return this.backingStore.mset(keyValuePairs);\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.backingStore.yieldKeys(prefix);\n }\n}\n\nexport interface ReadThroughBlobStoreOptions extends BlobStoreOptions {\n baseStore: BlobStore;\n backingStore: BlobStore;\n}\n\nexport class ReadThroughBlobStore extends BlobStore {\n baseStore: BlobStore;\n\n backingStore: BlobStore;\n\n constructor(opts: ReadThroughBlobStoreOptions) {\n super(opts);\n this.baseStore = opts.baseStore;\n this.backingStore = opts.backingStore;\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const originalUri = await blob.asUri();\n const newBlob = await this.backingStore.store(blob, opts);\n if (newBlob) {\n await this.baseStore.mset([[originalUri, newBlob]]);\n }\n return newBlob;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.baseStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.baseStore.mget(keys);\n }\n\n mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n throw new Error(\"Do not call ReadThroughBlobStore.mset directly\");\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.baseStore.yieldKeys(prefix);\n }\n}\n\nexport class SimpleWebBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for SimpleWebBlobStore\");\n }\n\n async hasValidPath(\n path: string | undefined,\n _opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n return (\n (await super.hasValidPath(path, { pathPrefix: \"https://\" })) ||\n (await super.hasValidPath(path, { pathPrefix: \"http://\" }))\n );\n }\n\n async _fetch(url: string): Promise<MediaBlob | undefined> {\n const ret = new MediaBlob({\n path: url,\n });\n const metadata: Record<string, unknown> = {};\n const fetchOptions = {\n method: \"GET\",\n };\n const res = await fetch(url, fetchOptions);\n metadata.status = res.status;\n\n const headers: Record<string, string> = {};\n for (const [key, value] of res.headers.entries()) {\n headers[key] = value;\n }\n metadata.headers = headers;\n\n metadata.ok = res.ok;\n if (res.ok) {\n const resMediaBlob = await MediaBlob.fromBlob(await res.blob());\n ret.data = resMediaBlob.data;\n }\n\n ret.metadata = metadata;\n return ret;\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return await Promise.all(blobMap);\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\n/**\n * A blob \"store\" that works with data: URLs that will turn the URL into\n * a blob.\n */\nexport class DataBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for DataBlobStore\");\n }\n\n hasValidPath(path: string, _opts?: BlobStoreStoreOptions): Promise<boolean> {\n return super.hasValidPath(path, { pathPrefix: \"data:\" });\n }\n\n _fetch(url: string): MediaBlob {\n return MediaBlob.fromDataUrl(url);\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return blobMap;\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\nexport interface MediaManagerConfiguration {\n /**\n * A store that, given a common URI, returns the corresponding MediaBlob.\n * The returned MediaBlob may have a different URI.\n * In many cases, this will be a ReadThroughStore or something similar\n * that has a cached version of the MediaBlob, but also a way to get\n * a new (or refreshed) version.\n */\n store: BlobStore;\n\n /**\n * BlobStores that can resolve a URL into the MediaBlob to save\n * in the canonical store. This list is evaluated in order.\n * If not provided, a default list (which involves a DataBlobStore\n * and a SimpleWebBlobStore) will be used.\n */\n resolvers?: BlobStore[];\n}\n\n/**\n * Responsible for converting a URI (typically a web URL) into a MediaBlob.\n * Allows for aliasing / caching of the requested URI and what it resolves to.\n * This MediaBlob is expected to be usable to provide to an LLM, either\n * through the Base64 of the media or through a canonical URI that the LLM\n * supports.\n */\nexport class MediaManager {\n store: BlobStore;\n\n resolvers: BlobStore[] | undefined;\n\n constructor(config: MediaManagerConfiguration) {\n this.store = config.store;\n this.resolvers = config.resolvers;\n }\n\n defaultResolvers(): BlobStore[] {\n return [new DataBlobStore({}), new SimpleWebBlobStore({})];\n }\n\n async _isInvalid(blob: MediaBlob | undefined): Promise<boolean> {\n return typeof blob === \"undefined\";\n }\n\n /**\n * Given the public URI, load what is at this URI and save it\n * in the store.\n * @param uri The URI to resolve using the resolver\n * @return A canonical MediaBlob for this URI\n */\n async _resolveAndSave(uri: string): Promise<MediaBlob | undefined> {\n let resolvedBlob: MediaBlob | undefined;\n\n const resolvers = this.resolvers || this.defaultResolvers();\n for (let co = 0; co < resolvers.length; co += 1) {\n const resolver = resolvers[co];\n if (await resolver.hasValidPath(uri)) {\n resolvedBlob = await resolver.fetch(uri);\n }\n }\n\n if (resolvedBlob) {\n return await this.store.store(resolvedBlob);\n } else {\n return new MediaBlob({});\n }\n }\n\n async getMediaBlob(uri: string): Promise<MediaBlob | undefined> {\n const aliasBlob = await this.store.fetch(uri);\n const ret = (await this._isInvalid(aliasBlob))\n ? await this._resolveAndSave(uri)\n : (aliasBlob as MediaBlob);\n return ret;\n }\n}\n"],"mappings":";;;;;AAiBA,SAAS,cAAc,WAA+B;CAEpD,IAAI,MAAM;CACV,MAAM,YAAY;AAClB,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK,WAAW;EACpD,MAAM,QAAQ,UAAU,SAAS,GAAG,IAAI,UAAU;AAClD,SAAO,OAAO,aAAa,GAAG,MAAM;;AAGtC,QAAO;;;;;;AAOT,IAAa,YAAb,MAAa,kBAAkBA,kCAAAA,aAA4C;CACzE,kBAAkB;CAElB,eAAe;EACb;EACA;EACA;EACA;EACA;EACD;CAED,OAAsB;EACpB,OAAO;EACP,MAAM;EACP;CAGD;CAEA;CAEA,YAAY,QAA6B;AACvC,QAAM,OAAO;AAEb,OAAK,OAAO,OAAO,QAAQ,KAAK;AAChC,OAAK,WAAW,OAAO;AACvB,OAAK,OAAO,OAAO;;CAGrB,IAAI,OAAe;AACjB,SAAO,KAAK,QAAQ;;CAGtB,IAAI,WAAmB;AACrB,SAAO,KAAK,MAAM,QAAQ;;CAG5B,IAAI,WAAmB;EACrB,MAAM,gBAAgB,KAAK,SAAS,QAAQ,WAAW;AACvD,SAAO,kBAAkB,KACrB,UACA,KAAK,SAAS,UAAU,gBAAgB,EAAE;;CAGhD,IAAI,WAAmB;EACrB,MAAM,YAAY,KAAK,SAAS,QAAQ,IAAI;AAC5C,SAAO,cAAc,KACjB,KAAK,WACL,KAAK,SAAS,UAAU,GAAG,UAAU;;CAG3C,IAAI,UAAsB;AACxB,MAAI,CAAC,KA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;;;;;;AA8BV,IAAa,eAAb,MAA0B;CACxB;CAEA;CAEA,YAAY,QAAmC;AAC7C,OAAK,QAAQ,OAAO;AACpB,OAAK,YAAY,OAAO;;CAG1B,mBAAgC;AAC9B,SAAO,CAAC,IAAI,cAAc,EAAE,CAAC,EAAE,IAAI,mBAAmB,EAAE,CAAC,CAAC;;CAG5D,MAAM,WAAW,MAA+C;AAC9D,SAAO,OAAO,SAAS;;;;;;;;CASzB,MAAM,gBAAgB,KAA6C;EACjE,IAAI;EAEJ,MAAM,YAAY,KAAK,aAAa,KAAK,kBAAkB;AAC3D,OAAK,IAAI,KAAK,GAAG,KAAK,UAAU,QAAQ,MAAM,GAAG;GAC/C,MAAM,WAAW,UAAU;AAC3B,OAAI,MAAM,SAAS,aAAa,IAAI,CAClC,gBAAe,MAAM,SAAS,MAAM,IAAI;;AAI5C,MAAI,aACF,QAAO,MAAM,KAAK,MAAM,MAAM,aAAa;MAE3C,QAAO,IAAI,UAAU,EAAE,CAAC;;CAI5B,MAAM,aAAa,KAA6C;EAC9D,MAAM,YAAY,MAAM,KAAK,MAAM,MAAM,IAAI;AAI7C,SAHa,MAAM,KAAK,WAAW,UAAU,GACzC,MAAM,KAAK,gBAAgB,IAAI,GAC9B"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { v1, v4 } from "uuid";
|
|
2
2
|
import { BaseStore } from "@langchain/core/stores";
|
|
3
3
|
import { Serializable } from "@langchain/core/load/serializable";
|
|
4
|
-
|
|
5
4
|
//#region src/experimental/utils/media_core.ts
|
|
6
5
|
function bytesToString(dataArray) {
|
|
7
6
|
let ret = "";
|
|
@@ -380,7 +379,7 @@ var MediaManager = class {
|
|
|
380
379
|
return await this._isInvalid(aliasBlob) ? await this._resolveAndSave(uri) : aliasBlob;
|
|
381
380
|
}
|
|
382
381
|
};
|
|
383
|
-
|
|
384
382
|
//#endregion
|
|
385
383
|
export { BackedBlobStore, BlobStore, DataBlobStore, MediaBlob, MediaManager, ReadThroughBlobStore, SimpleWebBlobStore };
|
|
384
|
+
|
|
386
385
|
//# sourceMappingURL=media_core.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media_core.js","names":[],"sources":["../../../src/experimental/utils/media_core.ts"],"sourcesContent":["import { v1, v4 } from \"uuid\"; // FIXME - it is importing the wrong uuid, so v6 and v7 aren't implemented\nimport { BaseStore } from \"@langchain/core/stores\";\nimport { Serializable } from \"@langchain/core/load/serializable\";\n\nexport type MediaBlobData = {\n value: string; // In Base64 encoding\n type: string; // The mime type and possibly encoding\n};\n\nexport interface MediaBlobParameters {\n data?: MediaBlobData;\n\n metadata?: Record<string, unknown>;\n\n path?: string;\n}\n\nfunction bytesToString(dataArray: Uint8Array): string {\n // Need to handle the array in smaller chunks to deal with stack size limits\n let ret = \"\";\n const chunkSize = 102400;\n for (let i = 0; i < dataArray.length; i += chunkSize) {\n const chunk = dataArray.subarray(i, i + chunkSize);\n ret += String.fromCharCode(...chunk);\n }\n\n return ret;\n}\n\n/**\n * Represents a chunk of data that can be identified by the path where the\n * data is (or will be) located, along with optional metadata about the data.\n */\nexport class MediaBlob extends Serializable implements MediaBlobParameters {\n lc_serializable = true;\n\n lc_namespace = [\n \"langchain\",\n \"google_common\",\n \"experimental\",\n \"utils\",\n \"media_core\",\n ];\n\n data: MediaBlobData = {\n value: \"\",\n type: \"text/plain\",\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>;\n\n path?: string;\n\n constructor(params: MediaBlobParameters) {\n super(params);\n\n this.data = params.data ?? this.data;\n this.metadata = params.metadata;\n this.path = params.path;\n }\n\n get size(): number {\n return this.asBytes.length;\n }\n\n get dataType(): string {\n return this.data?.type ?? \"\";\n }\n\n get encoding(): string {\n const charsetEquals = this.dataType.indexOf(\"charset=\");\n return charsetEquals === -1\n ? \"utf-8\"\n : this.dataType.substring(charsetEquals + 8);\n }\n\n get mimetype(): string {\n const semicolon = this.dataType.indexOf(\";\");\n return semicolon === -1\n ? this.dataType\n : this.dataType.substring(0, semicolon);\n }\n\n get asBytes(): Uint8Array {\n if (!this.data) {\n return Uint8Array.from([]);\n }\n const binString = atob(this.data?.value);\n const ret = new Uint8Array(binString.length);\n for (let co = 0; co < binString.length; co += 1) {\n ret[co] = binString.charCodeAt(co);\n }\n return ret;\n }\n\n async asString(): Promise<string> {\n return bytesToString(this.asBytes);\n }\n\n async asBase64(): Promise<string> {\n return this.data?.value ?? \"\";\n }\n\n async asDataUrl(): Promise<string> {\n return `data:${this.mimetype};base64,${await this.asBase64()}`;\n }\n\n async asUri(): Promise<string> {\n return this.path ?? (await this.asDataUrl());\n }\n\n async encode(): Promise<{ encoded: string; encoding: string }> {\n const dataUrl = await this.asDataUrl();\n const comma = dataUrl.indexOf(\",\");\n const encoded = dataUrl.substring(comma + 1);\n const encoding: string = dataUrl.indexOf(\"base64\") > -1 ? \"base64\" : \"8bit\";\n return {\n encoded,\n encoding,\n };\n }\n\n static fromDataUrl(url: string): MediaBlob {\n if (!url.startsWith(\"data:\")) {\n throw new Error(\"Not a data: URL\");\n }\n const colon = url.indexOf(\":\");\n const semicolon = url.indexOf(\";\");\n const mimeType = url.substring(colon + 1, semicolon);\n\n const comma = url.indexOf(\",\");\n const base64Data = url.substring(comma + 1);\n\n const data: MediaBlobData = {\n type: mimeType,\n value: base64Data,\n };\n\n return new MediaBlob({\n data,\n path: url,\n });\n }\n\n static async fromBlob(\n blob: Blob,\n other?: Omit<MediaBlobParameters, \"data\">\n ): Promise<MediaBlob> {\n const valueBuffer = await blob.arrayBuffer();\n const valueArray = new Uint8Array(valueBuffer);\n const valueStr = bytesToString(valueArray);\n const value = btoa(valueStr);\n\n return new MediaBlob({\n ...other,\n data: {\n value,\n type: blob.type,\n },\n });\n }\n}\n\nexport type ActionIfInvalidAction =\n | \"ignore\"\n | \"prefixPath\"\n | \"prefixUuid1\"\n | \"prefixUuid4\"\n | \"prefixUuid6\"\n | \"prefixUuid7\"\n | \"removePath\";\n\nexport interface BlobStoreStoreOptions {\n /**\n * If the path is missing or invalid in the blob, how should we create\n * a new path?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an emtpy string: Reject the blob\n * - \"ignore\": Attempt to store it anyway (but this may fail)\n * - \"prefixPath\": Use the default prefix for the BlobStore and get the\n * unique portion from the URL. The original path is stored in the metadata\n * - \"prefixUuid\": Use the default prefix for the BlobStore and get the\n * unique portion from a generated UUID. The original path is stored\n * in the metadata\n */\n actionIfInvalid?: ActionIfInvalidAction;\n\n /**\n * The expected prefix for URIs that are stored.\n * This may be used to test if a MediaBlob is valid and used to create a new\n * path if \"prefixPath\" or \"prefixUuid\" is set for actionIfInvalid.\n */\n pathPrefix?: string;\n}\n\nexport type ActionIfBlobMissingAction = \"emptyBlob\";\n\nexport interface BlobStoreFetchOptions {\n /**\n * If the blob is not found when fetching, what should we do?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an empty string: return undefined\n * - \"emptyBlob\": return a new MediaBlob that has the path set, but nothing else.\n */\n actionIfBlobMissing?: ActionIfBlobMissingAction;\n}\n\nexport interface BlobStoreOptions {\n defaultStoreOptions?: BlobStoreStoreOptions;\n\n defaultFetchOptions?: BlobStoreFetchOptions;\n}\n\n/**\n * A specialized Store that is designed to handle MediaBlobs and use the\n * key that is included in the blob to determine exactly how it is stored.\n *\n * The full details of a MediaBlob may be changed when it is stored.\n * For example, it may get additional or different Metadata. This should be\n * what is returned when the store() method is called.\n *\n * Although BlobStore extends BaseStore, not all of the methods from\n * BaseStore may be implemented (or even possible). Those that are not\n * implemented should be documented and throw an Error if called.\n */\nexport abstract class BlobStore extends BaseStore<string, MediaBlob> {\n lc_namespace = [\"langchain\", \"google-common\"]; // FIXME - What should this be? And why?\n\n defaultStoreOptions: BlobStoreStoreOptions;\n\n defaultFetchOptions: BlobStoreFetchOptions;\n\n constructor(opts?: BlobStoreOptions) {\n super(opts);\n this.defaultStoreOptions = opts?.defaultStoreOptions ?? {};\n this.defaultFetchOptions = opts?.defaultFetchOptions ?? {};\n }\n\n protected async _realKey(key: string | MediaBlob): Promise<string> {\n return typeof key === \"string\" ? key : await key.asUri();\n }\n\n /**\n * Is the path supported by this BlobStore?\n *\n * Although this is async, this is expected to be a relatively fast operation\n * (ie - you shouldn't make network calls).\n *\n * @param path The path to check\n * @param opts Any options (if needed) that may be used to determine if it is valid\n * @return If the path is supported\n */\n hasValidPath(\n path: string | undefined,\n opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n const prefix = opts?.pathPrefix ?? \"\";\n const isPrefixed = typeof path !== \"undefined\" && path.startsWith(prefix);\n return Promise.resolve(isPrefixed);\n }\n\n protected _blobPathSuffix(blob: MediaBlob): string {\n // Get the path currently set and make sure we treat it as a string\n const blobPath = `${blob.path}`;\n\n // Advance past the first set of /\n let pathStart = blobPath.indexOf(\"/\") + 1;\n while (blobPath.charAt(pathStart) === \"/\") {\n pathStart += 1;\n }\n\n // We will use the rest as the path for a replacement\n return blobPath.substring(pathStart);\n }\n\n protected async _newBlob(\n oldBlob: MediaBlob,\n newPath?: string\n ): Promise<MediaBlob> {\n const oldPath = oldBlob.path;\n const metadata = oldBlob?.metadata ?? {};\n metadata.langchainOldPath = oldPath;\n const newBlob = new MediaBlob({\n ...oldBlob,\n metadata,\n });\n if (newPath) {\n newBlob.path = newPath;\n } else if (newBlob.path) {\n delete newBlob.path;\n }\n return newBlob;\n }\n\n protected async _validBlobPrefixPath(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._blobPathSuffix(blob);\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected _validBlobPrefixUuidFunction(\n name: ActionIfInvalidAction | string\n ): string {\n switch (name) {\n case \"prefixUuid1\":\n return v1();\n case \"prefixUuid4\":\n return v4();\n // case \"prefixUuid6\": return v6();\n // case \"prefixUuid7\": return v7();\n default:\n throw new Error(`Unknown uuid function: ${name}`);\n }\n }\n\n protected async _validBlobPrefixUuid(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._validBlobPrefixUuidFunction(\n opts?.actionIfInvalid ?? \"prefixUuid4\"\n );\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected async _validBlobRemovePath(\n blob: MediaBlob,\n _opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n return this._newBlob(blob, undefined);\n }\n\n /**\n * Based on the blob and options, return a blob that has a valid path\n * that can be saved.\n * @param blob\n * @param opts\n */\n protected async _validStoreBlob(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob | undefined> {\n if (await this.hasValidPath(blob.path, opts)) {\n return blob;\n }\n switch (opts?.actionIfInvalid) {\n case \"ignore\":\n return blob;\n case \"prefixPath\":\n return this._validBlobPrefixPath(blob, opts);\n case \"prefixUuid1\":\n case \"prefixUuid4\":\n case \"prefixUuid6\":\n case \"prefixUuid7\":\n return this._validBlobPrefixUuid(blob, opts);\n case \"removePath\":\n return this._validBlobRemovePath(blob, opts);\n default:\n return undefined;\n }\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreStoreOptions = {\n ...this.defaultStoreOptions,\n ...opts,\n };\n const validBlob = await this._validStoreBlob(blob, allOpts);\n if (typeof validBlob !== \"undefined\") {\n const validKey = await validBlob.asUri();\n await this.mset([[validKey, validBlob]]);\n const savedKey = await validBlob.asUri();\n return await this.fetch(savedKey);\n }\n return undefined;\n }\n\n protected async _missingFetchBlobEmpty(\n path: string,\n _opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob> {\n return new MediaBlob({ path });\n }\n\n protected async _missingFetchBlob(\n path: string,\n opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob | undefined> {\n switch (opts?.actionIfBlobMissing) {\n case \"emptyBlob\":\n return this._missingFetchBlobEmpty(path, opts);\n default:\n return undefined;\n }\n }\n\n async fetch(\n key: string | MediaBlob,\n opts: BlobStoreFetchOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreFetchOptions = {\n ...this.defaultFetchOptions,\n ...opts,\n };\n const realKey = await this._realKey(key);\n const ret = await this.mget([realKey]);\n return ret?.[0] ?? (await this._missingFetchBlob(realKey, allOpts));\n }\n}\n\nexport interface BackedBlobStoreOptions extends BlobStoreOptions {\n backingStore: BaseStore<string, MediaBlob>;\n}\n\nexport class BackedBlobStore extends BlobStore {\n backingStore: BaseStore<string, MediaBlob>;\n\n constructor(opts: BackedBlobStoreOptions) {\n super(opts);\n this.backingStore = opts.backingStore;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.backingStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.backingStore.mget(keys);\n }\n\n mset(keyValuePairs: [string, MediaBlob][]): Promise<void> {\n return this.backingStore.mset(keyValuePairs);\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.backingStore.yieldKeys(prefix);\n }\n}\n\nexport interface ReadThroughBlobStoreOptions extends BlobStoreOptions {\n baseStore: BlobStore;\n backingStore: BlobStore;\n}\n\nexport class ReadThroughBlobStore extends BlobStore {\n baseStore: BlobStore;\n\n backingStore: BlobStore;\n\n constructor(opts: ReadThroughBlobStoreOptions) {\n super(opts);\n this.baseStore = opts.baseStore;\n this.backingStore = opts.backingStore;\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const originalUri = await blob.asUri();\n const newBlob = await this.backingStore.store(blob, opts);\n if (newBlob) {\n await this.baseStore.mset([[originalUri, newBlob]]);\n }\n return newBlob;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.baseStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.baseStore.mget(keys);\n }\n\n mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n throw new Error(\"Do not call ReadThroughBlobStore.mset directly\");\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.baseStore.yieldKeys(prefix);\n }\n}\n\nexport class SimpleWebBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for SimpleWebBlobStore\");\n }\n\n async hasValidPath(\n path: string | undefined,\n _opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n return (\n (await super.hasValidPath(path, { pathPrefix: \"https://\" })) ||\n (await super.hasValidPath(path, { pathPrefix: \"http://\" }))\n );\n }\n\n async _fetch(url: string): Promise<MediaBlob | undefined> {\n const ret = new MediaBlob({\n path: url,\n });\n const metadata: Record<string, unknown> = {};\n const fetchOptions = {\n method: \"GET\",\n };\n const res = await fetch(url, fetchOptions);\n metadata.status = res.status;\n\n const headers: Record<string, string> = {};\n for (const [key, value] of res.headers.entries()) {\n headers[key] = value;\n }\n metadata.headers = headers;\n\n metadata.ok = res.ok;\n if (res.ok) {\n const resMediaBlob = await MediaBlob.fromBlob(await res.blob());\n ret.data = resMediaBlob.data;\n }\n\n ret.metadata = metadata;\n return ret;\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return await Promise.all(blobMap);\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\n/**\n * A blob \"store\" that works with data: URLs that will turn the URL into\n * a blob.\n */\nexport class DataBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for DataBlobStore\");\n }\n\n hasValidPath(path: string, _opts?: BlobStoreStoreOptions): Promise<boolean> {\n return super.hasValidPath(path, { pathPrefix: \"data:\" });\n }\n\n _fetch(url: string): MediaBlob {\n return MediaBlob.fromDataUrl(url);\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return blobMap;\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\nexport interface MediaManagerConfiguration {\n /**\n * A store that, given a common URI, returns the corresponding MediaBlob.\n * The returned MediaBlob may have a different URI.\n * In many cases, this will be a ReadThroughStore or something similar\n * that has a cached version of the MediaBlob, but also a way to get\n * a new (or refreshed) version.\n */\n store: BlobStore;\n\n /**\n * BlobStores that can resolve a URL into the MediaBlob to save\n * in the canonical store. This list is evaluated in order.\n * If not provided, a default list (which involves a DataBlobStore\n * and a SimpleWebBlobStore) will be used.\n */\n resolvers?: BlobStore[];\n}\n\n/**\n * Responsible for converting a URI (typically a web URL) into a MediaBlob.\n * Allows for aliasing / caching of the requested URI and what it resolves to.\n * This MediaBlob is expected to be usable to provide to an LLM, either\n * through the Base64 of the media or through a canonical URI that the LLM\n * supports.\n */\nexport class MediaManager {\n store: BlobStore;\n\n resolvers: BlobStore[] | undefined;\n\n constructor(config: MediaManagerConfiguration) {\n this.store = config.store;\n this.resolvers = config.resolvers;\n }\n\n defaultResolvers(): BlobStore[] {\n return [new DataBlobStore({}), new SimpleWebBlobStore({})];\n }\n\n async _isInvalid(blob: MediaBlob | undefined): Promise<boolean> {\n return typeof blob === \"undefined\";\n }\n\n /**\n * Given the public URI, load what is at this URI and save it\n * in the store.\n * @param uri The URI to resolve using the resolver\n * @return A canonical MediaBlob for this URI\n */\n async _resolveAndSave(uri: string): Promise<MediaBlob | undefined> {\n let resolvedBlob: MediaBlob | undefined;\n\n const resolvers = this.resolvers || this.defaultResolvers();\n for (let co = 0; co < resolvers.length; co += 1) {\n const resolver = resolvers[co];\n if (await resolver.hasValidPath(uri)) {\n resolvedBlob = await resolver.fetch(uri);\n }\n }\n\n if (resolvedBlob) {\n return await this.store.store(resolvedBlob);\n } else {\n return new MediaBlob({});\n }\n }\n\n async getMediaBlob(uri: string): Promise<MediaBlob | undefined> {\n const aliasBlob = await this.store.fetch(uri);\n const ret = (await this._isInvalid(aliasBlob))\n ? await this._resolveAndSave(uri)\n : (aliasBlob as MediaBlob);\n return ret;\n }\n}\n"],"mappings":";;;;;AAiBA,SAAS,cAAc,WAA+B;CAEpD,IAAI,MAAM;CACV,MAAM,YAAY;AAClB,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK,WAAW;EACpD,MAAM,QAAQ,UAAU,SAAS,GAAG,IAAI,UAAU;AAClD,SAAO,OAAO,aAAa,GAAG,MAAM;;AAGtC,QAAO;;;;;;AAOT,IAAa,YAAb,MAAa,kBAAkB,aAA4C;CACzE,kBAAkB;CAElB,eAAe;EACb;EACA;EACA;EACA;EACA;EACD;CAED,OAAsB;EACpB,OAAO;EACP,MAAM;EACP;CAGD;CAEA;CAEA,YAAY,QAA6B;AACvC,QAAM,OAAO;AAEb,OAAK,OAAO,OAAO,QAAQ,KAAK;AAChC,OAAK,WAAW,OAAO;AACvB,OAAK,OAAO,OAAO;;CAGrB,IAAI,OAAe;AACjB,SAAO,KAAK,QAAQ;;CAGtB,IAAI,WAAmB;AACrB,SAAO,KAAK,MAAM,QAAQ;;CAG5B,IAAI,WAAmB;EACrB,MAAM,gBAAgB,KAAK,SAAS,QAAQ,WAAW;AACvD,SAAO,kBAAkB,KACrB,UACA,KAAK,SAAS,UAAU,gBAAgB,EAAE;;CAGhD,IAAI,WAAmB;EACrB,MAAM,YAAY,KAAK,SAAS,QAAQ,IAAI;AAC5C,SAAO,cAAc,KACjB,KAAK,WACL,KAAK,SAAS,UAAU,GAAG,UAAU;;CAG3C,IAAI,UAAsB;AACxB,MAAI,C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eAAb,MAA0B;CACxB;CAEA;CAEA,YAAY,QAAmC;AAC7C,OAAK,QAAQ,OAAO;AACpB,OAAK,YAAY,OAAO;;CAG1B,mBAAgC;AAC9B,SAAO,CAAC,IAAI,cAAc,EAAE,CAAC,EAAE,IAAI,mBAAmB,EAAE,CAAC,CAAC;;CAG5D,MAAM,WAAW,MAA+C;AAC9D,SAAO,OAAO,SAAS;;;;;;;;CASzB,MAAM,gBAAgB,KAA6C;EACjE,IAAI;EAEJ,MAAM,YAAY,KAAK,aAAa,KAAK,kBAAkB;AAC3D,OAAK,IAAI,KAAK,GAAG,KAAK,UAAU,QAAQ,MAAM,GAAG;GAC/C,MAAM,WAAW,UAAU;AAC3B,OAAI,MAAM,SAAS,aAAa,IAAI,CAClC,gBAAe,MAAM,SAAS,MAAM,IAAI;;AAI5C,MAAI,aACF,QAAO,MAAM,KAAK,MAAM,MAAM,aAAa;MAE3C,QAAO,IAAI,UAAU,EAAE,CAAC;;CAI5B,MAAM,aAAa,KAA6C;EAC9D,MAAM,YAAY,MAAM,KAAK,MAAM,MAAM,IAAI;AAI7C,SAHa,MAAM,KAAK,WAAW,UAAU,GACzC,MAAM,KAAK,gBAAgB,IAAI,GAC9B"}
|
|
1
|
+
{"version":3,"file":"media_core.js","names":[],"sources":["../../../src/experimental/utils/media_core.ts"],"sourcesContent":["import { v1, v4 } from \"uuid\"; // FIXME - it is importing the wrong uuid, so v6 and v7 aren't implemented\nimport { BaseStore } from \"@langchain/core/stores\";\nimport { Serializable } from \"@langchain/core/load/serializable\";\n\nexport type MediaBlobData = {\n value: string; // In Base64 encoding\n type: string; // The mime type and possibly encoding\n};\n\nexport interface MediaBlobParameters {\n data?: MediaBlobData;\n\n metadata?: Record<string, unknown>;\n\n path?: string;\n}\n\nfunction bytesToString(dataArray: Uint8Array): string {\n // Need to handle the array in smaller chunks to deal with stack size limits\n let ret = \"\";\n const chunkSize = 102400;\n for (let i = 0; i < dataArray.length; i += chunkSize) {\n const chunk = dataArray.subarray(i, i + chunkSize);\n ret += String.fromCharCode(...chunk);\n }\n\n return ret;\n}\n\n/**\n * Represents a chunk of data that can be identified by the path where the\n * data is (or will be) located, along with optional metadata about the data.\n */\nexport class MediaBlob extends Serializable implements MediaBlobParameters {\n lc_serializable = true;\n\n lc_namespace = [\n \"langchain\",\n \"google_common\",\n \"experimental\",\n \"utils\",\n \"media_core\",\n ];\n\n data: MediaBlobData = {\n value: \"\",\n type: \"text/plain\",\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>;\n\n path?: string;\n\n constructor(params: MediaBlobParameters) {\n super(params);\n\n this.data = params.data ?? this.data;\n this.metadata = params.metadata;\n this.path = params.path;\n }\n\n get size(): number {\n return this.asBytes.length;\n }\n\n get dataType(): string {\n return this.data?.type ?? \"\";\n }\n\n get encoding(): string {\n const charsetEquals = this.dataType.indexOf(\"charset=\");\n return charsetEquals === -1\n ? \"utf-8\"\n : this.dataType.substring(charsetEquals + 8);\n }\n\n get mimetype(): string {\n const semicolon = this.dataType.indexOf(\";\");\n return semicolon === -1\n ? this.dataType\n : this.dataType.substring(0, semicolon);\n }\n\n get asBytes(): Uint8Array {\n if (!this.data) {\n return Uint8Array.from([]);\n }\n const binString = atob(this.data?.value);\n const ret = new Uint8Array(binString.length);\n for (let co = 0; co < binString.length; co += 1) {\n ret[co] = binString.charCodeAt(co);\n }\n return ret;\n }\n\n async asString(): Promise<string> {\n return bytesToString(this.asBytes);\n }\n\n async asBase64(): Promise<string> {\n return this.data?.value ?? \"\";\n }\n\n async asDataUrl(): Promise<string> {\n return `data:${this.mimetype};base64,${await this.asBase64()}`;\n }\n\n async asUri(): Promise<string> {\n return this.path ?? (await this.asDataUrl());\n }\n\n async encode(): Promise<{ encoded: string; encoding: string }> {\n const dataUrl = await this.asDataUrl();\n const comma = dataUrl.indexOf(\",\");\n const encoded = dataUrl.substring(comma + 1);\n const encoding: string = dataUrl.indexOf(\"base64\") > -1 ? \"base64\" : \"8bit\";\n return {\n encoded,\n encoding,\n };\n }\n\n static fromDataUrl(url: string): MediaBlob {\n if (!url.startsWith(\"data:\")) {\n throw new Error(\"Not a data: URL\");\n }\n const colon = url.indexOf(\":\");\n const semicolon = url.indexOf(\";\");\n const mimeType = url.substring(colon + 1, semicolon);\n\n const comma = url.indexOf(\",\");\n const base64Data = url.substring(comma + 1);\n\n const data: MediaBlobData = {\n type: mimeType,\n value: base64Data,\n };\n\n return new MediaBlob({\n data,\n path: url,\n });\n }\n\n static async fromBlob(\n blob: Blob,\n other?: Omit<MediaBlobParameters, \"data\">\n ): Promise<MediaBlob> {\n const valueBuffer = await blob.arrayBuffer();\n const valueArray = new Uint8Array(valueBuffer);\n const valueStr = bytesToString(valueArray);\n const value = btoa(valueStr);\n\n return new MediaBlob({\n ...other,\n data: {\n value,\n type: blob.type,\n },\n });\n }\n}\n\nexport type ActionIfInvalidAction =\n | \"ignore\"\n | \"prefixPath\"\n | \"prefixUuid1\"\n | \"prefixUuid4\"\n | \"prefixUuid6\"\n | \"prefixUuid7\"\n | \"removePath\";\n\nexport interface BlobStoreStoreOptions {\n /**\n * If the path is missing or invalid in the blob, how should we create\n * a new path?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an emtpy string: Reject the blob\n * - \"ignore\": Attempt to store it anyway (but this may fail)\n * - \"prefixPath\": Use the default prefix for the BlobStore and get the\n * unique portion from the URL. The original path is stored in the metadata\n * - \"prefixUuid\": Use the default prefix for the BlobStore and get the\n * unique portion from a generated UUID. The original path is stored\n * in the metadata\n */\n actionIfInvalid?: ActionIfInvalidAction;\n\n /**\n * The expected prefix for URIs that are stored.\n * This may be used to test if a MediaBlob is valid and used to create a new\n * path if \"prefixPath\" or \"prefixUuid\" is set for actionIfInvalid.\n */\n pathPrefix?: string;\n}\n\nexport type ActionIfBlobMissingAction = \"emptyBlob\";\n\nexport interface BlobStoreFetchOptions {\n /**\n * If the blob is not found when fetching, what should we do?\n * Subclasses may define their own methods, but the following are supported\n * by default:\n * - Undefined or an empty string: return undefined\n * - \"emptyBlob\": return a new MediaBlob that has the path set, but nothing else.\n */\n actionIfBlobMissing?: ActionIfBlobMissingAction;\n}\n\nexport interface BlobStoreOptions {\n defaultStoreOptions?: BlobStoreStoreOptions;\n\n defaultFetchOptions?: BlobStoreFetchOptions;\n}\n\n/**\n * A specialized Store that is designed to handle MediaBlobs and use the\n * key that is included in the blob to determine exactly how it is stored.\n *\n * The full details of a MediaBlob may be changed when it is stored.\n * For example, it may get additional or different Metadata. This should be\n * what is returned when the store() method is called.\n *\n * Although BlobStore extends BaseStore, not all of the methods from\n * BaseStore may be implemented (or even possible). Those that are not\n * implemented should be documented and throw an Error if called.\n */\nexport abstract class BlobStore extends BaseStore<string, MediaBlob> {\n lc_namespace = [\"langchain\", \"google-common\"]; // FIXME - What should this be? And why?\n\n defaultStoreOptions: BlobStoreStoreOptions;\n\n defaultFetchOptions: BlobStoreFetchOptions;\n\n constructor(opts?: BlobStoreOptions) {\n super(opts);\n this.defaultStoreOptions = opts?.defaultStoreOptions ?? {};\n this.defaultFetchOptions = opts?.defaultFetchOptions ?? {};\n }\n\n protected async _realKey(key: string | MediaBlob): Promise<string> {\n return typeof key === \"string\" ? key : await key.asUri();\n }\n\n /**\n * Is the path supported by this BlobStore?\n *\n * Although this is async, this is expected to be a relatively fast operation\n * (ie - you shouldn't make network calls).\n *\n * @param path The path to check\n * @param opts Any options (if needed) that may be used to determine if it is valid\n * @return If the path is supported\n */\n hasValidPath(\n path: string | undefined,\n opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n const prefix = opts?.pathPrefix ?? \"\";\n const isPrefixed = typeof path !== \"undefined\" && path.startsWith(prefix);\n return Promise.resolve(isPrefixed);\n }\n\n protected _blobPathSuffix(blob: MediaBlob): string {\n // Get the path currently set and make sure we treat it as a string\n const blobPath = `${blob.path}`;\n\n // Advance past the first set of /\n let pathStart = blobPath.indexOf(\"/\") + 1;\n while (blobPath.charAt(pathStart) === \"/\") {\n pathStart += 1;\n }\n\n // We will use the rest as the path for a replacement\n return blobPath.substring(pathStart);\n }\n\n protected async _newBlob(\n oldBlob: MediaBlob,\n newPath?: string\n ): Promise<MediaBlob> {\n const oldPath = oldBlob.path;\n const metadata = oldBlob?.metadata ?? {};\n metadata.langchainOldPath = oldPath;\n const newBlob = new MediaBlob({\n ...oldBlob,\n metadata,\n });\n if (newPath) {\n newBlob.path = newPath;\n } else if (newBlob.path) {\n delete newBlob.path;\n }\n return newBlob;\n }\n\n protected async _validBlobPrefixPath(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._blobPathSuffix(blob);\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected _validBlobPrefixUuidFunction(\n name: ActionIfInvalidAction | string\n ): string {\n switch (name) {\n case \"prefixUuid1\":\n return v1();\n case \"prefixUuid4\":\n return v4();\n // case \"prefixUuid6\": return v6();\n // case \"prefixUuid7\": return v7();\n default:\n throw new Error(`Unknown uuid function: ${name}`);\n }\n }\n\n protected async _validBlobPrefixUuid(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n const prefix = opts?.pathPrefix ?? \"\";\n const suffix = this._validBlobPrefixUuidFunction(\n opts?.actionIfInvalid ?? \"prefixUuid4\"\n );\n const newPath = `${prefix}${suffix}`;\n return this._newBlob(blob, newPath);\n }\n\n protected async _validBlobRemovePath(\n blob: MediaBlob,\n _opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob> {\n return this._newBlob(blob, undefined);\n }\n\n /**\n * Based on the blob and options, return a blob that has a valid path\n * that can be saved.\n * @param blob\n * @param opts\n */\n protected async _validStoreBlob(\n blob: MediaBlob,\n opts?: BlobStoreStoreOptions\n ): Promise<MediaBlob | undefined> {\n if (await this.hasValidPath(blob.path, opts)) {\n return blob;\n }\n switch (opts?.actionIfInvalid) {\n case \"ignore\":\n return blob;\n case \"prefixPath\":\n return this._validBlobPrefixPath(blob, opts);\n case \"prefixUuid1\":\n case \"prefixUuid4\":\n case \"prefixUuid6\":\n case \"prefixUuid7\":\n return this._validBlobPrefixUuid(blob, opts);\n case \"removePath\":\n return this._validBlobRemovePath(blob, opts);\n default:\n return undefined;\n }\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreStoreOptions = {\n ...this.defaultStoreOptions,\n ...opts,\n };\n const validBlob = await this._validStoreBlob(blob, allOpts);\n if (typeof validBlob !== \"undefined\") {\n const validKey = await validBlob.asUri();\n await this.mset([[validKey, validBlob]]);\n const savedKey = await validBlob.asUri();\n return await this.fetch(savedKey);\n }\n return undefined;\n }\n\n protected async _missingFetchBlobEmpty(\n path: string,\n _opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob> {\n return new MediaBlob({ path });\n }\n\n protected async _missingFetchBlob(\n path: string,\n opts?: BlobStoreFetchOptions\n ): Promise<MediaBlob | undefined> {\n switch (opts?.actionIfBlobMissing) {\n case \"emptyBlob\":\n return this._missingFetchBlobEmpty(path, opts);\n default:\n return undefined;\n }\n }\n\n async fetch(\n key: string | MediaBlob,\n opts: BlobStoreFetchOptions = {}\n ): Promise<MediaBlob | undefined> {\n const allOpts: BlobStoreFetchOptions = {\n ...this.defaultFetchOptions,\n ...opts,\n };\n const realKey = await this._realKey(key);\n const ret = await this.mget([realKey]);\n return ret?.[0] ?? (await this._missingFetchBlob(realKey, allOpts));\n }\n}\n\nexport interface BackedBlobStoreOptions extends BlobStoreOptions {\n backingStore: BaseStore<string, MediaBlob>;\n}\n\nexport class BackedBlobStore extends BlobStore {\n backingStore: BaseStore<string, MediaBlob>;\n\n constructor(opts: BackedBlobStoreOptions) {\n super(opts);\n this.backingStore = opts.backingStore;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.backingStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.backingStore.mget(keys);\n }\n\n mset(keyValuePairs: [string, MediaBlob][]): Promise<void> {\n return this.backingStore.mset(keyValuePairs);\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.backingStore.yieldKeys(prefix);\n }\n}\n\nexport interface ReadThroughBlobStoreOptions extends BlobStoreOptions {\n baseStore: BlobStore;\n backingStore: BlobStore;\n}\n\nexport class ReadThroughBlobStore extends BlobStore {\n baseStore: BlobStore;\n\n backingStore: BlobStore;\n\n constructor(opts: ReadThroughBlobStoreOptions) {\n super(opts);\n this.baseStore = opts.baseStore;\n this.backingStore = opts.backingStore;\n }\n\n async store(\n blob: MediaBlob,\n opts: BlobStoreStoreOptions = {}\n ): Promise<MediaBlob | undefined> {\n const originalUri = await blob.asUri();\n const newBlob = await this.backingStore.store(blob, opts);\n if (newBlob) {\n await this.baseStore.mset([[originalUri, newBlob]]);\n }\n return newBlob;\n }\n\n mdelete(keys: string[]): Promise<void> {\n return this.baseStore.mdelete(keys);\n }\n\n mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n return this.baseStore.mget(keys);\n }\n\n mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n throw new Error(\"Do not call ReadThroughBlobStore.mset directly\");\n }\n\n yieldKeys(prefix: string | undefined): AsyncGenerator<string> {\n return this.baseStore.yieldKeys(prefix);\n }\n}\n\nexport class SimpleWebBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for SimpleWebBlobStore\");\n }\n\n async hasValidPath(\n path: string | undefined,\n _opts?: BlobStoreStoreOptions\n ): Promise<boolean> {\n return (\n (await super.hasValidPath(path, { pathPrefix: \"https://\" })) ||\n (await super.hasValidPath(path, { pathPrefix: \"http://\" }))\n );\n }\n\n async _fetch(url: string): Promise<MediaBlob | undefined> {\n const ret = new MediaBlob({\n path: url,\n });\n const metadata: Record<string, unknown> = {};\n const fetchOptions = {\n method: \"GET\",\n };\n const res = await fetch(url, fetchOptions);\n metadata.status = res.status;\n\n const headers: Record<string, string> = {};\n for (const [key, value] of res.headers.entries()) {\n headers[key] = value;\n }\n metadata.headers = headers;\n\n metadata.ok = res.ok;\n if (res.ok) {\n const resMediaBlob = await MediaBlob.fromBlob(await res.blob());\n ret.data = resMediaBlob.data;\n }\n\n ret.metadata = metadata;\n return ret;\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return await Promise.all(blobMap);\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\n/**\n * A blob \"store\" that works with data: URLs that will turn the URL into\n * a blob.\n */\nexport class DataBlobStore extends BlobStore {\n _notImplementedException() {\n throw new Error(\"Not implemented for DataBlobStore\");\n }\n\n hasValidPath(path: string, _opts?: BlobStoreStoreOptions): Promise<boolean> {\n return super.hasValidPath(path, { pathPrefix: \"data:\" });\n }\n\n _fetch(url: string): MediaBlob {\n return MediaBlob.fromDataUrl(url);\n }\n\n async mget(keys: string[]): Promise<(MediaBlob | undefined)[]> {\n const blobMap = keys.map(this._fetch);\n return blobMap;\n }\n\n async mdelete(_keys: string[]): Promise<void> {\n this._notImplementedException();\n }\n\n async mset(_keyValuePairs: [string, MediaBlob][]): Promise<void> {\n this._notImplementedException();\n }\n\n async *yieldKeys(_prefix: string | undefined): AsyncGenerator<string> {\n this._notImplementedException();\n yield \"\";\n }\n}\n\nexport interface MediaManagerConfiguration {\n /**\n * A store that, given a common URI, returns the corresponding MediaBlob.\n * The returned MediaBlob may have a different URI.\n * In many cases, this will be a ReadThroughStore or something similar\n * that has a cached version of the MediaBlob, but also a way to get\n * a new (or refreshed) version.\n */\n store: BlobStore;\n\n /**\n * BlobStores that can resolve a URL into the MediaBlob to save\n * in the canonical store. This list is evaluated in order.\n * If not provided, a default list (which involves a DataBlobStore\n * and a SimpleWebBlobStore) will be used.\n */\n resolvers?: BlobStore[];\n}\n\n/**\n * Responsible for converting a URI (typically a web URL) into a MediaBlob.\n * Allows for aliasing / caching of the requested URI and what it resolves to.\n * This MediaBlob is expected to be usable to provide to an LLM, either\n * through the Base64 of the media or through a canonical URI that the LLM\n * supports.\n */\nexport class MediaManager {\n store: BlobStore;\n\n resolvers: BlobStore[] | undefined;\n\n constructor(config: MediaManagerConfiguration) {\n this.store = config.store;\n this.resolvers = config.resolvers;\n }\n\n defaultResolvers(): BlobStore[] {\n return [new DataBlobStore({}), new SimpleWebBlobStore({})];\n }\n\n async _isInvalid(blob: MediaBlob | undefined): Promise<boolean> {\n return typeof blob === \"undefined\";\n }\n\n /**\n * Given the public URI, load what is at this URI and save it\n * in the store.\n * @param uri The URI to resolve using the resolver\n * @return A canonical MediaBlob for this URI\n */\n async _resolveAndSave(uri: string): Promise<MediaBlob | undefined> {\n let resolvedBlob: MediaBlob | undefined;\n\n const resolvers = this.resolvers || this.defaultResolvers();\n for (let co = 0; co < resolvers.length; co += 1) {\n const resolver = resolvers[co];\n if (await resolver.hasValidPath(uri)) {\n resolvedBlob = await resolver.fetch(uri);\n }\n }\n\n if (resolvedBlob) {\n return await this.store.store(resolvedBlob);\n } else {\n return new MediaBlob({});\n }\n }\n\n async getMediaBlob(uri: string): Promise<MediaBlob | undefined> {\n const aliasBlob = await this.store.fetch(uri);\n const ret = (await this._isInvalid(aliasBlob))\n ? await this._resolveAndSave(uri)\n : (aliasBlob as MediaBlob);\n return ret;\n }\n}\n"],"mappings":";;;;AAiBA,SAAS,cAAc,WAA+B;CAEpD,IAAI,MAAM;CACV,MAAM,YAAY;AAClB,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK,WAAW;EACpD,MAAM,QAAQ,UAAU,SAAS,GAAG,IAAI,UAAU;AAClD,SAAO,OAAO,aAAa,GAAG,MAAM;;AAGtC,QAAO;;;;;;AAO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,UAAU,EAAE,CAAC;;CAI5B,MAAM,aAAa,KAA6C;EAC9D,MAAM,YAAY,MAAM,KAAK,MAAM,MAAM,IAAI;AAI7C,SAHa,MAAM,KAAK,WAAW,UAAU,GACzC,MAAM,KAAK,gBAAgB,IAAI,GAC9B"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value:
|
|
2
|
-
const require_types = require(
|
|
3
|
-
const require_safety = require(
|
|
4
|
-
const require_zod_to_gemini_parameters = require(
|
|
5
|
-
const require_common = require(
|
|
6
|
-
const require_stream = require(
|
|
7
|
-
const require_connection = require(
|
|
8
|
-
const require_auth = require(
|
|
9
|
-
const require_chat_models = require(
|
|
10
|
-
const require_llms = require(
|
|
11
|
-
const require_embeddings = require(
|
|
12
|
-
const require_output_parsers = require(
|
|
13
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_types = require("./types.cjs");
|
|
3
|
+
const require_safety = require("./utils/safety.cjs");
|
|
4
|
+
const require_zod_to_gemini_parameters = require("./utils/zod_to_gemini_parameters.cjs");
|
|
5
|
+
const require_common = require("./utils/common.cjs");
|
|
6
|
+
const require_stream = require("./utils/stream.cjs");
|
|
7
|
+
const require_connection = require("./connection.cjs");
|
|
8
|
+
const require_auth = require("./auth.cjs");
|
|
9
|
+
const require_chat_models = require("./chat_models.cjs");
|
|
10
|
+
const require_llms = require("./llms.cjs");
|
|
11
|
+
const require_embeddings = require("./embeddings.cjs");
|
|
12
|
+
const require_output_parsers = require("./output_parsers.cjs");
|
|
14
13
|
exports.AbstractGoogleLLMConnection = require_connection.AbstractGoogleLLMConnection;
|
|
15
14
|
exports.ApiKeyGoogleAuth = require_auth.ApiKeyGoogleAuth;
|
|
16
15
|
exports.BaseGoogleEmbeddings = require_embeddings.BaseGoogleEmbeddings;
|
|
@@ -56,4 +55,4 @@ exports.modelToPublisher = require_common.modelToPublisher;
|
|
|
56
55
|
exports.removeAdditionalProperties = require_zod_to_gemini_parameters.removeAdditionalProperties;
|
|
57
56
|
exports.schemaToGeminiParameters = require_zod_to_gemini_parameters.schemaToGeminiParameters;
|
|
58
57
|
exports.simpleValue = require_stream.simpleValue;
|
|
59
|
-
exports.validateModelParams = require_common.validateModelParams;
|
|
58
|
+
exports.validateModelParams = require_common.validateModelParams;
|
package/dist/index.js
CHANGED
|
@@ -9,5 +9,4 @@ import { ChatConnection, ChatGoogleBase } from "./chat_models.js";
|
|
|
9
9
|
import { GoogleBaseLLM } from "./llms.js";
|
|
10
10
|
import { BaseGoogleEmbeddings } from "./embeddings.js";
|
|
11
11
|
import { BaseGoogleSearchOutputParser, MarkdownGoogleSearchOutputParser, SimpleGoogleSearchOutputParser } from "./output_parsers.js";
|
|
12
|
-
|
|
13
|
-
export { AbstractGoogleLLMConnection, ApiKeyGoogleAuth, BaseGoogleEmbeddings, BaseGoogleSearchOutputParser, ChatConnection, ChatGoogleBase, ComplexJsonStream, GeminiSearchToolAttributes, GeminiToolAttributes, GoogleAIConnection, GoogleAISafetyCategory, GoogleAISafetyError, GoogleAISafetyMethod, GoogleAISafetyThreshold, GoogleAbstractedFetchClient, GoogleBaseLLM, GoogleConnection, GoogleHostConnection, GoogleRawConnection, GoogleRequestCallbackHandler, GoogleRequestLogger, GoogleRequestRecorder, JsonStream, MarkdownGoogleSearchOutputParser, ReadableAbstractStream, ReadableJsonStream, ReadableSseJsonStream, ReadableSseStream, SimpleGoogleSearchOutputParser, SseJsonStream, SseStream, adjustObjectType, aiPlatformScope, complexValue, convertToGeminiTools, copyAIModelParams, copyAIModelParamsInto, copyAndValidateModelParamsInto, ensureAuthOptionScopes, jsonSchemaToGeminiParameters, modelToFamily, modelToPublisher, removeAdditionalProperties, schemaToGeminiParameters, simpleValue, validateModelParams };
|
|
12
|
+
export { AbstractGoogleLLMConnection, ApiKeyGoogleAuth, BaseGoogleEmbeddings, BaseGoogleSearchOutputParser, ChatConnection, ChatGoogleBase, ComplexJsonStream, GeminiSearchToolAttributes, GeminiToolAttributes, GoogleAIConnection, GoogleAISafetyCategory, GoogleAISafetyError, GoogleAISafetyMethod, GoogleAISafetyThreshold, GoogleAbstractedFetchClient, GoogleBaseLLM, GoogleConnection, GoogleHostConnection, GoogleRawConnection, GoogleRequestCallbackHandler, GoogleRequestLogger, GoogleRequestRecorder, JsonStream, MarkdownGoogleSearchOutputParser, ReadableAbstractStream, ReadableJsonStream, ReadableSseJsonStream, ReadableSseStream, SimpleGoogleSearchOutputParser, SseJsonStream, SseStream, adjustObjectType, aiPlatformScope, complexValue, convertToGeminiTools, copyAIModelParams, copyAIModelParamsInto, copyAndValidateModelParamsInto, ensureAuthOptionScopes, jsonSchemaToGeminiParameters, modelToFamily, modelToPublisher, removeAdditionalProperties, schemaToGeminiParameters, simpleValue, validateModelParams };
|
package/dist/llms.cjs
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
const require_gemini = require(
|
|
2
|
-
const require_common = require(
|
|
3
|
-
const require_failed_handler = require(
|
|
4
|
-
const require_connection = require(
|
|
5
|
-
const require_auth = require(
|
|
6
|
-
const require_chat_models = require(
|
|
1
|
+
const require_gemini = require("./utils/gemini.cjs");
|
|
2
|
+
const require_common = require("./utils/common.cjs");
|
|
3
|
+
const require_failed_handler = require("./utils/failed_handler.cjs");
|
|
4
|
+
const require_connection = require("./connection.cjs");
|
|
5
|
+
const require_auth = require("./auth.cjs");
|
|
6
|
+
const require_chat_models = require("./chat_models.cjs");
|
|
7
7
|
let _langchain_core_utils_env = require("@langchain/core/utils/env");
|
|
8
8
|
let _langchain_core_outputs = require("@langchain/core/outputs");
|
|
9
9
|
let _langchain_core_callbacks_manager = require("@langchain/core/callbacks/manager");
|
|
10
10
|
let _langchain_core_language_models_llms = require("@langchain/core/language_models/llms");
|
|
11
|
-
|
|
12
11
|
//#region src/llms.ts
|
|
13
12
|
var GoogleLLMConnection = class extends require_connection.AbstractGoogleLLMConnection {
|
|
14
13
|
async formatContents(input, _parameters) {
|
|
@@ -21,7 +20,7 @@ var GoogleLLMConnection = class extends require_connection.AbstractGoogleLLMConn
|
|
|
21
20
|
var ProxyChatGoogle = class extends require_chat_models.ChatGoogleBase {
|
|
22
21
|
constructor(fields) {
|
|
23
22
|
super(fields);
|
|
24
|
-
this._addVersion("@langchain/google-common", "2.1.
|
|
23
|
+
this._addVersion("@langchain/google-common", "2.1.26-dev-1773698445534");
|
|
25
24
|
}
|
|
26
25
|
buildAbstractedClient(fields) {
|
|
27
26
|
return fields.connection.client;
|
|
@@ -149,7 +148,7 @@ var GoogleBaseLLM = class extends _langchain_core_language_models_llms.LLM {
|
|
|
149
148
|
return generatedOutput;
|
|
150
149
|
}
|
|
151
150
|
};
|
|
152
|
-
|
|
153
151
|
//#endregion
|
|
154
152
|
exports.GoogleBaseLLM = GoogleBaseLLM;
|
|
153
|
+
|
|
155
154
|
//# sourceMappingURL=llms.cjs.map
|
package/dist/llms.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llms.cjs","names":["AbstractGoogleLLMConnection","ChatGoogleBase","LLM","ensureParams","DefaultGeminiSafetyHandler","ApiKeyGoogleAuth","copyAIModelParams","BaseLLM","CallbackManager","GenerationChunk"],"sources":["../src/llms.ts"],"sourcesContent":["import { CallbackManager, Callbacks } from \"@langchain/core/callbacks/manager\";\nimport { BaseLLM, LLM } from \"@langchain/core/language_models/llms\";\nimport {\n type BaseLanguageModelCallOptions,\n BaseLanguageModelInput,\n} from \"@langchain/core/language_models/base\";\nimport { BaseMessage, MessageContent } from \"@langchain/core/messages\";\nimport { GenerationChunk } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\nimport { AbstractGoogleLLMConnection } from \"./connection.js\";\nimport {\n GoogleAIBaseLLMInput,\n GoogleAIModelParams,\n GoogleAISafetySetting,\n GooglePlatformType,\n GeminiContent,\n GoogleAIResponseMimeType,\n} from \"./types.js\";\nimport {\n copyAIModelParams,\n copyAndValidateModelParamsInto,\n} from \"./utils/common.js\";\nimport { DefaultGeminiSafetyHandler } from \"./utils/gemini.js\";\nimport { ApiKeyGoogleAuth, GoogleAbstractedClient } from \"./auth.js\";\nimport { ensureParams } from \"./utils/failed_handler.js\";\nimport { ChatGoogleBase } from \"./chat_models.js\";\nimport type { GoogleBaseLLMInput, GoogleAISafetyHandler } from \"./types.js\";\n\nexport { GoogleBaseLLMInput };\n\nclass GoogleLLMConnection<AuthOptions> extends AbstractGoogleLLMConnection<\n MessageContent,\n AuthOptions\n> {\n async formatContents(\n input: MessageContent,\n _parameters: GoogleAIModelParams\n ): Promise<GeminiContent[]> {\n const parts = await this.api.messageContentToParts!(input);\n const contents: GeminiContent[] = [\n {\n role: \"user\", // Required by Vertex AI\n parts,\n },\n ];\n return contents;\n }\n}\n\ntype ProxyChatInput<AuthOptions> = GoogleAIBaseLLMInput<AuthOptions> & {\n connection: GoogleLLMConnection<AuthOptions>;\n};\n\nclass ProxyChatGoogle<AuthOptions> extends ChatGoogleBase<AuthOptions> {\n constructor(fields: ProxyChatInput<AuthOptions>) {\n super(fields);\n this._addVersion(\"@langchain/google-common\", __PKG_VERSION__);\n }\n\n buildAbstractedClient(\n fields: ProxyChatInput<AuthOptions>\n ): GoogleAbstractedClient {\n return fields.connection.client;\n }\n}\n\n/**\n * Integration with an LLM.\n */\nexport abstract class GoogleBaseLLM<AuthOptions>\n extends LLM<BaseLanguageModelCallOptions>\n implements GoogleBaseLLMInput<AuthOptions>\n{\n // Used for tracing, replace with the same name as your class\n static lc_name() {\n return \"GoogleLLM\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n authOptions: \"GOOGLE_AUTH_OPTIONS\",\n };\n }\n\n originalFields?: GoogleBaseLLMInput<AuthOptions>;\n\n lc_serializable = true;\n\n modelName = \"gemini-pro\";\n\n model = \"gemini-pro\";\n\n temperature = 0.7;\n\n maxOutputTokens = 1024;\n\n topP = 0.8;\n\n topK = 40;\n\n stopSequences: string[] = [];\n\n safetySettings: GoogleAISafetySetting[] = [];\n\n safetyHandler: GoogleAISafetyHandler;\n\n responseMimeType: GoogleAIResponseMimeType = \"text/plain\";\n\n protected connection: GoogleLLMConnection<AuthOptions>;\n\n protected streamedConnection: GoogleLLMConnection<AuthOptions>;\n\n constructor(fields?: GoogleBaseLLMInput<AuthOptions>) {\n super(ensureParams(fields));\n this.originalFields = fields;\n\n copyAndValidateModelParamsInto(fields, this);\n this.safetyHandler =\n fields?.safetyHandler ?? new DefaultGeminiSafetyHandler();\n\n const client = this.buildClient(fields);\n this.buildConnection(fields ?? {}, client);\n }\n\n abstract buildAbstractedClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient;\n\n buildApiKeyClient(apiKey: string): GoogleAbstractedClient {\n return new ApiKeyGoogleAuth(apiKey);\n }\n\n buildApiKey(fields?: GoogleAIBaseLLMInput<AuthOptions>): string | undefined {\n return fields?.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n }\n\n buildClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient {\n const apiKey = this.buildApiKey(fields);\n if (apiKey) {\n return this.buildApiKeyClient(apiKey);\n } else {\n return this.buildAbstractedClient(fields);\n }\n }\n\n buildConnection(\n fields: GoogleBaseLLMInput<AuthOptions>,\n client: GoogleAbstractedClient\n ) {\n this.connection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n\n this.streamedConnection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n }\n\n get platform(): GooglePlatformType {\n return this.connection.platform;\n }\n\n // Replace\n _llmType() {\n return \"googlellm\";\n }\n\n formatPrompt(prompt: string): MessageContent {\n return prompt;\n }\n\n /**\n * For some given input string and options, return a string output.\n *\n * Despite the fact that `invoke` is overridden below, we still need this\n * in order to handle public APi calls to `generate()`.\n */\n async _call(\n prompt: string,\n options: this[\"ParsedCallOptions\"]\n ): Promise<string> {\n const parameters = copyAIModelParams(this, options);\n const result = await this.connection.request(prompt, parameters, options);\n const ret = this.connection.api.responseToString(result);\n return ret;\n }\n\n // Normally, you should not override this method and instead should override\n // _streamResponseChunks. We are doing so here to allow for multimodal inputs into\n // the LLM.\n async *_streamIterator(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): AsyncGenerator<string> {\n // TODO: Refactor callback setup and teardown code into core\n const prompt = BaseLLM._convertInputToPromptValue(input);\n const [runnableConfig, callOptions] =\n this._separateRunnableConfigFromCallOptions(options);\n const callbackManager_ = await CallbackManager.configure(\n runnableConfig.callbacks,\n this.callbacks,\n runnableConfig.tags,\n this.tags,\n runnableConfig.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n const extra = {\n options: callOptions,\n invocation_params: this?.invocationParams(callOptions),\n batch_size: 1,\n };\n const runManagers = await callbackManager_?.handleLLMStart(\n this.toJSON(),\n [prompt.toString()],\n undefined,\n undefined,\n extra,\n undefined,\n undefined,\n runnableConfig.runName\n );\n let generation = new GenerationChunk({\n text: \"\",\n });\n const proxyChat = this.createProxyChat();\n try {\n for await (const chunk of proxyChat._streamIterator(input, options)) {\n const stringValue = this.connection.api.chunkToString(chunk);\n const generationChunk = new GenerationChunk({\n text: stringValue,\n });\n generation = generation.concat(generationChunk);\n yield stringValue;\n }\n } catch (err) {\n await Promise.all(\n (runManagers ?? []).map((runManager) => runManager?.handleLLMError(err))\n );\n throw err;\n }\n await Promise.all(\n (runManagers ?? []).map((runManager) =>\n runManager?.handleLLMEnd({\n generations: [[generation]],\n })\n )\n );\n }\n\n async predictMessages(\n messages: BaseMessage[],\n options?: string[] | BaseLanguageModelCallOptions,\n _callbacks?: Callbacks\n ): Promise<BaseMessage> {\n const { content } = messages[0];\n const result = await this.connection.request(\n content,\n {},\n options as BaseLanguageModelCallOptions\n );\n const ret = this.connection.api.responseToBaseMessage(result);\n return ret;\n }\n\n /**\n * Internal implementation detail to allow Google LLMs to support\n * multimodal input by delegating to the chat model implementation.\n *\n * TODO: Replace with something less hacky.\n */\n protected createProxyChat(): ChatGoogleBase<AuthOptions> {\n return new ProxyChatGoogle<AuthOptions>({\n ...this.originalFields,\n connection: this.connection,\n });\n }\n\n // TODO: Remove the need to override this - we are doing it to\n // allow the LLM to handle multimodal types of input.\n async invoke(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): Promise<string> {\n const stream = await this._streamIterator(input, options);\n let generatedOutput = \"\";\n for await (const chunk of stream) {\n generatedOutput += chunk;\n }\n return generatedOutput;\n }\n}\n"],"mappings":";;;;;;;;;;;;AA+BA,IAAM,sBAAN,cAA+CA,+CAG7C;CACA,MAAM,eACJ,OACA,aAC0B;AAQ1B,SANkC,CAChC;GACE,MAAM;GACN,OAJU,MAAM,KAAK,IAAI,sBAAuB,MAAM;GAKvD,CACF;;;AASL,IAAM,kBAAN,cAA2CC,mCAA4B;CACrE,YAAY,QAAqC;AAC/C,QAAM,OAAO;AACb,OAAK,YAAY,qCAA4C;;CAG/D,sBACE,QACwB;AACxB,SAAO,OAAO,WAAW;;;;;;AAO7B,IAAsB,gBAAtB,cACUC,yCAEV;CAEE,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,aAAoD;AACtD,SAAO,EACL,aAAa,uBACd;;CAGH;CAEA,kBAAkB;CAElB,YAAY;CAEZ,QAAQ;CAER,cAAc;CAEd,kBAAkB;CAElB,OAAO;CAEP,OAAO;CAEP,gBAA0B,EAAE;CAE5B,iBAA0C,EAAE;CAE5C;CAEA,mBAA6C;CAE7C,AAAU;CAEV,AAAU;CAEV,YAAY,QAA0C;AACpD,QAAMC,oCAAa,OAAO,CAAC;AAC3B,OAAK,iBAAiB;AAEtB,gDAA+B,QAAQ,KAAK;AAC5C,OAAK,gBACH,QAAQ,iBAAiB,IAAIC,2CAA4B;EAE3D,MAAM,SAAS,KAAK,YAAY,OAAO;AACvC,OAAK,gBAAgB,UAAU,EAAE,EAAE,OAAO;;C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CAAgB,EACnC,MAAM,IACP,CAAC;EACF,MAAM,YAAY,KAAK,iBAAiB;AACxC,MAAI;AACF,cAAW,MAAM,SAAS,UAAU,gBAAgB,OAAO,QAAQ,EAAE;IACnE,MAAM,cAAc,KAAK,WAAW,IAAI,cAAc,MAAM;IAC5D,MAAM,kBAAkB,IAAIA,wCAAgB,EAC1C,MAAM,aACP,CAAC;AACF,iBAAa,WAAW,OAAO,gBAAgB;AAC/C,UAAM;;WAED,KAAK;AACZ,SAAM,QAAQ,KACX,eAAe,EAAE,EAAE,KAAK,eAAe,YAAY,eAAe,IAAI,CAAC,CACzE;AACD,SAAM;;AAER,QAAM,QAAQ,KACX,eAAe,EAAE,EAAE,KAAK,eACvB,YAAY,aAAa,EACvB,aAAa,CAAC,CAAC,WAAW,CAAC,EAC5B,CAAC,CACH,CACF;;CAGH,MAAM,gBACJ,UACA,SACA,YACsB;EACtB,MAAM,EAAE,YAAY,SAAS;EAC7B,MAAM,SAAS,MAAM,KAAK,WAAW,QACnC,SACA,EAAE,EACF,QACD;AAED,SADY,KAAK,WAAW,IAAI,sBAAsB,OAAO;;;;;;;;CAU/D,AAAU,kBAA+C;AACvD,SAAO,IAAI,gBAA6B;GACtC,GAAG,KAAK;GACR,YAAY,KAAK;GAClB,CAAC;;CAKJ,MAAM,OACJ,OACA,SACiB;EACjB,MAAM,SAAS,MAAM,KAAK,gBAAgB,OAAO,QAAQ;EACzD,IAAI,kBAAkB;AACtB,aAAW,MAAM,SAAS,OACxB,oBAAmB;AAErB,SAAO"}
|
|
1
|
+
{"version":3,"file":"llms.cjs","names":["AbstractGoogleLLMConnection","ChatGoogleBase","LLM","ensureParams","DefaultGeminiSafetyHandler","ApiKeyGoogleAuth","copyAIModelParams","BaseLLM","CallbackManager","GenerationChunk"],"sources":["../src/llms.ts"],"sourcesContent":["import { CallbackManager, Callbacks } from \"@langchain/core/callbacks/manager\";\nimport { BaseLLM, LLM } from \"@langchain/core/language_models/llms\";\nimport {\n type BaseLanguageModelCallOptions,\n BaseLanguageModelInput,\n} from \"@langchain/core/language_models/base\";\nimport { BaseMessage, MessageContent } from \"@langchain/core/messages\";\nimport { GenerationChunk } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\nimport { AbstractGoogleLLMConnection } from \"./connection.js\";\nimport {\n GoogleAIBaseLLMInput,\n GoogleAIModelParams,\n GoogleAISafetySetting,\n GooglePlatformType,\n GeminiContent,\n GoogleAIResponseMimeType,\n} from \"./types.js\";\nimport {\n copyAIModelParams,\n copyAndValidateModelParamsInto,\n} from \"./utils/common.js\";\nimport { DefaultGeminiSafetyHandler } from \"./utils/gemini.js\";\nimport { ApiKeyGoogleAuth, GoogleAbstractedClient } from \"./auth.js\";\nimport { ensureParams } from \"./utils/failed_handler.js\";\nimport { ChatGoogleBase } from \"./chat_models.js\";\nimport type { GoogleBaseLLMInput, GoogleAISafetyHandler } from \"./types.js\";\n\nexport { GoogleBaseLLMInput };\n\nclass GoogleLLMConnection<AuthOptions> extends AbstractGoogleLLMConnection<\n MessageContent,\n AuthOptions\n> {\n async formatContents(\n input: MessageContent,\n _parameters: GoogleAIModelParams\n ): Promise<GeminiContent[]> {\n const parts = await this.api.messageContentToParts!(input);\n const contents: GeminiContent[] = [\n {\n role: \"user\", // Required by Vertex AI\n parts,\n },\n ];\n return contents;\n }\n}\n\ntype ProxyChatInput<AuthOptions> = GoogleAIBaseLLMInput<AuthOptions> & {\n connection: GoogleLLMConnection<AuthOptions>;\n};\n\nclass ProxyChatGoogle<AuthOptions> extends ChatGoogleBase<AuthOptions> {\n constructor(fields: ProxyChatInput<AuthOptions>) {\n super(fields);\n this._addVersion(\"@langchain/google-common\", __PKG_VERSION__);\n }\n\n buildAbstractedClient(\n fields: ProxyChatInput<AuthOptions>\n ): GoogleAbstractedClient {\n return fields.connection.client;\n }\n}\n\n/**\n * Integration with an LLM.\n */\nexport abstract class GoogleBaseLLM<AuthOptions>\n extends LLM<BaseLanguageModelCallOptions>\n implements GoogleBaseLLMInput<AuthOptions>\n{\n // Used for tracing, replace with the same name as your class\n static lc_name() {\n return \"GoogleLLM\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n authOptions: \"GOOGLE_AUTH_OPTIONS\",\n };\n }\n\n originalFields?: GoogleBaseLLMInput<AuthOptions>;\n\n lc_serializable = true;\n\n modelName = \"gemini-pro\";\n\n model = \"gemini-pro\";\n\n temperature = 0.7;\n\n maxOutputTokens = 1024;\n\n topP = 0.8;\n\n topK = 40;\n\n stopSequences: string[] = [];\n\n safetySettings: GoogleAISafetySetting[] = [];\n\n safetyHandler: GoogleAISafetyHandler;\n\n responseMimeType: GoogleAIResponseMimeType = \"text/plain\";\n\n protected connection: GoogleLLMConnection<AuthOptions>;\n\n protected streamedConnection: GoogleLLMConnection<AuthOptions>;\n\n constructor(fields?: GoogleBaseLLMInput<AuthOptions>) {\n super(ensureParams(fields));\n this.originalFields = fields;\n\n copyAndValidateModelParamsInto(fields, this);\n this.safetyHandler =\n fields?.safetyHandler ?? new DefaultGeminiSafetyHandler();\n\n const client = this.buildClient(fields);\n this.buildConnection(fields ?? {}, client);\n }\n\n abstract buildAbstractedClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient;\n\n buildApiKeyClient(apiKey: string): GoogleAbstractedClient {\n return new ApiKeyGoogleAuth(apiKey);\n }\n\n buildApiKey(fields?: GoogleAIBaseLLMInput<AuthOptions>): string | undefined {\n return fields?.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n }\n\n buildClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient {\n const apiKey = this.buildApiKey(fields);\n if (apiKey) {\n return this.buildApiKeyClient(apiKey);\n } else {\n return this.buildAbstractedClient(fields);\n }\n }\n\n buildConnection(\n fields: GoogleBaseLLMInput<AuthOptions>,\n client: GoogleAbstractedClient\n ) {\n this.connection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n\n this.streamedConnection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n }\n\n get platform(): GooglePlatformType {\n return this.connection.platform;\n }\n\n // Replace\n _llmType() {\n return \"googlellm\";\n }\n\n formatPrompt(prompt: string): MessageContent {\n return prompt;\n }\n\n /**\n * For some given input string and options, return a string output.\n *\n * Despite the fact that `invoke` is overridden below, we still need this\n * in order to handle public APi calls to `generate()`.\n */\n async _call(\n prompt: string,\n options: this[\"ParsedCallOptions\"]\n ): Promise<string> {\n const parameters = copyAIModelParams(this, options);\n const result = await this.connection.request(prompt, parameters, options);\n const ret = this.connection.api.responseToString(result);\n return ret;\n }\n\n // Normally, you should not override this method and instead should override\n // _streamResponseChunks. We are doing so here to allow for multimodal inputs into\n // the LLM.\n async *_streamIterator(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): AsyncGenerator<string> {\n // TODO: Refactor callback setup and teardown code into core\n const prompt = BaseLLM._convertInputToPromptValue(input);\n const [runnableConfig, callOptions] =\n this._separateRunnableConfigFromCallOptions(options);\n const callbackManager_ = await CallbackManager.configure(\n runnableConfig.callbacks,\n this.callbacks,\n runnableConfig.tags,\n this.tags,\n runnableConfig.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n const extra = {\n options: callOptions,\n invocation_params: this?.invocationParams(callOptions),\n batch_size: 1,\n };\n const runManagers = await callbackManager_?.handleLLMStart(\n this.toJSON(),\n [prompt.toString()],\n undefined,\n undefined,\n extra,\n undefined,\n undefined,\n runnableConfig.runName\n );\n let generation = new GenerationChunk({\n text: \"\",\n });\n const proxyChat = this.createProxyChat();\n try {\n for await (const chunk of proxyChat._streamIterator(input, options)) {\n const stringValue = this.connection.api.chunkToString(chunk);\n const generationChunk = new GenerationChunk({\n text: stringValue,\n });\n generation = generation.concat(generationChunk);\n yield stringValue;\n }\n } catch (err) {\n await Promise.all(\n (runManagers ?? []).map((runManager) => runManager?.handleLLMError(err))\n );\n throw err;\n }\n await Promise.all(\n (runManagers ?? []).map((runManager) =>\n runManager?.handleLLMEnd({\n generations: [[generation]],\n })\n )\n );\n }\n\n async predictMessages(\n messages: BaseMessage[],\n options?: string[] | BaseLanguageModelCallOptions,\n _callbacks?: Callbacks\n ): Promise<BaseMessage> {\n const { content } = messages[0];\n const result = await this.connection.request(\n content,\n {},\n options as BaseLanguageModelCallOptions\n );\n const ret = this.connection.api.responseToBaseMessage(result);\n return ret;\n }\n\n /**\n * Internal implementation detail to allow Google LLMs to support\n * multimodal input by delegating to the chat model implementation.\n *\n * TODO: Replace with something less hacky.\n */\n protected createProxyChat(): ChatGoogleBase<AuthOptions> {\n return new ProxyChatGoogle<AuthOptions>({\n ...this.originalFields,\n connection: this.connection,\n });\n }\n\n // TODO: Remove the need to override this - we are doing it to\n // allow the LLM to handle multimodal types of input.\n async invoke(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): Promise<string> {\n const stream = await this._streamIterator(input, options);\n let generatedOutput = \"\";\n for await (const chunk of stream) {\n generatedOutput += chunk;\n }\n return generatedOutput;\n }\n}\n"],"mappings":";;;;;;;;;;;AA+BA,IAAM,sBAAN,cAA+CA,mBAAAA,4BAG7C;CACA,MAAM,eACJ,OACA,aAC0B;AAQ1B,SANkC,CAChC;GACE,MAAM;GACN,OAJU,MAAM,KAAK,IAAI,sBAAuB,MAAM;GAKvD,CACF;;;AASL,IAAM,kBAAN,cAA2CC,oBAAAA,eAA4B;CACrE,YAAY,QAAqC;AAC/C,QAAM,OAAO;AACb,OAAK,YAAY,4BAAA,2BAA4C;;CAG/D,sBACE,QACwB;AACxB,SAAO,OAAO,WAAW;;;;;;AAO7B,IAAsB,gBAAtB,cACUC,qCAAAA,IAEV;CAEE,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,aAAoD;AACtD,SAAO,EACL,aAAa,uBACd;;CAGH;CAEA,kBAAkB;CAElB,YAAY;CAEZ,QAAQ;CAER,cAAc;CAEd,kBAAkB;CAEl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;;CAKJ,MAAM,OACJ,OACA,SACiB;EACjB,MAAM,SAAS,MAAM,KAAK,gBAAgB,OAAO,QAAQ;EACzD,IAAI,kBAAkB;AACtB,aAAW,MAAM,SAAS,OACxB,oBAAmB;AAErB,SAAO"}
|
package/dist/llms.js
CHANGED
|
@@ -8,7 +8,6 @@ import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
|
8
8
|
import { GenerationChunk } from "@langchain/core/outputs";
|
|
9
9
|
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
10
10
|
import { BaseLLM, LLM } from "@langchain/core/language_models/llms";
|
|
11
|
-
|
|
12
11
|
//#region src/llms.ts
|
|
13
12
|
var GoogleLLMConnection = class extends AbstractGoogleLLMConnection {
|
|
14
13
|
async formatContents(input, _parameters) {
|
|
@@ -21,7 +20,7 @@ var GoogleLLMConnection = class extends AbstractGoogleLLMConnection {
|
|
|
21
20
|
var ProxyChatGoogle = class extends ChatGoogleBase {
|
|
22
21
|
constructor(fields) {
|
|
23
22
|
super(fields);
|
|
24
|
-
this._addVersion("@langchain/google-common", "2.1.
|
|
23
|
+
this._addVersion("@langchain/google-common", "2.1.26-dev-1773698445534");
|
|
25
24
|
}
|
|
26
25
|
buildAbstractedClient(fields) {
|
|
27
26
|
return fields.connection.client;
|
|
@@ -149,7 +148,7 @@ var GoogleBaseLLM = class extends LLM {
|
|
|
149
148
|
return generatedOutput;
|
|
150
149
|
}
|
|
151
150
|
};
|
|
152
|
-
|
|
153
151
|
//#endregion
|
|
154
152
|
export { GoogleBaseLLM };
|
|
153
|
+
|
|
155
154
|
//# sourceMappingURL=llms.js.map
|
package/dist/llms.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llms.js","names":[],"sources":["../src/llms.ts"],"sourcesContent":["import { CallbackManager, Callbacks } from \"@langchain/core/callbacks/manager\";\nimport { BaseLLM, LLM } from \"@langchain/core/language_models/llms\";\nimport {\n type BaseLanguageModelCallOptions,\n BaseLanguageModelInput,\n} from \"@langchain/core/language_models/base\";\nimport { BaseMessage, MessageContent } from \"@langchain/core/messages\";\nimport { GenerationChunk } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\nimport { AbstractGoogleLLMConnection } from \"./connection.js\";\nimport {\n GoogleAIBaseLLMInput,\n GoogleAIModelParams,\n GoogleAISafetySetting,\n GooglePlatformType,\n GeminiContent,\n GoogleAIResponseMimeType,\n} from \"./types.js\";\nimport {\n copyAIModelParams,\n copyAndValidateModelParamsInto,\n} from \"./utils/common.js\";\nimport { DefaultGeminiSafetyHandler } from \"./utils/gemini.js\";\nimport { ApiKeyGoogleAuth, GoogleAbstractedClient } from \"./auth.js\";\nimport { ensureParams } from \"./utils/failed_handler.js\";\nimport { ChatGoogleBase } from \"./chat_models.js\";\nimport type { GoogleBaseLLMInput, GoogleAISafetyHandler } from \"./types.js\";\n\nexport { GoogleBaseLLMInput };\n\nclass GoogleLLMConnection<AuthOptions> extends AbstractGoogleLLMConnection<\n MessageContent,\n AuthOptions\n> {\n async formatContents(\n input: MessageContent,\n _parameters: GoogleAIModelParams\n ): Promise<GeminiContent[]> {\n const parts = await this.api.messageContentToParts!(input);\n const contents: GeminiContent[] = [\n {\n role: \"user\", // Required by Vertex AI\n parts,\n },\n ];\n return contents;\n }\n}\n\ntype ProxyChatInput<AuthOptions> = GoogleAIBaseLLMInput<AuthOptions> & {\n connection: GoogleLLMConnection<AuthOptions>;\n};\n\nclass ProxyChatGoogle<AuthOptions> extends ChatGoogleBase<AuthOptions> {\n constructor(fields: ProxyChatInput<AuthOptions>) {\n super(fields);\n this._addVersion(\"@langchain/google-common\", __PKG_VERSION__);\n }\n\n buildAbstractedClient(\n fields: ProxyChatInput<AuthOptions>\n ): GoogleAbstractedClient {\n return fields.connection.client;\n }\n}\n\n/**\n * Integration with an LLM.\n */\nexport abstract class GoogleBaseLLM<AuthOptions>\n extends LLM<BaseLanguageModelCallOptions>\n implements GoogleBaseLLMInput<AuthOptions>\n{\n // Used for tracing, replace with the same name as your class\n static lc_name() {\n return \"GoogleLLM\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n authOptions: \"GOOGLE_AUTH_OPTIONS\",\n };\n }\n\n originalFields?: GoogleBaseLLMInput<AuthOptions>;\n\n lc_serializable = true;\n\n modelName = \"gemini-pro\";\n\n model = \"gemini-pro\";\n\n temperature = 0.7;\n\n maxOutputTokens = 1024;\n\n topP = 0.8;\n\n topK = 40;\n\n stopSequences: string[] = [];\n\n safetySettings: GoogleAISafetySetting[] = [];\n\n safetyHandler: GoogleAISafetyHandler;\n\n responseMimeType: GoogleAIResponseMimeType = \"text/plain\";\n\n protected connection: GoogleLLMConnection<AuthOptions>;\n\n protected streamedConnection: GoogleLLMConnection<AuthOptions>;\n\n constructor(fields?: GoogleBaseLLMInput<AuthOptions>) {\n super(ensureParams(fields));\n this.originalFields = fields;\n\n copyAndValidateModelParamsInto(fields, this);\n this.safetyHandler =\n fields?.safetyHandler ?? new DefaultGeminiSafetyHandler();\n\n const client = this.buildClient(fields);\n this.buildConnection(fields ?? {}, client);\n }\n\n abstract buildAbstractedClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient;\n\n buildApiKeyClient(apiKey: string): GoogleAbstractedClient {\n return new ApiKeyGoogleAuth(apiKey);\n }\n\n buildApiKey(fields?: GoogleAIBaseLLMInput<AuthOptions>): string | undefined {\n return fields?.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n }\n\n buildClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient {\n const apiKey = this.buildApiKey(fields);\n if (apiKey) {\n return this.buildApiKeyClient(apiKey);\n } else {\n return this.buildAbstractedClient(fields);\n }\n }\n\n buildConnection(\n fields: GoogleBaseLLMInput<AuthOptions>,\n client: GoogleAbstractedClient\n ) {\n this.connection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n\n this.streamedConnection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n }\n\n get platform(): GooglePlatformType {\n return this.connection.platform;\n }\n\n // Replace\n _llmType() {\n return \"googlellm\";\n }\n\n formatPrompt(prompt: string): MessageContent {\n return prompt;\n }\n\n /**\n * For some given input string and options, return a string output.\n *\n * Despite the fact that `invoke` is overridden below, we still need this\n * in order to handle public APi calls to `generate()`.\n */\n async _call(\n prompt: string,\n options: this[\"ParsedCallOptions\"]\n ): Promise<string> {\n const parameters = copyAIModelParams(this, options);\n const result = await this.connection.request(prompt, parameters, options);\n const ret = this.connection.api.responseToString(result);\n return ret;\n }\n\n // Normally, you should not override this method and instead should override\n // _streamResponseChunks. We are doing so here to allow for multimodal inputs into\n // the LLM.\n async *_streamIterator(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): AsyncGenerator<string> {\n // TODO: Refactor callback setup and teardown code into core\n const prompt = BaseLLM._convertInputToPromptValue(input);\n const [runnableConfig, callOptions] =\n this._separateRunnableConfigFromCallOptions(options);\n const callbackManager_ = await CallbackManager.configure(\n runnableConfig.callbacks,\n this.callbacks,\n runnableConfig.tags,\n this.tags,\n runnableConfig.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n const extra = {\n options: callOptions,\n invocation_params: this?.invocationParams(callOptions),\n batch_size: 1,\n };\n const runManagers = await callbackManager_?.handleLLMStart(\n this.toJSON(),\n [prompt.toString()],\n undefined,\n undefined,\n extra,\n undefined,\n undefined,\n runnableConfig.runName\n );\n let generation = new GenerationChunk({\n text: \"\",\n });\n const proxyChat = this.createProxyChat();\n try {\n for await (const chunk of proxyChat._streamIterator(input, options)) {\n const stringValue = this.connection.api.chunkToString(chunk);\n const generationChunk = new GenerationChunk({\n text: stringValue,\n });\n generation = generation.concat(generationChunk);\n yield stringValue;\n }\n } catch (err) {\n await Promise.all(\n (runManagers ?? []).map((runManager) => runManager?.handleLLMError(err))\n );\n throw err;\n }\n await Promise.all(\n (runManagers ?? []).map((runManager) =>\n runManager?.handleLLMEnd({\n generations: [[generation]],\n })\n )\n );\n }\n\n async predictMessages(\n messages: BaseMessage[],\n options?: string[] | BaseLanguageModelCallOptions,\n _callbacks?: Callbacks\n ): Promise<BaseMessage> {\n const { content } = messages[0];\n const result = await this.connection.request(\n content,\n {},\n options as BaseLanguageModelCallOptions\n );\n const ret = this.connection.api.responseToBaseMessage(result);\n return ret;\n }\n\n /**\n * Internal implementation detail to allow Google LLMs to support\n * multimodal input by delegating to the chat model implementation.\n *\n * TODO: Replace with something less hacky.\n */\n protected createProxyChat(): ChatGoogleBase<AuthOptions> {\n return new ProxyChatGoogle<AuthOptions>({\n ...this.originalFields,\n connection: this.connection,\n });\n }\n\n // TODO: Remove the need to override this - we are doing it to\n // allow the LLM to handle multimodal types of input.\n async invoke(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): Promise<string> {\n const stream = await this._streamIterator(input, options);\n let generatedOutput = \"\";\n for await (const chunk of stream) {\n generatedOutput += chunk;\n }\n return generatedOutput;\n }\n}\n"],"mappings":";;;;;;;;;;;;AA+BA,IAAM,sBAAN,cAA+C,4BAG7C;CACA,MAAM,eACJ,OACA,aAC0B;AAQ1B,SANkC,CAChC;GACE,MAAM;GACN,OAJU,MAAM,KAAK,IAAI,sBAAuB,MAAM;GAKvD,CACF;;;AASL,IAAM,kBAAN,cAA2C,eAA4B;CACrE,YAAY,QAAqC;AAC/C,QAAM,OAAO;AACb,OAAK,YAAY,qCAA4C;;CAG/D,sBACE,QACwB;AACxB,SAAO,OAAO,WAAW;;;;;;AAO7B,IAAsB,gBAAtB,cACU,IAEV;CAEE,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,aAAoD;AACtD,SAAO,EACL,aAAa,uBACd;;CAGH;CAEA,kBAAkB;CAElB,YAAY;CAEZ,QAAQ;CAER,cAAc;CAEd,kBAAkB;CAElB,OAAO;CAEP,OAAO;CAEP,gBAA0B,EAAE;CAE5B,iBAA0C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"}
|
|
1
|
+
{"version":3,"file":"llms.js","names":[],"sources":["../src/llms.ts"],"sourcesContent":["import { CallbackManager, Callbacks } from \"@langchain/core/callbacks/manager\";\nimport { BaseLLM, LLM } from \"@langchain/core/language_models/llms\";\nimport {\n type BaseLanguageModelCallOptions,\n BaseLanguageModelInput,\n} from \"@langchain/core/language_models/base\";\nimport { BaseMessage, MessageContent } from \"@langchain/core/messages\";\nimport { GenerationChunk } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\nimport { AbstractGoogleLLMConnection } from \"./connection.js\";\nimport {\n GoogleAIBaseLLMInput,\n GoogleAIModelParams,\n GoogleAISafetySetting,\n GooglePlatformType,\n GeminiContent,\n GoogleAIResponseMimeType,\n} from \"./types.js\";\nimport {\n copyAIModelParams,\n copyAndValidateModelParamsInto,\n} from \"./utils/common.js\";\nimport { DefaultGeminiSafetyHandler } from \"./utils/gemini.js\";\nimport { ApiKeyGoogleAuth, GoogleAbstractedClient } from \"./auth.js\";\nimport { ensureParams } from \"./utils/failed_handler.js\";\nimport { ChatGoogleBase } from \"./chat_models.js\";\nimport type { GoogleBaseLLMInput, GoogleAISafetyHandler } from \"./types.js\";\n\nexport { GoogleBaseLLMInput };\n\nclass GoogleLLMConnection<AuthOptions> extends AbstractGoogleLLMConnection<\n MessageContent,\n AuthOptions\n> {\n async formatContents(\n input: MessageContent,\n _parameters: GoogleAIModelParams\n ): Promise<GeminiContent[]> {\n const parts = await this.api.messageContentToParts!(input);\n const contents: GeminiContent[] = [\n {\n role: \"user\", // Required by Vertex AI\n parts,\n },\n ];\n return contents;\n }\n}\n\ntype ProxyChatInput<AuthOptions> = GoogleAIBaseLLMInput<AuthOptions> & {\n connection: GoogleLLMConnection<AuthOptions>;\n};\n\nclass ProxyChatGoogle<AuthOptions> extends ChatGoogleBase<AuthOptions> {\n constructor(fields: ProxyChatInput<AuthOptions>) {\n super(fields);\n this._addVersion(\"@langchain/google-common\", __PKG_VERSION__);\n }\n\n buildAbstractedClient(\n fields: ProxyChatInput<AuthOptions>\n ): GoogleAbstractedClient {\n return fields.connection.client;\n }\n}\n\n/**\n * Integration with an LLM.\n */\nexport abstract class GoogleBaseLLM<AuthOptions>\n extends LLM<BaseLanguageModelCallOptions>\n implements GoogleBaseLLMInput<AuthOptions>\n{\n // Used for tracing, replace with the same name as your class\n static lc_name() {\n return \"GoogleLLM\";\n }\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n authOptions: \"GOOGLE_AUTH_OPTIONS\",\n };\n }\n\n originalFields?: GoogleBaseLLMInput<AuthOptions>;\n\n lc_serializable = true;\n\n modelName = \"gemini-pro\";\n\n model = \"gemini-pro\";\n\n temperature = 0.7;\n\n maxOutputTokens = 1024;\n\n topP = 0.8;\n\n topK = 40;\n\n stopSequences: string[] = [];\n\n safetySettings: GoogleAISafetySetting[] = [];\n\n safetyHandler: GoogleAISafetyHandler;\n\n responseMimeType: GoogleAIResponseMimeType = \"text/plain\";\n\n protected connection: GoogleLLMConnection<AuthOptions>;\n\n protected streamedConnection: GoogleLLMConnection<AuthOptions>;\n\n constructor(fields?: GoogleBaseLLMInput<AuthOptions>) {\n super(ensureParams(fields));\n this.originalFields = fields;\n\n copyAndValidateModelParamsInto(fields, this);\n this.safetyHandler =\n fields?.safetyHandler ?? new DefaultGeminiSafetyHandler();\n\n const client = this.buildClient(fields);\n this.buildConnection(fields ?? {}, client);\n }\n\n abstract buildAbstractedClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient;\n\n buildApiKeyClient(apiKey: string): GoogleAbstractedClient {\n return new ApiKeyGoogleAuth(apiKey);\n }\n\n buildApiKey(fields?: GoogleAIBaseLLMInput<AuthOptions>): string | undefined {\n return fields?.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n }\n\n buildClient(\n fields?: GoogleAIBaseLLMInput<AuthOptions>\n ): GoogleAbstractedClient {\n const apiKey = this.buildApiKey(fields);\n if (apiKey) {\n return this.buildApiKeyClient(apiKey);\n } else {\n return this.buildAbstractedClient(fields);\n }\n }\n\n buildConnection(\n fields: GoogleBaseLLMInput<AuthOptions>,\n client: GoogleAbstractedClient\n ) {\n this.connection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n\n this.streamedConnection = new GoogleLLMConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n }\n\n get platform(): GooglePlatformType {\n return this.connection.platform;\n }\n\n // Replace\n _llmType() {\n return \"googlellm\";\n }\n\n formatPrompt(prompt: string): MessageContent {\n return prompt;\n }\n\n /**\n * For some given input string and options, return a string output.\n *\n * Despite the fact that `invoke` is overridden below, we still need this\n * in order to handle public APi calls to `generate()`.\n */\n async _call(\n prompt: string,\n options: this[\"ParsedCallOptions\"]\n ): Promise<string> {\n const parameters = copyAIModelParams(this, options);\n const result = await this.connection.request(prompt, parameters, options);\n const ret = this.connection.api.responseToString(result);\n return ret;\n }\n\n // Normally, you should not override this method and instead should override\n // _streamResponseChunks. We are doing so here to allow for multimodal inputs into\n // the LLM.\n async *_streamIterator(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): AsyncGenerator<string> {\n // TODO: Refactor callback setup and teardown code into core\n const prompt = BaseLLM._convertInputToPromptValue(input);\n const [runnableConfig, callOptions] =\n this._separateRunnableConfigFromCallOptions(options);\n const callbackManager_ = await CallbackManager.configure(\n runnableConfig.callbacks,\n this.callbacks,\n runnableConfig.tags,\n this.tags,\n runnableConfig.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n const extra = {\n options: callOptions,\n invocation_params: this?.invocationParams(callOptions),\n batch_size: 1,\n };\n const runManagers = await callbackManager_?.handleLLMStart(\n this.toJSON(),\n [prompt.toString()],\n undefined,\n undefined,\n extra,\n undefined,\n undefined,\n runnableConfig.runName\n );\n let generation = new GenerationChunk({\n text: \"\",\n });\n const proxyChat = this.createProxyChat();\n try {\n for await (const chunk of proxyChat._streamIterator(input, options)) {\n const stringValue = this.connection.api.chunkToString(chunk);\n const generationChunk = new GenerationChunk({\n text: stringValue,\n });\n generation = generation.concat(generationChunk);\n yield stringValue;\n }\n } catch (err) {\n await Promise.all(\n (runManagers ?? []).map((runManager) => runManager?.handleLLMError(err))\n );\n throw err;\n }\n await Promise.all(\n (runManagers ?? []).map((runManager) =>\n runManager?.handleLLMEnd({\n generations: [[generation]],\n })\n )\n );\n }\n\n async predictMessages(\n messages: BaseMessage[],\n options?: string[] | BaseLanguageModelCallOptions,\n _callbacks?: Callbacks\n ): Promise<BaseMessage> {\n const { content } = messages[0];\n const result = await this.connection.request(\n content,\n {},\n options as BaseLanguageModelCallOptions\n );\n const ret = this.connection.api.responseToBaseMessage(result);\n return ret;\n }\n\n /**\n * Internal implementation detail to allow Google LLMs to support\n * multimodal input by delegating to the chat model implementation.\n *\n * TODO: Replace with something less hacky.\n */\n protected createProxyChat(): ChatGoogleBase<AuthOptions> {\n return new ProxyChatGoogle<AuthOptions>({\n ...this.originalFields,\n connection: this.connection,\n });\n }\n\n // TODO: Remove the need to override this - we are doing it to\n // allow the LLM to handle multimodal types of input.\n async invoke(\n input: BaseLanguageModelInput,\n options?: BaseLanguageModelCallOptions\n ): Promise<string> {\n const stream = await this._streamIterator(input, options);\n let generatedOutput = \"\";\n for await (const chunk of stream) {\n generatedOutput += chunk;\n }\n return generatedOutput;\n }\n}\n"],"mappings":";;;;;;;;;;;AA+BA,IAAM,sBAAN,cAA+C,4BAG7C;CACA,MAAM,eACJ,OACA,aAC0B;AAQ1B,SANkC,CAChC;GACE,MAAM;GACN,OAJU,MAAM,KAAK,IAAI,sBAAuB,MAAM;GAKvD,CACF;;;AASL,IAAM,kBAAN,cAA2C,eAA4B;CACrE,YAAY,QAAqC;AAC/C,QAAM,OAAO;AACb,OAAK,YAAY,4BAAA,2BAA4C;;CAG/D,sBACE,QACwB;AACxB,SAAO,OAAO,WAAW;;;;;;AAO7B,IAAsB,gBAAtB,cACU,IAEV;CAEE,OAAO,UAA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;GACtC,GAAG,KAAK;GACR,YAAY,KAAK;GAClB,CAAC;;CAKJ,MAAM,OACJ,OACA,SACiB;EACjB,MAAM,SAAS,MAAM,KAAK,gBAAgB,OAAO,QAAQ;EACzD,IAAI,kBAAkB;AACtB,aAAW,MAAM,SAAS,OACxB,oBAAmB;AAErB,SAAO"}
|
package/dist/output_parsers.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
let _langchain_core_output_parsers = require("@langchain/core/output_parsers");
|
|
2
|
-
|
|
3
2
|
//#region src/output_parsers.ts
|
|
4
3
|
var BaseGoogleSearchOutputParser = class extends _langchain_core_output_parsers.BaseLLMOutputParser {
|
|
5
4
|
lc_namespace = ["google_common", "output_parsers"];
|
|
@@ -125,9 +124,9 @@ var MarkdownGoogleSearchOutputParser = class extends BaseGoogleSearchOutputParse
|
|
|
125
124
|
return ret;
|
|
126
125
|
}
|
|
127
126
|
};
|
|
128
|
-
|
|
129
127
|
//#endregion
|
|
130
128
|
exports.BaseGoogleSearchOutputParser = BaseGoogleSearchOutputParser;
|
|
131
129
|
exports.MarkdownGoogleSearchOutputParser = MarkdownGoogleSearchOutputParser;
|
|
132
130
|
exports.SimpleGoogleSearchOutputParser = SimpleGoogleSearchOutputParser;
|
|
131
|
+
|
|
133
132
|
//# sourceMappingURL=output_parsers.cjs.map
|