Skip to content

refactor(js): update type refs to use the @genkit-ai/shared package #3251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: pj/share-pkg
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"license": "Apache-2.0",
"dependencies": {
"@genkit-ai/core": "workspace:*",
"@genkit-ai/shared": "workspace:*",
"@opentelemetry/api": "^1.9.0",
"@types/node": "^20.11.19",
"colorette": "^2.0.20",
Expand Down
3 changes: 2 additions & 1 deletion js/ai/src/check-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import { GenkitError, Operation } from '@genkit-ai/core';
import { GenkitError } from '@genkit-ai/core';
import { Registry } from '@genkit-ai/core/registry';
import { type Operation } from '@genkit-ai/shared';

export async function checkOperation<T = unknown>(
registry: Registry,
Expand Down
161 changes: 2 additions & 159 deletions js/ai/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,164 +14,7 @@
* limitations under the License.
*/

import { z } from '@genkit-ai/core';
import type { Embedding } from './embedder';

const EmptyPartSchema = z.object({
text: z.never().optional(),
media: z.never().optional(),
toolRequest: z.never().optional(),
toolResponse: z.never().optional(),
data: z.unknown().optional(),
metadata: z.record(z.unknown()).optional(),
custom: z.record(z.unknown()).optional(),
reasoning: z.never().optional(),
resource: z.never().optional(),
});

/**
* Zod schema for a text part.
*/
export const TextPartSchema = EmptyPartSchema.extend({
/** The text of the message. */
text: z.string(),
});

/**
* Zod schema for a reasoning part.
*/
export const ReasoningPartSchema = EmptyPartSchema.extend({
/** The reasoning text of the message. */
reasoning: z.string(),
});

/**
* Text part.
*/
export type TextPart = z.infer<typeof TextPartSchema>;

/**
* Zod schema of media.
*/
export const MediaSchema = z.object({
/** The media content type. Inferred from data uri if not provided. */
contentType: z.string().optional(),
/** A `data:` or `https:` uri containing the media content. */
url: z.string(),
});

/**
* Zod schema of a media part.
*/
export const MediaPartSchema = EmptyPartSchema.extend({
media: MediaSchema,
});

/**
* Media part.
*/
export type MediaPart = z.infer<typeof MediaPartSchema>;

/**
* Zod schema of a tool request.
*/
export const ToolRequestSchema = z.object({
/** The call id or reference for a specific request. */
ref: z.string().optional(),
/** The name of the tool to call. */
name: z.string(),
/** The input parameters for the tool, usually a JSON object. */
input: z.unknown().optional(),
});
export type ToolRequest = z.infer<typeof ToolRequestSchema>;

/**
* Zod schema of a tool request part.
*/
export const ToolRequestPartSchema = EmptyPartSchema.extend({
/** A request for a tool to be executed, usually provided by a model. */
toolRequest: ToolRequestSchema,
});

/**
* Tool part.
*/
export type ToolRequestPart = z.infer<typeof ToolRequestPartSchema>;

/**
* Zod schema of a tool response.
*/
export const ToolResponseSchema = z.object({
/** The call id or reference for a specific request. */
ref: z.string().optional(),
/** The name of the tool. */
name: z.string(),
/** The output data returned from the tool, usually a JSON object. */
output: z.unknown().optional(),
});
export type ToolResponse = z.infer<typeof ToolResponseSchema>;

/**
* Zod schema of a tool response part.
*/
export const ToolResponsePartSchema = EmptyPartSchema.extend({
/** A provided response to a tool call. */
toolResponse: ToolResponseSchema,
});

/**
* Tool response part.
*/
export type ToolResponsePart = z.infer<typeof ToolResponsePartSchema>;

/**
* Zod schema of a data part.
*/
export const DataPartSchema = EmptyPartSchema.extend({
data: z.unknown(),
});

/**
* Data part.
*/
export type DataPart = z.infer<typeof DataPartSchema>;

/**
* Zod schema of a custom part.
*/
export const CustomPartSchema = EmptyPartSchema.extend({
custom: z.record(z.any()),
});

/**
* Custom part.
*/
export type CustomPart = z.infer<typeof CustomPartSchema>;

/**
* Zod schema of a resource part.
*/
export const ResourcePartSchema = EmptyPartSchema.extend({
resource: z.object({
uri: z.string(),
}),
});

/**
* Resource part.
*/
export type ResourcePart = z.infer<typeof ResourcePartSchema>;

export const PartSchema = z.union([TextPartSchema, MediaPartSchema]);
export type Part = z.infer<typeof PartSchema>;

// We need both metadata and embedMetadata because they can
// contain the same fields (e.g. video start/stop) with different values.
export const DocumentDataSchema = z.object({
content: z.array(PartSchema),
metadata: z.record(z.string(), z.any()).optional(),
});
export type DocumentData = z.infer<typeof DocumentDataSchema>;
import type { DocumentData, DocumentPart, Embedding } from '@genkit-ai/shared';

function deepCopy<T>(value: T): T {
if (value === undefined) {
Expand All @@ -185,7 +28,7 @@ function deepCopy<T>(value: T): T {
* retrieved. Each document can contain multiple parts (for example text and an image)
*/
export class Document implements DocumentData {
content: Part[];
content: DocumentPart[];
metadata?: Record<string, any>;

constructor(data: DocumentData) {
Expand Down
49 changes: 16 additions & 33 deletions js/ai/src/embedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ import {
} from '@genkit-ai/core';
import type { Registry } from '@genkit-ai/core/registry';
import { toJsonSchema } from '@genkit-ai/core/schema';
import { Document, DocumentDataSchema, type DocumentData } from './document.js';

/**
* A batch (array) of embeddings.
*/
export 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.
*/
export const EmbeddingSchema = z.object({
embedding: z.array(z.number()),
metadata: z.record(z.string(), z.unknown()).optional(),
});
export type Embedding = z.infer<typeof EmbeddingSchema>;
import {
EmbedRequestSchema,
EmbedResponse,
EmbedResponseSchema,
EmbeddingSchema,
type DocumentData,
type Embedding,
type EmbeddingBatch,
} from '@genkit-ai/shared';
import { Document } from './document.js';
export {
EmbedRequestSchema,
EmbeddingSchema,
type Embedding,
type EmbeddingBatch,
};

/**
* A function used for embedder definition, encapsulates embedder implementation.
Expand All @@ -47,23 +47,6 @@ export type EmbedderFn<EmbedderOptions extends z.ZodTypeAny> = (
embedderOpts?: z.infer<EmbedderOptions>
) => Promise<EmbedResponse>;

/**
* Zod schema of an embed request.
*/
const EmbedRequestSchema = z.object({
input: z.array(DocumentDataSchema),
options: z.any().optional(),
});

/**
* Zod schema of an embed response.
*/
const EmbedResponseSchema = z.object({
embeddings: z.array(EmbeddingSchema),
// TODO: stats, etc.
});
type EmbedResponse = z.infer<typeof EmbedResponseSchema>;

/**
* Embedder action -- a subtype of {@link Action} with input/output types for embedders.
*/
Expand Down
50 changes: 15 additions & 35 deletions js/ai/src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,33 @@ import { logger } from '@genkit-ai/core/logging';
import type { Registry } from '@genkit-ai/core/registry';
import { toJsonSchema } from '@genkit-ai/core/schema';
import { SPAN_TYPE_ATTR, runInNewSpan } from '@genkit-ai/core/tracing';
import {
BaseDataPointSchema,
BaseEvalDataPoint,
BaseEvalDataPointSchema,
EvalStatusEnumSchema,
ScoreSchema,
} from '@genkit-ai/shared';
import { randomUUID } from 'crypto';

export {
BaseDataPointSchema,
BaseEvalDataPointSchema,
EvalStatusEnumSchema,
ScoreSchema,
type BaseEvalDataPoint,
};

export const ATTR_PREFIX = 'genkit';
export const SPAN_STATE_ATTR = ATTR_PREFIX + ':state';

export const BaseDataPointSchema = z.object({
input: z.unknown(),
output: z.unknown().optional(),
context: z.array(z.unknown()).optional(),
reference: z.unknown().optional(),
testCaseId: z.string().optional(),
traceIds: z.array(z.string()).optional(),
});

// DataPoint that is to be used for actions. This needs testCaseId to be present.
export const BaseEvalDataPointSchema = BaseDataPointSchema.extend({
testCaseId: z.string(),
});
export type BaseEvalDataPoint = z.infer<typeof BaseEvalDataPointSchema>;

const EvalStatusEnumSchema = z.enum(['UNKNOWN', 'PASS', 'FAIL']);

/** Enum that indicates if an evaluation has passed or failed */
export enum EvalStatusEnum {
UNKNOWN = 'UNKNOWN',
PASS = 'PASS',
FAIL = 'FAIL',
}

export const ScoreSchema = z.object({
id: z
.string()
.describe(
'Optional ID to differentiate different scores if applying in a single evaluation'
)
.optional(),
score: z.union([z.number(), z.string(), z.boolean()]).optional(),
status: EvalStatusEnumSchema.optional(),
error: z.string().optional(),
details: z
.object({
reasoning: z.string().optional(),
})
.passthrough()
.optional(),
});

// Update genkit-tools/src/utils/evals.ts if you change this value
export const EVALUATOR_METADATA_KEY_DISPLAY_NAME = 'evaluatorDisplayName';
export const EVALUATOR_METADATA_KEY_DEFINITION = 'evaluatorDefinition';
Expand Down
3 changes: 2 additions & 1 deletion js/ai/src/formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

import type { JSONSchema } from '@genkit-ai/core';
import type { Registry } from '@genkit-ai/core/registry';
import type { MessageData } from '@genkit-ai/shared';
import type { OutputOptions } from '../generate.js';
import type { MessageData, TextPart } from '../model.js';
import type { TextPart } from '../model.js';
import { arrayFormatter } from './array.js';
import { enumFormatter } from './enum.js';
import { jsonFormatter } from './json.js';
Expand Down
2 changes: 1 addition & 1 deletion js/ai/src/formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import type { JSONSchema } from '@genkit-ai/core';
import type { ModelRequest } from '@genkit-ai/shared';
import type { GenerateResponseChunk } from '../generate.js';
import type { Message } from '../message.js';
import type { ModelRequest } from '../model.js';

export type OutputContentTypes = 'application/json' | 'text/plain';

Expand Down
21 changes: 12 additions & 9 deletions js/ai/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
GenkitError,
isAction,
isDetachedAction,
Operation,
runWithContext,
sentinelNoopStreamingCallback,
type Action,
Expand All @@ -30,7 +29,18 @@ import {
import { Channel } from '@genkit-ai/core/async';
import { Registry } from '@genkit-ai/core/registry';
import { toJsonSchema } from '@genkit-ai/core/schema';
import type { DocumentData } from './document.js';
import type {
DocumentData,
GenerateActionOptions,
GenerateRequest,
GenerateResponseChunkData,
GenerateResponseData,
GenerationCommonConfigSchema,
MessageData,
Operation,
Part,
} from '@genkit-ai/shared';
import type {} from './document.js';
import {
injectInstructions,
resolveFormat,
Expand All @@ -44,16 +54,9 @@ import { GenerateResponseChunk } from './generate/chunk.js';
import { GenerateResponse } from './generate/response.js';
import { Message } from './message.js';
import {
GenerateResponseChunkData,
GenerateResponseData,
resolveModel,
type GenerateActionOptions,
type GenerateRequest,
type GenerationCommonConfigSchema,
type MessageData,
type ModelArgument,
type ModelMiddleware,
type Part,
type ToolRequestPart,
type ToolResponsePart,
} from './model.js';
Expand Down
Loading