Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.0] [Obs AI Assistant] Remove TokenCountEvent (#209549) #211947

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { TokenCount as TokenCountType, type Message } from './types';
import { type Message } from './types';

export enum StreamingChatResponseEventType {
ChatCompletionChunk = 'chatCompletionChunk',
Expand All @@ -16,7 +16,6 @@ export enum StreamingChatResponseEventType {
MessageAdd = 'messageAdd',
ChatCompletionError = 'chatCompletionError',
BufferFlush = 'bufferFlush',
TokenCount = 'tokenCount',
}

type StreamingChatResponseEventBase<
Expand Down Expand Up @@ -54,7 +53,6 @@ export type ConversationCreateEvent = StreamingChatResponseEventBase<
id: string;
title: string;
last_updated: string;
token_count?: TokenCountType;
};
}
>;
Expand All @@ -66,7 +64,6 @@ export type ConversationUpdateEvent = StreamingChatResponseEventBase<
id: string;
title: string;
last_updated: string;
token_count?: TokenCountType;
};
}
>;
Expand Down Expand Up @@ -95,33 +92,21 @@ export type BufferFlushEvent = StreamingChatResponseEventBase<
}
>;

export type TokenCountEvent = StreamingChatResponseEventBase<
StreamingChatResponseEventType.TokenCount,
{
tokens: {
completion: number;
prompt: number;
total: number;
};
}
>;

export type StreamingChatResponseEvent =
| ChatCompletionChunkEvent
| ChatCompletionMessageEvent
| ConversationCreateEvent
| ConversationUpdateEvent
| MessageAddEvent
| ChatCompletionErrorEvent
| TokenCountEvent
| BufferFlushEvent;

export type StreamingChatResponseEventWithoutError = Exclude<
StreamingChatResponseEvent,
ChatCompletionErrorEvent
>;

export type ChatEvent = ChatCompletionChunkEvent | TokenCountEvent | ChatCompletionMessageEvent;
export type ChatEvent = ChatCompletionChunkEvent | ChatCompletionMessageEvent;
export type MessageOrChatEvent = ChatEvent | MessageAddEvent;

export enum ChatCompletionErrorCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export {
export type {
ChatCompletionChunkEvent,
ChatCompletionMessageEvent,
TokenCountEvent,
ConversationCreateEvent,
ConversationUpdateEvent,
MessageAddEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ export interface Message {
};
}

export interface TokenCount {
prompt: number;
completion: number;
total: number;
}

export interface Conversation {
'@timestamp': string;
user?: {
Expand All @@ -61,7 +55,6 @@ export interface Conversation {
id: string;
title: string;
last_updated: string;
token_count?: TokenCount;
};
systemMessage?: string;
messages: Message[];
Expand All @@ -71,8 +64,8 @@ export interface Conversation {
public: boolean;
}

export type ConversationRequestBase = Omit<Conversation, 'user' | 'conversation' | 'namespace'> & {
conversation: { title: string; token_count?: TokenCount; id?: string };
type ConversationRequestBase = Omit<Conversation, 'user' | 'conversation' | 'namespace'> & {
conversation: { title: string; id?: string };
};

export type ConversationCreateRequest = ConversationRequestBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
withLatestFrom,
filter,
} from 'rxjs';
import { withoutTokenCountEvents } from './without_token_count_events';
import {
type ChatCompletionChunkEvent,
ChatEvent,
Expand Down Expand Up @@ -69,15 +68,12 @@ export function emitWithConcatenatedMessage<T extends ChatEvent>(
return (source$) => {
const shared = source$.pipe(shareReplay());

const withoutTokenCount$ = shared.pipe(filterChunkEvents());

const response$ = concat(
shared,
shared.pipe(
withoutTokenCountEvents(),
concatenateChatCompletionChunks(),
last(),
withLatestFrom(withoutTokenCount$),
withLatestFrom(shared.pipe(filterChunkEvents())),
mergeMap(([message, chunkEvent]) => {
return mergeWithEditedMessage(message, chunkEvent, callback);
})
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,6 @@ export const chatFeedbackEventSchema: EventTypeOpts<ChatFeedback> = {
description: 'The timestamp of the last message in the conversation.',
},
},
token_count: {
properties: {
completion: {
type: 'long',
_meta: {
description: 'The number of tokens in the completion.',
},
},
prompt: {
type: 'long',
_meta: {
description: 'The number of tokens in the prompt.',
},
},
total: {
type: 'long',
_meta: {
description: 'The total number of tokens in the conversation.',
},
},
},
},
},
},
labels: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { Readable } from 'stream';
import { AssistantScope } from '@kbn/ai-assistant-common';
import { aiAssistantSimulatedFunctionCalling } from '../..';
import { createFunctionResponseMessage } from '../../../common/utils/create_function_response_message';
import { withoutTokenCountEvents } from '../../../common/utils/without_token_count_events';
import { LangTracer } from '../../service/client/instrumentation/lang_tracer';
import { flushBuffer } from '../../service/util/flush_buffer';
import { observableIntoOpenAIStream } from '../../service/util/observable_into_openai_stream';
Expand Down Expand Up @@ -203,16 +202,14 @@ const chatRecallRoute = createObservabilityAIAssistantServerRoute({
recallAndScore({
analytics: (await resources.plugins.core.start()).analytics,
chat: (name, params) =>
client
.chat(name, {
...params,
stream: true,
connectorId,
simulateFunctionCalling,
signal,
tracer: new LangTracer(otelContext.active()),
})
.pipe(withoutTokenCountEvents()),
client.chat(name, {
...params,
stream: true,
connectorId,
simulateFunctionCalling,
signal,
tracer: new LangTracer(otelContext.active()),
}),
context,
logger: resources.logger,
messages: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import * as t from 'io-ts';
import { toBooleanRt } from '@kbn/io-ts-utils';
import {
type Conversation,
type ConversationCreateRequest,
type ConversationRequestBase,
type ConversationUpdateRequest,
type Message,
MessageRole,
Expand Down Expand Up @@ -57,17 +55,12 @@ const tokenCountRt = t.type({
total: t.number,
});

export const baseConversationRt: t.Type<ConversationRequestBase> = t.intersection([
export const conversationCreateRt: t.Type<ConversationCreateRequest> = t.intersection([
t.type({
'@timestamp': t.string,
conversation: t.intersection([
t.type({
title: t.string,
}),
t.partial({
token_count: tokenCountRt,
}),
]),
conversation: t.type({
title: t.string,
}),
messages: t.array(messageRt),
labels: t.record(t.string, t.string),
numeric_labels: t.record(t.string, t.number),
Expand All @@ -84,51 +77,21 @@ export const assistantScopeType = t.union([
t.literal('all'),
]);

export const conversationCreateRt: t.Type<ConversationCreateRequest> = t.intersection([
baseConversationRt,
t.type({
conversation: t.type({
title: t.string,
}),
}),
]);

export const conversationUpdateRt: t.Type<ConversationUpdateRequest> = t.intersection([
baseConversationRt,
conversationCreateRt,
t.type({
conversation: t.intersection([
t.type({
id: t.string,
title: t.string,
}),
t.partial({
token_count: tokenCountRt,
token_count: tokenCountRt, // deprecated, but kept for backwards compatibility
}),
]),
}),
]);

export const conversationRt: t.Type<Conversation> = t.intersection([
baseConversationRt,
t.intersection([
t.type({
namespace: t.string,
conversation: t.intersection([
t.type({
id: t.string,
last_updated: t.string,
}),
t.partial({
token_count: tokenCountRt,
}),
]),
}),
t.partial({
user: t.intersection([t.type({ name: t.string }), t.partial({ id: t.string })]),
}),
]),
]);

export const functionRt = t.intersection([
t.type({
name: t.string,
Expand Down
Loading