Skip to content

Commit

Permalink
fix: update auth to context (#1822)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbushi authored Feb 4, 2025
1 parent 076a28a commit 5e6a61b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 38 deletions.
6 changes: 3 additions & 3 deletions docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ export const selfSummaryFlow = ai.defineFlow(
```

When testing flows with Genkit dev tools, you are able to specify this auth
object in the UI, or on the command line with the `--auth` flag:
object in the UI, or on the command line with the `--context` flag:

```posix-terminal
genkit flow:run selfSummaryFlow '{"uid": "abc-def"}' --auth '{"uid": "abc-def"}'
genkit flow:run selfSummaryFlow '{"uid": "abc-def"}' --context '{"auth": {"email_verified": true}}'
```

## Cloud Functions for Firebase integration
Expand Down Expand Up @@ -153,7 +153,7 @@ above. When running this flow during development, you would pass the user object
in the same way:

```posix-terminal
genkit flow:run selfSummaryFlow '{"uid": "abc-def"}' --auth '{"admin": true}'
genkit flow:run selfSummaryFlow '{"uid": "abc-def"}' --context '{"auth": {"admin": true}}'
```

Whenever you expose a Cloud Function to the wider internet, it is vitally
Expand Down
5 changes: 2 additions & 3 deletions docs/evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const qaFlow = ai.defineFlow({
const factDocs = await ai.retrieve({
retriever: dummyRetriever,
query,
options: { k: 2 },
});

const llmResponse = await ai.generate({
Expand Down Expand Up @@ -306,10 +305,10 @@ field and an optional `reference` field, like below:
]
```

If your flow requires auth, you may specify it using the `--auth` argument:
If your flow requires auth, you may specify it using the `--context` argument:

```posix-terminal
genkit eval:flow qaFlow --input testInputs.json --auth "{\"email_verified\": true}"
genkit eval:flow qaFlow --input testInputs.json --context '{"auth": {"email_verified": true}}'
```

By default, the `eval:flow` and `eval:run` commands use all available metrics
Expand Down
10 changes: 3 additions & 7 deletions genkit-tools/cli/src/commands/eval-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { runWithManager } from '../utils/manager-utils';
interface EvalFlowRunCliOptions {
input?: string;
output?: string;
auth?: string;
context?: string;
evaluators?: string;
force?: boolean;
outputFormat: string;
Expand All @@ -65,11 +65,7 @@ export const evalFlow = new Command('eval:flow')
'--input <input>',
'Input dataset ID or JSON file to be used for evaluation'
)
.option(
'-a, --auth <JSON>',
'JSON object passed to authPolicy and stored in local state as auth',
''
)
.option('-c, --context <JSON>', 'JSON object passed to context', '')
.option(
'-o, --output <filename>',
'Name of the output file to write evaluation results. Defaults to json output.'
Expand Down Expand Up @@ -149,7 +145,7 @@ export const evalFlow = new Command('eval:flow')
manager,
actionRef,
inferenceDataset,
auth: options.auth,
context: options.context,
});

const evalRun = await runEvaluation({
Expand Down
10 changes: 3 additions & 7 deletions genkit-tools/cli/src/commands/flow-batch-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface FlowBatchRunOptions {
wait?: boolean;
output?: string;
label?: string;
auth?: string;
context?: string;
}

/** Command to run flows with batch input. */
Expand All @@ -34,11 +34,7 @@ export const flowBatchRun = new Command('flow:batchRun')
.argument('<flowName>', 'name of the flow to run')
.argument('<inputFileName>', 'JSON batch data to use to run the flow')
.option('-w, --wait', 'Wait for the flow to complete', false)
.option(
'-a, --auth <JSON>',
'JSON object passed to authPolicy and stored in local state as auth',
''
)
.option('-c, --context <JSON>', 'JSON object passed to context', '')
.option('--output <filename>', 'name of the output file to store the output')
.option('--label [label]', 'label flow run in this batch')
.action(
Expand All @@ -64,7 +60,7 @@ export const flowBatchRun = new Command('flow:batchRun')
let response = await manager.runAction({
key: `/flow/${flowName}`,
input: data,
context: options.auth ? JSON.parse(options.auth) : undefined,
context: options.context ? JSON.parse(options.context) : undefined,
telemetryLabels: options.label
? { batchRun: options.label }
: undefined,
Expand Down
10 changes: 3 additions & 7 deletions genkit-tools/cli/src/commands/flow-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface FlowRunOptions {
wait?: boolean;
output?: string;
stream?: boolean;
auth?: string;
context?: string;
}

/** Command to run a flow. */
Expand All @@ -33,11 +33,7 @@ export const flowRun = new Command('flow:run')
.argument('[data]', 'JSON data to use to start the flow')
.option('-w, --wait', 'Wait for the flow to complete', false)
.option('-s, --stream', 'Stream output', false)
.option(
'-a, --auth <JSON>',
'JSON object passed to authPolicy and stored in local state as auth',
''
)
.option('-c, --context <JSON>', 'JSON object passed to context', '')
.option(
'--output <filename>',
'name of the output file to store the extracted data'
Expand All @@ -50,7 +46,7 @@ export const flowRun = new Command('flow:run')
{
key: `/flow/${flowName}`,
input: data ? JSON.parse(data) : undefined,
context: options.auth ? JSON.parse(options.auth) : undefined,
context: options.context ? JSON.parse(options.context) : undefined,
},
options.stream
? (chunk) => console.log(JSON.stringify(chunk, undefined, ' '))
Expand Down
22 changes: 12 additions & 10 deletions genkit-tools/common/src/eval/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function runNewEvaluation(
manager,
actionRef,
inferenceDataset,
auth: request.options?.auth,
context: request.options?.context,
actionConfig: request.options?.actionConfig,
});
const evaluatorActions = await getMatchingEvaluatorActions(
Expand All @@ -136,10 +136,11 @@ export async function runInference(params: {
manager: RuntimeManager;
actionRef: string;
inferenceDataset: Dataset;
auth?: string;
context?: string;
actionConfig?: any;
}): Promise<EvalInput[]> {
const { manager, actionRef, inferenceDataset, auth, actionConfig } = params;
const { manager, actionRef, inferenceDataset, context, actionConfig } =
params;
if (!isSupportedActionRef(actionRef)) {
throw new Error('Inference is only supported on flows and models');
}
Expand All @@ -148,7 +149,7 @@ export async function runInference(params: {
manager,
actionRef,
inferenceDataset,
auth,
context,
actionConfig,
});
return evalDataset;
Expand Down Expand Up @@ -235,10 +236,11 @@ async function bulkRunAction(params: {
manager: RuntimeManager;
actionRef: string;
inferenceDataset: Dataset;
auth?: string;
context?: string;
actionConfig?: any;
}): Promise<EvalInput[]> {
const { manager, actionRef, inferenceDataset, auth, actionConfig } = params;
const { manager, actionRef, inferenceDataset, context, actionConfig } =
params;
const isModelAction = actionRef.startsWith('/model');
if (inferenceDataset.length === 0) {
throw new Error('Cannot run inference, no data provided');
Expand Down Expand Up @@ -268,7 +270,7 @@ async function bulkRunAction(params: {
manager,
actionRef,
sample,
auth,
context,
})
);
}
Expand All @@ -285,15 +287,15 @@ async function runFlowAction(params: {
manager: RuntimeManager;
actionRef: string;
sample: FullInferenceSample;
auth?: any;
context?: any;
}): Promise<InferenceRunState> {
const { manager, actionRef, sample, auth } = { ...params };
const { manager, actionRef, sample, context } = { ...params };
let state: InferenceRunState;
try {
const runActionResponse = await manager.runAction({
key: actionRef,
input: sample.input,
context: auth ? JSON.parse(auth) : undefined,
context: context ? JSON.parse(context) : undefined,
});
state = {
...sample,
Expand Down
2 changes: 1 addition & 1 deletion genkit-tools/common/src/types/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const RunNewEvaluationRequestSchema = z.object({
evaluators: z.array(z.string()).optional(),
options: z
.object({
auth: z.string().optional(),
context: z.string().optional(),
actionConfig: z
.any()
.describe('addition parameters required for inference')
Expand Down

0 comments on commit 5e6a61b

Please sign in to comment.