@celljs/ai-anthropic 3.4.3 → 3.4.4
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/lib/common/api/anthropic-api.d.ts +3 -3
- package/lib/common/api/anthropic-api.d.ts.map +1 -1
- package/lib/common/api/anthropic-api.js +12 -4
- package/lib/common/api/anthropic-api.js.map +1 -1
- package/lib/common/api/anthropic-api.spec.js +37 -44
- package/lib/common/api/anthropic-api.spec.js.map +1 -1
- package/lib/common/api/api-protocol.d.ts +3 -2
- package/lib/common/api/api-protocol.d.ts.map +1 -1
- package/lib/common/api/api-protocol.js.map +1 -1
- package/lib/common/model/anthropic-chat-model.js +6 -6
- package/lib/common/model/anthropic-chat-model.js.map +1 -1
- package/lib/common/test/mock-anthropic-api.d.ts +3 -2
- package/lib/common/test/mock-anthropic-api.d.ts.map +1 -1
- package/lib/common/test/mock-anthropic-api.js +15 -2
- package/lib/common/test/mock-anthropic-api.js.map +1 -1
- package/package.json +6 -6
- package/src/common/api/anthropic-api.spec.ts +38 -44
- package/src/common/api/anthropic-api.ts +16 -8
- package/src/common/api/api-protocol.ts +3 -2
- package/src/common/model/anthropic-chat-model.ts +6 -6
- package/src/common/test/mock-anthropic-api.ts +19 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
-
import { RestOperations } from '@celljs/http';
|
|
2
|
+
import { ResponseEntity, RestOperations } from '@celljs/http';
|
|
3
3
|
import { ChatRequest } from './chat-request';
|
|
4
4
|
import { ChatResponse } from './chat-response';
|
|
5
5
|
import { AnthropicAPI, AnthropicAPIOptions } from './api-protocol';
|
|
@@ -10,7 +10,7 @@ export declare class AnthropicAPIImpl implements AnthropicAPI {
|
|
|
10
10
|
protected get baseUrl(): string;
|
|
11
11
|
protected get apiKey(): string | undefined;
|
|
12
12
|
protected get anthropicVersion(): string;
|
|
13
|
-
chat(chatRequest: ChatRequest): Promise<ChatResponse
|
|
14
|
-
streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse
|
|
13
|
+
chat(chatRequest: ChatRequest): Promise<ResponseEntity<ChatResponse>>;
|
|
14
|
+
streamingChat(chatRequest: ChatRequest): Promise<Observable<ResponseEntity<ChatResponse>>>;
|
|
15
15
|
}
|
|
16
16
|
//# sourceMappingURL=anthropic-api.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-api.d.ts","sourceRoot":"","sources":["../../../src/common/api/anthropic-api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"anthropic-api.d.ts","sourceRoot":"","sources":["../../../src/common/api/anthropic-api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAMnE,qBACa,gBAAiB,YAAW,YAAY;IAGjD,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,cAAc,CAAC;IAGlD,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAIjD,SAAS,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,mBAAmB,CAAC;IAExD,SAAS,KAAK,OAAO,IAAI,MAAM,CAE9B;IAED,SAAS,KAAK,MAAM,IAAI,MAAM,GAAG,SAAS,CAEzC;IAED,SAAS,KAAK,gBAAgB,IAAI,MAAM,CAEvC;IAEK,IAAI,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;IAWrE,aAAa,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC;CAiDnG"}
|
|
@@ -26,13 +26,17 @@ let AnthropicAPIImpl = class AnthropicAPIImpl {
|
|
|
26
26
|
}
|
|
27
27
|
async chat(chatRequest) {
|
|
28
28
|
core_1.Assert.isTrue(!chatRequest.stream, 'Request must set the stream property to false.');
|
|
29
|
-
const { data } = await this.restOperations
|
|
29
|
+
const { data, status, headers } = await this.restOperations
|
|
30
30
|
.post('/v1/messages', (0, class_transformer_1.instanceToPlain)(chatRequest), { baseURL: this.baseUrl });
|
|
31
|
-
return
|
|
31
|
+
return {
|
|
32
|
+
status,
|
|
33
|
+
headers,
|
|
34
|
+
body: (0, class_transformer_1.plainToInstance)(chat_response_1.ChatResponse, data)
|
|
35
|
+
};
|
|
32
36
|
}
|
|
33
37
|
async streamingChat(chatRequest) {
|
|
34
38
|
core_1.Assert.isTrue(chatRequest.stream, 'Request must set the stream property to true.');
|
|
35
|
-
const { data } = await this.restOperations
|
|
39
|
+
const { data, status, headers } = await this.restOperations
|
|
36
40
|
.post('/v1/messages', (0, class_transformer_1.instanceToPlain)(chatRequest), {
|
|
37
41
|
baseURL: this.baseUrl,
|
|
38
42
|
headers: {
|
|
@@ -53,7 +57,11 @@ let AnthropicAPIImpl = class AnthropicAPIImpl {
|
|
|
53
57
|
return true;
|
|
54
58
|
}
|
|
55
59
|
return !isInsideTool;
|
|
56
|
-
}), (0, operators_1.concatMap)(group => group.pipe((0, operators_1.reduce)((acc, curr) => utils_1.StreamUtil.mergeToolUseEvents(acc, curr.data), new event_1.ToolUseAggregationEvent()))), (0, operators_1.map)(event =>
|
|
60
|
+
}), (0, operators_1.concatMap)(group => group.pipe((0, operators_1.reduce)((acc, curr) => utils_1.StreamUtil.mergeToolUseEvents(acc, curr.data), new event_1.ToolUseAggregationEvent()))), (0, operators_1.map)(event => ({
|
|
61
|
+
status,
|
|
62
|
+
headers,
|
|
63
|
+
body: utils_1.StreamUtil.eventToChatResponse(event)
|
|
64
|
+
})), (0, operators_1.filter)(response => !!response.body.type));
|
|
57
65
|
}
|
|
58
66
|
};
|
|
59
67
|
tslib_1.__decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-api.js","sourceRoot":"","sources":["../../../src/common/api/anthropic-api.ts"],"names":[],"mappings":";;;;AAAA,uCAA6E;AAE7E,8CAA0E;AAC1E,
|
|
1
|
+
{"version":3,"file":"anthropic-api.js","sourceRoot":"","sources":["../../../src/common/api/anthropic-api.ts"],"names":[],"mappings":";;;;AAAA,uCAA6E;AAE7E,8CAA0E;AAC1E,uCAA8D;AAE9D,mDAA+C;AAC/C,iDAAmE;AACnE,yDAAqE;AACrE,6CAAuD;AACvD,mCAAmF;AACnF,oCAAsC;AAG/B,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAYzB,IAAc,OAAO;;QACjB,OAAO,MAAA,MAAA,MAAA,IAAI,CAAC,OAAO,0CAAE,OAAO,mCAAI,MAAA,IAAI,CAAC,cAAc,0CAAE,OAAO,mCAAI,2BAA2B,CAAC;IAChG,CAAC;IAED,IAAc,MAAM;;QAChB,OAAO,MAAA,MAAA,IAAI,CAAC,OAAO,0CAAE,MAAM,mCAAI,MAAA,IAAI,CAAC,cAAc,0CAAE,MAAM,CAAC;IAC/D,CAAC;IAED,IAAc,gBAAgB;;QAC1B,OAAO,MAAA,MAAA,MAAA,IAAI,CAAC,OAAO,0CAAE,gBAAgB,mCAAI,MAAA,IAAI,CAAC,cAAc,0CAAE,gBAAgB,mCAAI,YAAY,CAAC;IACnG,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,WAAwB;QAC/B,aAAM,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,MAAM,EAAE,gDAAgD,CAAC,CAAC;QACrF,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,MAA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CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBACvD,YAAY,GAAG,KAAK,CAAC;gBACrB,OAAO,IAAI,CAAC;aACf;YACD,OAAO,CAAC,YAAY,CAAC;QACzB,CAAC,CAAC,EACF,IAAA,qBAAS,EAAC,KAAK,CAAC,EAAE,CACd,KAAK,CAAC,IAAI,CACN,IAAA,kBAAM,EACF,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,kBAAU,CAAC,kBAAkB,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,EAC5D,IAAI,+BAAuB,EAAE,CAChC,CACJ,CACJ,EACD,IAAA,eAAG,EAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACV,MAAM;YACN,OAAO;YACP,IAAI,EAAE,kBAAU,CAAC,mBAAmB,CAAC,KAAK,CAAC;SAC9C,CAAC,CAAC,EACH,IAAA,kBAAM,EAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAC3C,CAAC;IAEV,CAAC;CACJ,CAAA;AAlFG;IAAC,IAAA,gBAAS,EAAC,qBAAc,CAAC;;wDACwB;AAElD;IAAC,IAAA,YAAK,EAAC,uBAAuB,CAAC;;iDACkB;AAEjD;IAAC,IAAA,gBAAS,EAAC,kCAAmB,CAAC;IAC9B,IAAA,eAAQ,GAAE;;wDAC6C;AAV/C,gBAAgB;IAD5B,IAAA,gBAAS,EAAC,2BAAY,CAAC;GACX,gBAAgB,CAoF5B;AApFY,4CAAgB"}
|
|
@@ -16,62 +16,51 @@ describe('AnthropicAPIImpl', () => {
|
|
|
16
16
|
beforeEach(() => {
|
|
17
17
|
container.rebind(http_1.RestOperations).toConstantValue({
|
|
18
18
|
post: async (url, data, config) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
text: 'How can I assist you today?'
|
|
28
|
-
}
|
|
29
|
-
],
|
|
30
|
-
model: data.model,
|
|
31
|
-
stop_reason: 'end_turn',
|
|
32
|
-
stop_sequence: undefined,
|
|
33
|
-
usage: {
|
|
34
|
-
input_tokens: 10,
|
|
35
|
-
output_tokens: 20
|
|
19
|
+
const responseBody = {
|
|
20
|
+
id: 'msg_123',
|
|
21
|
+
type: 'message',
|
|
22
|
+
role: message_1.Role.ASSISTANT,
|
|
23
|
+
content: [
|
|
24
|
+
{
|
|
25
|
+
type: content_block_1.ContentBlockType.TEXT,
|
|
26
|
+
text: 'How can I assist you today?'
|
|
36
27
|
}
|
|
37
|
-
|
|
28
|
+
],
|
|
29
|
+
model: data.model,
|
|
30
|
+
stop_reason: 'end_turn',
|
|
31
|
+
stop_sequence: undefined,
|
|
32
|
+
usage: {
|
|
33
|
+
input_tokens: 10,
|
|
34
|
+
output_tokens: 20
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
if (data.stream) {
|
|
38
38
|
const steam = new ReadableStream({
|
|
39
39
|
start(controller) {
|
|
40
40
|
controller.enqueue('data: ' + JSON.stringify({
|
|
41
41
|
type: 'message_start',
|
|
42
|
-
data:
|
|
42
|
+
data: responseBody
|
|
43
43
|
}) + '\n\n');
|
|
44
44
|
controller.close();
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
|
-
return {
|
|
47
|
+
return {
|
|
48
|
+
data: steam,
|
|
49
|
+
status: 200,
|
|
50
|
+
headers: { 'content-type': 'text/event-stream' }
|
|
51
|
+
};
|
|
48
52
|
}
|
|
49
53
|
return {
|
|
50
|
-
data:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
role: message_1.Role.ASSISTANT,
|
|
54
|
-
content: [
|
|
55
|
-
{
|
|
56
|
-
type: content_block_1.ContentBlockType.TEXT,
|
|
57
|
-
text: 'How can I assist you today?'
|
|
58
|
-
}
|
|
59
|
-
],
|
|
60
|
-
model: data.model,
|
|
61
|
-
stop_reason: 'end_turn',
|
|
62
|
-
stop_sequence: undefined,
|
|
63
|
-
usage: {
|
|
64
|
-
input_tokens: 10,
|
|
65
|
-
output_tokens: 20
|
|
66
|
-
}
|
|
67
|
-
}
|
|
54
|
+
data: responseBody,
|
|
55
|
+
status: 200,
|
|
56
|
+
headers: { 'content-type': 'application/json' }
|
|
68
57
|
};
|
|
69
58
|
}
|
|
70
59
|
});
|
|
71
60
|
anthropicAPI = container.get(api_protocol_1.AnthropicAPI);
|
|
72
61
|
});
|
|
73
62
|
describe('chat', () => {
|
|
74
|
-
it('should return a ChatResponse', async () => {
|
|
63
|
+
it('should return a ResponseEntity with ChatResponse', async () => {
|
|
75
64
|
const messages = [
|
|
76
65
|
new message_1.AnthropicMessage([new content_block_1.ContentBlock(content_block_1.ContentBlockType.TEXT, undefined, 'Hello')], message_1.Role.USER)
|
|
77
66
|
];
|
|
@@ -82,8 +71,10 @@ describe('AnthropicAPIImpl', () => {
|
|
|
82
71
|
.withStream(false)
|
|
83
72
|
.build();
|
|
84
73
|
const response = await anthropicAPI.chat(chatRequest);
|
|
85
|
-
(0, chai_1.expect)(response).to.
|
|
86
|
-
(0, chai_1.expect)(response.
|
|
74
|
+
(0, chai_1.expect)(response.status).to.equal(200);
|
|
75
|
+
(0, chai_1.expect)(response.headers).to.deep.include({ 'content-type': 'application/json' });
|
|
76
|
+
(0, chai_1.expect)(response.body).to.be.instanceOf(chat_response_1.ChatResponse);
|
|
77
|
+
(0, chai_1.expect)(response.body.model).to.equal(anthropic_model_1.AnthropicModel.CLAUDE_3_HAIKU);
|
|
87
78
|
});
|
|
88
79
|
it('should throw an error if stream mode is enabled', async () => {
|
|
89
80
|
const messages = [
|
|
@@ -105,7 +96,7 @@ describe('AnthropicAPIImpl', () => {
|
|
|
105
96
|
});
|
|
106
97
|
});
|
|
107
98
|
describe('streamingChat', () => {
|
|
108
|
-
it('should return an Observable of ChatResponse', async () => {
|
|
99
|
+
it('should return an Observable of ResponseEntity with ChatResponse', async () => {
|
|
109
100
|
const messages = [
|
|
110
101
|
new message_1.AnthropicMessage([new content_block_1.ContentBlock(content_block_1.ContentBlockType.TEXT, undefined, 'Hello')], message_1.Role.USER)
|
|
111
102
|
];
|
|
@@ -118,8 +109,10 @@ describe('AnthropicAPIImpl', () => {
|
|
|
118
109
|
const response$ = await anthropicAPI.streamingChat(chatRequest);
|
|
119
110
|
response$.subscribe({
|
|
120
111
|
next: response => {
|
|
121
|
-
(0, chai_1.expect)(response).to.
|
|
122
|
-
(0, chai_1.expect)(response.
|
|
112
|
+
(0, chai_1.expect)(response.status).to.equal(200);
|
|
113
|
+
(0, chai_1.expect)(response.headers).to.deep.include({ 'content-type': 'text/event-stream' });
|
|
114
|
+
(0, chai_1.expect)(response.body).to.be.instanceOf(chat_response_1.ChatResponse);
|
|
115
|
+
(0, chai_1.expect)(response.body.model).to.equal(anthropic_model_1.AnthropicModel.CLAUDE_3_HAIKU);
|
|
123
116
|
}
|
|
124
117
|
});
|
|
125
118
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-api.spec.js","sourceRoot":"","sources":["../../../src/common/api/anthropic-api.spec.ts"],"names":[],"mappings":";;AAAA,+BAA8B;AAC9B,iDAA6C;AAC7C,uCAA8C;AAC9C,2DAAyD;AACzD,iDAA8C;AAC9C,uDAAmD;AACnD,mDAAiE;AACjE,uCAAmD;AACnD,mDAA+C;AAC/C,oBAAkB;AAElB,MAAM,SAAS,GAAG,IAAA,gCAAe,GAAE,CAAC;AAEpC,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAC9B,IAAI,YAA0B,CAAC;IAE/B,UAAU,CAAC,GAAG,EAAE;QACZ,SAAS,CAAC,MAAM,CAAC,qBAAc,CAAC,CAAC,eAAe,CAAC;YAC7C,IAAI,EAAE,KAAK,EAAE,GAAW,EAAE,IAAS,EAAE,MAAW,EAAE,EAAE;gBAChD,
|
|
1
|
+
{"version":3,"file":"anthropic-api.spec.js","sourceRoot":"","sources":["../../../src/common/api/anthropic-api.spec.ts"],"names":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AEb,IAAI;gBACA,MAAM,YAAY,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;gBAC9C,aAAM,CAAC,IAAI,CAAC,6BAA6B,CAAC,CAAC;aAC9C;YAAC,OAAO,CAAC,EAAE;gBACR,IAAA,aAAM,EAAC,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,+CAA+C,CAAC,CAAC;aAC/E;QACL,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import { ChatRequest } from './chat-request';
|
|
3
3
|
import { ChatResponse } from './chat-response';
|
|
4
|
+
import { ResponseEntity } from '@celljs/http';
|
|
4
5
|
export declare const AnthropicAPI: unique symbol;
|
|
5
6
|
export declare const AnthropicAPIOptions: unique symbol;
|
|
6
7
|
/**
|
|
@@ -29,12 +30,12 @@ export interface AnthropicAPI {
|
|
|
29
30
|
* @param chatRequest Chat request.
|
|
30
31
|
* @return Chat response.
|
|
31
32
|
*/
|
|
32
|
-
chat(chatRequest: ChatRequest): Promise<ChatResponse
|
|
33
|
+
chat(chatRequest: ChatRequest): Promise<ResponseEntity<ChatResponse>>;
|
|
33
34
|
/**
|
|
34
35
|
* Streaming response for the chat completion request.
|
|
35
36
|
* @param chatRequest Chat request. The request must set the stream property to true.
|
|
36
37
|
* @return Chat response as a {@link Flux} stream.
|
|
37
38
|
*/
|
|
38
|
-
streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse
|
|
39
|
+
streamingChat(chatRequest: ChatRequest): Promise<Observable<ResponseEntity<ChatResponse>>>;
|
|
39
40
|
}
|
|
40
41
|
//# sourceMappingURL=api-protocol.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-protocol.d.ts","sourceRoot":"","sources":["../../../src/common/api/api-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"api-protocol.d.ts","sourceRoot":"","sources":["../../../src/common/api/api-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,eAAO,MAAM,YAAY,eAAyB,CAAC;AACnD,eAAO,MAAM,mBAAmB,eAAgC,CAAC;AAEjE;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAChC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,YAAY;IACzB;;;;;;;OAOG;IACH,IAAI,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC;IACtE;;;;OAIG;IACH,aAAa,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;CAE9F"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-protocol.js","sourceRoot":"","sources":["../../../src/common/api/api-protocol.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"api-protocol.js","sourceRoot":"","sources":["../../../src/common/api/api-protocol.ts"],"names":[],"mappings":";;;AAKa,QAAA,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AACtC,QAAA,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC"}
|
|
@@ -9,13 +9,13 @@ const anthropic_chat_options_1 = require("./anthropic-chat-options");
|
|
|
9
9
|
const operators_1 = require("rxjs/operators");
|
|
10
10
|
let AnthropicChatModel = class AnthropicChatModel {
|
|
11
11
|
getContentBlockTypeByMedia(media) {
|
|
12
|
-
if (media.
|
|
12
|
+
if (media.mimeType.toString().startsWith('image')) {
|
|
13
13
|
return api_1.ContentBlockType.IMAGE;
|
|
14
14
|
}
|
|
15
|
-
else if (media.
|
|
15
|
+
else if (media.mimeType.toString().startsWith('pdf')) {
|
|
16
16
|
return api_1.ContentBlockType.DOCUMENT;
|
|
17
17
|
}
|
|
18
|
-
throw new core_1.IllegalArgumentError(`Unsupported media type: ${media.
|
|
18
|
+
throw new core_1.IllegalArgumentError(`Unsupported media type: ${media.mimeType.toString()}. Supported types are: images (image/*) and PDF documents (application/pdf)`);
|
|
19
19
|
}
|
|
20
20
|
fromMediaData(mediaData) {
|
|
21
21
|
if (mediaData instanceof Uint8Array) {
|
|
@@ -44,7 +44,7 @@ let AnthropicChatModel = class AnthropicChatModel {
|
|
|
44
44
|
if (message.media.length > 0) {
|
|
45
45
|
const mediaContent = message.media.map(media => {
|
|
46
46
|
const contentBlockType = this.getContentBlockTypeByMedia(media);
|
|
47
|
-
const source = new api_1.Source('base64', media.
|
|
47
|
+
const source = new api_1.Source('base64', media.mimeType.toString(), this.fromMediaData(media.data));
|
|
48
48
|
return new api_1.ContentBlock(contentBlockType, source);
|
|
49
49
|
});
|
|
50
50
|
contents.push(...mediaContent);
|
|
@@ -134,12 +134,12 @@ let AnthropicChatModel = class AnthropicChatModel {
|
|
|
134
134
|
async call(prompt) {
|
|
135
135
|
const request = this.createRequest(prompt, false);
|
|
136
136
|
const chatResponse = await this.chatApi.chat(request);
|
|
137
|
-
return this.toChatResponse(chatResponse);
|
|
137
|
+
return this.toChatResponse(chatResponse.body);
|
|
138
138
|
}
|
|
139
139
|
async stream(prompt) {
|
|
140
140
|
const request = this.createRequest(prompt, true);
|
|
141
141
|
const chatResponse = await this.chatApi.streamingChat(request);
|
|
142
|
-
return chatResponse.pipe((0, operators_1.map)(chunk => this.toChatResponse(chunk)));
|
|
142
|
+
return chatResponse.pipe((0, operators_1.map)(chunk => this.toChatResponse(chunk.body)));
|
|
143
143
|
}
|
|
144
144
|
};
|
|
145
145
|
tslib_1.__decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-chat-model.js","sourceRoot":"","sources":["../../../src/common/model/anthropic-chat-model.ts"],"names":[],"mappings":";;;;AAAA,6CAeyB;AACzB,uCAAkF;AAElF,gCAAiK;AACjK,qEAAgE;AAChE,8CAAqC;AAG9B,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAUjB,0BAA0B,CAAC,KAAY;QAC7C,IAAI,KAAK,CAAC,
|
|
1
|
+
{"version":3,"file":"anthropic-chat-model.js","sourceRoot":"","sources":["../../../src/common/model/anthropic-chat-model.ts"],"names":[],"mappings":";;;;AAAA,6CAeyB;AACzB,uCAAkF;AAElF,gCAAiK;AACjK,qEAAgE;AAChE,8CAAqC;AAG9B,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAUjB,0BAA0B,CAAC,KAAY;QAC7C,IAAI,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;YAC/C,OAAO,sBAAgB,CAAC,KAAK,CAAC;SACjC;aAAM,IAAI,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;YACpD,OAAO,sBAAgB,CAAC,QAAQ,CAAC;SACpC;QACD,MAAM,IAAI,2BAAoB,CAAC,2BAA2B,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,6EAA6E,CAAC,CAAC;IACtK,CAAC;IAES,aAAa,CAAC,SAAc;QAClC,IAAI,SAAS,YAAY,UAAU,EAAE;YACjC,OAA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,CAAC,CAAC,CAAC;IAClE,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,MAAc;QACrB,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAClD,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACtD,OAAO,IAAI,CAAC,cAAc,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAClD,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,MAAc;QACvB,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACjD,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QAC/D,OAAO,YAAY,CAAC,IAAI,CAAC,IAAA,eAAG,EAAC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC5E,CAAC;CAEJ,CAAA;AAlKG;IAAC,IAAA,gBAAS,EAAC,6CAAoB,CAAC;sCACG,6CAAoB;0DAAC;AAExD;IAAC,IAAA,gBAAS,EAAC,kBAAY,CAAC;;mDACiB;AAEzC;IAAC,IAAA,gBAAS,EAAC,aAAM,CAAC;;kDACgB;AARzB,kBAAkB;IAD9B,IAAA,gBAAS,EAAC,mBAAS,CAAC;GACR,kBAAkB,CAmK9B;AAnKY,gDAAkB"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
+
import { ResponseEntity } from '@celljs/http';
|
|
2
3
|
import { AnthropicAPI } from '../api/api-protocol';
|
|
3
4
|
import { ChatRequest } from '../api/chat-request';
|
|
4
5
|
import { ChatResponse } from '../api/chat-response';
|
|
5
6
|
export declare class MockAnthropicAPI implements AnthropicAPI {
|
|
6
|
-
chat(chatRequest: ChatRequest): Promise<ChatResponse
|
|
7
|
-
streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse
|
|
7
|
+
chat(chatRequest: ChatRequest): Promise<ResponseEntity<ChatResponse>>;
|
|
8
|
+
streamingChat(chatRequest: ChatRequest): Promise<Observable<ResponseEntity<ChatResponse>>>;
|
|
8
9
|
}
|
|
9
10
|
//# sourceMappingURL=mock-anthropic-api.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-anthropic-api.d.ts","sourceRoot":"","sources":["../../../src/common/test/mock-anthropic-api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAQ,MAAM,MAAM,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAIpD,qBACa,gBAAiB,YAAW,YAAY;IAC3C,IAAI,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"mock-anthropic-api.d.ts","sourceRoot":"","sources":["../../../src/common/test/mock-anthropic-api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAQ,MAAM,MAAM,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAIpD,qBACa,gBAAiB,YAAW,YAAY;IAC3C,IAAI,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;IAiCrE,aAAa,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC;CAgCnG"}
|
|
@@ -13,7 +13,7 @@ let MockAnthropicAPI = class MockAnthropicAPI {
|
|
|
13
13
|
if (chatRequest.stream) {
|
|
14
14
|
throw new Error('Request must set the stream property to false.');
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
const response = new chat_response_1.ChatResponse('msg_123', 'message', message_1.Role.ASSISTANT, [
|
|
17
17
|
{
|
|
18
18
|
type: content_block_1.ContentBlockType.TEXT,
|
|
19
19
|
text: 'How can I assist you today?'
|
|
@@ -22,6 +22,13 @@ let MockAnthropicAPI = class MockAnthropicAPI {
|
|
|
22
22
|
inputTokens: 10,
|
|
23
23
|
outputTokens: 20
|
|
24
24
|
});
|
|
25
|
+
return {
|
|
26
|
+
status: 200,
|
|
27
|
+
headers: {
|
|
28
|
+
'x-request-id': 'mock-req-123'
|
|
29
|
+
},
|
|
30
|
+
body: response
|
|
31
|
+
};
|
|
25
32
|
}
|
|
26
33
|
async streamingChat(chatRequest) {
|
|
27
34
|
if (!chatRequest.stream) {
|
|
@@ -36,7 +43,13 @@ let MockAnthropicAPI = class MockAnthropicAPI {
|
|
|
36
43
|
inputTokens: 10,
|
|
37
44
|
outputTokens: 20
|
|
38
45
|
});
|
|
39
|
-
return (0, rxjs_1.from)([
|
|
46
|
+
return (0, rxjs_1.from)([{
|
|
47
|
+
status: 200,
|
|
48
|
+
headers: {
|
|
49
|
+
'x-request-id': 'mock-req-123'
|
|
50
|
+
},
|
|
51
|
+
body: response
|
|
52
|
+
}]);
|
|
40
53
|
}
|
|
41
54
|
};
|
|
42
55
|
MockAnthropicAPI = tslib_1.__decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-anthropic-api.js","sourceRoot":"","sources":["../../../src/common/test/mock-anthropic-api.ts"],"names":[],"mappings":";;;;AAAA,uCAAyC;AACzC,+BAAwC;
|
|
1
|
+
{"version":3,"file":"mock-anthropic-api.js","sourceRoot":"","sources":["../../../src/common/test/mock-anthropic-api.ts"],"names":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AI,EAAE,QAAQ;aACjB,CAAC,CAAC,CAAC;IACR,CAAC;CACJ,CAAA;AAlEY,gBAAgB;IAD5B,IAAA,gBAAS,EAAC,2BAAY,CAAC;GACX,gBAAgB,CAkE5B;AAlEY,4CAAgB"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ai-anthropic",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.4",
|
|
4
4
|
"description": "Authropic models support",
|
|
5
5
|
"main": "lib/common/index.js",
|
|
6
6
|
"typings": "lib/common/index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@celljs/ai-core": "3.4.
|
|
9
|
-
"@celljs/core": "3.4.
|
|
10
|
-
"@celljs/http": "3.4.
|
|
8
|
+
"@celljs/ai-core": "3.4.4",
|
|
9
|
+
"@celljs/core": "3.4.4",
|
|
10
|
+
"@celljs/http": "3.4.4",
|
|
11
11
|
"class-transformer": "^0.5.1",
|
|
12
12
|
"rxjs": "^6.6.0",
|
|
13
13
|
"tslib": "^2.8.0"
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"test": "cell-component test:js"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@celljs/component": "3.4.
|
|
43
|
+
"@celljs/component": "3.4.4"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "5d4a2384da3a67b3c16de24d4692bf754acfeda1"
|
|
46
46
|
}
|
|
@@ -17,56 +17,46 @@ describe('AnthropicAPIImpl', () => {
|
|
|
17
17
|
beforeEach(() => {
|
|
18
18
|
container.rebind(RestOperations).toConstantValue({
|
|
19
19
|
post: async (url: string, data: any, config: any) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
text: 'How can I assist you today?'
|
|
29
|
-
}
|
|
30
|
-
],
|
|
31
|
-
model: data.model,
|
|
32
|
-
stop_reason: 'end_turn',
|
|
33
|
-
stop_sequence: undefined,
|
|
34
|
-
usage: {
|
|
35
|
-
input_tokens: 10,
|
|
36
|
-
output_tokens: 20
|
|
20
|
+
const responseBody = {
|
|
21
|
+
id: 'msg_123',
|
|
22
|
+
type: 'message',
|
|
23
|
+
role: Role.ASSISTANT,
|
|
24
|
+
content: [
|
|
25
|
+
{
|
|
26
|
+
type: ContentBlockType.TEXT,
|
|
27
|
+
text: 'How can I assist you today?'
|
|
37
28
|
}
|
|
38
|
-
|
|
29
|
+
],
|
|
30
|
+
model: data.model,
|
|
31
|
+
stop_reason: 'end_turn',
|
|
32
|
+
stop_sequence: undefined,
|
|
33
|
+
usage: {
|
|
34
|
+
input_tokens: 10,
|
|
35
|
+
output_tokens: 20
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (data.stream) {
|
|
39
40
|
const steam = new ReadableStream({
|
|
40
41
|
start(controller) {
|
|
41
42
|
controller.enqueue('data: ' + JSON.stringify({
|
|
42
43
|
type: 'message_start',
|
|
43
|
-
data:
|
|
44
|
+
data: responseBody
|
|
44
45
|
}) + '\n\n');
|
|
45
46
|
controller.close();
|
|
46
47
|
}
|
|
47
48
|
});
|
|
48
|
-
return {
|
|
49
|
+
return {
|
|
50
|
+
data: steam,
|
|
51
|
+
status: 200,
|
|
52
|
+
headers: { 'content-type': 'text/event-stream' }
|
|
53
|
+
};
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
return {
|
|
52
|
-
data:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
role: Role.ASSISTANT,
|
|
56
|
-
content: [
|
|
57
|
-
{
|
|
58
|
-
type: ContentBlockType.TEXT,
|
|
59
|
-
text: 'How can I assist you today?'
|
|
60
|
-
}
|
|
61
|
-
],
|
|
62
|
-
model: data.model,
|
|
63
|
-
stop_reason: 'end_turn',
|
|
64
|
-
stop_sequence: undefined,
|
|
65
|
-
usage: {
|
|
66
|
-
input_tokens: 10,
|
|
67
|
-
output_tokens: 20
|
|
68
|
-
}
|
|
69
|
-
}
|
|
57
|
+
data: responseBody,
|
|
58
|
+
status: 200,
|
|
59
|
+
headers: { 'content-type': 'application/json' }
|
|
70
60
|
};
|
|
71
61
|
}
|
|
72
62
|
});
|
|
@@ -74,7 +64,7 @@ describe('AnthropicAPIImpl', () => {
|
|
|
74
64
|
});
|
|
75
65
|
|
|
76
66
|
describe('chat', () => {
|
|
77
|
-
it('should return a ChatResponse', async () => {
|
|
67
|
+
it('should return a ResponseEntity with ChatResponse', async () => {
|
|
78
68
|
const messages = [
|
|
79
69
|
new AnthropicMessage(
|
|
80
70
|
[new ContentBlock(ContentBlockType.TEXT, undefined, 'Hello')],
|
|
@@ -89,8 +79,10 @@ describe('AnthropicAPIImpl', () => {
|
|
|
89
79
|
.build();
|
|
90
80
|
|
|
91
81
|
const response = await anthropicAPI.chat(chatRequest);
|
|
92
|
-
expect(response).to.
|
|
93
|
-
expect(response.
|
|
82
|
+
expect(response.status).to.equal(200);
|
|
83
|
+
expect(response.headers).to.deep.include({ 'content-type': 'application/json' });
|
|
84
|
+
expect(response.body).to.be.instanceOf(ChatResponse);
|
|
85
|
+
expect(response.body.model).to.equal(AnthropicModel.CLAUDE_3_HAIKU);
|
|
94
86
|
});
|
|
95
87
|
|
|
96
88
|
it('should throw an error if stream mode is enabled', async () => {
|
|
@@ -117,7 +109,7 @@ describe('AnthropicAPIImpl', () => {
|
|
|
117
109
|
});
|
|
118
110
|
|
|
119
111
|
describe('streamingChat', () => {
|
|
120
|
-
it('should return an Observable of ChatResponse', async () => {
|
|
112
|
+
it('should return an Observable of ResponseEntity with ChatResponse', async () => {
|
|
121
113
|
const messages = [
|
|
122
114
|
new AnthropicMessage(
|
|
123
115
|
[new ContentBlock(ContentBlockType.TEXT, undefined, 'Hello')],
|
|
@@ -134,8 +126,10 @@ describe('AnthropicAPIImpl', () => {
|
|
|
134
126
|
const response$ = await anthropicAPI.streamingChat(chatRequest);
|
|
135
127
|
response$.subscribe({
|
|
136
128
|
next: response => {
|
|
137
|
-
expect(response).to.
|
|
138
|
-
expect(response.
|
|
129
|
+
expect(response.status).to.equal(200);
|
|
130
|
+
expect(response.headers).to.deep.include({ 'content-type': 'text/event-stream' });
|
|
131
|
+
expect(response.body).to.be.instanceOf(ChatResponse);
|
|
132
|
+
expect(response.body.model).to.equal(AnthropicModel.CLAUDE_3_HAIKU);
|
|
139
133
|
}
|
|
140
134
|
});
|
|
141
135
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Assert, Autowired, Component, Optional, Value } from '@celljs/core';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
3
|
import { map, filter, groupBy, concatMap, reduce } from 'rxjs/operators';
|
|
4
|
-
import { RestOperations } from '@celljs/http';
|
|
4
|
+
import { ResponseEntity, RestOperations } from '@celljs/http';
|
|
5
5
|
import { ChatRequest } from './chat-request';
|
|
6
6
|
import { ChatResponse } from './chat-response';
|
|
7
7
|
import { AnthropicAPI, AnthropicAPIOptions } from './api-protocol';
|
|
@@ -35,16 +35,20 @@ export class AnthropicAPIImpl implements AnthropicAPI {
|
|
|
35
35
|
return this.options?.anthropicVersion ?? this.defaultOptions?.anthropicVersion ?? '2023-06-01';
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
async chat(chatRequest: ChatRequest): Promise<ChatResponse
|
|
38
|
+
async chat(chatRequest: ChatRequest): Promise<ResponseEntity<ChatResponse>> {
|
|
39
39
|
Assert.isTrue(!chatRequest.stream, 'Request must set the stream property to false.');
|
|
40
|
-
const { data } = await this.restOperations
|
|
40
|
+
const { data, status, headers } = await this.restOperations
|
|
41
41
|
.post('/v1/messages', instanceToPlain(chatRequest), { baseURL: this.baseUrl });
|
|
42
42
|
|
|
43
|
-
return
|
|
43
|
+
return {
|
|
44
|
+
status,
|
|
45
|
+
headers,
|
|
46
|
+
body: plainToInstance(ChatResponse, data)
|
|
47
|
+
};
|
|
44
48
|
}
|
|
45
|
-
async streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse
|
|
49
|
+
async streamingChat(chatRequest: ChatRequest): Promise<Observable<ResponseEntity<ChatResponse>>> {
|
|
46
50
|
Assert.isTrue(chatRequest.stream, 'Request must set the stream property to true.');
|
|
47
|
-
const { data } = await this.restOperations
|
|
51
|
+
const { data, status, headers } = await this.restOperations
|
|
48
52
|
.post<ReadableStream>(
|
|
49
53
|
'/v1/messages',
|
|
50
54
|
instanceToPlain(chatRequest),
|
|
@@ -82,8 +86,12 @@ export class AnthropicAPIImpl implements AnthropicAPI {
|
|
|
82
86
|
)
|
|
83
87
|
)
|
|
84
88
|
),
|
|
85
|
-
map(event =>
|
|
86
|
-
|
|
89
|
+
map(event => ({
|
|
90
|
+
status,
|
|
91
|
+
headers,
|
|
92
|
+
body: StreamUtil.eventToChatResponse(event)
|
|
93
|
+
})),
|
|
94
|
+
filter(response => !!response.body.type)
|
|
87
95
|
);
|
|
88
96
|
|
|
89
97
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import { ChatRequest } from './chat-request';
|
|
3
3
|
import { ChatResponse } from './chat-response';
|
|
4
|
+
import { ResponseEntity } from '@celljs/http';
|
|
4
5
|
|
|
5
6
|
export const AnthropicAPI = Symbol('AnthropicAPI');
|
|
6
7
|
export const AnthropicAPIOptions = Symbol('AnthropicAPIOptions');
|
|
@@ -34,12 +35,12 @@ export interface AnthropicAPI {
|
|
|
34
35
|
* @param chatRequest Chat request.
|
|
35
36
|
* @return Chat response.
|
|
36
37
|
*/
|
|
37
|
-
chat(chatRequest: ChatRequest): Promise<ChatResponse
|
|
38
|
+
chat(chatRequest: ChatRequest): Promise<ResponseEntity<ChatResponse>>;
|
|
38
39
|
/**
|
|
39
40
|
* Streaming response for the chat completion request.
|
|
40
41
|
* @param chatRequest Chat request. The request must set the stream property to true.
|
|
41
42
|
* @return Chat response as a {@link Flux} stream.
|
|
42
43
|
*/
|
|
43
|
-
streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse
|
|
44
|
+
streamingChat(chatRequest: ChatRequest): Promise<Observable<ResponseEntity<ChatResponse>>>;
|
|
44
45
|
|
|
45
46
|
}
|
|
@@ -32,12 +32,12 @@ export class AnthropicChatModel implements ChatModel {
|
|
|
32
32
|
protected readonly logger: Logger;
|
|
33
33
|
|
|
34
34
|
protected getContentBlockTypeByMedia(media: Media): ContentBlockType {
|
|
35
|
-
if (media.
|
|
35
|
+
if (media.mimeType.toString().startsWith('image')) {
|
|
36
36
|
return ContentBlockType.IMAGE;
|
|
37
|
-
} else if (media.
|
|
37
|
+
} else if (media.mimeType.toString().startsWith('pdf')) {
|
|
38
38
|
return ContentBlockType.DOCUMENT;
|
|
39
39
|
}
|
|
40
|
-
throw new IllegalArgumentError(`Unsupported media type: ${media.
|
|
40
|
+
throw new IllegalArgumentError(`Unsupported media type: ${media.mimeType.toString()}. Supported types are: images (image/*) and PDF documents (application/pdf)`);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
protected fromMediaData(mediaData: any): string {
|
|
@@ -68,7 +68,7 @@ export class AnthropicChatModel implements ChatModel {
|
|
|
68
68
|
if (message.media.length > 0) {
|
|
69
69
|
const mediaContent = message.media.map(media => {
|
|
70
70
|
const contentBlockType = this.getContentBlockTypeByMedia(media);
|
|
71
|
-
const source = new Source('base64', media.
|
|
71
|
+
const source = new Source('base64', media.mimeType.toString(), this.fromMediaData(media.data));
|
|
72
72
|
return new ContentBlock(contentBlockType, source);
|
|
73
73
|
});
|
|
74
74
|
contents.push(...mediaContent);
|
|
@@ -175,13 +175,13 @@ export class AnthropicChatModel implements ChatModel {
|
|
|
175
175
|
async call(prompt: Prompt): Promise<ChatResponse> {
|
|
176
176
|
const request = this.createRequest(prompt, false);
|
|
177
177
|
const chatResponse = await this.chatApi.chat(request);
|
|
178
|
-
return this.toChatResponse(chatResponse);
|
|
178
|
+
return this.toChatResponse(chatResponse.body);
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
async stream(prompt: Prompt): Promise<Observable<ChatResponse>> {
|
|
182
182
|
const request = this.createRequest(prompt, true);
|
|
183
183
|
const chatResponse = await this.chatApi.streamingChat(request);
|
|
184
|
-
return chatResponse.pipe(map(chunk => this.toChatResponse(chunk)));
|
|
184
|
+
return chatResponse.pipe(map(chunk => this.toChatResponse(chunk.body)));
|
|
185
185
|
}
|
|
186
186
|
|
|
187
187
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Component } from '@celljs/core';
|
|
2
2
|
import { Observable, from } from 'rxjs';
|
|
3
|
+
import { ResponseEntity } from '@celljs/http';
|
|
3
4
|
import { AnthropicAPI } from '../api/api-protocol';
|
|
4
5
|
import { ChatRequest } from '../api/chat-request';
|
|
5
6
|
import { ChatResponse } from '../api/chat-response';
|
|
@@ -8,12 +9,12 @@ import { Role } from '../api/message';
|
|
|
8
9
|
|
|
9
10
|
@Component(AnthropicAPI)
|
|
10
11
|
export class MockAnthropicAPI implements AnthropicAPI {
|
|
11
|
-
async chat(chatRequest: ChatRequest): Promise<ChatResponse
|
|
12
|
+
async chat(chatRequest: ChatRequest): Promise<ResponseEntity<ChatResponse>> {
|
|
12
13
|
if (chatRequest.stream) {
|
|
13
14
|
throw new Error('Request must set the stream property to false.');
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
const response = new ChatResponse(
|
|
17
18
|
'msg_123',
|
|
18
19
|
'message',
|
|
19
20
|
Role.ASSISTANT,
|
|
@@ -31,9 +32,17 @@ export class MockAnthropicAPI implements AnthropicAPI {
|
|
|
31
32
|
outputTokens: 20
|
|
32
33
|
}
|
|
33
34
|
);
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
status: 200,
|
|
38
|
+
headers: {
|
|
39
|
+
'x-request-id': 'mock-req-123'
|
|
40
|
+
},
|
|
41
|
+
body: response
|
|
42
|
+
};
|
|
34
43
|
}
|
|
35
44
|
|
|
36
|
-
async streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse
|
|
45
|
+
async streamingChat(chatRequest: ChatRequest): Promise<Observable<ResponseEntity<ChatResponse>>> {
|
|
37
46
|
if (!chatRequest.stream) {
|
|
38
47
|
throw new Error('Request must set the stream property to true.');
|
|
39
48
|
}
|
|
@@ -57,6 +66,12 @@ export class MockAnthropicAPI implements AnthropicAPI {
|
|
|
57
66
|
}
|
|
58
67
|
);
|
|
59
68
|
|
|
60
|
-
return from([
|
|
69
|
+
return from([{
|
|
70
|
+
status: 200,
|
|
71
|
+
headers: {
|
|
72
|
+
'x-request-id': 'mock-req-123'
|
|
73
|
+
},
|
|
74
|
+
body: response
|
|
75
|
+
}]);
|
|
61
76
|
}
|
|
62
77
|
}
|