-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
147 lines (132 loc) · 4.45 KB
/
context.ts
File metadata and controls
147 lines (132 loc) · 4.45 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as contextApi from "@opentelemetry/api";
import { HumanloopRuntimeError } from "./error";
import {
HUMANLOOP_CONTEXT_DECORATOR,
HUMANLOOP_CONTEXT_EVALUATION,
HUMANLOOP_CONTEXT_TRACE_ID,
} from "./otel/constants";
export function getTraceId(): string | undefined {
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID);
const value = contextApi.context.active().getValue(key);
return (value || undefined) as string | undefined;
}
export function setTraceId(flowLogId: string): contextApi.Context {
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID);
return contextApi.context.active().setValue(key, flowLogId);
}
export type DecoratorContext = {
path: string;
type: "prompt" | "tool" | "flow";
version: Record<string, unknown>;
};
export function setDecoratorContext(
decoratorContext: DecoratorContext,
): contextApi.Context {
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_DECORATOR);
return contextApi.context.active().setValue(key, decoratorContext);
}
export function getDecoratorContext(): DecoratorContext | undefined {
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_DECORATOR);
return (contextApi.context.active().getValue(key) || undefined) as
| DecoratorContext
| undefined;
}
export class EvaluationContext {
public sourceDatapointId: string;
public runId: string;
public fileId: string;
public path: string;
private _logged: boolean;
private _callback: (log_id: string) => Promise<void>;
constructor({
sourceDatapointId,
runId,
evalCallback,
fileId,
path,
}: {
sourceDatapointId: string;
runId: string;
evalCallback: (log_id: string) => Promise<void>;
fileId: string;
path: string;
}) {
this.sourceDatapointId = sourceDatapointId;
this.runId = runId;
this._callback = evalCallback;
this.fileId = fileId;
this.path = path;
this._logged = false;
}
public get logged(): boolean {
return this._logged;
}
public logArgsWithContext({
logArgs,
forOtel,
path,
fileId,
}: {
logArgs: Record<string, any>;
forOtel: boolean;
path?: string;
fileId?: string;
}): [Record<string, any>, ((log_id: string) => Promise<void>) | null] {
if (path === undefined && fileId === undefined) {
throw new HumanloopRuntimeError(
"Internal error: Evaluation context called without providing a path or file_id",
);
}
if (this._logged) {
return [logArgs, null];
}
if (this.path !== undefined && this.path === path) {
this._logged = true;
return [
forOtel
? {
...logArgs,
source_datapoint_id: this.sourceDatapointId,
run_id: this.runId,
}
: {
...logArgs,
sourceDatapointId: this.sourceDatapointId,
runId: this.runId,
},
this._callback,
];
} else if (this.fileId !== undefined && this.fileId === fileId) {
this._logged = true;
return [
forOtel
? {
...logArgs,
sourceDatapointId: this.sourceDatapointId,
runId: this.runId,
}
: {
...logArgs,
sourceDatapointId: this.sourceDatapointId,
runId: this.runId,
},
this._callback,
];
} else {
return [logArgs, null];
}
}
}
// ... existing code ...
export function setEvaluationContext(
evaluationContext: EvaluationContext,
): contextApi.Context {
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_EVALUATION);
return contextApi.context.active().setValue(key, evaluationContext);
}
export function getEvaluationContext(): EvaluationContext | undefined {
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_EVALUATION);
return (contextApi.context.active().getValue(key) || undefined) as
| EvaluationContext
| undefined;
}