@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,9 @@
|
|
|
1
|
+
import { ChatResponse } from '../api/chat-response';
|
|
2
|
+
import { AnthropicEvent } from '../api/event';
|
|
3
|
+
export declare abstract class StreamUtil {
|
|
4
|
+
static isToolUseStart(event: AnthropicEvent): boolean;
|
|
5
|
+
static isToolUseFinish(event: AnthropicEvent): boolean;
|
|
6
|
+
static mergeToolUseEvents(previousEvent: AnthropicEvent, event: AnthropicEvent): AnthropicEvent;
|
|
7
|
+
static eventToChatResponse(event: AnthropicEvent): ChatResponse;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=stream-util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-util.d.ts","sourceRoot":"","sources":["../../../src/common/utils/stream-util.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAgC,MAAM,sBAAsB,CAAC;AAClF,OAAO,EAWH,cAAc,EACjB,MAAM,cAAc,CAAC;AAGtB,8BAAsB,UAAU;IAC5B,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO;IAOrD,MAAM,CAAC,eAAe,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO;IAOtD,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,cAAc,EAAE,KAAK,EAAE,cAAc,GAAG,cAAc;IAsC/F,MAAM,CAAC,mBAAmB,CAAC,KAAK,EAAE,cAAc,GAAG,YAAY;CAuElE"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamUtil = void 0;
|
|
4
|
+
const core_1 = require("@celljs/core");
|
|
5
|
+
const chat_response_1 = require("../api/chat-response");
|
|
6
|
+
const event_1 = require("../api/event");
|
|
7
|
+
const content_block_1 = require("../api/content-block");
|
|
8
|
+
class StreamUtil {
|
|
9
|
+
static isToolUseStart(event) {
|
|
10
|
+
if (!event || !event.type || event.type !== event_1.EventType.CONTENT_BLOCK_START) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return event.contentBlock.type === content_block_1.ContentBlockType.TOOL_USE;
|
|
14
|
+
}
|
|
15
|
+
static isToolUseFinish(event) {
|
|
16
|
+
if (!event || !event.type || event.type !== event_1.EventType.CONTENT_BLOCK_STOP) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
static mergeToolUseEvents(previousEvent, event) {
|
|
22
|
+
if (!previousEvent || !event) {
|
|
23
|
+
return event;
|
|
24
|
+
}
|
|
25
|
+
const eventAggregator = previousEvent;
|
|
26
|
+
if (event.type === event_1.EventType.CONTENT_BLOCK_START) {
|
|
27
|
+
const contentBlockStart = event;
|
|
28
|
+
if (contentBlockStart.contentBlock.type === content_block_1.ContentBlockType.TOOL_USE) {
|
|
29
|
+
const cbToolUse = contentBlockStart.contentBlock;
|
|
30
|
+
eventAggregator.id = cbToolUse.id;
|
|
31
|
+
eventAggregator.name = cbToolUse.name;
|
|
32
|
+
eventAggregator.index = contentBlockStart.index;
|
|
33
|
+
eventAggregator.partialJson = '';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (event.type === event_1.EventType.CONTENT_BLOCK_DELTA) {
|
|
37
|
+
const contentBlockDelta = event;
|
|
38
|
+
core_1.Assert.isTrue(contentBlockDelta.delta.type === content_block_1.ContentBlockType.INPUT_JSON_DELTA, 'The json content block delta should have been aggregated. Unsupported content block type: ' + contentBlockDelta.delta.type);
|
|
39
|
+
if (contentBlockDelta.delta.type === content_block_1.ContentBlockType.INPUT_JSON_DELTA) {
|
|
40
|
+
return eventAggregator.appendPartialJson(contentBlockDelta.delta.partialJson);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (event.type === event_1.EventType.CONTENT_BLOCK_STOP) {
|
|
44
|
+
if (!eventAggregator.isEmpty()) {
|
|
45
|
+
eventAggregator.squashIntoContentBlock();
|
|
46
|
+
return eventAggregator;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return event;
|
|
50
|
+
}
|
|
51
|
+
static eventToChatResponse(event) {
|
|
52
|
+
if (event.type === event_1.EventType.MESSAGE_START) {
|
|
53
|
+
chat_response_1.ChatResponseBuilderReference.reset();
|
|
54
|
+
const messageStartEvent = event;
|
|
55
|
+
chat_response_1.ChatResponseBuilderReference.get()
|
|
56
|
+
.withType(event.type)
|
|
57
|
+
.withId(messageStartEvent.message.id)
|
|
58
|
+
.withRole(messageStartEvent.message.role)
|
|
59
|
+
.withModel(messageStartEvent.message.model)
|
|
60
|
+
.withUsage(messageStartEvent.message.usage)
|
|
61
|
+
.withContent([]);
|
|
62
|
+
}
|
|
63
|
+
else if (event.type === event_1.EventType.TOOL_USE_AGGREGATE) {
|
|
64
|
+
const eventToolUseBuilder = event;
|
|
65
|
+
if (eventToolUseBuilder.toolContentBlocks && eventToolUseBuilder.toolContentBlocks.length > 0) {
|
|
66
|
+
const content = eventToolUseBuilder.toolContentBlocks.map(toolToUse => new content_block_1.ContentBlock(content_block_1.ContentBlockType.TOOL_USE, undefined, undefined, undefined, toolToUse.id, toolToUse.name, toolToUse.input));
|
|
67
|
+
chat_response_1.ChatResponseBuilderReference.get().withContent(content);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (event.type === event_1.EventType.CONTENT_BLOCK_START) {
|
|
71
|
+
const contentBlockStartEvent = event;
|
|
72
|
+
core_1.Assert.isTrue(contentBlockStartEvent.contentBlock.type === content_block_1.ContentBlockType.TEXT, 'The json content block should have been aggregated. Unsupported content block type: ' + contentBlockStartEvent.contentBlock.type);
|
|
73
|
+
if (contentBlockStartEvent.contentBlock.type === content_block_1.ContentBlockType.TEXT) {
|
|
74
|
+
const contentBlockText = contentBlockStartEvent.contentBlock;
|
|
75
|
+
chat_response_1.ChatResponseBuilderReference
|
|
76
|
+
.get()
|
|
77
|
+
.withType(event.type)
|
|
78
|
+
.withContent([new content_block_1.ContentBlock(content_block_1.ContentBlockType.TEXT, undefined, contentBlockText.text, contentBlockStartEvent.index)]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (event.type === event_1.EventType.CONTENT_BLOCK_DELTA) {
|
|
82
|
+
const contentBlockDeltaEvent = event;
|
|
83
|
+
if (contentBlockDeltaEvent.delta.type === content_block_1.ContentBlockType.TEXT_DELTA) {
|
|
84
|
+
const deltaTxt = contentBlockDeltaEvent.delta;
|
|
85
|
+
chat_response_1.ChatResponseBuilderReference
|
|
86
|
+
.get()
|
|
87
|
+
.withType(event.type)
|
|
88
|
+
.withContent([new content_block_1.ContentBlock(content_block_1.ContentBlockType.TEXT_DELTA, undefined, deltaTxt.text, contentBlockDeltaEvent.index)]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (event.type === event_1.EventType.MESSAGE_DELTA) {
|
|
92
|
+
chat_response_1.ChatResponseBuilderReference.get().withType(event.type);
|
|
93
|
+
const messageDeltaEvent = event;
|
|
94
|
+
if (messageDeltaEvent.delta.stopReason) {
|
|
95
|
+
chat_response_1.ChatResponseBuilderReference.get().withStopReason(messageDeltaEvent.delta.stopReason);
|
|
96
|
+
}
|
|
97
|
+
if (messageDeltaEvent.delta.stopSequence) {
|
|
98
|
+
chat_response_1.ChatResponseBuilderReference.get().withStopSequence(messageDeltaEvent.delta.stopSequence);
|
|
99
|
+
}
|
|
100
|
+
if (messageDeltaEvent.usage) {
|
|
101
|
+
chat_response_1.ChatResponseBuilderReference.get().withOutputTokens(messageDeltaEvent.usage.outputTokens);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (event.type === event_1.EventType.MESSAGE_STOP) {
|
|
105
|
+
// pass through
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
chat_response_1.ChatResponseBuilderReference.get().withType(event.type).withContent([]);
|
|
109
|
+
}
|
|
110
|
+
return chat_response_1.ChatResponseBuilderReference.get().build();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.StreamUtil = StreamUtil;
|
|
114
|
+
//# sourceMappingURL=stream-util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-util.js","sourceRoot":"","sources":["../../../src/common/utils/stream-util.ts"],"names":[],"mappings":";;;AAAA,uCAAsC;AACtC,wDAAkF;AAClF,wCAYsB;AACtB,wDAAsE;AAEtE,MAAsB,UAAU;IAC5B,MAAM,CAAC,cAAc,CAAC,KAAqB;QACvC,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,IAAI,I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file":"package.spec.d.ts","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":"AAAA,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IAElC,EAAE,CAAC,0CAA0C,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;AAC7D,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@celljs/ai-anthropic",
|
|
3
|
+
"version": "3.4.0",
|
|
4
|
+
"description": "Authropic models support",
|
|
5
|
+
"main": "lib/common/index.js",
|
|
6
|
+
"typings": "lib/common/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@celljs/ai-core": "3.4.0",
|
|
9
|
+
"@celljs/core": "3.4.0",
|
|
10
|
+
"@celljs/http": "3.4.0",
|
|
11
|
+
"class-transformer": "^0.5.1",
|
|
12
|
+
"rxjs": "^6.6.0",
|
|
13
|
+
"tslib": "^2.8.0"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"cell-component"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/cellbang/cell/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/cellbang/cell",
|
|
30
|
+
"files": [
|
|
31
|
+
"lib",
|
|
32
|
+
"src",
|
|
33
|
+
"cell.yml"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"lint": "cell-component lint",
|
|
37
|
+
"build": "cell-component build",
|
|
38
|
+
"watch": "cell-component watch",
|
|
39
|
+
"clean": "cell-component clean",
|
|
40
|
+
"test": "cell-component test:js"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@celljs/component": "3.4.0"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "b114f353f7970576de97cc116e9a67a1a60cb686"
|
|
46
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { ChatRequest } from './chat-request';
|
|
3
|
+
import { RestOperations } from '@celljs/http';
|
|
4
|
+
import { createContainer } from '../test/test-container';
|
|
5
|
+
import { AnthropicAPI } from './api-protocol';
|
|
6
|
+
import { AnthropicModel } from './anthropic-model';
|
|
7
|
+
import { ContentBlock, ContentBlockType } from './content-block';
|
|
8
|
+
import { Role, AnthropicMessage } from './message';
|
|
9
|
+
import { ChatResponse } from './chat-response';
|
|
10
|
+
import '../index';
|
|
11
|
+
|
|
12
|
+
const container = createContainer();
|
|
13
|
+
|
|
14
|
+
describe('AnthropicAPIImpl', () => {
|
|
15
|
+
let anthropicAPI: AnthropicAPI;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
container.rebind(RestOperations).toConstantValue({
|
|
19
|
+
post: async (url: string, data: any, config: any) => {
|
|
20
|
+
if (data.stream) {
|
|
21
|
+
const chatResponse = {
|
|
22
|
+
id: 'msg_123',
|
|
23
|
+
type: 'message',
|
|
24
|
+
role: Role.ASSISTANT,
|
|
25
|
+
content: [
|
|
26
|
+
{
|
|
27
|
+
type: ContentBlockType.TEXT,
|
|
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
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const steam = new ReadableStream({
|
|
40
|
+
start(controller) {
|
|
41
|
+
controller.enqueue('data: ' + JSON.stringify({
|
|
42
|
+
type: 'message_start',
|
|
43
|
+
data: chatResponse
|
|
44
|
+
}) + '\n\n');
|
|
45
|
+
controller.close();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return { data: steam };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
data: {
|
|
53
|
+
id: 'msg_123',
|
|
54
|
+
type: 'message',
|
|
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
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
anthropicAPI = container.get(AnthropicAPI);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('chat', () => {
|
|
77
|
+
it('should return a ChatResponse', async () => {
|
|
78
|
+
const messages = [
|
|
79
|
+
new AnthropicMessage(
|
|
80
|
+
[new ContentBlock(ContentBlockType.TEXT, undefined, 'Hello')],
|
|
81
|
+
Role.USER
|
|
82
|
+
)
|
|
83
|
+
];
|
|
84
|
+
const chatRequest = ChatRequest.builder()
|
|
85
|
+
.withModel(AnthropicModel.CLAUDE_3_HAIKU)
|
|
86
|
+
.withMessages(messages)
|
|
87
|
+
.withMaxTokens(1000)
|
|
88
|
+
.withStream(false)
|
|
89
|
+
.build();
|
|
90
|
+
|
|
91
|
+
const response = await anthropicAPI.chat(chatRequest);
|
|
92
|
+
expect(response).to.be.instanceOf(ChatResponse);
|
|
93
|
+
expect(response.model).to.equal(AnthropicModel.CLAUDE_3_HAIKU);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should throw an error if stream mode is enabled', async () => {
|
|
97
|
+
const messages = [
|
|
98
|
+
new AnthropicMessage(
|
|
99
|
+
[new ContentBlock(ContentBlockType.TEXT, undefined, 'Hello')],
|
|
100
|
+
Role.USER
|
|
101
|
+
)
|
|
102
|
+
];
|
|
103
|
+
const chatRequest = ChatRequest.builder()
|
|
104
|
+
.withModel(AnthropicModel.CLAUDE_3_HAIKU)
|
|
105
|
+
.withMessages(messages)
|
|
106
|
+
.withMaxTokens(1000)
|
|
107
|
+
.withStream(true)
|
|
108
|
+
.build();
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
await anthropicAPI.chat(chatRequest);
|
|
112
|
+
expect.fail('Should have thrown an error');
|
|
113
|
+
} catch (e) {
|
|
114
|
+
expect(e.message).to.equal('Request must set the stream property to false.');
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('streamingChat', () => {
|
|
120
|
+
it('should return an Observable of ChatResponse', async () => {
|
|
121
|
+
const messages = [
|
|
122
|
+
new AnthropicMessage(
|
|
123
|
+
[new ContentBlock(ContentBlockType.TEXT, undefined, 'Hello')],
|
|
124
|
+
Role.USER
|
|
125
|
+
)
|
|
126
|
+
];
|
|
127
|
+
const chatRequest = ChatRequest.builder()
|
|
128
|
+
.withModel(AnthropicModel.CLAUDE_3_HAIKU)
|
|
129
|
+
.withMessages(messages)
|
|
130
|
+
.withMaxTokens(1000)
|
|
131
|
+
.withStream(true)
|
|
132
|
+
.build();
|
|
133
|
+
|
|
134
|
+
const response$ = await anthropicAPI.streamingChat(chatRequest);
|
|
135
|
+
response$.subscribe({
|
|
136
|
+
next: response => {
|
|
137
|
+
expect(response).to.be.instanceOf(ChatResponse);
|
|
138
|
+
expect(response.model).to.equal(AnthropicModel.CLAUDE_3_HAIKU);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should throw an error if stream mode is disabled', async () => {
|
|
144
|
+
const messages = [
|
|
145
|
+
new AnthropicMessage(
|
|
146
|
+
[new ContentBlock(ContentBlockType.TEXT, undefined, 'Hello')],
|
|
147
|
+
Role.USER
|
|
148
|
+
)
|
|
149
|
+
];
|
|
150
|
+
const chatRequest = ChatRequest.builder()
|
|
151
|
+
.withModel(AnthropicModel.CLAUDE_3_HAIKU)
|
|
152
|
+
.withMessages(messages)
|
|
153
|
+
.withMaxTokens(1000)
|
|
154
|
+
.withStream(false)
|
|
155
|
+
.build();
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
await anthropicAPI.streamingChat(chatRequest);
|
|
159
|
+
expect.fail('Should have thrown an error');
|
|
160
|
+
} catch (e) {
|
|
161
|
+
expect(e.message).to.equal('Request must set the stream property to true.');
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Assert, Autowired, Component, Optional, Value } from '@celljs/core';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { map, filter, groupBy, concatMap, reduce } from 'rxjs/operators';
|
|
4
|
+
import { RestOperations } from '@celljs/http';
|
|
5
|
+
import { ChatRequest } from './chat-request';
|
|
6
|
+
import { ChatResponse } from './chat-response';
|
|
7
|
+
import { AnthropicAPI, AnthropicAPIOptions } from './api-protocol';
|
|
8
|
+
import { instanceToPlain, plainToInstance } from 'class-transformer';
|
|
9
|
+
import { SSEUtil, StreamEvent } from '@celljs/ai-core';
|
|
10
|
+
import { EventType, AnthropicStreamEvent, ToolUseAggregationEvent } from './event';
|
|
11
|
+
import { StreamUtil } from '../utils';
|
|
12
|
+
|
|
13
|
+
@Component(AnthropicAPI)
|
|
14
|
+
export class AnthropicAPIImpl implements AnthropicAPI {
|
|
15
|
+
|
|
16
|
+
@Autowired(RestOperations)
|
|
17
|
+
protected readonly restOperations: RestOperations;
|
|
18
|
+
|
|
19
|
+
@Value('cell.ai.anthropic.api')
|
|
20
|
+
protected readonly options?: AnthropicAPIOptions;
|
|
21
|
+
|
|
22
|
+
@Autowired(AnthropicAPIOptions)
|
|
23
|
+
@Optional()
|
|
24
|
+
protected readonly defaultOptions?: AnthropicAPIOptions;
|
|
25
|
+
|
|
26
|
+
protected get baseUrl(): string {
|
|
27
|
+
return this.options?.baseUrl ?? this.defaultOptions?.baseUrl ?? 'https://api.anthropic.com';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected get apiKey(): string | undefined {
|
|
31
|
+
return this.options?.apiKey ?? this.defaultOptions?.apiKey;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected get anthropicVersion(): string {
|
|
35
|
+
return this.options?.anthropicVersion ?? this.defaultOptions?.anthropicVersion ?? '2023-06-01';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async chat(chatRequest: ChatRequest): Promise<ChatResponse> {
|
|
39
|
+
Assert.isTrue(!chatRequest.stream, 'Request must set the stream property to false.');
|
|
40
|
+
const { data } = await this.restOperations
|
|
41
|
+
.post('/v1/messages', instanceToPlain(chatRequest), { baseURL: this.baseUrl });
|
|
42
|
+
|
|
43
|
+
return plainToInstance(ChatResponse, data);
|
|
44
|
+
}
|
|
45
|
+
async streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse>> {
|
|
46
|
+
Assert.isTrue(chatRequest.stream, 'Request must set the stream property to true.');
|
|
47
|
+
const { data } = await this.restOperations
|
|
48
|
+
.post<ReadableStream>(
|
|
49
|
+
'/v1/messages',
|
|
50
|
+
instanceToPlain(chatRequest),
|
|
51
|
+
{
|
|
52
|
+
baseURL: this.baseUrl,
|
|
53
|
+
headers: {
|
|
54
|
+
'Accept': 'text/event-stream',
|
|
55
|
+
},
|
|
56
|
+
responseType: 'stream'
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
let isInsideTool = false;
|
|
60
|
+
return SSEUtil.toObservable<StreamEvent<{}>>(data)
|
|
61
|
+
.pipe(
|
|
62
|
+
map(item => plainToInstance(AnthropicStreamEvent, item)),
|
|
63
|
+
filter(item => item.data.type !== EventType.PING),
|
|
64
|
+
map(item => {
|
|
65
|
+
if (StreamUtil.isToolUseStart(item.data)) {
|
|
66
|
+
isInsideTool = true;
|
|
67
|
+
}
|
|
68
|
+
return item;
|
|
69
|
+
}),
|
|
70
|
+
groupBy(item => {
|
|
71
|
+
if (isInsideTool && StreamUtil.isToolUseFinish(item.data)) {
|
|
72
|
+
isInsideTool = false;
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return !isInsideTool;
|
|
76
|
+
}),
|
|
77
|
+
concatMap(group =>
|
|
78
|
+
group.pipe(
|
|
79
|
+
reduce(
|
|
80
|
+
(acc, curr) => StreamUtil.mergeToolUseEvents(acc, curr.data),
|
|
81
|
+
new ToolUseAggregationEvent()
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
),
|
|
85
|
+
map(event => StreamUtil.eventToChatResponse(event)),
|
|
86
|
+
filter(response => !!response.type)
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper class for the Anthropic model.
|
|
3
|
+
*/
|
|
4
|
+
export enum AnthropicModel {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The claude-3-5-sonnet-20241022 model.
|
|
8
|
+
*/
|
|
9
|
+
CLAUDE_3_5_SONNET = 'claude-3-5-sonnet-latest',
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The CLAUDE_3_OPUS
|
|
13
|
+
*/
|
|
14
|
+
CLAUDE_3_OPUS = 'claude-3-opus-latest',
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The CLAUDE_3_SONNET
|
|
18
|
+
*/
|
|
19
|
+
CLAUDE_3_SONNET = 'claude-3-sonnet-20240229',
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The CLAUDE 3.5 HAIKU
|
|
23
|
+
*/
|
|
24
|
+
CLAUDE_3_5_HAIKU = 'claude-3-5-haiku-latest',
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The CLAUDE_3_HAIKU
|
|
28
|
+
*/
|
|
29
|
+
CLAUDE_3_HAIKU = 'claude-3-haiku-20240307',
|
|
30
|
+
|
|
31
|
+
// Legacy models
|
|
32
|
+
/**
|
|
33
|
+
* The CLAUDE_2_1
|
|
34
|
+
*/
|
|
35
|
+
CLAUDE_2_1 = 'claude-2.1',
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The CLAUDE_2_0
|
|
39
|
+
*/
|
|
40
|
+
CLAUDE_2 = 'claude-2.0'
|
|
41
|
+
|
|
42
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { ChatRequest } from './chat-request';
|
|
3
|
+
import { ChatResponse } from './chat-response';
|
|
4
|
+
|
|
5
|
+
export const AnthropicAPI = Symbol('AnthropicAPI');
|
|
6
|
+
export const AnthropicAPIOptions = Symbol('AnthropicAPIOptions');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Anthropic API options.
|
|
10
|
+
*/
|
|
11
|
+
export interface AnthropicAPIOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Base URL of the Anthropic API.
|
|
14
|
+
*/
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* API key to authenticate with the Anthropic API.
|
|
19
|
+
*/
|
|
20
|
+
apiKey?: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Version of the Anthropic API.
|
|
24
|
+
*/
|
|
25
|
+
anthropicVersion?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface AnthropicAPI {
|
|
29
|
+
/**
|
|
30
|
+
* Generate the next message in a chat with a provided model.
|
|
31
|
+
* This is a streaming endpoint (controlled by the 'stream' request property), so
|
|
32
|
+
* there will be a series of responses. The final response object will include
|
|
33
|
+
* statistics and additional data from the request.
|
|
34
|
+
* @param chatRequest Chat request.
|
|
35
|
+
* @return Chat response.
|
|
36
|
+
*/
|
|
37
|
+
chat(chatRequest: ChatRequest): Promise<ChatResponse>;
|
|
38
|
+
/**
|
|
39
|
+
* Streaming response for the chat completion request.
|
|
40
|
+
* @param chatRequest Chat request. The request must set the stream property to true.
|
|
41
|
+
* @return Chat response as a {@link Flux} stream.
|
|
42
|
+
*/
|
|
43
|
+
streamingChat(chatRequest: ChatRequest): Promise<Observable<ChatResponse>>;
|
|
44
|
+
|
|
45
|
+
}
|