Skip to content

Commit 18c5cab

Browse files
committed
Merge branch 'main' into feat/create-context-caching-for-vertexai
2 parents f3828f3 + 9aad90e commit 18c5cab

File tree

160 files changed

+437
-5089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+437
-5089
lines changed

docs/monitoring.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Firebase Genkit is fully instrumented with
55

66
## Telemetry Configuration
77

8-
Genkit automatically manages tracing and metrics without requiring explicit configuration. You can enable telemetry exports for Firebase or Google Cloud using their respective plugins and helper functions. Using either plugin poweres the [Firebase AI Monitoring dashboard (private preview)](https://forms.gle/Lp5S1NxbZUXsWc457) that has an AI-idiomatic view of telemetry data.
8+
Genkit automatically manages tracing and metrics without requiring explicit configuration. You can enable telemetry exports for Firebase or Google Cloud using their respective plugins and helper functions. Using either plugin powers the [Genkit Monitoring dashboard](https://console.firebase.google.com/project/_/genai_monitoring) that has an AI-centric view of telemetry data.
99

1010
### For Firebase:
1111

genkit-tools/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "genkit-cli",
3-
"version": "1.0.0-rc.1",
3+
"version": "1.0.0-rc.2",
44
"description": "CLI for interacting with the Google Genkit AI framework",
55
"license": "Apache-2.0",
66
"keywords": [

genkit-tools/cli/src/commands/eval-flow.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ import {
2828
runEvaluation,
2929
runInference,
3030
} from '@genkit-ai/tools-common/eval';
31-
import { confirmLlmUse, logger } from '@genkit-ai/tools-common/utils';
31+
import {
32+
confirmLlmUse,
33+
loadEvalInference,
34+
logger,
35+
} from '@genkit-ai/tools-common/utils';
3236
import { Command } from 'commander';
33-
import { readFile } from 'fs/promises';
3437
import { runWithManager } from '../utils/manager-utils';
3538

3639
interface EvalFlowRunCliOptions {
@@ -172,8 +175,11 @@ async function readInputs(
172175
parsedData = JSON.parse(dataField!);
173176
break;
174177
case SourceType.FILE:
175-
parsedData = JSON.parse(await readFile(input!, 'utf8'));
176-
break;
178+
try {
179+
return await loadEvalInference(input!);
180+
} catch (e) {
181+
throw new Error(`Error parsing the input from file. Error: ${e}`);
182+
}
177183
case SourceType.DATASET:
178184
const datasetStore = await getDatasetStore();
179185
const data = await datasetStore.getDataset(input!);

genkit-tools/cli/src/commands/eval-run.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Action, EvalInput } from '@genkit-ai/tools-common';
17+
import { Action, EvalInputDataset } from '@genkit-ai/tools-common';
1818
import {
1919
EvalExporter,
2020
getAllEvaluatorActions,
@@ -25,10 +25,10 @@ import {
2525
import {
2626
confirmLlmUse,
2727
generateTestCaseId,
28+
loadEvalInputDataset,
2829
logger,
2930
} from '@genkit-ai/tools-common/utils';
3031
import { Command } from 'commander';
31-
import { readFile } from 'fs/promises';
3232
import { runWithManager } from '../utils/manager-utils';
3333

3434
interface EvalRunCliOptions {
@@ -92,8 +92,8 @@ export const evalRun = new Command('eval:run')
9292
}
9393
}
9494

95-
const evalDataset: EvalInput[] = JSON.parse(
96-
(await readFile(dataset)).toString('utf-8')
95+
const evalDataset: EvalInputDataset = (
96+
await loadEvalInputDataset(dataset)
9797
).map((testCase: any) => ({
9898
...testCase,
9999
testCaseId: testCase.testCaseId || generateTestCaseId(),

genkit-tools/common/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@genkit-ai/tools-common",
3-
"version": "1.0.0-rc.1",
3+
"version": "1.0.0-rc.2",
44
"scripts": {
55
"compile": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
66
"build:clean": "rimraf ./lib",

genkit-tools/common/src/types/eval.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ export const ModelInferenceInputJSONSchema = zodToJsonSchema(
4545
}
4646
) as JSONSchema7;
4747

48+
/**
49+
* A single sample to be used for inference.
50+
**/
51+
export const EvalInferenceSampleSchema = z.object({
52+
testCaseId: z.string().optional(),
53+
input: z.any(),
54+
reference: z.any().optional(),
55+
});
56+
4857
/**
4958
* A set of samples that is ready for inference.
5059
*
5160
* This should be used in user-facing surfaces (CLI/API inputs) to accommodate various user input formats. For internal wire-transfer/storage, prefer {@link Dataset}.
5261
*/
53-
export const EvalInferenceInputSchema = z.array(
54-
z.object({
55-
testCaseId: z.string().optional(),
56-
input: z.any(),
57-
reference: z.any().optional(),
58-
})
59-
);
62+
export const EvalInferenceInputSchema = z.array(EvalInferenceSampleSchema);
6063
export type EvalInferenceInput = z.infer<typeof EvalInferenceInputSchema>;
6164

6265
/**
@@ -87,6 +90,9 @@ export const EvalInputSchema = z.object({
8790
});
8891
export type EvalInput = z.infer<typeof EvalInputSchema>;
8992

93+
export const EvalInputDatasetSchema = z.array(EvalInputSchema);
94+
export type EvalInputDataset = z.infer<typeof EvalInputDatasetSchema>;
95+
9096
export const EvalMetricSchema = z.object({
9197
evaluator: z.string(),
9298
scoreId: z.string().optional(),

genkit-tools/common/src/utils/eval.ts

+77
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616

1717
import { randomUUID } from 'crypto';
18+
import { createReadStream } from 'fs';
19+
import { readFile } from 'fs/promises';
1820
import * as inquirer from 'inquirer';
21+
import { createInterface } from 'readline';
1922
import {
2023
EvalField,
2124
EvaluationExtractor,
@@ -25,6 +28,14 @@ import {
2528
findToolsConfig,
2629
isEvalField,
2730
} from '../plugin';
31+
import {
32+
EvalInferenceInput,
33+
EvalInferenceInputSchema,
34+
EvalInferenceSampleSchema,
35+
EvalInputDataset,
36+
EvalInputDatasetSchema,
37+
EvalInputSchema,
38+
} from '../types';
2839
import { Action } from '../types/action';
2940
import { DocumentData, RetrieverResponse } from '../types/retrievers';
3041
import { NestedSpanData, TraceData } from '../types/trace';
@@ -222,3 +233,69 @@ export async function getEvalExtractors(
222233
export function generateTestCaseId() {
223234
return randomUUID();
224235
}
236+
237+
/** Load a {@link EvalInferenceInput} file. Supports JSON / JSONL */
238+
export async function loadEvalInference(
239+
fileName: string
240+
): Promise<EvalInferenceInput> {
241+
const isJsonl = fileName.endsWith('.jsonl');
242+
243+
if (isJsonl) {
244+
return await readJsonlForInference(fileName);
245+
} else {
246+
const parsedData = JSON.parse(await readFile(fileName, 'utf8'));
247+
return EvalInferenceInputSchema.parse(parsedData);
248+
}
249+
}
250+
251+
/** Load a {@link Eval} file. Supports JSON / JSONL */
252+
export async function loadEvalInputDataset(
253+
fileName: string
254+
): Promise<EvalInputDataset> {
255+
const isJsonl = fileName.endsWith('.jsonl');
256+
257+
if (isJsonl) {
258+
return await readJsonlForEvaluation(fileName);
259+
} else {
260+
const parsedData = JSON.parse(await readFile(fileName, 'utf8'));
261+
return EvalInputDatasetSchema.parse(parsedData);
262+
}
263+
}
264+
265+
async function readJsonlForInference(
266+
fileName: string
267+
): Promise<EvalInferenceInput> {
268+
const lines = await readLines(fileName);
269+
const samples: EvalInferenceInput = [];
270+
for (const line of lines) {
271+
const parsedSample = EvalInferenceSampleSchema.parse(JSON.parse(line));
272+
samples.push(parsedSample);
273+
}
274+
return samples;
275+
}
276+
277+
async function readJsonlForEvaluation(
278+
fileName: string
279+
): Promise<EvalInputDataset> {
280+
const lines = await readLines(fileName);
281+
const inputs: EvalInputDataset = [];
282+
for (const line of lines) {
283+
const parsedSample = EvalInputSchema.parse(JSON.parse(line));
284+
inputs.push(parsedSample);
285+
}
286+
return inputs;
287+
}
288+
289+
async function readLines(fileName: string): Promise<string[]> {
290+
const lines: string[] = [];
291+
const fileStream = createReadStream(fileName);
292+
const rl = createInterface({
293+
input: fileStream,
294+
crlfDelay: Infinity,
295+
});
296+
297+
for await (const line of rl) {
298+
lines.push(line);
299+
}
300+
return lines;
301+
}

genkit-tools/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
"cross-spawn": "^7.0.5"
2626
}
2727
},
28-
"packageManager": "pnpm@9.13.2+sha256.ccce81bf7498c5f0f80e31749c1f8f03baba99d168f64590fc7e13fad3ea1938"
28+
"packageManager": "pnpm@9.15.1+sha256.9e534e70afef06374f6126b44bda5760947135ce16a30aef1010e965fb7e3e3e"
2929
}

genkit-tools/telemetry-server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"genai",
88
"generative-ai"
99
],
10-
"version": "1.0.0-rc.1",
10+
"version": "1.0.0-rc.2",
1111
"type": "commonjs",
1212
"scripts": {
1313
"compile": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",

js/ai/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"genai",
88
"generative-ai"
99
],
10-
"version": "1.0.0-rc.1",
10+
"version": "1.0.0-rc.2",
1111
"type": "commonjs",
1212
"scripts": {
1313
"check": "tsc",

js/ai/src/chat.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
Session,
3434
SessionStore,
3535
runWithSession,
36-
} from './session';
36+
} from './session.js';
3737

3838
export const MAIN_THREAD = 'main';
3939

@@ -242,7 +242,7 @@ export class Chat {
242242
return this._messages ?? [];
243243
}
244244

245-
async updateMessages(messages: MessageData[]): Promise<void> {
245+
private async updateMessages(messages: MessageData[]): Promise<void> {
246246
this._messages = messages;
247247
await this.session.updateMessages(this.threadName, messages);
248248
}

js/ai/src/prompt.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
GenerateRequestSchema,
2828
GenerateResponseChunkSchema,
2929
ModelArgument,
30+
ModelMiddleware,
3031
} from './model.js';
3132
import { ToolAction } from './tool.js';
3233

@@ -51,6 +52,7 @@ export type PromptAction<I extends z.ZodTypeAny = z.ZodTypeAny> = Action<
5152
type: 'prompt';
5253
};
5354
};
55+
__config: PromptConfig;
5456
};
5557

5658
/**
@@ -62,6 +64,7 @@ export interface PromptConfig<I extends z.ZodTypeAny = z.ZodTypeAny> {
6264
inputSchema?: I;
6365
inputJsonSchema?: JSONSchema7;
6466
metadata?: Record<string, any>;
67+
use?: ModelMiddleware[];
6568
}
6669

6770
/**
@@ -147,12 +150,16 @@ export function definePrompt<I extends z.ZodTypeAny>(
147150
const a = defineAction(
148151
registry,
149152
{
150-
...config,
153+
name: config.name,
154+
inputJsonSchema: config.inputJsonSchema,
155+
inputSchema: config.inputSchema,
156+
description: config.description,
151157
actionType: 'prompt',
152158
metadata: { ...(config.metadata || { prompt: {} }), type: 'prompt' },
153159
},
154160
fn
155161
);
162+
(a as PromptAction<I>).__config = config;
156163
return a as PromptAction<I>;
157164
}
158165

js/ai/src/retriever.ts

+1
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ function itemToMetadata(
370370
if (Array.isArray(options.metadata) && typeof item === 'object') {
371371
const out: Record<string, any> = {};
372372
options.metadata.forEach((key) => (out[key] = item[key]));
373+
return out;
373374
}
374375
if (typeof options.metadata === 'function') return options.metadata(item);
375376
if (!options.metadata && typeof item === 'object') {

js/ai/src/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { z } from '@genkit-ai/core';
1818
import { Registry } from '@genkit-ai/core/registry';
1919
import { v4 as uuidv4 } from 'uuid';
20-
import { Chat, ChatOptions, MAIN_THREAD, PromptRenderOptions } from './chat';
20+
import { Chat, ChatOptions, MAIN_THREAD, PromptRenderOptions } from './chat.js';
2121
import {
2222
ExecutablePrompt,
2323
GenerateOptions,

js/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"genai",
88
"generative-ai"
99
],
10-
"version": "1.0.0-rc.1",
10+
"version": "1.0.0-rc.2",
1111
"type": "commonjs",
1212
"scripts": {
1313
"check": "tsc",

js/genkit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"genai",
88
"generative-ai"
99
],
10-
"version": "1.0.0-rc.1",
10+
"version": "1.0.0-rc.2",
1111
"type": "commonjs",
1212
"main": "./lib/cjs/index.js",
1313
"scripts": {

js/genkit/src/client/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const __flowStreamDelimiter = '\n\n';
2222
* For example:
2323
*
2424
* ```js
25-
* import { streamFlow } from '@genkit-ai/core/flow-client';
25+
* import { streamFlow } from 'genkit/client';
2626
*
2727
* const response = streamFlow({
2828
* url: 'https://my-flow-deployed-url',

0 commit comments

Comments
 (0)