Skip to content

WIP, tests work, but needs reflection orchestra #3235

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 1 commit into
base: main
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
5 changes: 5 additions & 0 deletions js/core/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const instrumentationKey = '__GENKIT_TELEMETRY_INSTRUMENTED';
* @hidden
*/
export async function ensureBasicTelemetryInstrumentation() {
console.log("enabling basic telemetry");
if (global[instrumentationKey]) {
return await global[instrumentationKey];
}
Expand All @@ -54,6 +55,7 @@ export async function ensureBasicTelemetryInstrumentation() {
export async function enableTelemetry(
telemetryConfig: TelemetryConfig | Promise<TelemetryConfig>
) {
console.log("enabling telemetry");
if (process.env.GENKIT_TELEMETRY_SERVER) {
setTelemetryServerUrl(process.env.GENKIT_TELEMETRY_SERVER);
}
Expand Down Expand Up @@ -102,6 +104,7 @@ export async function cleanUpTracing(): Promise<void> {
* Creates a new SpanProcessor for exporting data to the telemetry server.
*/
function createTelemetryServerProcessor(): SpanProcessor {
console.log("creating processor telemetry");
const exporter = new TraceServerExporter();
return isDevEnv()
? new SimpleSpanProcessor(exporter)
Expand All @@ -110,6 +113,7 @@ function createTelemetryServerProcessor(): SpanProcessor {

/** Flush metrics if present. */
function maybeFlushMetrics(): Promise<void> {
console.log("maybe flushing telemetry");
if (nodeOtelConfig?.metricReader) {
return nodeOtelConfig.metricReader.forceFlush();
}
Expand All @@ -122,6 +126,7 @@ function maybeFlushMetrics(): Promise<void> {
* @hidden
*/
export async function flushTracing() {
console.log("flushing tracins");
if (nodeOtelConfig?.spanProcessors) {
await Promise.all(nodeOtelConfig.spanProcessors.map((p) => p.forceFlush()));
}
Expand Down
3 changes: 3 additions & 0 deletions js/core/src/tracing/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class TraceServerExporter implements SpanExporter {
spans: ReadableSpan[],
resultCallback: (result: ExportResult) => void
): void {
console.log("export telemetry");
this._sendSpans(spans, resultCallback);
}

Expand Down Expand Up @@ -106,6 +107,7 @@ export class TraceServerExporter implements SpanExporter {
* Exports any pending spans in exporter
*/
forceFlush(): Promise<void> {
console.log("force flush 2 telemetry");
return Promise.resolve();
}

Expand Down Expand Up @@ -137,6 +139,7 @@ export class TraceServerExporter implements SpanExporter {
}

private async save(traceId, spans: ReadableSpan[]): Promise<void> {
console.log("fsaving 2 telemetry");
if (!telemetryServerUrl) {
logger.debug(
`Telemetry server is not configured, trace ${traceId} not saved!`
Expand Down
4 changes: 4 additions & 0 deletions js/core/src/tracing/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function newTrace<T>(
},
fn: (metadata: SpanMetadata, rootSpan: ApiSpan) => Promise<T>
) {
console.log("newTrace")
registry = (registry as HasRegistry).registry
? (registry as HasRegistry).registry
: (registry as Registry);
Expand Down Expand Up @@ -90,6 +91,8 @@ export async function runInNewSpan<T>(
},
fn: (metadata: SpanMetadata, otSpan: ApiSpan, isRoot: boolean) => Promise<T>
): Promise<T> {

console.log("runINnew span")
await ensureBasicTelemetryInstrumentation();
const resolvedRegistry = (registry as HasRegistry).registry
? (registry as HasRegistry).registry
Expand Down Expand Up @@ -154,6 +157,7 @@ export async function appendSpan(
metadata: SpanMetadata,
labels?: Record<string, string>
) {
console.log("append span")
await ensureBasicTelemetryInstrumentation();

const tracer = trace.getTracer(TRACER_NAME, TRACER_VERSION);
Expand Down
3 changes: 3 additions & 0 deletions js/core/src/tracing/multiSpanProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ export class MultiSpanProcessor implements SpanProcessor {
constructor(private processors: SpanProcessor[]) {}

forceFlush(): Promise<void> {
console.log("multispan ")
return Promise.all(this.processors.map((p) => p.forceFlush())).then();
}

onStart(span: Span, parentContext: Context): void {

console.log("multispan ")
this.processors.map((p) => p.onStart(span, parentContext));
}

Expand Down
3 changes: 2 additions & 1 deletion js/genkit/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ export function genkit(options: GenkitOptions): Genkit {
return new Genkit(options);
}

const shutdown = async () => {
export const shutdown = async () => {
logger.info('Shutting down all Genkit servers...');
await ReflectionServer.stopAll();
process.exit(0);
Expand All @@ -852,5 +852,6 @@ process.on('SIGINT', shutdown);
let disableReflectionApi = false;

export function __disableReflectionApi() {
console.log("disabling reflection");
disableReflectionApi = true;
}
8 changes: 7 additions & 1 deletion js/genkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@
* @module /
*/
export * from './common.js';
export { Genkit, genkit, type GenkitOptions } from './genkit.js';
export {
Genkit,
__disableReflectionApi,
genkit,
shutdown,
type GenkitOptions,
} from './genkit.js';
35 changes: 24 additions & 11 deletions js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions js/testapps/evals/jest.config.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"testEnvironment": "node",
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"moduleFileExtensions": ["ts", "tsx", "js", "json"],
"testMatch": ["**/?(*.)+(spec|test).ts"],
"verbose": true,
"preset": "ts-jest/presets/default-esm",
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"useESM": true
}
]
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
}
8 changes: 7 additions & 1 deletion js/testapps/evals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"build:clean": "rimraf ./lib",
"build:watch": "tsc --watch",
"dev": "tsx --watch src/index.ts",
"genkit:dev": "genkit start -- tsx --watch src/index.ts"
"genkit:dev": "genkit start -- tsx --watch src/index.ts",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles",
"test:watch": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --watch"
},
"keywords": [],
"author": "",
Expand All @@ -28,8 +30,12 @@
"pdfjs-dist-legacy": "^1.0.1"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.12",
"cross-env": "^7.0.3",
"jest": "^29.7.0",
"rimraf": "^6.0.1",
"ts-jest": "^29.1.2",
"tsx": "^4.19.2",
"typescript": "^5.3.3"
}
Expand Down
33 changes: 0 additions & 33 deletions js/testapps/evals/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@
import { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
import { genkitEval, GenkitMetric } from '@genkit-ai/evaluator';
import {
gemini15Flash,
gemini15Pro,
googleAI,
textEmbeddingGecko001,
} from '@genkit-ai/googleai';
import { vertexAI } from '@genkit-ai/vertexai';
import {
vertexAIEvaluation,
VertexAIEvaluationMetricType,
} from '@genkit-ai/vertexai/evaluation';
import { genkit } from 'genkit';
import { langchain } from 'genkitx-langchain';

// Turn off safety checks for evaluation so that the LLM as an evaluator can
// respond appropriately to potentially harmful content without error.
Expand Down Expand Up @@ -70,37 +63,11 @@ export const ai = genkit({
},
],
}),
vertexAI({
location: 'us-central1',
}),
vertexAIEvaluation({
location: 'us-central1',
metrics: [
VertexAIEvaluationMetricType.BLEU,
VertexAIEvaluationMetricType.GROUNDEDNESS,
VertexAIEvaluationMetricType.SAFETY,
{
type: VertexAIEvaluationMetricType.ROUGE,
metricSpec: {
rougeType: 'rougeLsum',
useStemmer: true,
splitSummaries: 'true',
},
},
],
}),
devLocalVectorstore([
{
indexName: 'pdfQA',
embedder: textEmbeddingGecko001,
},
]),
langchain({
evaluators: {
criteria: ['coherence'],
labeledCriteria: ['correctness'],
judge: gemini15Flash,
},
}),
],
});
10 changes: 3 additions & 7 deletions js/testapps/evals/src/pdf-rag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { z } from 'genkit';
import { Document } from 'genkit/retriever';
import { chunk } from 'llm-chunk';
import path from 'path';
import { getDocument } from 'pdfjs-dist-legacy';
import * as pdfjsDistLegacy from 'pdfjs-dist-legacy';
import { ai } from './genkit.js';

export const pdfChatRetriever = devLocalRetrieverRef('pdfQA');
Expand Down Expand Up @@ -100,11 +100,7 @@ export const simpleEcho = ai.defineFlow(
outputSchema: z.string(),
},
async (i) => {
const llmResponse = await ai.generate({
model: gemini15Flash,
prompt: i,
});
return llmResponse.text;
return i;
}
);

Expand Down Expand Up @@ -143,7 +139,7 @@ export const indexPdf = ai.defineFlow(
);

async function extractText(filePath: string): Promise<string> {
let doc = await getDocument(filePath).promise;
let doc = await pdfjsDistLegacy.getDocument(filePath).promise;

let pdfTxt = '';
const numPages = doc.numPages;
Expand Down
28 changes: 28 additions & 0 deletions js/testapps/evals/src/sample.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { __disableReflectionApi, shutdown } from 'genkit';
import { pdfQA, simpleEcho } from './pdf-rag.js';

describe('Math functions', () => {
beforeAll(() => {
__disableReflectionApi();
});

// afterAll(async () => {
// await shutdown();
// });
// test('adds 1 + 2 to equal 3', () => {
// expect(1 + 2).toBe(3);
// });

// test('multiplies 3 * 4 to equal 12', () => {
// expect(3 * 4).toBe(12);
// });

test('simpleEcho', async () => {

expect(await simpleEcho('Hellozzz world')).toBe('Hellozzz world');
});

// test('pdfQA', async () => {
// expect(await pdfQA('Can I feed milk to my cats?')).toContain('No');
// });
});