Skip to content
Merged
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
38 changes: 38 additions & 0 deletions genkit-tools/common/src/manager/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,44 @@ export class RuntimeManager {
return response.data as Record<string, Action>;
}

/**
* Retrieves all valuess.
*/
async listValues(
input: apis.ListValuesRequest
): Promise<Record<string, unknown>> {
const runtime = input.runtimeId
? this.getRuntimeById(input.runtimeId)
: this.getMostRecentRuntime();
if (!runtime) {
throw new Error(
input?.runtimeId
? `No runtime found with ID ${input.runtimeId}.`
: 'No runtimes found. Make sure your app is running using `genkit start -- ...`. See getting started documentation.'
);
}
try {
const response = await axios.get(
`${runtime.reflectionServerUrl}/api/values`,
{
params: {
type: input.type,
},
}
);
return response.data as Record<string, unknown>;
} catch (err) {
if ((err as AxiosError).response?.status === 404) {
return {};
} else if ((err as AxiosError).response?.status === 400) {
throw new GenkitToolsError(
`Bad request: ${(err as AxiosError).response?.data}`
);
}
this.httpErrorHandler(err as AxiosError, 'Error listing values.');
}
}

/**
* Runs an action.
*/
Expand Down
7 changes: 7 additions & 0 deletions genkit-tools/common/src/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ export const TOOLS_SERVER_ROUTER = (manager: RuntimeManager) =>
return manager.listActions(input);
}),

/** Retrieves all values. */
listValues: loggedProcedure
.input(apis.ListValuesRequestSchema)
.query(async ({ input }): Promise<Record<string, unknown>> => {
return manager.listValues(input);
}),

/** Generate a .prompt file from messages and model config. */
createPrompt: loggedProcedure
.input(apis.CreatePromptRequestSchema)
Expand Down
16 changes: 16 additions & 0 deletions genkit-tools/common/src/types/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ export const ListActionsRequestSchema = z

export type ListActionsRequest = z.infer<typeof ListActionsRequestSchema>;

export const ListValuesRequestSchema = z.object({
runtimeId: z
.string()
.optional()
.describe(
'ID of the Genkit runtime to run the action on. Typically $pid-$port.'
),
type: z
.string()
.describe(
"The type of values to fetch. Currently only supports 'defaultModel'"
),
});

export type ListValuesRequest = z.infer<typeof ListValuesRequestSchema>;

export const RunActionRequestSchema = z.object({
runtimeId: z
.string()
Expand Down
24 changes: 24 additions & 0 deletions js/core/src/reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ export class ReflectionServer {
await this.stop();
});

server.get('/api/values', async (req, response, next) => {
logger.debug('Fetching values.');
try {
const type = req.query.type;
if (!type) {
response.status(400).send('Query parameter "type" is required.');
return;
}
if (type !== 'defaultModel') {
response
.status(400)
.send(
`'type' ${type} is not supported. Only 'defaultModel' is supported`
);
return;
}
const values = await this.registry.listValues(type as string);
response.send(values);
} catch (err) {
const { message, stack } = err as Error;
next({ message, stack });
}
});

server.get('/api/actions', async (_, response, next) => {
logger.debug('Fetching actions.');
try {
Expand Down
23 changes: 22 additions & 1 deletion js/genkit/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
generate,
generateStream,
loadPromptFolder,
modelRef,
prompt,
rerank,
retrieve,
Expand All @@ -46,6 +47,7 @@ import {
type GenerationCommonConfigSchema,
type IndexerParams,
type ModelArgument,
type ModelReference,
type Part,
type PromptConfig,
type PromptGenerateOptions,
Expand Down Expand Up @@ -953,7 +955,7 @@ export class Genkit implements HasRegistry {
this.registry.registerValue(
'defaultModel',
'defaultModel',
this.options.model
toModelRef(this.options.model)
);
}
if (this.options.promptDir !== null) {
Expand Down Expand Up @@ -1086,3 +1088,22 @@ let disableReflectionApi = false;
export function __disableReflectionApi() {
disableReflectionApi = true;
}

/** Helper method to map ModelArgument to ModelReference */
function toModelRef(
modelArg: ModelArgument<any> | undefined
): ModelReference<any> | undefined {
if (modelArg === undefined) {
return undefined;
}
if (typeof modelArg === 'string') {
return modelRef({ name: modelArg });
}
if ((modelArg as ModelReference<any>).name) {
return modelArg as ModelReference<any>;
}
const modelAction = modelArg as ModelAction;
return modelRef({
name: modelAction.__action.name,
});
}
Loading