@celljs/ai-anthropic 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +82 -0
- package/lib/common/api/anthropic-api.d.ts +16 -0
- package/lib/common/api/anthropic-api.d.ts.map +1 -0
- package/lib/common/api/anthropic-api.js +76 -0
- package/lib/common/api/anthropic-api.js.map +1 -0
- package/lib/common/api/anthropic-api.spec.d.ts +2 -0
- package/lib/common/api/anthropic-api.spec.d.ts.map +1 -0
- package/lib/common/api/anthropic-api.spec.js +146 -0
- package/lib/common/api/anthropic-api.spec.js.map +1 -0
- package/lib/common/api/anthropic-model.d.ts +34 -0
- package/lib/common/api/anthropic-model.d.ts.map +1 -0
- package/lib/common/api/anthropic-model.js +39 -0
- package/lib/common/api/anthropic-model.js.map +1 -0
- package/lib/common/api/api-protocol.d.ts +40 -0
- package/lib/common/api/api-protocol.d.ts.map +1 -0
- package/lib/common/api/api-protocol.js +6 -0
- package/lib/common/api/api-protocol.js.map +1 -0
- package/lib/common/api/chat-request.d.ts +144 -0
- package/lib/common/api/chat-request.d.ts.map +1 -0
- package/lib/common/api/chat-request.js +151 -0
- package/lib/common/api/chat-request.js.map +1 -0
- package/lib/common/api/chat-response.d.ts +72 -0
- package/lib/common/api/chat-response.d.ts.map +1 -0
- package/lib/common/api/chat-response.js +96 -0
- package/lib/common/api/chat-response.js.map +1 -0
- package/lib/common/api/content-block.d.ts +100 -0
- package/lib/common/api/content-block.d.ts.map +1 -0
- package/lib/common/api/content-block.js +84 -0
- package/lib/common/api/content-block.js.map +1 -0
- package/lib/common/api/event.d.ts +188 -0
- package/lib/common/api/event.d.ts.map +1 -0
- package/lib/common/api/event.js +249 -0
- package/lib/common/api/event.js.map +1 -0
- package/lib/common/api/index.d.ts +10 -0
- package/lib/common/api/index.d.ts.map +1 -0
- package/lib/common/api/index.js +13 -0
- package/lib/common/api/index.js.map +1 -0
- package/lib/common/api/message.d.ts +41 -0
- package/lib/common/api/message.d.ts.map +1 -0
- package/lib/common/api/message.js +45 -0
- package/lib/common/api/message.js.map +1 -0
- package/lib/common/api/usage.d.ts +15 -0
- package/lib/common/api/usage.d.ts.map +1 -0
- package/lib/common/api/usage.js +24 -0
- package/lib/common/api/usage.js.map +1 -0
- package/lib/common/index.d.ts +3 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +6 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/model/anthropic-chat-model.d.ts +20 -0
- package/lib/common/model/anthropic-chat-model.d.ts.map +1 -0
- package/lib/common/model/anthropic-chat-model.js +161 -0
- package/lib/common/model/anthropic-chat-model.js.map +1 -0
- package/lib/common/model/anthropic-chat-model.spec.d.ts +2 -0
- package/lib/common/model/anthropic-chat-model.spec.d.ts.map +1 -0
- package/lib/common/model/anthropic-chat-model.spec.js +64 -0
- package/lib/common/model/anthropic-chat-model.spec.js.map +1 -0
- package/lib/common/model/anthropic-chat-options.d.ts +55 -0
- package/lib/common/model/anthropic-chat-options.d.ts.map +1 -0
- package/lib/common/model/anthropic-chat-options.js +157 -0
- package/lib/common/model/anthropic-chat-options.js.map +1 -0
- package/lib/common/model/index.d.ts +3 -0
- package/lib/common/model/index.d.ts.map +1 -0
- package/lib/common/model/index.js +6 -0
- package/lib/common/model/index.js.map +1 -0
- package/lib/common/module.d.ts +4 -0
- package/lib/common/module.d.ts.map +1 -0
- package/lib/common/module.js +6 -0
- package/lib/common/module.js.map +1 -0
- package/lib/common/test/mock-anthropic-api.d.ts +9 -0
- package/lib/common/test/mock-anthropic-api.d.ts.map +1 -0
- package/lib/common/test/mock-anthropic-api.js +46 -0
- package/lib/common/test/mock-anthropic-api.js.map +1 -0
- package/lib/common/test/test-container.d.ts +3 -0
- package/lib/common/test/test-container.d.ts.map +1 -0
- package/lib/common/test/test-container.js +15 -0
- package/lib/common/test/test-container.js.map +1 -0
- package/lib/common/utils/index.d.ts +2 -0
- package/lib/common/utils/index.d.ts.map +1 -0
- package/lib/common/utils/index.js +5 -0
- package/lib/common/utils/index.js.map +1 -0
- package/lib/common/utils/stream-util.d.ts +9 -0
- package/lib/common/utils/stream-util.d.ts.map +1 -0
- package/lib/common/utils/stream-util.js +114 -0
- package/lib/common/utils/stream-util.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +4 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +46 -0
- package/src/common/api/anthropic-api.spec.ts +165 -0
- package/src/common/api/anthropic-api.ts +90 -0
- package/src/common/api/anthropic-model.ts +42 -0
- package/src/common/api/api-protocol.ts +45 -0
- package/src/common/api/chat-request.ts +280 -0
- package/src/common/api/chat-response.ts +161 -0
- package/src/common/api/content-block.ts +155 -0
- package/src/common/api/event.ts +376 -0
- package/src/common/api/index.ts +9 -0
- package/src/common/api/message.ts +49 -0
- package/src/common/api/usage.ts +24 -0
- package/src/common/index.ts +2 -0
- package/src/common/model/anthropic-chat-model.spec.ts +67 -0
- package/src/common/model/anthropic-chat-model.ts +187 -0
- package/src/common/model/anthropic-chat-options.ts +152 -0
- package/src/common/model/index.ts +2 -0
- package/src/common/module.ts +4 -0
- package/src/common/test/mock-anthropic-api.ts +62 -0
- package/src/common/test/test-container.ts +12 -0
- package/src/common/utils/index.ts +1 -0
- package/src/common/utils/stream-util.ts +142 -0
- package/src/package.spec.ts +4 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { Type, Expose } from 'class-transformer';
|
|
2
|
+
import { AnthropicMessage } from './message';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Tool description.
|
|
6
|
+
*/
|
|
7
|
+
export class Tool {
|
|
8
|
+
/**
|
|
9
|
+
* The name of the tool.
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A description of the tool.
|
|
15
|
+
*/
|
|
16
|
+
description: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The input schema of the tool.
|
|
20
|
+
*/
|
|
21
|
+
inputSchema: Record<string, any>;
|
|
22
|
+
|
|
23
|
+
constructor(name: string, description: string, inputSchema: Record<string, any>) {
|
|
24
|
+
this.name = name;
|
|
25
|
+
this.description = description;
|
|
26
|
+
this.inputSchema = inputSchema;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Metadata about the request.
|
|
32
|
+
*/
|
|
33
|
+
export class Metadata {
|
|
34
|
+
/**
|
|
35
|
+
* An external identifier for the user who is associated with the
|
|
36
|
+
* request. This should be a uuid, hash value, or other opaque identifier.
|
|
37
|
+
* Anthropic may use this id to help detect abuse. Do not include any identifying
|
|
38
|
+
* information such as name, email address, or phone number.
|
|
39
|
+
*/
|
|
40
|
+
@Expose({ name: 'user_id' })
|
|
41
|
+
userId: string;
|
|
42
|
+
|
|
43
|
+
constructor(userId: string) {
|
|
44
|
+
this.userId = userId;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Chat completion request object.
|
|
50
|
+
*/
|
|
51
|
+
export class ChatRequest {
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The model that will complete your prompt. See the list of
|
|
55
|
+
* <a href="https://docs.anthropic.com/claude/docs/models-overview">models</a> for
|
|
56
|
+
* additional details and options.
|
|
57
|
+
*/
|
|
58
|
+
model: string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Input messages.
|
|
62
|
+
*/
|
|
63
|
+
@Type(() => AnthropicMessage)
|
|
64
|
+
messages: AnthropicMessage[];
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* System prompt. A system prompt is a way of providing context and
|
|
68
|
+
* instructions to Claude, such as specifying a particular goal or role. See our
|
|
69
|
+
* <a href="https://docs.anthropic.com/claude/docs/system-prompts">guide</a> to system
|
|
70
|
+
* prompts.
|
|
71
|
+
*/
|
|
72
|
+
system?: string;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The maximum number of tokens to generate before stopping. Note
|
|
76
|
+
* that our models may stop before reaching this maximum. This parameter only
|
|
77
|
+
* specifies the absolute maximum number of tokens to generate. Different models have
|
|
78
|
+
* different maximum values for this parameter.
|
|
79
|
+
*/
|
|
80
|
+
@Expose({ name: 'max_tokens' })
|
|
81
|
+
maxTokens: number;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* An object describing metadata about the request.
|
|
85
|
+
*/
|
|
86
|
+
metadata?: Metadata;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Custom text sequences that will cause the model to stop
|
|
90
|
+
* generating. Our models will normally stop when they have naturally completed their
|
|
91
|
+
* turn, which will result in a response stop_reason of "end_turn". If you want the
|
|
92
|
+
* model to stop generating when it encounters custom strings of text, you can use the
|
|
93
|
+
* stop_sequences parameter. If the model encounters one of the custom sequences, the
|
|
94
|
+
* response stop_reason value will be "stop_sequence" and the response stop_sequence
|
|
95
|
+
* value will contain the matched stop sequence.
|
|
96
|
+
*/
|
|
97
|
+
@Expose({ name: 'stop_sequences' })
|
|
98
|
+
stopSequences?: string[];
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Whether to incrementally stream the response using server-sent
|
|
102
|
+
* events.
|
|
103
|
+
*/
|
|
104
|
+
stream: boolean;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Amount of randomness injected into the response.Defaults to 1.0.
|
|
108
|
+
* Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple
|
|
109
|
+
* choice, and closer to 1.0 for creative and generative tasks. Note that even with
|
|
110
|
+
* temperature of 0.0, the results will not be fully deterministic.
|
|
111
|
+
*/
|
|
112
|
+
temperature?: number;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Use nucleus sampling. In nucleus sampling, we compute the cumulative
|
|
116
|
+
* distribution over all the options for each subsequent token in decreasing
|
|
117
|
+
* probability order and cut it off once it reaches a particular probability specified
|
|
118
|
+
* by top_p. You should either alter temperature or top_p, but not both. Recommended
|
|
119
|
+
* for advanced use cases only. You usually only need to use temperature.
|
|
120
|
+
*/
|
|
121
|
+
@Expose({ name: 'top_p' })
|
|
122
|
+
topP?: number;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Only sample from the top K options for each subsequent token. Used to
|
|
126
|
+
* remove "long tail" low probability responses. Learn more technical details here.
|
|
127
|
+
* Recommended for advanced use cases only. You usually only need to use temperature.
|
|
128
|
+
*/
|
|
129
|
+
@Expose({ name: 'top_k' })
|
|
130
|
+
topK?: number;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Definitions of tools that the model may use. If provided the model may
|
|
134
|
+
* return tool_use content blocks that represent the model's use of those tools. You
|
|
135
|
+
* can then run those tools using the tool input generated by the model and then
|
|
136
|
+
* optionally return results back to the model using tool_result content blocks.
|
|
137
|
+
*/
|
|
138
|
+
@Type(() => Tool)
|
|
139
|
+
tools?: Tool[];
|
|
140
|
+
|
|
141
|
+
constructor(
|
|
142
|
+
model: string,
|
|
143
|
+
messages: AnthropicMessage[],
|
|
144
|
+
system: string | undefined,
|
|
145
|
+
maxTokens: number,
|
|
146
|
+
temperature: number | undefined,
|
|
147
|
+
stream: boolean,
|
|
148
|
+
metadata?: Metadata,
|
|
149
|
+
stopSequences?: string[],
|
|
150
|
+
topP?: number,
|
|
151
|
+
topK?: number,
|
|
152
|
+
tools?: Tool[],
|
|
153
|
+
) {
|
|
154
|
+
this.model = model;
|
|
155
|
+
this.messages = messages;
|
|
156
|
+
this.system = system;
|
|
157
|
+
this.maxTokens = maxTokens;
|
|
158
|
+
this.metadata = metadata;
|
|
159
|
+
this.stopSequences = stopSequences;
|
|
160
|
+
this.stream = stream;
|
|
161
|
+
this.temperature = temperature;
|
|
162
|
+
this.topP = topP;
|
|
163
|
+
this.topK = topK;
|
|
164
|
+
this.tools = tools;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Builder class for ChatRequest
|
|
169
|
+
*/
|
|
170
|
+
static builder(): ChatRequestBuilder {
|
|
171
|
+
return new ChatRequestBuilder();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static from(request: ChatRequest): ChatRequestBuilder {
|
|
175
|
+
return new ChatRequestBuilder(request);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Builder interface for ChatRequest
|
|
181
|
+
*/
|
|
182
|
+
export class ChatRequestBuilder {
|
|
183
|
+
private model: string;
|
|
184
|
+
private messages: AnthropicMessage[];
|
|
185
|
+
private system?: string;
|
|
186
|
+
private maxTokens: number;
|
|
187
|
+
private metadata?: Metadata;
|
|
188
|
+
private stopSequences?: string[];
|
|
189
|
+
private stream: boolean = false;
|
|
190
|
+
private temperature?: number;
|
|
191
|
+
private topP?: number;
|
|
192
|
+
private topK?: number;
|
|
193
|
+
private tools?: Tool[];
|
|
194
|
+
|
|
195
|
+
constructor(request?: ChatRequest) {
|
|
196
|
+
if (request) {
|
|
197
|
+
this.model = request.model;
|
|
198
|
+
this.messages = request.messages;
|
|
199
|
+
this.system = request.system;
|
|
200
|
+
this.maxTokens = request.maxTokens;
|
|
201
|
+
this.metadata = request.metadata;
|
|
202
|
+
this.stopSequences = request.stopSequences;
|
|
203
|
+
this.stream = request.stream;
|
|
204
|
+
this.temperature = request.temperature;
|
|
205
|
+
this.topP = request.topP;
|
|
206
|
+
this.topK = request.topK;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
withMessages(messages: AnthropicMessage[]): ChatRequestBuilder {
|
|
211
|
+
this.messages = messages;
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
withStream(stream: boolean): ChatRequestBuilder {
|
|
216
|
+
this.stream = stream;
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
withModel(model: string): ChatRequestBuilder {
|
|
221
|
+
this.model = model;
|
|
222
|
+
return this;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
withSystem(system: string): ChatRequestBuilder {
|
|
226
|
+
this.system = system;
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
withStopSequences(stopSequences: string[]): ChatRequestBuilder {
|
|
231
|
+
this.stopSequences = stopSequences;
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
withMaxTokens(maxTokens: number): ChatRequestBuilder {
|
|
236
|
+
this.maxTokens = maxTokens;
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
withTemperature(temperature: number): ChatRequestBuilder {
|
|
241
|
+
this.temperature = temperature;
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
withTopP(topP: number): ChatRequestBuilder {
|
|
246
|
+
this.topP = topP;
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
withTopK(topK: number): ChatRequestBuilder {
|
|
251
|
+
this.topK = topK;
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
withMetadata(metadata: Metadata): ChatRequestBuilder {
|
|
256
|
+
this.metadata = metadata;
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
withTools(tools: Tool[]): ChatRequestBuilder {
|
|
261
|
+
this.tools = tools;
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
build(): ChatRequest {
|
|
266
|
+
return new ChatRequest(
|
|
267
|
+
this.model,
|
|
268
|
+
this.messages,
|
|
269
|
+
this.system,
|
|
270
|
+
this.maxTokens,
|
|
271
|
+
this.temperature,
|
|
272
|
+
this.stream,
|
|
273
|
+
this.metadata,
|
|
274
|
+
this.stopSequences,
|
|
275
|
+
this.topP,
|
|
276
|
+
this.topK,
|
|
277
|
+
this.tools
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { Expose, Type } from 'class-transformer';
|
|
2
|
+
import { Role } from './message';
|
|
3
|
+
import { Assert } from '@celljs/core';
|
|
4
|
+
import { ContentBlock } from './content-block';
|
|
5
|
+
import { Usage } from './usage';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Chat completion response object.
|
|
9
|
+
*/
|
|
10
|
+
export class ChatResponse {
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Unique object identifier. The format and length of IDs may change over
|
|
14
|
+
* time.
|
|
15
|
+
*/
|
|
16
|
+
id: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Object type. For Messages, this is always "message".
|
|
20
|
+
*/
|
|
21
|
+
type: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Conversational role of the generated message. This will always be
|
|
25
|
+
* "assistant".
|
|
26
|
+
*/
|
|
27
|
+
role: Role;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Content generated by the model. This is an array of content blocks.
|
|
31
|
+
*/
|
|
32
|
+
@Type(() => ContentBlock)
|
|
33
|
+
content: ContentBlock[];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The model that handled the request.
|
|
37
|
+
*/
|
|
38
|
+
model: string;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The reason the model stopped generating tokens. This will be one
|
|
42
|
+
* of "end_turn", "max_tokens", "stop_sequence", "tool_use", or "timeout".
|
|
43
|
+
*/
|
|
44
|
+
@Expose({ name: 'stop_reason' })
|
|
45
|
+
stopReason: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Which custom stop sequence was generated, if any.
|
|
49
|
+
*/
|
|
50
|
+
@Expose({ name: 'stop_sequence' })
|
|
51
|
+
stopSequence?: string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Input and output token usage.
|
|
55
|
+
*/
|
|
56
|
+
usage: Usage;
|
|
57
|
+
|
|
58
|
+
constructor(
|
|
59
|
+
id: string,
|
|
60
|
+
type: string,
|
|
61
|
+
role: Role,
|
|
62
|
+
content: ContentBlock[],
|
|
63
|
+
model: string,
|
|
64
|
+
stopReason: string,
|
|
65
|
+
stopSequence: string | undefined,
|
|
66
|
+
usage: any
|
|
67
|
+
) {
|
|
68
|
+
this.id = id;
|
|
69
|
+
this.type = type;
|
|
70
|
+
this.role = role;
|
|
71
|
+
this.content = content;
|
|
72
|
+
this.model = model;
|
|
73
|
+
this.stopReason = stopReason;
|
|
74
|
+
this.stopSequence = stopSequence;
|
|
75
|
+
this.usage = usage;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static builder(): ChatResponseBuilder {
|
|
79
|
+
return new ChatResponseBuilder();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class ChatResponseBuilder {
|
|
84
|
+
private type: string;
|
|
85
|
+
private id: string;
|
|
86
|
+
private role: Role;
|
|
87
|
+
private content: ContentBlock[];
|
|
88
|
+
private model: string;
|
|
89
|
+
private stopReason: string;
|
|
90
|
+
private stopSequence?: string;
|
|
91
|
+
private usage: Usage;
|
|
92
|
+
|
|
93
|
+
constructor() {
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
withType(type: string): ChatResponseBuilder {
|
|
97
|
+
this.type = type;
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
withId(id: string): ChatResponseBuilder {
|
|
102
|
+
this.id = id;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
withRole(role: Role): ChatResponseBuilder {
|
|
107
|
+
this.role = role;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
withContent(content: ContentBlock[]): ChatResponseBuilder {
|
|
112
|
+
this.content = content;
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
withModel(model: string): ChatResponseBuilder {
|
|
117
|
+
this.model = model;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
withStopReason(stopReason: string): ChatResponseBuilder {
|
|
122
|
+
this.stopReason = stopReason;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
withStopSequence(stopSequence: string): ChatResponseBuilder {
|
|
127
|
+
this.stopSequence = stopSequence;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
withUsage(usage: Usage): ChatResponseBuilder {
|
|
132
|
+
this.usage = usage;
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
withOutputTokens(outputTokens: number): ChatResponseBuilder {
|
|
137
|
+
Assert.isTrue(!!this.usage, 'Usage must be set before setting output tokens');
|
|
138
|
+
this.usage.outputTokens = outputTokens;
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
build(): ChatResponse {
|
|
143
|
+
return new ChatResponse(this.id, this.type, this.role, this.content, this.model, this.stopReason, this.stopSequence, this.usage);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export class ChatResponseBuilderReference {
|
|
148
|
+
|
|
149
|
+
private static instance: ChatResponseBuilder;
|
|
150
|
+
|
|
151
|
+
static get(): ChatResponseBuilder {
|
|
152
|
+
if (!this.instance) {
|
|
153
|
+
this.instance = new ChatResponseBuilder();
|
|
154
|
+
}
|
|
155
|
+
return this.instance;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static reset(): void {
|
|
159
|
+
this.instance = new ChatResponseBuilder();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Expose } from 'class-transformer';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The type of the content block.
|
|
5
|
+
*/
|
|
6
|
+
export enum ContentBlockType {
|
|
7
|
+
/**
|
|
8
|
+
* Tool request
|
|
9
|
+
*/
|
|
10
|
+
TOOL_USE = 'tool_use',
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Send tool result back to LLM.
|
|
14
|
+
*/
|
|
15
|
+
TOOL_RESULT = 'tool_result',
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Text message.
|
|
19
|
+
*/
|
|
20
|
+
TEXT = 'text',
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Text delta message. Returned from the streaming response.
|
|
24
|
+
*/
|
|
25
|
+
TEXT_DELTA = 'text_delta',
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Tool use input partial JSON delta streaming.
|
|
29
|
+
*/
|
|
30
|
+
INPUT_JSON_DELTA = 'input_json_delta',
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Image message.
|
|
34
|
+
*/
|
|
35
|
+
IMAGE = 'image',
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Document message.
|
|
39
|
+
*/
|
|
40
|
+
DOCUMENT = 'document',
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The source of the media content. (Applicable for "image" types only)
|
|
46
|
+
*/
|
|
47
|
+
export class Source {
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The type of the media content. Only "base64" is supported at the moment.
|
|
51
|
+
*/
|
|
52
|
+
type: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The media type of the content. For example, "image/png" or "image/jpeg".
|
|
56
|
+
*/
|
|
57
|
+
@Expose({ name: 'media_type' })
|
|
58
|
+
mediaType: string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The base64-encoded data of the content.
|
|
62
|
+
*/
|
|
63
|
+
data: string;
|
|
64
|
+
|
|
65
|
+
constructor(type: string, mediaType: string, data: string) {
|
|
66
|
+
this.type = type;
|
|
67
|
+
this.mediaType = mediaType;
|
|
68
|
+
this.data = data;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create source
|
|
73
|
+
* @param mediaType The media type of the content.
|
|
74
|
+
* @param data The content data.
|
|
75
|
+
*/
|
|
76
|
+
public static from(mediaType: string, data: string): Source {
|
|
77
|
+
return new Source('base64', mediaType, data);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The content block of the message.
|
|
84
|
+
*/
|
|
85
|
+
export class ContentBlock {
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The content type can be "text", "image", "tool_use", "tool_result" or "text_delta".
|
|
89
|
+
*/
|
|
90
|
+
type: ContentBlockType;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The source of the media content. Applicable for "image" types only.
|
|
94
|
+
*/
|
|
95
|
+
source?: Source;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The text of the message. Applicable for "text" types only.
|
|
99
|
+
*/
|
|
100
|
+
text?: string;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The index of the content block. Applicable only for streaming responses.
|
|
104
|
+
*/
|
|
105
|
+
index?: number;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The id of the tool use. Applicable only for tool_use response.
|
|
109
|
+
*/
|
|
110
|
+
id?: string;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The name of the tool use. Applicable only for tool_use response.
|
|
114
|
+
*/
|
|
115
|
+
name?: string;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The input of the tool use. Applicable only for tool_use response.
|
|
119
|
+
*/
|
|
120
|
+
input?: Record<string, any>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The id of the tool use. Applicable only for tool_result response.
|
|
124
|
+
*/
|
|
125
|
+
@Expose({ name: 'tool_use_id' })
|
|
126
|
+
toolUseId?: string;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The content of the tool result. Applicable only for tool_result response.
|
|
130
|
+
*/
|
|
131
|
+
content?: string;
|
|
132
|
+
|
|
133
|
+
constructor(
|
|
134
|
+
type: ContentBlockType,
|
|
135
|
+
source?: Source,
|
|
136
|
+
text?: string,
|
|
137
|
+
index?: number,
|
|
138
|
+
id?: string,
|
|
139
|
+
name?: string,
|
|
140
|
+
input?: Record<string, any>,
|
|
141
|
+
toolUseId?: string,
|
|
142
|
+
content?: string
|
|
143
|
+
) {
|
|
144
|
+
this.type = type;
|
|
145
|
+
this.source = source;
|
|
146
|
+
this.text = text;
|
|
147
|
+
this.index = index;
|
|
148
|
+
this.id = id;
|
|
149
|
+
this.name = name;
|
|
150
|
+
this.input = input;
|
|
151
|
+
this.toolUseId = toolUseId;
|
|
152
|
+
this.content = content;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
}
|