@genkit-ai/ai
Version:
Genkit AI framework generative AI APIs.
99 lines • 2.58 kB
JavaScript
import {
getContext,
run
} from "@genkit-ai/core";
import { cancelOperation } from "./cancel-operation.js";
import { checkOperation } from "./check-operation.js";
import {
embed,
embedMany
} from "./embedder.js";
import {
generate,
generateStream
} from "./generate.js";
class GenkitAI {
registry;
constructor(registry) {
this.registry = registry;
}
/**
* Embeds the given `content` using the specified `embedder`.
*/
embed(params) {
return embed(this.registry, params);
}
/**
* A veneer for interacting with embedder models in bulk.
*/
embedMany(params) {
return embedMany(this.registry, params);
}
async generate(options) {
let resolvedOptions;
if (options instanceof Promise) {
resolvedOptions = await options;
} else if (typeof options === "string" || Array.isArray(options)) {
resolvedOptions = {
prompt: options
};
} else {
resolvedOptions = options;
}
return generate(this.registry, resolvedOptions);
}
generateStream(options) {
if (typeof options === "string" || Array.isArray(options)) {
options = { prompt: options };
}
return generateStream(this.registry, options);
}
/**
* Checks the status of of a given operation. Returns a new operation which will contain the updated status.
*
* ```ts
* let operation = await ai.generateOperation({
* model: googleAI.model('veo-2.0-generate-001'),
* prompt: 'A banana riding a bicycle.',
* });
*
* while (!operation.done) {
* operation = await ai.checkOperation(operation!);
* await new Promise((resolve) => setTimeout(resolve, 5000));
* }
* ```
*
* @param operation
* @returns
*/
checkOperation(operation) {
return checkOperation(this.registry, operation);
}
/**
* Cancels a given operation. Returns a new operation which will contain the updated status.
*
* @param operation
* @returns
*/
cancelOperation(operation) {
return cancelOperation(this.registry, operation);
}
run(name, funcOrInput, maybeFunc) {
if (maybeFunc) {
return run(name, funcOrInput, maybeFunc, this.registry);
}
return run(name, funcOrInput, this.registry);
}
/**
* Returns current action (or flow) invocation context. Can be used to access things like auth
* data set by HTTP server frameworks. If invoked outside of an action (e.g. flow or tool) will
* return `undefined`.
*/
currentContext() {
return getContext();
}
}
export {
GenkitAI
};
//# sourceMappingURL=genkit-ai.mjs.map