-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
97 lines (82 loc) · 3.1 KB
/
context.ts
File metadata and controls
97 lines (82 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import hash from "stable-hash";
import { FlowLogRequest, PromptLogRequest } from "../api";
import { DatapointResponse } from "../api";
import { Humanloop } from "../index";
import { Version } from "./types";
type EvaluationContextState = {
fileId?: string;
path?: string;
uploadCallback: (logId: string, datapoint: DatapointResponse) => void;
evaluatedVersion?: Version;
};
type EvaluationContextKey = {
inputs: Record<string, unknown> | undefined;
messages: Humanloop.ChatMessage[] | undefined;
};
type EvaluationContextValue = {
runId: string;
sourceDatapointId: string;
uploadCallback: (logId: string) => void;
};
class EvaluationContext {
private state?: EvaluationContextState;
private static instance: EvaluationContext;
private inputMappings: Map<string, EvaluationContextValue[]> = new Map();
private constructor() {}
public static getInstance(): EvaluationContext {
if (!EvaluationContext.instance) {
EvaluationContext.instance = new EvaluationContext();
}
return EvaluationContext.instance;
}
public setState(state: EvaluationContextState): void {
this.state = state;
}
public getState(): Omit<EvaluationContextState, "uploadCallback"> | undefined {
return this.state === undefined
? this.state
: {
fileId: this.state.fileId,
path: this.state.path,
evaluatedVersion: this.state.evaluatedVersion,
};
}
public addDatapoint(datapoint: DatapointResponse, runId: string): void {
if (this.state === undefined) {
throw new Error("EvaluationContext state is not set");
}
const key = hash({ inputs: datapoint.inputs, messages: datapoint.messages });
if (!this.inputMappings.has(key)) {
this.inputMappings.set(key, []);
}
this.inputMappings.get(key)!.push({
runId,
sourceDatapointId: datapoint.id,
uploadCallback: (logId: string) =>
this.state!.uploadCallback(logId, datapoint),
});
}
public getDatapoint(key: EvaluationContextKey): EvaluationContextValue {
if (key.inputs !== undefined && "inputs" in key.inputs) {
key = { ...key, inputs: key.inputs.inputs as Record<string, unknown> };
}
const mappings = this.inputMappings.get(hash(key));
if (!mappings || mappings.length === 0) {
throw new Error(
`No input mappings found for: ${JSON.stringify(key)}. Try using peekDatapoint() first.`,
);
}
return mappings.pop()!;
}
public peekDatapoint(key: EvaluationContextKey): boolean {
const mappings = this.inputMappings.get(hash(key));
return mappings !== undefined && mappings.length > 0;
}
public isEvaluatedFile(args: FlowLogRequest | PromptLogRequest) {
return (
this.state &&
(this.state.fileId === args.id || this.state.path === args.path)
);
}
}
export const evaluationContext = EvaluationContext.getInstance();