-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow.ts
More file actions
151 lines (139 loc) · 5.91 KB
/
flow.ts
File metadata and controls
151 lines (139 loc) · 5.91 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
148
149
150
151
import * as contextApi from "@opentelemetry/api";
import { ReadableSpan, Tracer } from "@opentelemetry/sdk-trace-node";
import { HumanloopClient } from "../Client";
import { ChatMessage, FlowLogRequest, FlowLogResponse } from "../api";
import { getTraceId, setDecoratorContext, setTraceId } from "../context";
import { jsonifyIfNotString, writeToOpenTelemetrySpan } from "../otel";
import {
HUMANLOOP_FILE_TYPE_KEY,
HUMANLOOP_FLOW_SPAN_NAME,
HUMANLOOP_LOG_KEY,
HUMANLOOP_PATH_KEY,
} from "../otel/constants";
export function flowUtilityFactory<I, O>(
client: HumanloopClient,
opentelemetryTracer: Tracer,
callable: (
args: I extends Record<string, unknown> & {
messages?: ChatMessage[];
}
? I
: never,
) => O,
path: string,
attributes?: Record<string, unknown>,
): (args: I) => Promise<O | undefined> & {
file: {
type: string;
version: { attributes?: Record<string, unknown> };
callable: (args: I) => Promise<O | undefined>;
};
} {
const flowKernel = { attributes: attributes || {} };
const fileType = "flow";
const wrappedFunction = async (
inputs: I extends Record<string, unknown> & {
messages?: ChatMessage[];
}
? I
: never,
) => {
return contextApi.context.with(
setDecoratorContext({
path: path,
type: fileType,
version: flowKernel,
}),
async () => {
return opentelemetryTracer.startActiveSpan(
HUMANLOOP_FLOW_SPAN_NAME,
async (span) => {
span.setAttribute(HUMANLOOP_PATH_KEY, path);
span.setAttribute(HUMANLOOP_FILE_TYPE_KEY, fileType);
const traceId = getTraceId();
const logInputs = { ...inputs } as Record<string, unknown>;
const logMessages = logInputs.messages as
| ChatMessage[]
| undefined;
delete logInputs.messages;
const initLogInputs: FlowLogRequest = {
inputs: logInputs,
messages: logMessages,
traceParentId: traceId,
};
const flowLogResponse: FlowLogResponse =
// @ts-ignore
await client.flows._log({
path,
flow: flowKernel,
logStatus: "incomplete",
...initLogInputs,
});
return await contextApi.context.with(
setTraceId(flowLogResponse.id),
async () => {
let logOutput: string | undefined;
let outputMessage: ChatMessage | undefined;
let logError: string | undefined;
let funcOutput: O | undefined;
try {
funcOutput = await callable(inputs);
if (
// @ts-ignore
funcOutput instanceof Object &&
"role" in funcOutput &&
"content" in funcOutput
) {
outputMessage =
funcOutput as unknown as ChatMessage;
logOutput = undefined;
} else {
logOutput = jsonifyIfNotString(
callable,
funcOutput,
);
outputMessage = undefined;
}
logError = undefined;
} catch (err: any) {
console.error(
`Error calling ${callable.name}:`,
err,
);
logOutput = undefined;
outputMessage = undefined;
logError = err.message || String(err);
funcOutput = undefined as unknown as O;
}
const updatedFlowLog = {
log_status: "complete",
output: logOutput,
error: logError,
output_message: outputMessage,
id: flowLogResponse.id,
};
writeToOpenTelemetrySpan(
span as unknown as ReadableSpan,
// @ts-ignore
updatedFlowLog,
HUMANLOOP_LOG_KEY,
);
span.end();
return funcOutput;
},
);
},
);
},
);
};
// @ts-ignore
return Object.assign(wrappedFunction, {
file: {
path: path,
type: fileType,
version: flowKernel,
callable: wrappedFunction,
},
});
}