Skip to content

Commit 0abe865

Browse files
author
Andrei Bratu
committed
flow decorator upserts two versions
1 parent 77892d5 commit 0abe865

6 files changed

Lines changed: 531 additions & 387 deletions

File tree

src/evals/run.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { HumanloopRuntimeError } from "../error";
4848
import { Humanloop, HumanloopClient } from "../index";
4949
import {
5050
Dataset,
51+
EvalFileType,
5152
Evaluator,
5253
EvaluatorCheck,
5354
File as FileEvalConfig,
@@ -412,7 +413,7 @@ function callableIsHumanloopDecorator<
412413
I extends Record<string, unknown> & { messages?: any[] },
413414
O,
414415
>(file: FileEvalConfig<I, O>): boolean {
415-
return file.callable !== undefined && "decorator" in file.callable;
416+
return file.callable !== undefined && "file" in file.callable;
416417
}
417418

418419
function fileOrFileInsideHLUtility<
@@ -476,7 +477,7 @@ function getFileType<I extends Record<string, unknown> & { messages?: any[] }, O
476477
): FileEvalConfig<I, O> {
477478
// Determine the `type` of the `file` to Evaluate - if not `type` provided, default to `flow`
478479
try {
479-
let type_ = file.type as FileType;
480+
let type_ = file.type as EvalFileType;
480481
console.info(
481482
`${CYAN}Evaluating your ${type_} function corresponding to \`${file.path || file.id}\` on Humanloop${RESET}\n\n`,
482483
);
@@ -541,7 +542,7 @@ async function getHLFile<I extends Record<string, unknown> & { message?: any[] }
541542
fileConfig: FileEvalConfig<I, O>,
542543
): Promise<[EvaluatedFile, FileEvalConfig<I, O>["callable"]]> {
543544
let file_ = fileOrFileInsideHLUtility(fileConfig);
544-
file_ = getFileType(fileConfig);
545+
file_ = getFileType(file_);
545546

546547
return await resolveFile(client, file_);
547548
}
@@ -626,14 +627,27 @@ async function resolveFile<I extends Record<string, unknown> & { message?: any[]
626627
`You are trying to create a new version of the File by passing the ${callable} argument. You must pass either the \`file.path\` or \`file.fileId\` argument and provide proper \`file.version\` for upserting the File.`,
627628
);
628629
}
629-
let hlFile = await safeGetDefaultFileVersion(client, fileConfig);
630630

631631
if ((versionId || environment) && (callable || version)) {
632632
throw new HumanloopRuntimeError(
633633
"You are trying to create a local Evaluation while requesting a specific File version by version ID or environment.",
634634
);
635635
}
636636

637+
let hlFile: EvaluatedFile;
638+
639+
try {
640+
hlFile = (await safeGetDefaultFileVersion(client, fileConfig)) as EvaluatedFile;
641+
} catch (error: any) {
642+
if (!version || !path || fileId) {
643+
throw new HumanloopRuntimeError(
644+
"File does not exist on Humanloop. Please provide a `file.path` and a version to create a new version.",
645+
);
646+
}
647+
console.log("UPSERTING FILE", JSON.stringify(fileConfig, null, 2));
648+
return [await upsertFile(client, fileConfig), callable];
649+
}
650+
637651
if (version) {
638652
// User responsibility to provide adequate file.version for upserting the file
639653
console.info(
@@ -716,7 +730,7 @@ async function upsertFile<I extends Record<string, unknown> & { messages?: any[]
716730
switch (type) {
717731
case "flow":
718732
// Be more lenient with Flow versions as they are arbitrary json
719-
const flowVersion = { attributes: version };
733+
const flowVersion = version ? { ...version } : { attributes: {} };
720734
const fileDictWithFlowVersion = { ...fileConfig, ...flowVersion };
721735
hlFile = await client.flows.upsert(fileDictWithFlowVersion as FlowRequest);
722736
break;
@@ -726,9 +740,6 @@ async function upsertFile<I extends Record<string, unknown> & { messages?: any[]
726740
case "tool":
727741
hlFile = await client.tools.upsert(fileDict as ToolRequest);
728742
break;
729-
case "evaluator":
730-
hlFile = await client.evaluators.upsert(fileDict as EvaluatorRequest);
731-
break;
732743
default:
733744
throw new HumanloopRuntimeError(`Unsupported File type: ${type}`);
734745
}

src/evals/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ interface Identifiers {
5050
environment?: string;
5151
}
5252

53+
export type EvalFileType = "prompt" | "tool" | "flow" | "agent";
54+
5355
export interface File<I extends Record<string, unknown> & { messages?: any[] }, O>
5456
extends Identifiers {
5557
/** The type of File this callable relates to on Humanloop. */
56-
type?: FileType;
58+
type?: EvalFileType;
5759
/** The contents uniquely define the version of the File on Humanloop. */
5860
version?: Version;
5961
/**

0 commit comments

Comments
 (0)