-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathhttp_response.ts
76 lines (66 loc) · 2.27 KB
/
http_response.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
import { BaseMessage } from "@langchain/core/messages";
import {
BaseTransformOutputParser,
StringOutputParser,
} from "@langchain/core/output_parsers";
export type HttpResponseOutputParserInput = {
outputParser?: BaseTransformOutputParser;
contentType?: "text/plain" | "text/event-stream";
};
/**
* OutputParser that formats chunks emitted from an LLM for different HTTP content types.
*/
export class HttpResponseOutputParser extends BaseTransformOutputParser<Uint8Array> {
static lc_name() {
return "HttpResponseOutputParser";
}
lc_namespace = ["langchain", "output_parser"];
lc_serializable = true;
outputParser: BaseTransformOutputParser = new StringOutputParser();
contentType: "text/plain" | "text/event-stream" = "text/plain";
constructor(fields?: HttpResponseOutputParserInput) {
super(fields);
this.outputParser = fields?.outputParser ?? this.outputParser;
this.contentType = fields?.contentType ?? this.contentType;
}
async *_transform(
inputGenerator: AsyncGenerator<string | BaseMessage>
): AsyncGenerator<Uint8Array> {
for await (const chunk of this.outputParser._transform(inputGenerator)) {
if (typeof chunk === "string") {
yield this.parse(chunk);
} else {
yield this.parse(JSON.stringify(chunk));
}
}
if (this.contentType === "text/event-stream") {
const encoder = new TextEncoder();
yield encoder.encode(`event: end\n\n`);
}
}
/**
* Parses a string output from an LLM call. This method is meant to be
* implemented by subclasses to define how a string output from an LLM
* should be parsed.
* @param text The string output from an LLM call.
* @param callbacks Optional callbacks.
* @returns A promise of the parsed output.
*/
async parse(text: string): Promise<Uint8Array> {
const chunk = await this.outputParser.parse(text);
const encoder = new TextEncoder();
if (this.contentType === "text/event-stream") {
return encoder.encode(`event: data\ndata: ${JSON.stringify(chunk)}\n\n`);
}
let parsedChunk;
if (typeof chunk === "string") {
parsedChunk = chunk;
} else {
parsedChunk = JSON.stringify(chunk);
}
return encoder.encode(parsedChunk);
}
getFormatInstructions(): string {
return "";
}
}