-
Notifications
You must be signed in to change notification settings - Fork 323
/
types.ts
217 lines (190 loc) · 5.12 KB
/
types.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import type { Tokenizers } from "../GlobalsHelper.js";
import type { BaseEvent } from "../internal/type.js";
import type { BaseTool, JSONObject, ToolOutput, UUID } from "../types.js";
export type LLMStartEvent = BaseEvent<{
id: UUID;
messages: ChatMessage[];
}>;
export type LLMToolCallEvent = BaseEvent<{
toolCall: ToolCall;
}>;
export type LLMToolResultEvent = BaseEvent<{
toolCall: ToolCall;
toolResult: ToolOutput;
}>;
export type LLMEndEvent = BaseEvent<{
id: UUID;
response: ChatResponse;
}>;
export type LLMStreamEvent = BaseEvent<{
id: UUID;
chunk: ChatResponseChunk;
}>;
/**
* @internal
*/
export interface LLMChat<
AdditionalChatOptions extends object = object,
AdditionalMessageOptions extends object = object,
> {
chat(
params:
| LLMChatParamsStreaming<AdditionalChatOptions>
| LLMChatParamsNonStreaming<AdditionalChatOptions>,
): Promise<
| ChatResponse<AdditionalMessageOptions>
| AsyncIterable<ChatResponseChunk<AdditionalMessageOptions>>
>;
}
/**
* Unified language model interface
*/
export interface LLM<
AdditionalChatOptions extends object = object,
AdditionalMessageOptions extends object = object,
> extends LLMChat<AdditionalChatOptions> {
metadata: LLMMetadata;
/**
* Get a chat response from the LLM
*/
chat(
params: LLMChatParamsStreaming<
AdditionalChatOptions,
AdditionalMessageOptions
>,
): Promise<AsyncIterable<ChatResponseChunk>>;
chat(
params: LLMChatParamsNonStreaming<
AdditionalChatOptions,
AdditionalMessageOptions
>,
): Promise<ChatResponse<AdditionalMessageOptions>>;
/**
* Get a prompt completion from the LLM
*/
complete(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
complete(
params: LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse>;
}
export type MessageType = "user" | "assistant" | "system" | "memory";
export type TextChatMessage<AdditionalMessageOptions extends object = object> =
{
content: string;
role: MessageType;
options?: undefined | AdditionalMessageOptions;
};
export type ChatMessage<AdditionalMessageOptions extends object = object> = {
content: MessageContent;
role: MessageType;
options?: undefined | AdditionalMessageOptions;
};
export interface ChatResponse<
AdditionalMessageOptions extends object = object,
> {
message: ChatMessage<AdditionalMessageOptions>;
/**
* Raw response from the LLM
*
* If LLM response an iterable of chunks, this will be an array of those chunks
*/
raw: object | null;
}
export type ChatResponseChunk<
AdditionalMessageOptions extends object = object,
> = {
raw: object | null;
delta: string;
options?: undefined | AdditionalMessageOptions;
};
export interface CompletionResponse {
text: string;
/**
* Raw response from the LLM
*
* It's possible that this is `null` if the LLM response an iterable of chunks
*/
raw: object | null;
}
export type LLMMetadata = {
model: string;
temperature: number;
topP: number;
maxTokens?: number;
contextWindow: number;
tokenizer: Tokenizers | undefined;
};
export interface LLMChatParamsBase<
AdditionalChatOptions extends object = object,
AdditionalMessageOptions extends object = object,
> {
messages: ChatMessage<AdditionalMessageOptions>[];
additionalChatOptions?: AdditionalChatOptions;
tools?: BaseTool[];
}
export interface LLMChatParamsStreaming<
AdditionalChatOptions extends object = object,
AdditionalMessageOptions extends object = object,
> extends LLMChatParamsBase<AdditionalChatOptions, AdditionalMessageOptions> {
stream: true;
}
export interface LLMChatParamsNonStreaming<
AdditionalChatOptions extends object = object,
AdditionalMessageOptions extends object = object,
> extends LLMChatParamsBase<AdditionalChatOptions, AdditionalMessageOptions> {
stream?: false;
}
export interface LLMCompletionParamsBase {
prompt: MessageContent;
}
export interface LLMCompletionParamsStreaming extends LLMCompletionParamsBase {
stream: true;
}
export interface LLMCompletionParamsNonStreaming
extends LLMCompletionParamsBase {
stream?: false | null;
}
export type MessageContentTextDetail = {
type: "text";
text: string;
};
export type MessageContentImageDetail = {
type: "image_url";
image_url: { url: string };
};
export type MessageContentDetail =
| MessageContentTextDetail
| MessageContentImageDetail;
/**
* Extended type for the content of a message that allows for multi-modal messages.
*/
export type MessageContent = string | MessageContentDetail[];
export type ToolCall = {
name: string;
input: JSONObject;
id: string;
};
// happened in streaming response, the tool call is not ready yet
export type PartialToolCall = {
name: string;
id: string;
// input is not ready yet, JSON.parse(input) will throw an error
input: string;
};
export type ToolResult = {
id: string;
result: string;
isError: boolean;
};
export type ToolCallOptions = {
toolCall: ToolCall | PartialToolCall;
};
export type ToolResultOptions = {
toolResult: ToolResult;
};
export type ToolCallLLMMessageOptions =
| ToolResultOptions
| ToolCallOptions
| {};