UNPKG

@genkit-ai/ai

Version:

Genkit AI framework generative AI APIs.

578 lines (572 loc) 21.5 kB
import { z, Action, ActionFnArg, ActionMetadata } from '@genkit-ai/core'; import { Registry } from '@genkit-ai/core/registry'; import { Part } from './parts.js'; /** * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * A batch (array) of embeddings. */ type EmbeddingBatch = { embedding: number[]; }[]; /** * EmbeddingSchema includes the embedding and also metadata so you know * which of multiple embeddings corresponds to which part of a document. */ declare const EmbeddingSchema: z.ZodObject<{ embedding: z.ZodArray<z.ZodNumber, "many">; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; }, "strip", z.ZodTypeAny, { embedding: number[]; metadata?: Record<string, unknown> | undefined; }, { embedding: number[]; metadata?: Record<string, unknown> | undefined; }>; type Embedding = z.infer<typeof EmbeddingSchema>; /** * A function used for embedder definition, encapsulates embedder implementation. */ type EmbedderFn<EmbedderOptions extends z.ZodTypeAny> = (input: Document[], embedderOpts?: z.infer<EmbedderOptions>) => Promise<EmbedResponse>; /** * Zod schema of an embed request. */ declare const EmbedRequestSchema: z.ZodObject<{ input: z.ZodArray<z.ZodObject<{ content: z.ZodArray<z.ZodUnion<[z.ZodObject<{ media: z.ZodOptional<z.ZodNever>; toolRequest: z.ZodOptional<z.ZodNever>; toolResponse: z.ZodOptional<z.ZodNever>; data: z.ZodOptional<z.ZodUnknown>; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; custom: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; reasoning: z.ZodOptional<z.ZodNever>; resource: z.ZodOptional<z.ZodNever>; } & { text: z.ZodString; }, "strip", z.ZodTypeAny, { text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }, { text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }>, z.ZodObject<{ text: z.ZodOptional<z.ZodNever>; toolRequest: z.ZodOptional<z.ZodNever>; toolResponse: z.ZodOptional<z.ZodNever>; data: z.ZodOptional<z.ZodUnknown>; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; custom: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; reasoning: z.ZodOptional<z.ZodNever>; resource: z.ZodOptional<z.ZodNever>; } & { media: z.ZodObject<{ contentType: z.ZodOptional<z.ZodString>; url: z.ZodString; }, "strip", z.ZodTypeAny, { url: string; contentType?: string | undefined; }, { url: string; contentType?: string | undefined; }>; }, "strip", z.ZodTypeAny, { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }, { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }>]>, "many">; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>; }, "strip", z.ZodTypeAny, { content: ({ text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; } | { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; })[]; metadata?: Record<string, any> | undefined; }, { content: ({ text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; } | { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; })[]; metadata?: Record<string, any> | undefined; }>, "many">; options: z.ZodOptional<z.ZodAny>; }, "strip", z.ZodTypeAny, { input: { content: ({ text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; } | { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; })[]; metadata?: Record<string, any> | undefined; }[]; options?: any; }, { input: { content: ({ text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; } | { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; })[]; metadata?: Record<string, any> | undefined; }[]; options?: any; }>; interface EmbedRequest<O = any> { input: Document[]; options?: O; } /** * Zod schema of an embed response. */ declare const EmbedResponseSchema: z.ZodObject<{ embeddings: z.ZodArray<z.ZodObject<{ embedding: z.ZodArray<z.ZodNumber, "many">; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; }, "strip", z.ZodTypeAny, { embedding: number[]; metadata?: Record<string, unknown> | undefined; }, { embedding: number[]; metadata?: Record<string, unknown> | undefined; }>, "many">; }, "strip", z.ZodTypeAny, { embeddings: { embedding: number[]; metadata?: Record<string, unknown> | undefined; }[]; }, { embeddings: { embedding: number[]; metadata?: Record<string, unknown> | undefined; }[]; }>; type EmbedResponse = z.infer<typeof EmbedResponseSchema>; /** * Embedder action -- a subtype of {@link Action} with input/output types for embedders. */ type EmbedderAction<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny> = Action<typeof EmbedRequestSchema, typeof EmbedResponseSchema> & { __configSchema?: CustomOptions; }; /** * Options of an `embed` function. */ interface EmbedderParams<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny> { embedder: EmbedderArgument<CustomOptions>; content: string | DocumentData; metadata?: Record<string, unknown>; options?: z.infer<CustomOptions>; } interface EmbedderOptions<ConfigSchema extends z.ZodTypeAny> { name: string; configSchema?: ConfigSchema; info?: EmbedderInfo; } /** * Creates embedder model for the provided {@link EmbedderFn} model implementation. * * Unlike `defineEmbedder` this function does not register the embedder in the reigistry. */ declare function embedder<ConfigSchema extends z.ZodTypeAny = z.ZodTypeAny>(options: EmbedderOptions<ConfigSchema>, runner: (input: EmbedRequest<z.infer<ConfigSchema>>, opts: ActionFnArg<any>) => Promise<EmbedResponse>): EmbedderAction<ConfigSchema>; /** * Creates embedder model for the provided {@link EmbedderFn} model implementation. */ declare function defineEmbedder<ConfigSchema extends z.ZodTypeAny = z.ZodTypeAny>(registry: Registry, options: EmbedderOptions<ConfigSchema>, runner: EmbedderFn<ConfigSchema>): EmbedderAction<ConfigSchema>; /** * A union type representing all the types that can refer to an embedder. */ type EmbedderArgument<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny> = string | EmbedderAction<CustomOptions> | EmbedderReference<CustomOptions>; /** * A veneer for interacting with embedder models. */ declare function embed<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny>(registry: Registry, params: EmbedderParams<CustomOptions>): Promise<Embedding[]>; /** * A veneer for interacting with embedder models in bulk. */ declare function embedMany<ConfigSchema extends z.ZodTypeAny = z.ZodTypeAny>(registry: Registry, params: { embedder: EmbedderArgument<ConfigSchema>; content: string[] | DocumentData[]; metadata?: Record<string, unknown>; options?: z.infer<ConfigSchema>; }): Promise<EmbeddingBatch>; /** * Zod schema of embedder info object. */ declare const EmbedderInfoSchema: z.ZodObject<{ /** Friendly label for this model (e.g. "Google AI - Gemini Pro") */ label: z.ZodOptional<z.ZodString>; /** Supported model capabilities. */ supports: z.ZodOptional<z.ZodObject<{ /** Model can input this type of data. */ input: z.ZodOptional<z.ZodArray<z.ZodEnum<["text", "image", "video"]>, "many">>; /** Model can support multiple languages */ multilingual: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { input?: ("text" | "image" | "video")[] | undefined; multilingual?: boolean | undefined; }, { input?: ("text" | "image" | "video")[] | undefined; multilingual?: boolean | undefined; }>>; /** Embedding dimension */ dimensions: z.ZodOptional<z.ZodNumber>; }, "strip", z.ZodTypeAny, { label?: string | undefined; supports?: { input?: ("text" | "image" | "video")[] | undefined; multilingual?: boolean | undefined; } | undefined; dimensions?: number | undefined; }, { label?: string | undefined; supports?: { input?: ("text" | "image" | "video")[] | undefined; multilingual?: boolean | undefined; } | undefined; dimensions?: number | undefined; }>; type EmbedderInfo = z.infer<typeof EmbedderInfoSchema>; /** * A reference object that can used to resolve an embedder instance. Include additional type information * about the specific embedder, e.g. custom config options schema. */ interface EmbedderReference<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny> { name: string; configSchema?: CustomOptions; info?: EmbedderInfo; config?: z.infer<CustomOptions>; version?: string; } /** * Helper method to configure a {@link EmbedderReference} to a plugin. */ declare function embedderRef<CustomOptionsSchema extends z.ZodTypeAny = z.ZodTypeAny>(options: EmbedderReference<CustomOptionsSchema> & { namespace?: string; }): EmbedderReference<CustomOptionsSchema>; /** * Packages embedder information into ActionMetadata object. */ declare function embedderActionMetadata({ name, info, configSchema, }: { name: string; info?: EmbedderInfo; configSchema?: z.ZodTypeAny; }): ActionMetadata; /** * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ declare const DocumentDataSchema: z.ZodObject<{ content: z.ZodArray<z.ZodUnion<[z.ZodObject<{ media: z.ZodOptional<z.ZodNever>; toolRequest: z.ZodOptional<z.ZodNever>; toolResponse: z.ZodOptional<z.ZodNever>; data: z.ZodOptional<z.ZodUnknown>; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; custom: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; reasoning: z.ZodOptional<z.ZodNever>; resource: z.ZodOptional<z.ZodNever>; } & { text: z.ZodString; }, "strip", z.ZodTypeAny, { text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }, { text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }>, z.ZodObject<{ text: z.ZodOptional<z.ZodNever>; toolRequest: z.ZodOptional<z.ZodNever>; toolResponse: z.ZodOptional<z.ZodNever>; data: z.ZodOptional<z.ZodUnknown>; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; custom: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; reasoning: z.ZodOptional<z.ZodNever>; resource: z.ZodOptional<z.ZodNever>; } & { media: z.ZodObject<{ contentType: z.ZodOptional<z.ZodString>; url: z.ZodString; }, "strip", z.ZodTypeAny, { url: string; contentType?: string | undefined; }, { url: string; contentType?: string | undefined; }>; }, "strip", z.ZodTypeAny, { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }, { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; }>]>, "many">; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>; }, "strip", z.ZodTypeAny, { content: ({ text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; } | { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; })[]; metadata?: Record<string, any> | undefined; }, { content: ({ text: string; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; media?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; } | { media: { url: string; contentType?: string | undefined; }; custom?: Record<string, unknown> | undefined; metadata?: Record<string, unknown> | undefined; text?: undefined; toolRequest?: undefined; toolResponse?: undefined; data?: unknown; reasoning?: undefined; resource?: undefined; })[]; metadata?: Record<string, any> | undefined; }>; type DocumentData = z.infer<typeof DocumentDataSchema>; /** * Document represents document content along with its metadata that can be embedded, indexed or * retrieved. Each document can contain multiple parts (for example text and an image) */ declare class Document implements DocumentData { content: Part[]; metadata?: Record<string, any>; constructor(data: DocumentData); static fromText(text: string, metadata?: Record<string, any>): Document; static fromMedia(url: string, contentType?: string, metadata?: Record<string, unknown>): Document; static fromData(data: string, dataType?: string, metadata?: Record<string, unknown>): Document; /** * Concatenates all `text` parts present in the document with no delimiter. * @returns A string of all concatenated text parts. */ get text(): string; /** * Media array getter. * @returns the array of media parts. */ get media(): { url: string; contentType?: string; }[]; /** * Gets the first item in the document. Either text or media url. */ get data(): string; /** * Gets the contentType of the data that is returned by data() */ get dataType(): string | undefined; toJSON(): DocumentData; /** * Embedders may return multiple embeddings for a single document. * But storage still requires a 1:1 relationship. So we create an * array of Documents from a single document - one per embedding. * @param embeddings The embeddings to create the documents from. * @returns an array of documents based on this document and the embeddings. */ getEmbeddingDocuments(embeddings: Embedding[]): Document[]; } declare function checkUniqueDocuments(documents: Document[]): boolean; export { Document as D, type EmbedderAction as E, DocumentDataSchema as a, type DocumentData as b, embedderActionMetadata as c, embedderRef as d, embed as e, type EmbedderArgument as f, type EmbedderInfo as g, type EmbedderParams as h, type EmbedderReference as i, type Embedding as j, type EmbeddingBatch as k, EmbeddingSchema as l, type EmbedderFn as m, type EmbedRequest as n, type EmbedderOptions as o, embedder as p, defineEmbedder as q, embedMany as r, EmbedderInfoSchema as s, checkUniqueDocuments as t };