diff --git a/js/ai/src/embedder.ts b/js/ai/src/embedder.ts index 92fab96ee..1da93ad08 100644 --- a/js/ai/src/embedder.ts +++ b/js/ai/src/embedder.ts @@ -16,7 +16,6 @@ import { Action, defineAction } from '@genkit-ai/core'; import { lookupAction } from '@genkit-ai/core/registry'; -import { setCustomMetadataAttributes } from '@genkit-ai/core/tracing'; import * as z from 'zod'; import { Document, DocumentData, DocumentDataSchema } from './document.js'; @@ -83,13 +82,11 @@ export function defineEmbedder< info: options.info, }, }, - (i) => { - setCustomMetadataAttributes({ subtype: 'embedder' }); - return runner( + (i) => + runner( i.input.map((dd) => new Document(dd)), i.options - ); - } + ) ); const ewm = withMetadata( embedder as Action, diff --git a/js/ai/src/evaluator.ts b/js/ai/src/evaluator.ts index 1e27a8319..5ce0636a7 100644 --- a/js/ai/src/evaluator.ts +++ b/js/ai/src/evaluator.ts @@ -17,11 +17,7 @@ import { Action, defineAction } from '@genkit-ai/core'; import { logger } from '@genkit-ai/core/logging'; import { lookupAction } from '@genkit-ai/core/registry'; -import { - SPAN_TYPE_ATTR, - runInNewSpan, - setCustomMetadataAttributes, -} from '@genkit-ai/core/tracing'; +import { SPAN_TYPE_ATTR, runInNewSpan } from '@genkit-ai/core/tracing'; import * as z from 'zod'; export const ATTR_PREFIX = 'genkit'; @@ -143,7 +139,6 @@ export function defineEvaluator< metadata: metadata, }, async (i) => { - setCustomMetadataAttributes({ subtype: 'evaluator' }); let evalResponses: EvalResponses = []; for (let index = 0; index < i.dataset.length; index++) { const datapoint = i.dataset[index]; diff --git a/js/ai/src/model.ts b/js/ai/src/model.ts index 4d118917a..0b4af7b55 100644 --- a/js/ai/src/model.ts +++ b/js/ai/src/model.ts @@ -21,7 +21,6 @@ import { StreamingCallback, } from '@genkit-ai/core'; import { toJsonSchema } from '@genkit-ai/core/schema'; -import { setCustomMetadataAttributes } from '@genkit-ai/core/tracing'; import { performance } from 'node:perf_hooks'; import { z } from 'zod'; import { conformOutput, validateSupport } from './model/middleware.js'; @@ -311,7 +310,6 @@ export function defineModel< }, }, (input) => { - setCustomMetadataAttributes({ subtype: 'model' }); const startTimeMs = performance.now(); return runner(input, getStreamingCallback()) .then((response) => { diff --git a/js/ai/src/prompt.ts b/js/ai/src/prompt.ts index 1ec0c06ac..df62c30e0 100644 --- a/js/ai/src/prompt.ts +++ b/js/ai/src/prompt.ts @@ -14,9 +14,8 @@ * limitations under the License. */ -import { Action, action, JSONSchema7 } from '@genkit-ai/core'; -import { lookupAction, registerAction } from '@genkit-ai/core/registry'; -import { setCustomMetadataAttributes } from '@genkit-ai/core/tracing'; +import { Action, defineAction, JSONSchema7 } from '@genkit-ai/core'; +import { lookupAction } from '@genkit-ai/core/registry'; import z from 'zod'; import { GenerateOptions } from './generate'; import { GenerateRequest, GenerateRequestSchema, ModelArgument } from './model'; @@ -52,20 +51,17 @@ export function definePrompt( }, fn: PromptFn ): PromptAction { - const a = action( + const a = defineAction( { + actionType: 'prompt', name, description, inputSchema, inputJsonSchema, metadata: { ...(metadata || { prompt: {} }), type: 'prompt' }, }, - (i: I): Promise => { - setCustomMetadataAttributes({ subtype: 'prompt' }); - return fn(i); - } + fn ); - registerAction('prompt', a); return a as PromptAction; } diff --git a/js/ai/src/retriever.ts b/js/ai/src/retriever.ts index e8b2afcdf..f88514454 100644 --- a/js/ai/src/retriever.ts +++ b/js/ai/src/retriever.ts @@ -16,7 +16,6 @@ import { Action, defineAction } from '@genkit-ai/core'; import { lookupAction } from '@genkit-ai/core/registry'; -import { setCustomMetadataAttributes } from '@genkit-ai/core/tracing'; import * as z from 'zod'; import { Document, DocumentData, DocumentDataSchema } from './document.js'; import { EmbedderInfo } from './embedder.js'; @@ -128,10 +127,7 @@ export function defineRetriever< info: options.info, }, }, - (i) => { - setCustomMetadataAttributes({ subtype: 'retriever' }); - return runner(new Document(i.query), i.options); - } + (i) => runner(new Document(i.query), i.options) ); const rwm = retrieverWithMetadata( retriever as Action< @@ -169,13 +165,11 @@ export function defineIndexer( embedderInfo: options.embedderInfo, }, }, - (i) => { - setCustomMetadataAttributes({ subtype: 'indexer' }); - return runner( + (i) => + runner( i.documents.map((dd) => new Document(dd)), i.options - ); - } + ) ); const iwm = indexerWithMetadata( indexer as Action, diff --git a/js/ai/src/tool.ts b/js/ai/src/tool.ts index c20043bc0..412391d13 100644 --- a/js/ai/src/tool.ts +++ b/js/ai/src/tool.ts @@ -128,10 +128,7 @@ export function defineTool( outputJsonSchema, metadata: { ...(metadata || {}), type: 'tool' }, }, - (i) => { - setCustomMetadataAttributes({ subtype: 'tool' }); - return fn(i); - } + (i) => fn(i) ); return a as ToolAction; } diff --git a/js/core/src/action.ts b/js/core/src/action.ts index 44454a5f2..09fd1466c 100644 --- a/js/core/src/action.ts +++ b/js/core/src/action.ts @@ -21,7 +21,11 @@ import * as z from 'zod'; import { ActionType, lookupPlugin, registerAction } from './registry.js'; import { parseSchema } from './schema.js'; import * as telemetry from './telemetry.js'; -import { SPAN_TYPE_ATTR, runInNewSpan } from './tracing.js'; +import { + SPAN_TYPE_ATTR, + runInNewSpan, + setCustomMetadataAttributes, +} from './tracing.js'; export { Status, StatusCodes, StatusSchema } from './statusTypes.js'; export { JSONSchema7 }; @@ -176,7 +180,10 @@ export function defineAction< }, fn: (input: z.infer) => Promise> ): Action { - const act = action(config, fn); + const act = action(config, (i: I): Promise> => { + setCustomMetadataAttributes({ subtype: config.actionType }); + return fn(i); + }); act.__action.actionType = config.actionType; registerAction(config.actionType, act); return act;