@celljs/ai-core 3.0.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 +1 -0
- package/cell.yml +0 -0
- package/lib/common/chat/index.d.ts +5 -0
- package/lib/common/chat/index.d.ts.map +1 -0
- package/lib/common/chat/index.js +21 -0
- package/lib/common/chat/index.js.map +1 -0
- package/lib/common/chat/message/abstract-message.d.ts +10 -0
- package/lib/common/chat/message/abstract-message.d.ts.map +1 -0
- package/lib/common/chat/message/abstract-message.js +15 -0
- package/lib/common/chat/message/abstract-message.js.map +1 -0
- package/lib/common/chat/message/assistant-message.d.ts +9 -0
- package/lib/common/chat/message/assistant-message.d.ts.map +1 -0
- package/lib/common/chat/message/assistant-message.js +15 -0
- package/lib/common/chat/message/assistant-message.js.map +1 -0
- package/lib/common/chat/message/function-message.d.ts +9 -0
- package/lib/common/chat/message/function-message.d.ts.map +1 -0
- package/lib/common/chat/message/function-message.js +15 -0
- package/lib/common/chat/message/function-message.js.map +1 -0
- package/lib/common/chat/message/index.d.ts +7 -0
- package/lib/common/chat/message/index.d.ts.map +1 -0
- package/lib/common/chat/message/index.js +23 -0
- package/lib/common/chat/message/index.js.map +1 -0
- package/lib/common/chat/message/message-protocol.d.ts +41 -0
- package/lib/common/chat/message/message-protocol.d.ts.map +1 -0
- package/lib/common/chat/message/message-protocol.js +22 -0
- package/lib/common/chat/message/message-protocol.js.map +1 -0
- package/lib/common/chat/message/system-message.d.ts +9 -0
- package/lib/common/chat/message/system-message.d.ts.map +1 -0
- package/lib/common/chat/message/system-message.js +15 -0
- package/lib/common/chat/message/system-message.js.map +1 -0
- package/lib/common/chat/message/user-message.d.ts +9 -0
- package/lib/common/chat/message/user-message.d.ts.map +1 -0
- package/lib/common/chat/message/user-message.js +15 -0
- package/lib/common/chat/message/user-message.js.map +1 -0
- package/lib/common/chat/metadata/index.d.ts +2 -0
- package/lib/common/chat/metadata/index.d.ts.map +1 -0
- package/lib/common/chat/metadata/index.js +18 -0
- package/lib/common/chat/metadata/index.js.map +1 -0
- package/lib/common/chat/metadata/metadata-protocol.d.ts +108 -0
- package/lib/common/chat/metadata/metadata-protocol.d.ts.map +1 -0
- package/lib/common/chat/metadata/metadata-protocol.js +41 -0
- package/lib/common/chat/metadata/metadata-protocol.js.map +1 -0
- package/lib/common/chat/model/chat-protocol.d.ts +15 -0
- package/lib/common/chat/model/chat-protocol.d.ts.map +1 -0
- package/lib/common/chat/model/chat-protocol.js +3 -0
- package/lib/common/chat/model/chat-protocol.js.map +1 -0
- package/lib/common/chat/model/index.d.ts +2 -0
- package/lib/common/chat/model/index.d.ts.map +1 -0
- package/lib/common/chat/model/index.js +18 -0
- package/lib/common/chat/model/index.js.map +1 -0
- package/lib/common/chat/prompt/index.d.ts +3 -0
- package/lib/common/chat/prompt/index.d.ts.map +1 -0
- package/lib/common/chat/prompt/index.js +19 -0
- package/lib/common/chat/prompt/index.js.map +1 -0
- package/lib/common/chat/prompt/prompt-protocol.d.ts +29 -0
- package/lib/common/chat/prompt/prompt-protocol.d.ts.map +1 -0
- package/lib/common/chat/prompt/prompt-protocol.js +3 -0
- package/lib/common/chat/prompt/prompt-protocol.js.map +1 -0
- package/lib/common/chat/prompt/prompt.d.ts +14 -0
- package/lib/common/chat/prompt/prompt.d.ts.map +1 -0
- package/lib/common/chat/prompt/prompt.js +56 -0
- package/lib/common/chat/prompt/prompt.js.map +1 -0
- package/lib/common/index.d.ts +4 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +20 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/model/function/function-protocol.d.ts +47 -0
- package/lib/common/model/function/function-protocol.d.ts.map +1 -0
- package/lib/common/model/function/function-protocol.js +3 -0
- package/lib/common/model/function/function-protocol.js.map +1 -0
- package/lib/common/model/function/index.d.ts +2 -0
- package/lib/common/model/function/index.d.ts.map +1 -0
- package/lib/common/model/function/index.js +18 -0
- package/lib/common/model/function/index.js.map +1 -0
- package/lib/common/model/index.d.ts +2 -0
- package/lib/common/model/index.d.ts.map +1 -0
- package/lib/common/model/index.js +18 -0
- package/lib/common/model/index.js.map +1 -0
- package/lib/common/model/model-protocol.d.ts +160 -0
- package/lib/common/model/model-protocol.d.ts.map +1 -0
- package/lib/common/model/model-protocol.js +12 -0
- package/lib/common/model/model-protocol.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/utils/index.d.ts +2 -0
- package/lib/common/utils/index.d.ts.map +1 -0
- package/lib/common/utils/index.js +18 -0
- package/lib/common/utils/index.js.map +1 -0
- package/lib/common/utils/prompt-util.d.ts +5 -0
- package/lib/common/utils/prompt-util.d.ts.map +1 -0
- package/lib/common/utils/prompt-util.js +11 -0
- package/lib/common/utils/prompt-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 +43 -0
- package/src/common/chat/index.ts +4 -0
- package/src/common/chat/message/abstract-message.ts +13 -0
- package/src/common/chat/message/assistant-message.ts +12 -0
- package/src/common/chat/message/function-message.ts +12 -0
- package/src/common/chat/message/index.ts +6 -0
- package/src/common/chat/message/message-protocol.ts +49 -0
- package/src/common/chat/message/system-message.ts +12 -0
- package/src/common/chat/message/user-message.ts +12 -0
- package/src/common/chat/metadata/index.ts +1 -0
- package/src/common/chat/metadata/metadata-protocol.ts +154 -0
- package/src/common/chat/model/chat-protocol.ts +22 -0
- package/src/common/chat/model/index.ts +1 -0
- package/src/common/chat/prompt/index.ts +2 -0
- package/src/common/chat/prompt/prompt-protocol.ts +44 -0
- package/src/common/chat/prompt/prompt.ts +57 -0
- package/src/common/index.ts +3 -0
- package/src/common/model/function/function-protocol.ts +58 -0
- package/src/common/model/function/index.ts +1 -0
- package/src/common/model/index.ts +2 -0
- package/src/common/model/model-protocol.ts +184 -0
- package/src/common/module.ts +4 -0
- package/src/common/utils/index.ts +1 -0
- package/src/common/utils/prompt-util.ts +7 -0
- package/src/package.spec.ts +4 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Media, Message, MessageType } from './message-protocol';
|
|
2
|
+
|
|
3
|
+
export abstract class AbstractMessage implements Message {
|
|
4
|
+
static MESSAGE_TYPE = 'messageType';
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
readonly messageType: MessageType,
|
|
8
|
+
readonly content: string,
|
|
9
|
+
readonly media: Media[] = [],
|
|
10
|
+
readonly metadata: Record<string, any> = {}) {
|
|
11
|
+
this.metadata[AbstractMessage.MESSAGE_TYPE] = messageType;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractMessage } from './abstract-message';
|
|
2
|
+
import { Media, MessageType } from './message-protocol';
|
|
3
|
+
|
|
4
|
+
export class AssistantMessage extends AbstractMessage {
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
override readonly content: string,
|
|
8
|
+
override readonly media: Media[] = [],
|
|
9
|
+
override readonly metadata: Record<string, any> = {}) {
|
|
10
|
+
super(MessageType.ASSISTANT, content, media, metadata);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractMessage } from './abstract-message';
|
|
2
|
+
import { Media, MessageType } from './message-protocol';
|
|
3
|
+
|
|
4
|
+
export class FunctionMessage extends AbstractMessage {
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
override readonly content: string,
|
|
8
|
+
override readonly media: Media[] = [],
|
|
9
|
+
override readonly metadata: Record<string, any> = {}) {
|
|
10
|
+
super(MessageType.FUNCTION, content, media, metadata);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { MediaType } from '@celljs/http';
|
|
2
|
+
import { Content } from '../../model/model-protocol';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The Media class represents the data and metadata of a media attachment in a message. It
|
|
6
|
+
* consists of a MIME type and the raw data.
|
|
7
|
+
*
|
|
8
|
+
* This class is used as a parameter in the constructor of the UserMessage class.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export interface Media {
|
|
12
|
+
|
|
13
|
+
readonly mediaType: MediaType;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get the media data object
|
|
17
|
+
*/
|
|
18
|
+
readonly data: any;
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The MessageType enum represents the type of a message in a chat application. It can be
|
|
24
|
+
* one of the following: USER, ASSISTANT, SYSTEM, FUNCTION.
|
|
25
|
+
*/
|
|
26
|
+
export enum MessageType {
|
|
27
|
+
USER = 'user',
|
|
28
|
+
ASSISTANT = 'assistant',
|
|
29
|
+
SYSTEM = 'system',
|
|
30
|
+
FUNCTION = 'function'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The Message interface represents a message that can be sent or received in a chat
|
|
35
|
+
* application. Messages can have content, media attachments, properties, and message
|
|
36
|
+
* types.
|
|
37
|
+
*
|
|
38
|
+
* @see Media
|
|
39
|
+
* @see MessageType
|
|
40
|
+
*/
|
|
41
|
+
export interface Message extends Content {
|
|
42
|
+
readonly messageType: MessageType;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export namespace Message {
|
|
46
|
+
export function isMessage(obj: any): obj is Message {
|
|
47
|
+
return obj && obj.messageType !== undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractMessage } from './abstract-message';
|
|
2
|
+
import { Media, MessageType } from './message-protocol';
|
|
3
|
+
|
|
4
|
+
export class SystemMessage extends AbstractMessage {
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
override readonly content: string,
|
|
8
|
+
override readonly media: Media[] = [],
|
|
9
|
+
override readonly metadata: Record<string, any> = {}) {
|
|
10
|
+
super(MessageType.SYSTEM, content, media, metadata);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractMessage } from './abstract-message';
|
|
2
|
+
import { Media, MessageType } from './message-protocol';
|
|
3
|
+
|
|
4
|
+
export class UserMessage extends AbstractMessage {
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
override readonly content: string,
|
|
8
|
+
override readonly media: Media[] = [],
|
|
9
|
+
override readonly metadata: Record<string, any> = {}) {
|
|
10
|
+
super(MessageType.USER, content, media, metadata);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './metadata-protocol';
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { IllegalArgumentError } from '@celljs/core';
|
|
2
|
+
import { ResponseMetadata } from '../../model/model-protocol';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Abstract Data Type (ADT) encapsulating metadata on the usage of an AI provider's API
|
|
6
|
+
* per AI request.
|
|
7
|
+
*/
|
|
8
|
+
export interface Usage {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The number of tokens used in the prompt of the AI request.
|
|
12
|
+
*/
|
|
13
|
+
readonly promptTokens: number;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The number of tokens returned in the generation (aka completion)
|
|
17
|
+
* of the AI's response.
|
|
18
|
+
*/
|
|
19
|
+
readonly generationTokens: number;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The total number of tokens from both the prompt of an AI request
|
|
23
|
+
* and generation of the AI's response.
|
|
24
|
+
*/
|
|
25
|
+
readonly totalTokens: number;
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Abstract Data Type (ADT) encapsulating metadata from an AI provider's API rate limits
|
|
31
|
+
* granted to the API key in use and the API key's current balance.
|
|
32
|
+
*/
|
|
33
|
+
export interface RateLimit {
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The maximum number of requests that are permitted before exhausting the
|
|
37
|
+
* rate limit.
|
|
38
|
+
*/
|
|
39
|
+
readonly requestsLimit: number;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The remaining number of requests that are permitted before exhausting the
|
|
43
|
+
* requestsLimit rate limit.
|
|
44
|
+
*/
|
|
45
|
+
readonly requestsRemaining: number;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The duration time until the rate limit (based on requests) resets
|
|
49
|
+
* to its requestsLimit initial state.
|
|
50
|
+
*/
|
|
51
|
+
readonly requestsReset: number;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The maximum number of tokens that are permitted before exhausting the rate
|
|
55
|
+
* limit.
|
|
56
|
+
*/
|
|
57
|
+
readonly tokensLimit: number;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The remaining number of tokens that are permitted before exhausting the
|
|
61
|
+
* tokensLimit rate limit.
|
|
62
|
+
*/
|
|
63
|
+
readonly tokensRemaining: number;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The duration time until the rate limit (based on tokens) resets to
|
|
67
|
+
* its tokensLimit initial state.
|
|
68
|
+
*/
|
|
69
|
+
readonly tokensReset: number;
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Abstract Data Type (ADT) modeling filter metadata for all prompts sent during an AI
|
|
75
|
+
* request.
|
|
76
|
+
*/
|
|
77
|
+
export interface PromptFilterMetadata<T = any> {
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Index of the prompt filter metadata contained in the AI response.
|
|
81
|
+
*/
|
|
82
|
+
readonly promptIndex: number;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Returns the underlying AI provider metadata for filtering applied to prompt
|
|
86
|
+
* content.
|
|
87
|
+
* @param <T> {@link Class Type} used to cast the filtered content metadata into
|
|
88
|
+
* the AI provider-specific type.
|
|
89
|
+
* @return the underlying AI provider metadata for filtering applied to prompt
|
|
90
|
+
* content.
|
|
91
|
+
*/
|
|
92
|
+
readonly contentFilterMetadata: T;
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Abstract Data Type (ADT) modeling metadata gathered by the AI during request
|
|
98
|
+
* processing.
|
|
99
|
+
*/
|
|
100
|
+
export type PromptMetadata = PromptFilterMetadata[];
|
|
101
|
+
|
|
102
|
+
export namespace PromptMetadata {
|
|
103
|
+
/**
|
|
104
|
+
* Returns an Optional {@link PromptFilterMetadata} at the given index.
|
|
105
|
+
* @param promptIndex index of the {@link PromptFilterMetadata} contained in this
|
|
106
|
+
* {@link PromptMetadata}.
|
|
107
|
+
* @return Optional {@link PromptFilterMetadata} at the given index.
|
|
108
|
+
* @throws IllegalArgumentError if the prompt index is less than 0.
|
|
109
|
+
*/
|
|
110
|
+
export function findByPromptIndex(promptMetadata: PromptMetadata, promptIndex: number): PromptFilterMetadata | undefined {
|
|
111
|
+
|
|
112
|
+
if (promptIndex < 0) {
|
|
113
|
+
throw new IllegalArgumentError(`Prompt index [${promptIndex}] must be greater than equal to 0`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return promptMetadata.find(promptFilterMetadata => promptFilterMetadata.promptIndex === promptIndex);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface ChatResponseMetadata extends ResponseMetadata {
|
|
121
|
+
/**
|
|
122
|
+
* AI provider specific metadata on rate limits.
|
|
123
|
+
* @see RateLimit
|
|
124
|
+
*/
|
|
125
|
+
readonly rateLimit: RateLimit;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* AI provider specific metadata on API usage.
|
|
129
|
+
* @see Usage
|
|
130
|
+
*/
|
|
131
|
+
readonly usage: Usage;
|
|
132
|
+
|
|
133
|
+
readonly promptMetadata: PromptMetadata;
|
|
134
|
+
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export namespace ChatResponseMetadata {
|
|
138
|
+
export const EMPTY = {
|
|
139
|
+
rateLimit: {
|
|
140
|
+
requestsLimit: 0,
|
|
141
|
+
requestsRemaining: 0,
|
|
142
|
+
requestsReset: 0,
|
|
143
|
+
tokensLimit: 0,
|
|
144
|
+
tokensRemaining: 0,
|
|
145
|
+
tokensReset: 0
|
|
146
|
+
},
|
|
147
|
+
usage: {
|
|
148
|
+
promptTokens: 0,
|
|
149
|
+
generationTokens: 0,
|
|
150
|
+
totalTokens: 0
|
|
151
|
+
},
|
|
152
|
+
promptMetadata: [] as PromptMetadata
|
|
153
|
+
} as ChatResponseMetadata;
|
|
154
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Model, ModelResponse, ModelResult, StreamingModel } from '../../model/model-protocol';
|
|
2
|
+
import { AssistantMessage } from '../message';
|
|
3
|
+
import { Prompt } from '../prompt/prompt-protocol';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Represents a response returned by the AI.
|
|
7
|
+
*/
|
|
8
|
+
export interface Generation extends ModelResult<AssistantMessage> {
|
|
9
|
+
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ChatResponse extends ModelResponse<Generation> {
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface StreamingChatModel extends StreamingModel<Prompt, ChatResponse> {
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ChatModel extends Model<Prompt, ChatResponse>, StreamingChatModel {
|
|
21
|
+
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './chat-protocol';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ModelOptions, ModelRequest } from '../../model/model-protocol';
|
|
2
|
+
import { Media, Message } from '../message';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The ChatOptions represent the common options, portable across different chat models.
|
|
6
|
+
*/
|
|
7
|
+
export interface ChatOptions extends ModelOptions {
|
|
8
|
+
|
|
9
|
+
readonly temperature?: number;
|
|
10
|
+
readonly topP?: number;
|
|
11
|
+
readonly topK?: number;
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Prompt extends ModelRequest<Message[]> {
|
|
16
|
+
readonly contents: string;
|
|
17
|
+
copy(): Prompt;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PromptTemplateStringActions {
|
|
21
|
+
|
|
22
|
+
render(model?: Map<String, Object>): string;
|
|
23
|
+
|
|
24
|
+
}
|
|
25
|
+
|
|
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
|
+
|
|
44
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { IllegalArgumentError } from '@celljs/core';
|
|
2
|
+
import { ModelOptions } from '../../model/model-protocol';
|
|
3
|
+
import { Message, MessageType, UserMessage } from '../message';
|
|
4
|
+
import { ChatOptions, Prompt } from './prompt-protocol';
|
|
5
|
+
|
|
6
|
+
export class PromptImpl implements Prompt {
|
|
7
|
+
|
|
8
|
+
protected messages: Message[];
|
|
9
|
+
|
|
10
|
+
protected modelOptions: ChatOptions;
|
|
11
|
+
constructor(messages: Message[] | Message | string, modelOptions: ChatOptions = {}) {
|
|
12
|
+
this.modelOptions = modelOptions;
|
|
13
|
+
if (Array.isArray(messages)) {
|
|
14
|
+
this.messages = messages;
|
|
15
|
+
} else if (typeof messages === 'string') {
|
|
16
|
+
this.messages = [new UserMessage(messages)];
|
|
17
|
+
} else {
|
|
18
|
+
this.messages = [messages];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private instructionsCopy(): Message[] {
|
|
23
|
+
return this.messages.map(message => {
|
|
24
|
+
if (message.messageType === MessageType.USER) {
|
|
25
|
+
return new UserMessage(message.content);
|
|
26
|
+
} else if (message.messageType === MessageType.ASSISTANT) {
|
|
27
|
+
return new UserMessage(message.content);
|
|
28
|
+
} else if (message.messageType === MessageType.SYSTEM) {
|
|
29
|
+
return new UserMessage(message.content);
|
|
30
|
+
} else if (message.messageType === MessageType.FUNCTION) {
|
|
31
|
+
return new UserMessage(message.content);
|
|
32
|
+
} else {
|
|
33
|
+
throw new IllegalArgumentError(`Unsupported message type: ${message.messageType}`);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
copy(): Prompt {
|
|
38
|
+
return new PromptImpl(this.instructionsCopy(), this.modelOptions);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get contents(): string {
|
|
42
|
+
let contents = '';
|
|
43
|
+
for (const message of this.messages) {
|
|
44
|
+
contents += message.content;
|
|
45
|
+
}
|
|
46
|
+
return contents;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get instructions(): Message[] {
|
|
50
|
+
return this.messages;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get options(): ModelOptions {
|
|
54
|
+
return this.modelOptions;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ChatOptions } from '../../chat/prompt';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a model function call handler. Implementations are registered with the
|
|
5
|
+
* Models and called on prompts that trigger the function call.
|
|
6
|
+
*/
|
|
7
|
+
export interface FunctionCallback {
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Function name. Unique within the model.
|
|
11
|
+
*/
|
|
12
|
+
readonly name: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The function description. This description is used by the model do
|
|
16
|
+
* decide if the function should be called or not.
|
|
17
|
+
*/
|
|
18
|
+
readonly description: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The JSON schema of the function input type.
|
|
22
|
+
*/
|
|
23
|
+
readonly inputTypeSchema: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Called when a model detects and triggers a function call. The model is responsible
|
|
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
|
|
29
|
+
* function. The arguments are defined as JSON schema usually registered with the the
|
|
30
|
+
* model.
|
|
31
|
+
* @return String containing the function call response.
|
|
32
|
+
*/
|
|
33
|
+
call(functionInput: string): Promise<string>;
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FunctionCallingOptions {
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Function Callbacks to be registered with the ChatClient. For Prompt Options the
|
|
41
|
+
* functionCallbacks are automatically enabled for the duration of the prompt
|
|
42
|
+
* execution. For Default Options the FunctionCallbacks are registered but disabled by
|
|
43
|
+
* default. You have to use "functions" property to list the function names from the
|
|
44
|
+
* ChatClient registry to be used in the chat completion requests.
|
|
45
|
+
*/
|
|
46
|
+
functionCallbacks: FunctionCallback[];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* List of function names from the ChatClient registry to be used in the next
|
|
50
|
+
* chat completion requests.
|
|
51
|
+
*/
|
|
52
|
+
functions: Set<String>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface PortableFunctionCallingOptions extends FunctionCallingOptions, ChatOptions {
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './function-protocol';
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a set of interfaces and classes for a generic API designed to interact with
|
|
3
|
+
* various AI models. This package includes interfaces for handling AI model calls,
|
|
4
|
+
* requests, responses, results, and associated metadata. It is designed to offer a
|
|
5
|
+
* flexible and adaptable framework for interacting with different types of AI models,
|
|
6
|
+
* abstracting the complexities involved in model invocation and result processing. The
|
|
7
|
+
* use of generics enhances the API's capability to work with a wide range of models,
|
|
8
|
+
* ensuring a broad applicability across diverse AI scenarios.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { Media } from '../chat';
|
|
12
|
+
import { Observable } from 'rxjs';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Interface representing the customizable options for AI model interactions. This marker
|
|
16
|
+
* interface allows for the specification of various settings and parameters that can
|
|
17
|
+
* influence the behavior and output of AI models. It is designed to provide flexibility
|
|
18
|
+
* and adaptability in different AI scenarios, ensuring that the AI models can be
|
|
19
|
+
* fine-tuned according to specific requirements.
|
|
20
|
+
*/
|
|
21
|
+
export interface ModelOptions {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Interface representing metadata associated with the results of an AI model. This
|
|
25
|
+
* interface focuses on providing additional context and insights into the results
|
|
26
|
+
* generated by AI models. It could include information like computation time, model
|
|
27
|
+
* version, or other relevant details that enhance understanding and management of AI
|
|
28
|
+
* model outputs in various applications.
|
|
29
|
+
*/
|
|
30
|
+
export interface ResultMetadata {}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Interface representing metadata associated with an AI model's response. This interface
|
|
34
|
+
* is designed to provide additional information about the generative response from an AI
|
|
35
|
+
* model, including processing details and model-specific data. It serves as a value
|
|
36
|
+
* object within the core domain, enhancing the understanding and management of AI model
|
|
37
|
+
* responses in various applications.
|
|
38
|
+
*/
|
|
39
|
+
export interface ResponseMetadata {}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* This interface provides methods to access the main output of the AI model and the
|
|
43
|
+
* metadata associated with this result. It is designed to offer a standardized and
|
|
44
|
+
* comprehensive way to handle and interpret the outputs generated by AI models, catering
|
|
45
|
+
* to diverse AI applications and use cases.
|
|
46
|
+
*
|
|
47
|
+
* @param <T> the type of the output generated by the AI model
|
|
48
|
+
*/
|
|
49
|
+
export interface ModelResult<T> {
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Retrieves the output generated by the AI model.
|
|
53
|
+
* The output generated by the AI model
|
|
54
|
+
*/
|
|
55
|
+
readonly output: T;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Retrieves the metadata associated with the result of an AI model.
|
|
59
|
+
* The metadata associated with the result
|
|
60
|
+
*/
|
|
61
|
+
readonly metadata: ResultMetadata;
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Interface representing a request to an AI model. This interface encapsulates the
|
|
67
|
+
* necessary information required to interact with an AI model, including instructions or
|
|
68
|
+
* inputs (of generic type T) and additional model options. It provides a standardized way
|
|
69
|
+
* to send requests to AI models, ensuring that all necessary details are included and can
|
|
70
|
+
* be easily managed.
|
|
71
|
+
*
|
|
72
|
+
* @param <T> the type of instructions or input required by the AI model
|
|
73
|
+
*/
|
|
74
|
+
export interface ModelRequest<T> {
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Retrieves the instructions or input required by the AI model.
|
|
78
|
+
* The instructions or input required by the AI model
|
|
79
|
+
*/
|
|
80
|
+
readonly instructions: T; // required input
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Retrieves the customizable options for AI model interactions.
|
|
84
|
+
* The customizable options for AI model interactions
|
|
85
|
+
*/
|
|
86
|
+
readonly options: ModelOptions;
|
|
87
|
+
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Interface representing the response received from an AI model. This interface provides
|
|
92
|
+
* methods to access the main result or a list of results generated by the AI model, along
|
|
93
|
+
* with the response metadata. It serves as a standardized way to encapsulate and manage
|
|
94
|
+
* the output from AI models, ensuring easy retrieval and processing of the generated
|
|
95
|
+
* information.
|
|
96
|
+
*
|
|
97
|
+
* @param <T> the type of the result(s) provided by the AI model
|
|
98
|
+
*/
|
|
99
|
+
export interface ModelResponse<T extends ModelResult<any>> {
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Retrieves the result of the AI model.
|
|
103
|
+
* The result generated by the AI model
|
|
104
|
+
*/
|
|
105
|
+
readonly result: T;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Retrieves the list of generated outputs by the AI model.
|
|
109
|
+
* The list of generated outputs
|
|
110
|
+
*/
|
|
111
|
+
readonly results: T[];
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Retrieves the response metadata associated with the AI model's response.
|
|
115
|
+
* The response metadata
|
|
116
|
+
*/
|
|
117
|
+
readonly metadata: ResponseMetadata;
|
|
118
|
+
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The Model interface provides a generic API for invoking AI models. It is designed
|
|
123
|
+
* to handle the interaction with various types of AI models by abstracting the process of
|
|
124
|
+
* sending requests and receiving responses. The interface uses Java generics to
|
|
125
|
+
* accommodate different types of requests and responses, enhancing flexibility and
|
|
126
|
+
* adaptability across different AI model implementations.
|
|
127
|
+
*
|
|
128
|
+
* @param <TReq> the generic type of the request to the AI model
|
|
129
|
+
* @param <TRes> the generic type of the response from the AI model
|
|
130
|
+
*/
|
|
131
|
+
export interface Model<TReq extends ModelRequest<any>, TRes extends ModelResponse<any>> {
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Executes a method call to the AI model.
|
|
135
|
+
* @param request the request object to be sent to the AI model
|
|
136
|
+
* @return the response from the AI model
|
|
137
|
+
*/
|
|
138
|
+
call(request: TReq): Promise<TRes>;
|
|
139
|
+
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Data structure that contains content and metadata.
|
|
144
|
+
*/
|
|
145
|
+
export interface Content {
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Get the content of the message.
|
|
149
|
+
*/
|
|
150
|
+
readonly content: string; // TODO consider getText
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Get the media associated with the content.
|
|
154
|
+
*/
|
|
155
|
+
readonly media: Media[];
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* return Get the metadata associated with the content.
|
|
159
|
+
*/
|
|
160
|
+
readonly metadata: Record<string, any>;
|
|
161
|
+
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The StreamingModel interface provides a generic API for invoking an AI models
|
|
166
|
+
* with streaming response. It abstracts the process of sending requests and receiving a
|
|
167
|
+
* streaming responses. The interface uses Java generics to accommodate different types of
|
|
168
|
+
* requests and responses, enhancing flexibility and adaptability across different AI
|
|
169
|
+
* model implementations.
|
|
170
|
+
*
|
|
171
|
+
* @param <TReq> the generic type of the request to the AI model
|
|
172
|
+
* @param <TResChunk> the generic type of a single item in the streaming response from the
|
|
173
|
+
* AI model
|
|
174
|
+
*/
|
|
175
|
+
export interface StreamingModel<TReq extends ModelRequest<any>, TResChunk extends ModelResponse<any>> {
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Executes a method call to the AI model.
|
|
179
|
+
* @param request the request object to be sent to the AI model
|
|
180
|
+
* @return the streaming response from the AI model
|
|
181
|
+
*/
|
|
182
|
+
stream(request: TReq): Observable<TResChunk>;
|
|
183
|
+
|
|
184
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './prompt-util';
|