-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
outputs.ts
108 lines (93 loc) · 2.64 KB
/
outputs.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { type BaseMessage, type BaseMessageChunk } from "./messages/index.js";
export const RUN_KEY = "__run";
/**
* Output of a single generation.
*/
export interface Generation {
/**
* Generated text output
*/
text: string;
/**
* Raw generation info response from the provider.
* May include things like reason for finishing (e.g. in {@link OpenAI})
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
generationInfo?: Record<string, any>;
}
export type GenerationChunkFields = {
text: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
generationInfo?: Record<string, any>;
};
/**
* Chunk of a single generation. Used for streaming.
*/
export class GenerationChunk implements Generation {
public text: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public generationInfo?: Record<string, any>;
constructor(fields: GenerationChunkFields) {
this.text = fields.text;
this.generationInfo = fields.generationInfo;
}
concat(chunk: GenerationChunk): GenerationChunk {
return new GenerationChunk({
text: this.text + chunk.text,
generationInfo: {
...this.generationInfo,
...chunk.generationInfo,
},
});
}
}
/**
* Contains all relevant information returned by an LLM.
*/
export type LLMResult = {
/**
* List of the things generated. Each input could have multiple {@link Generation | generations}, hence this is a list of lists.
*/
generations: Generation[][];
/**
* Dictionary of arbitrary LLM-provider specific output.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
llmOutput?: Record<string, any>;
/**
* Dictionary of run metadata
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[RUN_KEY]?: Record<string, any>;
};
export interface ChatGeneration extends Generation {
message: BaseMessage;
}
export type ChatGenerationChunkFields = GenerationChunkFields & {
message: BaseMessageChunk;
};
export class ChatGenerationChunk
extends GenerationChunk
implements ChatGeneration
{
public message: BaseMessageChunk;
constructor(fields: ChatGenerationChunkFields) {
super(fields);
this.message = fields.message;
}
concat(chunk: ChatGenerationChunk) {
return new ChatGenerationChunk({
text: this.text + chunk.text,
generationInfo: {
...this.generationInfo,
...chunk.generationInfo,
},
message: this.message.concat(chunk.message),
});
}
}
export interface ChatResult {
generations: ChatGeneration[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
llmOutput?: Record<string, any>;
}