@celljs/ai-core 3.2.0 → 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.
Files changed (115) hide show
  1. package/README.md +107 -0
  2. package/lib/common/chat/message/assistant-message.d.ts +3 -2
  3. package/lib/common/chat/message/assistant-message.d.ts.map +1 -1
  4. package/lib/common/chat/message/assistant-message.js +2 -1
  5. package/lib/common/chat/message/assistant-message.js.map +1 -1
  6. package/lib/common/chat/message/index.d.ts +1 -0
  7. package/lib/common/chat/message/index.d.ts.map +1 -1
  8. package/lib/common/chat/message/index.js +1 -0
  9. package/lib/common/chat/message/index.js.map +1 -1
  10. package/lib/common/chat/message/message-protocol.d.ts +23 -1
  11. package/lib/common/chat/message/message-protocol.d.ts.map +1 -1
  12. package/lib/common/chat/message/message-protocol.js +1 -0
  13. package/lib/common/chat/message/message-protocol.js.map +1 -1
  14. package/lib/common/chat/message/tool-response-message.d.ts +32 -0
  15. package/lib/common/chat/message/tool-response-message.d.ts.map +1 -0
  16. package/lib/common/chat/message/tool-response-message.js +23 -0
  17. package/lib/common/chat/message/tool-response-message.js.map +1 -0
  18. package/lib/common/chat/metadata/chat-generation-metadata.d.ts +28 -0
  19. package/lib/common/chat/metadata/chat-generation-metadata.d.ts.map +1 -0
  20. package/lib/common/chat/metadata/chat-generation-metadata.js +21 -0
  21. package/lib/common/chat/metadata/chat-generation-metadata.js.map +1 -0
  22. package/lib/common/chat/metadata/index.d.ts +1 -0
  23. package/lib/common/chat/metadata/index.d.ts.map +1 -1
  24. package/lib/common/chat/metadata/index.js +1 -0
  25. package/lib/common/chat/metadata/index.js.map +1 -1
  26. package/lib/common/chat/metadata/metadata-protocol.d.ts +19 -3
  27. package/lib/common/chat/metadata/metadata-protocol.d.ts.map +1 -1
  28. package/lib/common/chat/metadata/metadata-protocol.js +72 -2
  29. package/lib/common/chat/metadata/metadata-protocol.js.map +1 -1
  30. package/lib/common/chat/model/chat-protocol.d.ts +7 -0
  31. package/lib/common/chat/model/chat-protocol.d.ts.map +1 -1
  32. package/lib/common/chat/model/chat-protocol.js +16 -1
  33. package/lib/common/chat/model/chat-protocol.js.map +1 -1
  34. package/lib/common/chat/prompt/index.d.ts +1 -0
  35. package/lib/common/chat/prompt/index.d.ts.map +1 -1
  36. package/lib/common/chat/prompt/index.js +1 -0
  37. package/lib/common/chat/prompt/index.js.map +1 -1
  38. package/lib/common/chat/prompt/prompt-protocol.d.ts +17 -16
  39. package/lib/common/chat/prompt/prompt-protocol.d.ts.map +1 -1
  40. package/lib/common/chat/prompt/prompt-protocol.js +2 -0
  41. package/lib/common/chat/prompt/prompt-protocol.js.map +1 -1
  42. package/lib/common/chat/prompt/prompt-template.d.ts +8 -0
  43. package/lib/common/chat/prompt/prompt-template.d.ts.map +1 -0
  44. package/lib/common/chat/prompt/prompt-template.js +34 -0
  45. package/lib/common/chat/prompt/prompt-template.js.map +1 -0
  46. package/lib/common/embedding/embedding-protocol.d.ts +11 -8
  47. package/lib/common/embedding/embedding-protocol.d.ts.map +1 -1
  48. package/lib/common/embedding/embedding-protocol.js +10 -1
  49. package/lib/common/embedding/embedding-protocol.js.map +1 -1
  50. package/lib/common/error/error.d.ts +5 -0
  51. package/lib/common/error/error.d.ts.map +1 -0
  52. package/lib/common/error/error.js +11 -0
  53. package/lib/common/error/error.js.map +1 -0
  54. package/lib/common/error/index.d.ts +2 -0
  55. package/lib/common/error/index.d.ts.map +1 -0
  56. package/lib/common/error/index.js +5 -0
  57. package/lib/common/error/index.js.map +1 -0
  58. package/lib/common/index.d.ts +2 -0
  59. package/lib/common/index.d.ts.map +1 -1
  60. package/lib/common/index.js +2 -0
  61. package/lib/common/index.js.map +1 -1
  62. package/lib/common/model/function/function-protocol.d.ts +5 -2
  63. package/lib/common/model/function/function-protocol.d.ts.map +1 -1
  64. package/lib/common/model/function/function-protocol.js +8 -0
  65. package/lib/common/model/function/function-protocol.js.map +1 -1
  66. package/lib/common/model/model-protocol.d.ts +2 -1
  67. package/lib/common/model/model-protocol.d.ts.map +1 -1
  68. package/lib/common/sse/index.d.ts +4 -0
  69. package/lib/common/sse/index.d.ts.map +1 -0
  70. package/lib/common/sse/index.js +7 -0
  71. package/lib/common/sse/index.js.map +1 -0
  72. package/lib/common/sse/line-decoder.d.ts +15 -0
  73. package/lib/common/sse/line-decoder.d.ts.map +1 -0
  74. package/lib/common/sse/line-decoder.js +54 -0
  75. package/lib/common/sse/line-decoder.js.map +1 -0
  76. package/lib/common/sse/sse-decoder.d.ts +27 -0
  77. package/lib/common/sse/sse-decoder.d.ts.map +1 -0
  78. package/lib/common/sse/sse-decoder.js +74 -0
  79. package/lib/common/sse/sse-decoder.js.map +1 -0
  80. package/lib/common/sse/sse-protocol.d.ts +23 -0
  81. package/lib/common/sse/sse-protocol.d.ts.map +1 -0
  82. package/lib/common/sse/sse-protocol.js +4 -0
  83. package/lib/common/sse/sse-protocol.js.map +1 -0
  84. package/lib/common/utils/index.d.ts +1 -0
  85. package/lib/common/utils/index.d.ts.map +1 -1
  86. package/lib/common/utils/index.js +1 -0
  87. package/lib/common/utils/index.js.map +1 -1
  88. package/lib/common/utils/sse-util.d.ts +11 -0
  89. package/lib/common/utils/sse-util.d.ts.map +1 -0
  90. package/lib/common/utils/sse-util.js +113 -0
  91. package/lib/common/utils/sse-util.js.map +1 -0
  92. package/package.json +5 -5
  93. package/src/common/chat/message/assistant-message.ts +5 -3
  94. package/src/common/chat/message/index.ts +1 -0
  95. package/src/common/chat/message/message-protocol.ts +27 -1
  96. package/src/common/chat/message/tool-response-message.ts +40 -0
  97. package/src/common/chat/metadata/chat-generation-metadata.ts +38 -0
  98. package/src/common/chat/metadata/index.ts +1 -0
  99. package/src/common/chat/metadata/metadata-protocol.ts +85 -6
  100. package/src/common/chat/model/chat-protocol.ts +13 -0
  101. package/src/common/chat/prompt/index.ts +1 -0
  102. package/src/common/chat/prompt/prompt-protocol.ts +18 -28
  103. package/src/common/chat/prompt/prompt-template.ts +26 -0
  104. package/src/common/embedding/embedding-protocol.ts +17 -8
  105. package/src/common/error/error.ts +7 -0
  106. package/src/common/error/index.ts +1 -0
  107. package/src/common/index.ts +2 -0
  108. package/src/common/model/function/function-protocol.ts +8 -2
  109. package/src/common/model/model-protocol.ts +4 -2
  110. package/src/common/sse/index.ts +3 -0
  111. package/src/common/sse/line-decoder.ts +65 -0
  112. package/src/common/sse/sse-decoder.ts +88 -0
  113. package/src/common/sse/sse-protocol.ts +25 -0
  114. package/src/common/utils/index.ts +1 -0
  115. package/src/common/utils/sse-util.ts +115 -0
@@ -2,4 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./prompt-util"), exports);
5
+ tslib_1.__exportStar(require("./sse-util"), exports);
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/utils/index.ts"],"names":[],"mappings":";;;AAAA,wDAA8B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/utils/index.ts"],"names":[],"mappings":";;;AAAA,wDAA8B;AAC9B,qDAA2B"}
@@ -0,0 +1,11 @@
1
+ import { Observable } from 'rxjs';
2
+ import { StreamEvent } from '../sse';
3
+ /**
4
+ * SSE utility class.
5
+ */
6
+ export declare class SSEUtil {
7
+ static toObservable<Data>(readableStream: ReadableStream, controller?: AbortController): Observable<StreamEvent<Data>>;
8
+ private static handleSSE;
9
+ private static readableStreamAsyncIterable;
10
+ }
11
+ //# sourceMappingURL=sse-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse-util.d.ts","sourceRoot":"","sources":["../../../src/common/utils/sse-util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAc,MAAM,MAAM,CAAC;AAC9C,OAAO,EAAE,WAAW,EAA+B,MAAM,QAAQ,CAAC;AAGlE;;GAEG;AACH,qBAAa,OAAO;IAEhB,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,CAAC,EAAE,eAAe,GAAG,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAyCtH,OAAO,CAAC,MAAM,CAAC,SAAS;IAsCxB,OAAO,CAAC,MAAM,CAAC,2BAA2B;CA0B7C"}
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SSEUtil = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const sse_1 = require("../sse");
6
+ /**
7
+ * SSE utility class.
8
+ */
9
+ class SSEUtil {
10
+ static toObservable(readableStream, controller) {
11
+ return new rxjs_1.Observable(subscriber => {
12
+ (async () => {
13
+ const decoder = new sse_1.SSEDecoder();
14
+ const lineDecoder = new sse_1.LineDecoderImpl();
15
+ try {
16
+ const iterable = this.readableStreamAsyncIterable(readableStream);
17
+ for await (const chunk of iterable) {
18
+ const lines = lineDecoder.decode(chunk);
19
+ for (const line of lines) {
20
+ const sse = decoder.decode(line);
21
+ this.handleSSE(sse, subscriber, controller);
22
+ }
23
+ }
24
+ const remainingLines = lineDecoder.flush();
25
+ for (const line of remainingLines) {
26
+ const sse = decoder.decode(line);
27
+ if (sse) {
28
+ this.handleSSE(sse, subscriber, controller);
29
+ }
30
+ }
31
+ subscriber.complete();
32
+ }
33
+ catch (e) {
34
+ if (e instanceof Error && e.name === 'AbortError') {
35
+ subscriber.complete();
36
+ }
37
+ else {
38
+ subscriber.error(e);
39
+ }
40
+ }
41
+ finally {
42
+ controller === null || controller === void 0 ? void 0 : controller.abort();
43
+ }
44
+ })();
45
+ });
46
+ }
47
+ static handleSSE(sse, subscriber, controller) {
48
+ if (sse) {
49
+ if (sse.data.startsWith('[DONE]')) {
50
+ subscriber.complete();
51
+ controller === null || controller === void 0 ? void 0 : controller.abort();
52
+ return;
53
+ }
54
+ if (!sse.event || sse.event === 'error') {
55
+ let data;
56
+ try {
57
+ data = JSON.parse(sse.data);
58
+ }
59
+ catch (e) {
60
+ console.error('Could not parse message into JSON:', sse.data);
61
+ console.error('From chunk:', sse.raw);
62
+ subscriber.error(e);
63
+ return;
64
+ }
65
+ if (data && data.error) {
66
+ subscriber.error(data.error);
67
+ return;
68
+ }
69
+ subscriber.next({
70
+ event: sse.event,
71
+ data,
72
+ raw: sse.raw
73
+ });
74
+ if (data.done === true) {
75
+ subscriber.complete();
76
+ controller === null || controller === void 0 ? void 0 : controller.abort();
77
+ }
78
+ }
79
+ }
80
+ }
81
+ static readableStreamAsyncIterable(stream) {
82
+ if (stream[Symbol.asyncIterator]) {
83
+ return stream;
84
+ }
85
+ const reader = stream.getReader();
86
+ return {
87
+ async next() {
88
+ try {
89
+ const result = await reader.read();
90
+ if (result === null || result === void 0 ? void 0 : result.done) {
91
+ reader.releaseLock();
92
+ }
93
+ return result;
94
+ }
95
+ catch (e) {
96
+ reader.releaseLock();
97
+ throw e;
98
+ }
99
+ },
100
+ async return() {
101
+ const cancelPromise = reader.cancel();
102
+ reader.releaseLock();
103
+ await cancelPromise;
104
+ return { done: true, value: undefined };
105
+ },
106
+ [Symbol.asyncIterator]() {
107
+ return this;
108
+ },
109
+ };
110
+ }
111
+ }
112
+ exports.SSEUtil = SSEUtil;
113
+ //# sourceMappingURL=sse-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse-util.js","sourceRoot":"","sources":["../../../src/common/utils/sse-util.ts"],"names":[],"mappings":";;;AAAA,+BAA8C;AAC9C,gCAAkE;AAGlE;;GAEG;AACH,MAAa,OAAO;IAEhB,MAAM,CAAC,YAAY,CAAO,cAA8B,EAAE,UAA4B;QAClF,OAAO,IAAI,iBAAU,CAAoB,UAAU,CAAC,EAAE;YAClD,CAAC,KAAK,IAAI,EAAE;gBACR,MAAM,OAAO,GAAG,IAAI,gBAAU,EAAE,CAAC;gBACjC,MAAM,WAAW,GAAG,IAAI,qBAAe,EAAE,CAAC;gBAE1C,IAAI;oBACA,MAAM,QAAQ,GA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M,MAAM,CAAC,IAAI,EAAE,CAAC;oBACnC,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,EAAE;wBAAC,MAAM,CAAC,WAAW,EAAE,CAAC;qBAAE;oBAC1C,OAAO,MAAM,CAAC;iBACjB;gBAAC,OAAO,CAAC,EAAE;oBACR,MAAM,CAAC,WAAW,EAAE,CAAC;oBACrB,MAAM,CAAC,CAAC;iBACX;YACL,CAAC;YACD,KAAK,CAAC,MAAM;gBACR,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;gBACtC,MAAM,CAAC,WAAW,EAAE,CAAC;gBACrB,MAAM,aAAa,CAAC;gBACpB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;YAC5C,CAAC;YACD,CAAC,MAAM,CAAC,aAAa,CAAC;gBAClB,OAAO,IAAI,CAAC;YAChB,CAAC;SACJ,CAAC;IACN,CAAC;CACJ;AA3GD,0BA2GC"}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@celljs/ai-core",
3
- "version": "3.2.0",
3
+ "version": "3.4.0",
4
4
  "description": "Core domain for AI programming",
5
5
  "main": "lib/common/index.js",
6
6
  "typings": "lib/common/index.d.ts",
7
7
  "dependencies": {
8
- "@celljs/core": "3.2.0",
9
- "@celljs/http": "3.2.0",
8
+ "@celljs/core": "3.4.0",
9
+ "@celljs/http": "3.4.0",
10
10
  "rxjs": "^6.6.0",
11
11
  "tslib": "^2.8.0"
12
12
  },
@@ -38,7 +38,7 @@
38
38
  "test": "cell-component test:js"
39
39
  },
40
40
  "devDependencies": {
41
- "@celljs/component": "3.2.0"
41
+ "@celljs/component": "3.4.0"
42
42
  },
43
- "gitHead": "8547a0ea0468f20441c46736c544875f65d7dc30"
43
+ "gitHead": "b114f353f7970576de97cc116e9a67a1a60cb686"
44
44
  }
@@ -1,12 +1,14 @@
1
1
  import { AbstractMessage } from './abstract-message';
2
- import { Media, MessageType } from './message-protocol';
2
+ import { Media, MessageType, ToolCall } from './message-protocol';
3
3
 
4
4
  export class AssistantMessage extends AbstractMessage {
5
5
 
6
6
  constructor(
7
- override readonly content: string,
7
+ override readonly content: string = '',
8
8
  override readonly media: Media[] = [],
9
- override readonly metadata: Record<string, any> = {}) {
9
+ override readonly metadata: Record<string, any> = {},
10
+ readonly toolCalls: ToolCall[] = []
11
+ ) {
10
12
  super(MessageType.ASSISTANT, content, media, metadata);
11
13
  }
12
14
  }
@@ -4,3 +4,4 @@ export * from './user-message';
4
4
  export * from './system-message';
5
5
  export * from './assistant-message';
6
6
  export * from './function-message';
7
+ export * from './tool-response-message';
@@ -27,7 +27,8 @@ export enum MessageType {
27
27
  USER = 'user',
28
28
  ASSISTANT = 'assistant',
29
29
  SYSTEM = 'system',
30
- FUNCTION = 'function'
30
+ FUNCTION = 'function',
31
+ TOOL = 'tool'
31
32
  }
32
33
 
33
34
  /**
@@ -47,3 +48,28 @@ export namespace Message {
47
48
  return obj && obj.messageType !== undefined;
48
49
  }
49
50
  }
51
+
52
+ /**
53
+ * Represents a tool call.
54
+ */
55
+ export interface ToolCall {
56
+ /**
57
+ * The unique identifier for the tool call.
58
+ */
59
+ id: string;
60
+
61
+ /**
62
+ * The type of the tool.
63
+ */
64
+ type: string;
65
+
66
+ /**
67
+ * The name of the tool.
68
+ */
69
+ name: string;
70
+
71
+ /**
72
+ * The arguments passed to the tool.
73
+ */
74
+ arguments: string;
75
+ }
@@ -0,0 +1,40 @@
1
+ import { AbstractMessage } from './abstract-message';
2
+ import { MessageType } from './message-protocol';
3
+
4
+ /**
5
+ * Represents a tool response.
6
+ */
7
+ export interface ToolResponse {
8
+ /**
9
+ * The unique identifier for the tool.
10
+ */
11
+ id: string;
12
+
13
+ /**
14
+ * The name of the tool.
15
+ */
16
+ name: string;
17
+
18
+ /**
19
+ * The response data from the tool.
20
+ */
21
+ responseData: string;
22
+ }
23
+
24
+ /**
25
+ * The ToolResponseMessage class represents a message with a function content in a chat
26
+ * application.
27
+ */
28
+ export class ToolResponseMessage extends AbstractMessage {
29
+
30
+ /**
31
+ * Creates an instance of ToolResponseMessage.
32
+ * @param responses The list of tool responses.
33
+ * @param metadata Additional metadata.
34
+ */
35
+ constructor(
36
+ readonly responses: ToolResponse[], metadata: Record<string, any> = {}) {
37
+ super(MessageType.TOOL, '', [], metadata);
38
+ this.responses = responses;
39
+ }
40
+ }
@@ -0,0 +1,38 @@
1
+ import { ResultMetadata } from '../../model/model-protocol';
2
+
3
+ /**
4
+ * Abstract Data Type (ADT) encapsulating information on the completion choices in the AI
5
+ * response.
6
+ */
7
+ export interface ChatGenerationMetadata extends ResultMetadata {
8
+
9
+ /**
10
+ * The reason this choice completed for the generation.
11
+ */
12
+ readonly contentFilterMetadata?: any;
13
+
14
+ /**
15
+ * The reason this choice completed for the generation.
16
+ */
17
+ readonly finishReason?: string;
18
+ }
19
+
20
+ export class ChatGenerationMetadata {
21
+
22
+ static EMPTY: ChatGenerationMetadata = ChatGenerationMetadata.from();
23
+
24
+ /**
25
+ * Factory method used to construct a new {@link ChatGenerationMetadata} from the
26
+ * given finish reason and content filter metadata.
27
+ *
28
+ * @param finishReason The reason for completion.
29
+ * @param contentFilterMetadata The content filter metadata.
30
+ * @returns A new instance of {@link ChatGenerationMetadata}.
31
+ */
32
+ static from(finishReason?: string, contentFilterMetadata?: any): ChatGenerationMetadata {
33
+ return {
34
+ finishReason, contentFilterMetadata
35
+ };
36
+ }
37
+ }
38
+
@@ -1 +1,2 @@
1
1
  export * from './metadata-protocol';
2
+ export * from './chat-generation-metadata';
@@ -26,6 +26,12 @@ export interface Usage {
26
26
 
27
27
  }
28
28
 
29
+ export namespace Usage {
30
+ export function from(promptTokens: number, generationTokens: number): Usage {
31
+ return { promptTokens, generationTokens, totalTokens: promptTokens + generationTokens };
32
+ }
33
+ }
34
+
29
35
  /**
30
36
  * Abstract Data Type (ADT) encapsulating metadata from an AI provider's API rate limits
31
37
  * granted to the API key in use and the API key's current balance.
@@ -118,24 +124,92 @@ export namespace PromptMetadata {
118
124
  }
119
125
 
120
126
  export interface ChatResponseMetadata extends ResponseMetadata {
127
+ id?: string;
128
+ model?: string;
121
129
  /**
122
130
  * AI provider specific metadata on rate limits.
123
131
  * @see RateLimit
124
132
  */
125
- readonly rateLimit: RateLimit;
133
+ rateLimit: RateLimit;
126
134
 
127
135
  /**
128
136
  * AI provider specific metadata on API usage.
129
137
  * @see Usage
130
138
  */
131
- readonly usage: Usage;
139
+ usage: Usage;
132
140
 
133
- readonly promptMetadata: PromptMetadata;
141
+ promptMetadata: PromptMetadata;
142
+
143
+ }
144
+
145
+ export class ChatResponseMetadataBuilder {
146
+
147
+ private readonly chatResponseMetadata: ChatResponseMetadata = {
148
+ id: '',
149
+ model: '',
150
+ rateLimit: {
151
+ requestsLimit: 0,
152
+ requestsRemaining: 0,
153
+ requestsReset: 0,
154
+ tokensLimit: 0,
155
+ tokensRemaining: 0,
156
+ tokensReset: 0
157
+ },
158
+ usage: {
159
+ promptTokens: 0,
160
+ generationTokens: 0,
161
+ totalTokens: 0
162
+ },
163
+ promptMetadata: [] as PromptMetadata,
164
+ extra: {}
165
+ };
166
+
167
+ id(id: string): ChatResponseMetadataBuilder {
168
+ this.chatResponseMetadata.id = id;
169
+ return this;
170
+ }
171
+
172
+ model(model: string): ChatResponseMetadataBuilder {
173
+ this.chatResponseMetadata.model = model;
174
+ return this;
175
+ }
176
+
177
+ rateLimit(rateLimit: RateLimit): ChatResponseMetadataBuilder {
178
+ this.chatResponseMetadata.rateLimit = rateLimit;
179
+ return this;
180
+ }
181
+
182
+ usage(usage: Usage): ChatResponseMetadataBuilder {
183
+ this.chatResponseMetadata.usage = usage;
184
+ return this;
185
+ }
186
+
187
+ promptMetadata(promptMetadata: PromptMetadata): ChatResponseMetadataBuilder {
188
+ this.chatResponseMetadata.promptMetadata = promptMetadata;
189
+ return this;
190
+ }
191
+
192
+ keyValue(key: string, value: any): ChatResponseMetadataBuilder {
193
+ if (!key) {
194
+ throw new IllegalArgumentError('Key must not be empty');
195
+ }
196
+ if (value) {
197
+ this.chatResponseMetadata.extra[key] = value;
198
+ }
199
+
200
+ return this;
201
+ }
202
+
203
+ build(): ChatResponseMetadata {
204
+ return this.chatResponseMetadata;
205
+ }
134
206
 
135
207
  }
136
208
 
137
209
  export namespace ChatResponseMetadata {
138
- export const EMPTY = {
210
+ export const EMPTY: ChatResponseMetadata = {
211
+ id: '',
212
+ model: '',
139
213
  rateLimit: {
140
214
  requestsLimit: 0,
141
215
  requestsRemaining: 0,
@@ -149,6 +223,11 @@ export namespace ChatResponseMetadata {
149
223
  generationTokens: 0,
150
224
  totalTokens: 0
151
225
  },
152
- promptMetadata: [] as PromptMetadata
153
- } as ChatResponseMetadata;
226
+ promptMetadata: [] as PromptMetadata,
227
+ extra: {}
228
+ };
229
+
230
+ export function builder(): ChatResponseMetadataBuilder {
231
+ return new ChatResponseMetadataBuilder();
232
+ }
154
233
  }
@@ -1,5 +1,6 @@
1
1
  import { Model, ModelResponse, ModelResult, StreamingModel } from '../../model/model-protocol';
2
2
  import { AssistantMessage } from '../message';
3
+ import { ChatGenerationMetadata, ChatResponseMetadata } from '../metadata';
3
4
  import { Prompt } from '../prompt/prompt-protocol';
4
5
 
5
6
  export const StreamingChatModel = Symbol('StreamingChatModel');
@@ -16,6 +17,18 @@ export interface ChatResponse extends ModelResponse<Generation> {
16
17
 
17
18
  }
18
19
 
20
+ export namespace Generation {
21
+ export function from(output: AssistantMessage, metadata = ChatGenerationMetadata.EMPTY): Generation {
22
+ return { output, metadata };
23
+ }
24
+ }
25
+
26
+ export namespace ChatResponse {
27
+ export function from(results: Generation[], metadata = ChatResponseMetadata.EMPTY): ChatResponse {
28
+ return { result: results[0], results, metadata };
29
+ }
30
+ }
31
+
19
32
  export interface StreamingChatModel extends StreamingModel<Prompt, ChatResponse> {
20
33
 
21
34
  }
@@ -1,2 +1,3 @@
1
1
  export * from './prompt-protocol';
2
2
  export * from './prompt';
3
+ export * from './prompt-template';
@@ -1,15 +1,20 @@
1
1
  import { ModelOptions, ModelRequest } from '../../model/model-protocol';
2
- import { Media, Message } from '../message';
2
+ import { Message, MessageType } from '../message/message-protocol';
3
+
4
+ export const PromptTemplate = Symbol('PromptTemplate');
3
5
 
4
6
  /**
5
7
  * The ChatOptions represent the common options, portable across different chat models.
6
8
  */
7
9
  export interface ChatOptions extends ModelOptions {
8
-
9
- readonly temperature?: number;
10
- readonly topP?: number;
11
- readonly topK?: number;
12
-
10
+ model?: string;
11
+ frequencyPenalty?: number;
12
+ maxTokens?: number;
13
+ presencePenalty?: number;
14
+ stopSequences?: string[];
15
+ temperature?: number;
16
+ topK?: number;
17
+ topP?: number;
13
18
  }
14
19
 
15
20
  export interface Prompt extends ModelRequest<Message[]> {
@@ -17,28 +22,13 @@ export interface Prompt extends ModelRequest<Message[]> {
17
22
  copy(): Prompt;
18
23
  }
19
24
 
20
- export interface PromptTemplateStringActions {
21
-
22
- render(model?: Map<String, Object>): string;
23
-
25
+ export interface PromptTemplateContext {
26
+ variables?: Record<string, any>;
27
+ chatOptions?: ChatOptions;
28
+ messageType?: MessageType;
24
29
  }
25
30
 
26
- export interface PromptTemplateActions extends PromptTemplateStringActions {
27
-
28
- create(model: Map<String, Object>): Prompt;
29
-
30
- }
31
-
32
- export interface PromptTemplateChatActions {
33
-
34
- createMessages(): Message[];
35
- createMessages(model: Map<String, Object>): Message[];
36
-
37
- }
38
-
39
- export interface PromptTemplateMessageActions {
40
-
41
- createMessage(mediaList: Media[]): Message;
42
- createMessage(model: Map<String, Object>): Message;
43
-
31
+ export interface PromptTemplate {
32
+ render(template: string, ctx?: PromptTemplateContext): Promise<string>;
33
+ create(template: string, ctx?: PromptTemplateContext): Promise<Prompt>;
44
34
  }
@@ -0,0 +1,26 @@
1
+ import { Autowired, Component, ExpressionHandler, IllegalArgumentError } from '@celljs/core';
2
+ import { Prompt, PromptTemplate, PromptTemplateContext } from './prompt-protocol';
3
+ import { PromptImpl } from './prompt';
4
+ import { MessageType, SystemMessage } from '../message';
5
+
6
+ @Component(PromptTemplate)
7
+ export class PromptTemplateImpl implements PromptTemplate {
8
+
9
+ @Autowired(ExpressionHandler)
10
+ protected readonly expressionHandler: ExpressionHandler;
11
+
12
+ async render(template: string, ctx?: PromptTemplateContext): Promise<string> {
13
+ if (ctx?.variables) {
14
+ return this.expressionHandler.handle(template, ctx, { ignoreSpecialChar: true, bracketBegin: '{', bracketEnd: '}' });
15
+ }
16
+ return template;
17
+ }
18
+ async create(template: string, ctx?: PromptTemplateContext): Promise<Prompt> {
19
+ if (!ctx?.messageType || ctx?.messageType === MessageType.USER) {
20
+ return new PromptImpl(await this.render(template, ctx), ctx?.chatOptions);
21
+ } else if (ctx?.messageType === MessageType.SYSTEM) {
22
+ return new PromptImpl(new SystemMessage(await this.render(template, ctx)), ctx?.chatOptions);
23
+ }
24
+ throw new IllegalArgumentError(`Invalid message type: ${ctx?.messageType}`);
25
+ }
26
+ }
@@ -4,6 +4,16 @@ import { Model, ModelOptions, ModelRequest, ModelResponse, ModelResult, Response
4
4
 
5
5
  export const EmbeddingModel = Symbol('EmbeddingModel');
6
6
 
7
+ /**
8
+ * Enum representing the modality type of the source data.
9
+ */
10
+ export enum ModalityType {
11
+ TEXT = 'TEXT',
12
+ IMAGE = 'IMAGE',
13
+ AUDIO = 'AUDIO',
14
+ VIDEO = 'VIDEO'
15
+ }
16
+
7
17
  /**
8
18
  * Represents the metadata for an embedding result.
9
19
  */
@@ -29,14 +39,13 @@ export interface EmbeddingResultMetadata extends ResultMetadata {
29
39
  documentData: any;
30
40
  }
31
41
 
32
- /**
33
- * Enum representing the modality type of the source data.
34
- */
35
- export enum ModalityType {
36
- TEXT = 'TEXT',
37
- IMAGE = 'IMAGE',
38
- AUDIO = 'AUDIO',
39
- VIDEO = 'VIDEO'
42
+ export namespace EmbeddingResultMetadata {
43
+ export const EMPTY: EmbeddingResultMetadata = {
44
+ modalityType: ModalityType.TEXT,
45
+ documentId: '',
46
+ mimeType: MimeTypeUtils.TEXT_PLAIN,
47
+ documentData: undefined
48
+ };
40
49
  }
41
50
 
42
51
  /**
@@ -0,0 +1,7 @@
1
+ import { CustomError } from '@celljs/core';
2
+
3
+ export class AIError extends CustomError {
4
+ constructor(message: string) {
5
+ super(message);
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ export * from './error';
@@ -1,4 +1,6 @@
1
1
  export * from './chat';
2
2
  export * from './model';
3
3
  export * from './embedding';
4
+ export * from './sse';
4
5
  export * from './utils';
6
+ export * from './error';
@@ -25,12 +25,12 @@ export interface FunctionCallback {
25
25
  /**
26
26
  * Called when a model detects and triggers a function call. The model is responsible
27
27
  * to pass the function arguments in the pre-configured JSON schema format.
28
- * @param functionInput JSON string with the function arguments to be passed to the
28
+ * @param functionArguments JSON string with the function arguments to be passed to the
29
29
  * function. The arguments are defined as JSON schema usually registered with the the
30
30
  * model.
31
31
  * @return String containing the function call response.
32
32
  */
33
- call(functionInput: string): Promise<string>;
33
+ call(functionArguments: string): Promise<string>;
34
34
 
35
35
  }
36
36
 
@@ -52,6 +52,12 @@ export interface FunctionCallingOptions {
52
52
  functions: Set<String>;
53
53
  }
54
54
 
55
+ export namespace FunctionCallingOptions {
56
+ export function is(obj: any): obj is FunctionCallingOptions {
57
+ return obj && obj.functionCallbacks !== undefined && obj.functions !== undefined;
58
+ }
59
+ }
60
+
55
61
  export interface PortableFunctionCallingOptions extends FunctionCallingOptions, ChatOptions {
56
62
 
57
63
  }