Skip to content
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

feat: show a link to the Dev UI on running evals #2024

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions genkit-tools/cli/src/commands/eval-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ import {
} from '@genkit-ai/tools-common/eval';
import {
confirmLlmUse,
getDevUiUrl,
hasAction,
loadInferenceDatasetFile,
logger,
} from '@genkit-ai/tools-common/utils';
import * as clc from 'colorette';
import { Command } from 'commander';
import { runWithManager } from '../utils/manager-utils';

Expand Down Expand Up @@ -167,9 +169,18 @@ export const evalFlow = new Command('eval:flow')
await exportFn(evalRun, options.output);
}

console.log(
`Succesfully ran evaluation, with evalId: ${evalRun.key.evalRunId}`
);
const devUiUrl = await getDevUiUrl();
if (devUiUrl) {
logger.info(
clc.green(
`\nView the evaluation results at: ${devUiUrl}/evaluate/${evalRun.key.evalRunId}`
)
);
} else {
logger.info(
`Succesfully ran evaluation, with evalId: ${evalRun.key.evalRunId}`
);
}
});
}
);
Expand Down
17 changes: 14 additions & 3 deletions genkit-tools/cli/src/commands/eval-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import {
} from '@genkit-ai/tools-common/eval';
import {
confirmLlmUse,
getDevUiUrl,
loadEvaluationDatasetFile,
logger,
} from '@genkit-ai/tools-common/utils';
import * as clc from 'colorette';
import { Command } from 'commander';
import { runWithManager } from '../utils/manager-utils';

Expand Down Expand Up @@ -113,8 +115,17 @@ export const evalRun = new Command('eval:run')
await exportFn(evalRun, options.output);
}

console.log(
`Succesfully ran evaluation, with evalId: ${evalRun.key.evalRunId}`
);
const devUiUrl = await getDevUiUrl();
if (devUiUrl) {
logger.info(
clc.green(
`\nView the evaluation results at: ${devUiUrl}/evaluate/${evalRun.key.evalRunId}`
)
);
} else {
logger.info(
`Succesfully ran evaluation, with evalId: ${evalRun.key.evalRunId}`
);
}
});
});
3 changes: 2 additions & 1 deletion genkit-tools/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { RuntimeManager } from '@genkit-ai/tools-common/manager';
import { startServer } from '@genkit-ai/tools-common/server';
import { logger } from '@genkit-ai/tools-common/utils';
import { logger, setDevUiUrl } from '@genkit-ai/tools-common/utils';
import { spawn } from 'child_process';
import { Command } from 'commander';
import getPort, { makeRange } from 'get-port';
Expand Down Expand Up @@ -56,6 +56,7 @@ export const start = new Command('start')
if (options.open) {
open(`http://localhost:${port}`);
}
await setDevUiUrl(`http://localhost:${port}`);
}
await managerPromise.then((manager: RuntimeManager) => {
const telemetryServerUrl = manager?.telemetryServerUrl;
Expand Down
20 changes: 2 additions & 18 deletions genkit-tools/cli/src/commands/ui-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

import {
DevToolsInfo,
findServersDir,
isValidDevToolsInfo,
logger,
waitUntilHealthy,
} from '@genkit-ai/tools-common/utils';
Expand All @@ -33,24 +35,6 @@ interface StartOptions {
open?: boolean;
}

export interface DevToolsInfo {
/** URL of the dev tools server. */
url: string;
/** Timestamp of when the dev tools server was started. */
timestamp: string;
}

/**
* Checks if the provided data is a valid dev tools server state file.
*/
export function isValidDevToolsInfo(data: any): data is DevToolsInfo {
return (
typeof data === 'object' &&
typeof data.url === 'string' &&
typeof data.timestamp === 'string'
);
}

/** Command to start the Genkit Developer UI. */
export const uiStart = new Command('ui:start')
.description(
Expand Down
3 changes: 2 additions & 1 deletion genkit-tools/cli/src/commands/ui-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

import {
DevToolsInfo,
findServersDir,
isValidDevToolsInfo,
logger,
waitUntilUnresponsive,
} from '@genkit-ai/tools-common/utils';
Expand All @@ -24,7 +26,6 @@ import * as clc from 'colorette';
import { Command } from 'commander';
import fs from 'fs/promises';
import path from 'path';
import { DevToolsInfo, isValidDevToolsInfo } from './ui-start';

/** Command to stop the Genkit Developer UI. */
export const uiStop = new Command('ui:stop')
Expand Down
62 changes: 62 additions & 0 deletions genkit-tools/common/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
import * as fs from 'fs/promises';
import * as path from 'path';
import { Runtime } from '../manager/types';
import { logger } from './logger';

export interface DevToolsInfo {
/** URL of the dev tools server. */
url: string;
/** Timestamp of when the dev tools server was started. */
timestamp: string;
}

/**
* Finds the project root by looking for a `package.json` file.
Expand Down Expand Up @@ -215,3 +223,57 @@ export async function retriable<T>(
attempt++;
}
}

/**
* Checks if the provided data is a valid dev tools server state file.
*/
export function isValidDevToolsInfo(data: any): data is DevToolsInfo {
return (
typeof data === 'object' &&
typeof data.url === 'string' &&
typeof data.timestamp === 'string'
);
}

/**
* Finds the URL of the currently running Genkit Dev UI instance (if available).
*/
export async function getDevUiUrl(
projectRoot?: string
): Promise<string | undefined> {
const serversDir = await findServersDir(projectRoot);
const toolsJsonPath = path.join(serversDir, 'tools.json');
Copy link
Collaborator

Choose a reason for hiding this comment

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

you're always writing to tools.json? this assumes we track only the last instance of the dev ui? do we want to track all instances?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not know that we'd have multiple Dev UI instances on the same runtime. What would the difference between the instances be?

I can add support for multiple instances if we want, I just need some direction on how we do that for other state files.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume like if you're working on a local version of the Dev UI.

try {
const toolsJsonContent = await fs.readFile(toolsJsonPath, 'utf-8');
const serverInfo = JSON.parse(toolsJsonContent) as DevToolsInfo;
if (isValidDevToolsInfo(serverInfo)) {
return (await checkServerHealth(serverInfo.url))
? serverInfo.url
: undefined;
}
} catch (error) {
logger.info('Error reading tools config', error);
return undefined;
}
}

/**
* Sets the Genkit Dev UI URL
*/
export async function setDevUiUrl(
url: string,
projectRoot?: string
): Promise<void> {
const serversDir = await findServersDir(projectRoot);
const toolsJsonPath = path.join(serversDir, 'tools.json');
try {
const serverInfo = {
url,
timestamp: new Date().toISOString(),
} as DevToolsInfo;
await fs.mkdir(serversDir, { recursive: true });
await fs.writeFile(toolsJsonPath, JSON.stringify(serverInfo, null, 2));
} catch (error) {
logger.info('Error writing tools config', error);
}
}
2 changes: 1 addition & 1 deletion js/testapps/evals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build:watch": "tsc --watch",
"build-and-run": "pnpm build && node lib/index.js",
"dev": "tsx --watch src/index.ts",
Copy link
Contributor

Choose a reason for hiding this comment

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

We already have this, do we need to change genkit:dev?

"genkit:dev": "genkit start -- tsx --watch src/index.ts"
"genkit:dev": "tsx --watch src/index.ts"
},
"keywords": [],
"author": "",
Expand Down
Loading