Skip to content

Commit

Permalink
[9.0] [Obs AI Assistant] Remove TokenCountEvent (#209549) (#211947)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `9.0`:
- [[Obs AI Assistant] Remove TokenCountEvent
(#209549)](#209549)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Søren
Louv-Jansen","email":"soren.louv@elastic.co"},"sourceCommit":{"committedDate":"2025-02-20T17:59:19Z","message":"[Obs
AI Assistant] Remove TokenCountEvent (#209549)\n\nCloses
https://github.com/elastic/kibana/issues/205479\n\nThis filters out the
`ChatCompletionTokenCountEvent` from the inference\nplugin. This greatly
simplifies handling ChatCompletion events in the\nObs AI
Assistant.","sha":"c4826bdfbfb88a31b455d2647e0268f8ffb4299f","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:prev-minor","Team:Obs
AI Assistant","ci:project-deploy-observability","v9.1.0"],"title":"[Obs
AI Assistant] Remove
TokenCountEvent","number":209549,"url":"https://github.com/elastic/kibana/pull/209549","mergeCommit":{"message":"[Obs
AI Assistant] Remove TokenCountEvent (#209549)\n\nCloses
https://github.com/elastic/kibana/issues/205479\n\nThis filters out the
`ChatCompletionTokenCountEvent` from the inference\nplugin. This greatly
simplifies handling ChatCompletion events in the\nObs AI
Assistant.","sha":"c4826bdfbfb88a31b455d2647e0268f8ffb4299f"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/209549","number":209549,"mergeCommit":{"message":"[Obs
AI Assistant] Remove TokenCountEvent (#209549)\n\nCloses
https://github.com/elastic/kibana/issues/205479\n\nThis filters out the
`ChatCompletionTokenCountEvent` from the inference\nplugin. This greatly
simplifies handling ChatCompletion events in the\nObs AI
Assistant.","sha":"c4826bdfbfb88a31b455d2647e0268f8ffb4299f"}}]}]
BACKPORT-->

Co-authored-by: Søren Louv-Jansen <soren.louv@elastic.co>
  • Loading branch information
kibanamachine and sorenlouv authored Feb 20, 2025
1 parent dbe2e4c commit d89c42c
Show file tree
Hide file tree
Showing 26 changed files with 100 additions and 545 deletions.
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

0 comments on commit d89c42c

Please sign in to comment.