forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversation_complete.ts
214 lines (190 loc) · 5.82 KB
/
conversation_complete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { i18n } from '@kbn/i18n';
import { TokenCount as TokenCountType, type Message } from './types';
export enum StreamingChatResponseEventType {
ChatCompletionChunk = 'chatCompletionChunk',
ConversationCreate = 'conversationCreate',
ConversationUpdate = 'conversationUpdate',
MessageAdd = 'messageAdd',
ChatCompletionError = 'chatCompletionError',
BufferFlush = 'bufferFlush',
TokenCount = 'tokenCount',
}
type StreamingChatResponseEventBase<
TEventType extends StreamingChatResponseEventType,
TData extends {}
> = {
type: TEventType;
} & TData;
export type ChatCompletionChunkEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.ChatCompletionChunk,
{
id: string;
message: {
content?: string;
function_call?: {
name?: string;
arguments?: string;
};
};
}
>;
export type ConversationCreateEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.ConversationCreate,
{
conversation: {
id: string;
title: string;
last_updated: string;
token_count?: TokenCountType;
};
}
>;
export type ConversationUpdateEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.ConversationUpdate,
{
conversation: {
id: string;
title: string;
last_updated: string;
token_count?: TokenCountType;
};
}
>;
export type MessageAddEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.MessageAdd,
{ message: Message; id: string }
>;
export type ChatCompletionErrorEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.ChatCompletionError,
{
error: {
message: string;
stack?: string;
code?: ChatCompletionErrorCode;
meta?: Record<string, any>;
};
}
>;
export type BufferFlushEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.BufferFlush,
{
data?: string;
}
>;
export type TokenCountEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.TokenCount,
{
tokens: {
completion: number;
prompt: number;
total: number;
};
}
>;
export type StreamingChatResponseEvent =
| ChatCompletionChunkEvent
| ConversationCreateEvent
| ConversationUpdateEvent
| MessageAddEvent
| ChatCompletionErrorEvent
| TokenCountEvent
| BufferFlushEvent;
export type StreamingChatResponseEventWithoutError = Exclude<
StreamingChatResponseEvent,
ChatCompletionErrorEvent
>;
export type ChatEvent = ChatCompletionChunkEvent | TokenCountEvent;
export type MessageOrChatEvent = ChatEvent | MessageAddEvent;
export enum ChatCompletionErrorCode {
InternalError = 'internalError',
NotFoundError = 'notFoundError',
TokenLimitReachedError = 'tokenLimitReachedError',
FunctionNotFoundError = 'functionNotFoundError',
FunctionLimitExceededError = 'functionLimitExceededError',
}
interface ErrorMetaAttributes {
[ChatCompletionErrorCode.InternalError]: {};
[ChatCompletionErrorCode.NotFoundError]: {};
[ChatCompletionErrorCode.TokenLimitReachedError]: {
tokenLimit?: number;
tokenCount?: number;
};
[ChatCompletionErrorCode.FunctionNotFoundError]: {
name: string;
};
[ChatCompletionErrorCode.FunctionLimitExceededError]: {};
}
export class ChatCompletionError<T extends ChatCompletionErrorCode> extends Error {
constructor(
public code: T,
message: string,
public meta: ErrorMetaAttributes[T] = {} as ErrorMetaAttributes[T]
) {
super(message);
}
}
export function createTokenLimitReachedError(tokenLimit?: number, tokenCount?: number) {
return new ChatCompletionError(
ChatCompletionErrorCode.TokenLimitReachedError,
i18n.translate('xpack.observabilityAiAssistant.chatCompletionError.tokenLimitReachedError', {
defaultMessage: `Token limit reached. Token limit is {tokenLimit}, but the current conversation has {tokenCount} tokens.`,
values: { tokenLimit, tokenCount },
}),
{ tokenLimit, tokenCount }
);
}
export function createConversationNotFoundError() {
return new ChatCompletionError(
ChatCompletionErrorCode.NotFoundError,
i18n.translate('xpack.observabilityAiAssistant.chatCompletionError.conversationNotFoundError', {
defaultMessage: 'Conversation not found',
})
);
}
export function createInternalServerError(
originalErrorMessage: string = i18n.translate(
'xpack.observabilityAiAssistant.chatCompletionError.internalServerError',
{
defaultMessage: 'An internal server error occurred',
}
)
) {
return new ChatCompletionError(ChatCompletionErrorCode.InternalError, originalErrorMessage);
}
export function createFunctionNotFoundError(name: string) {
return new ChatCompletionError(
ChatCompletionErrorCode.FunctionNotFoundError,
`Function ${name} called but was not available`
);
}
export function createFunctionLimitExceededError() {
return new ChatCompletionError(
ChatCompletionErrorCode.FunctionLimitExceededError,
`Function limit exceeded`
);
}
export function isTokenLimitReachedError(
error: Error
): error is ChatCompletionError<ChatCompletionErrorCode.TokenLimitReachedError> {
return (
error instanceof ChatCompletionError &&
error.code === ChatCompletionErrorCode.TokenLimitReachedError
);
}
export function isFunctionNotFoundError(
error: Error
): error is ChatCompletionError<ChatCompletionErrorCode.FunctionNotFoundError> {
return (
error instanceof ChatCompletionError &&
error.code === ChatCompletionErrorCode.FunctionNotFoundError
);
}
export function isChatCompletionError(error: Error): error is ChatCompletionError<any> {
return error instanceof ChatCompletionError;
}