-
Notifications
You must be signed in to change notification settings - Fork 337
/
EngineResponse.ts
90 lines (78 loc) · 1.91 KB
/
EngineResponse.ts
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
import type { NodeWithScore } from "./Node.js";
import type {
ChatMessage,
ChatResponse,
ChatResponseChunk,
} from "./llm/types.js";
import { extractText } from "./llm/utils.js";
export class EngineResponse implements ChatResponse, ChatResponseChunk {
sourceNodes?: NodeWithScore[];
metadata: Record<string, unknown> = {};
message: ChatMessage;
raw: object | null;
#stream: boolean;
private constructor(
chatResponse: ChatResponse,
stream: boolean,
sourceNodes?: NodeWithScore[],
) {
this.message = chatResponse.message;
this.raw = chatResponse.raw;
this.sourceNodes = sourceNodes;
this.#stream = stream;
}
static fromResponse(
response: string,
stream: boolean,
sourceNodes?: NodeWithScore[],
): EngineResponse {
return new EngineResponse(
EngineResponse.toChatResponse(response),
stream,
sourceNodes,
);
}
private static toChatResponse(
response: string,
raw: object | null = null,
): ChatResponse {
return {
message: {
content: response,
role: "assistant",
},
raw,
};
}
static fromChatResponse(
chatResponse: ChatResponse,
sourceNodes?: NodeWithScore[],
): EngineResponse {
return new EngineResponse(chatResponse, false, sourceNodes);
}
static fromChatResponseChunk(
chunk: ChatResponseChunk,
sourceNodes?: NodeWithScore[],
): EngineResponse {
return new EngineResponse(
this.toChatResponse(chunk.delta, chunk.raw),
true,
sourceNodes,
);
}
// @deprecated use 'message' instead
get response(): string {
return extractText(this.message.content);
}
get delta(): string {
if (!this.#stream) {
console.warn(
"delta is only available for streaming responses. Consider using 'message' instead.",
);
}
return extractText(this.message.content);
}
toString() {
return this.response ?? "";
}
}