Skip to content

Commit

Permalink
add support for telemetry metadata to inference APIs / nlToEsql
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Feb 3, 2025
1 parent a263f35 commit da21c9f
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export {
isToolValidationError,
isTokenLimitReachedError,
isToolNotFoundError,
type ChatCompleteMetadata,
type ConnectorTelemetryMetadata,
} from './src/chat_complete';
export {
OutputEventType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Observable } from 'rxjs';
import type { ToolCallsOf, ToolOptions } from './tools';
import type { Message } from './messages';
import type { ChatCompletionEvent, ChatCompletionTokenCount } from './events';
import type { ChatCompleteMetadata } from './metadata';

/**
* Request a completion from the LLM based on a prompt or conversation.
Expand Down Expand Up @@ -109,6 +110,10 @@ export type ChatCompleteOptions<
* Optional signal that can be used to forcefully abort the request.
*/
abortSignal?: AbortSignal;
/**
* Optional metadata related to call execution.
*/
metadata?: ChatCompleteMetadata;
} & TToolOptions;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export {
type UnvalidatedToolCall,
type ToolChoice,
} from './tools';
export type { ChatCompleteMetadata, ConnectorTelemetryMetadata } from './metadata';
export {
isChatCompletionChunkEvent,
isChatCompletionEvent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

/**
* Set of metadata that can be used then calling the inference APIs
*
* @public
*/
export interface ChatCompleteMetadata {
connectorTelemetry?: ConnectorTelemetryMetadata;
}

/**
* Pass through for the connector telemetry
*/
export interface ConnectorTelemetryMetadata {
pluginId?: string;
aggregateBy?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/

import type { Observable } from 'rxjs';
import { Message, FunctionCallingMode, FromToolSchema, ToolSchema } from '../chat_complete';
import {
Message,
FunctionCallingMode,
FromToolSchema,
ToolSchema,
ChatCompleteMetadata,
} from '../chat_complete';
import { Output, OutputEvent } from './events';

/**
Expand Down Expand Up @@ -117,6 +123,10 @@ export interface OutputOptions<
*/
onValidationError?: boolean | number;
};
/**
* Optional metadata related to call execution.
*/
metadata?: ChatCompleteMetadata;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function createOutputApi(chatCompleteApi: ChatCompleteAPI) {
functionCalling,
stream,
abortSignal,
metadata,
retry,
}: DefaultOutputOptions): OutputCompositeResponse<string, ToolSchema | undefined, boolean> {
if (stream && retry !== undefined) {
Expand All @@ -56,6 +57,7 @@ export function createOutputApi(chatCompleteApi: ChatCompleteAPI) {
modelName,
functionCalling,
abortSignal,
metadata,
system,
messages,
...(schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const bedrockClaudeAdapter: InferenceConnectorAdapter = {
temperature = 0,
modelName,
abortSignal,
telemetryMetadata,
metadata,
}) => {
const noToolUsage = toolChoice === ToolChoiceType.none;

Expand All @@ -45,10 +45,10 @@ export const bedrockClaudeAdapter: InferenceConnectorAdapter = {
tools: noToolUsage ? [] : toolsToBedrock(tools, messages),
toolChoice: toolChoiceToBedrock(toolChoice),
temperature,
telemetryMetadata,
model: modelName,
stopSequences: ['\n\nHuman:'],
signal: abortSignal,
...(metadata?.connectorTelemetry ? { telemetryMetadata: metadata.connectorTelemetry } : {}),
};

return from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const geminiAdapter: InferenceConnectorAdapter = {
temperature = 0,
modelName,
abortSignal,
telemetryMetadata,
metadata,
}) => {
return from(
executor.invoke({
Expand All @@ -47,7 +47,9 @@ export const geminiAdapter: InferenceConnectorAdapter = {
model: modelName,
signal: abortSignal,
stopSequences: ['\n\nHuman:'],
telemetryMetadata,
...(metadata?.connectorTelemetry
? { telemetryMetadata: metadata.connectorTelemetry }
: {}),
},
})
).pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const inferenceAdapter: InferenceConnectorAdapter = {
modelName,
logger,
abortSignal,
telemetryMetadata,
metadata,
}) => {
const useSimulatedFunctionCalling =
functionCalling === 'auto'
Expand All @@ -51,7 +51,9 @@ export const inferenceAdapter: InferenceConnectorAdapter = {
subActionParams: {
body: request,
signal: abortSignal,
telemetryMetadata,
...(metadata?.connectorTelemetry
? { telemetryMetadata: metadata.connectorTelemetry }
: {}),
},
})
).pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const openAIAdapter: InferenceConnectorAdapter = {
modelName,
logger,
abortSignal,
telemetryMetadata,
metadata,
}) => {
const useSimulatedFunctionCalling =
functionCalling === 'auto'
Expand Down Expand Up @@ -71,7 +71,9 @@ export const openAIAdapter: InferenceConnectorAdapter = {
body: JSON.stringify(request),
signal: abortSignal,
stream: true,
telemetryMetadata,
...(metadata?.connectorTelemetry
? { telemetryMetadata: metadata.connectorTelemetry }
: {}),
},
})
).pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function createChatCompleteApi({ request, actions, logger }: CreateChatCo
modelName,
stream,
abortSignal,
metadata,
}: ChatCompleteOptions<ToolOptions, boolean>): ChatCompleteCompositeResponse<
ToolOptions,
boolean
Expand Down Expand Up @@ -87,6 +88,7 @@ export function createChatCompleteApi({ request, actions, logger }: CreateChatCo
functionCalling,
modelName,
abortSignal,
metadata,
});
}),
chunksIntoMessage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* 2.0.
*/

import type { TelemetryMetadata } from '@kbn/actions-plugin/server/lib';
import type {
ChatCompletionChunkEvent,
ChatCompletionTokenCountEvent,
FunctionCallingMode,
Message,
ToolOptions,
ChatCompleteMetadata,
} from '@kbn/inference-common';
import type { Logger } from '@kbn/logging';
import type { Observable } from 'rxjs';
Expand Down Expand Up @@ -43,7 +43,7 @@ export type InferenceAdapterChatCompleteOptions = {
temperature?: number;
modelName?: string;
abortSignal?: AbortSignal;
telemetryMetadata?: TelemetryMetadata;
metadata?: ChatCompleteMetadata;
} & ToolOptions;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
OutputCompleteEvent,
OutputEventType,
FunctionCallingMode,
ChatCompleteMetadata,
} from '@kbn/inference-common';
import { correctCommonEsqlMistakes, generateFakeToolCallId } from '../../../../common';
import { InferenceClient } from '../../..';
Expand All @@ -35,6 +36,7 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({
functionCalling,
logger,
system,
metadata,
}: {
connectorId: string;
systemMessage: string;
Expand All @@ -44,6 +46,7 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({
docBase: EsqlDocumentBase;
functionCalling?: FunctionCallingMode;
logger: Pick<Logger, 'debug'>;
metadata?: ChatCompleteMetadata;
system?: string;
}) => {
return function askLlmToRespond({
Expand Down Expand Up @@ -73,6 +76,7 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({
chatCompleteApi({
connectorId,
functionCalling,
metadata,
stream: true,
system: `${systemMessage}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Message,
withoutOutputUpdateEvents,
FunctionCallingMode,
ChatCompleteMetadata,
} from '@kbn/inference-common';
import { InferenceClient } from '../../..';
import { requestDocumentationSchema } from './shared';
Expand All @@ -22,13 +23,15 @@ export const requestDocumentation = ({
messages,
connectorId,
functionCalling,
metadata,
toolOptions: { tools, toolChoice },
}: {
outputApi: InferenceClient['output'];
system: string;
messages: Message[];
connectorId: string;
functionCalling?: FunctionCallingMode;
metadata?: ChatCompleteMetadata;
toolOptions: ToolOptions;
}) => {
const hasTools = !isEmpty(tools) && toolChoice !== ToolChoiceType.none;
Expand All @@ -38,6 +41,7 @@ export const requestDocumentation = ({
connectorId,
stream: true,
functionCalling,
metadata,
system,
previousMessages: messages,
input: `Based on the previous conversation, request documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function naturalLanguageToEsql<TToolOptions extends ToolOptions>({
logger,
functionCalling,
system,
metadata,
...rest
}: NlToEsqlTaskParams<TToolOptions>): Observable<NlToEsqlTaskEvent<TToolOptions>> {
return from(loadDocBase()).pipe(
Expand All @@ -38,6 +39,7 @@ export function naturalLanguageToEsql<TToolOptions extends ToolOptions>({
logger,
systemMessage,
functionCalling,
metadata,
toolOptions: {
tools,
toolChoice,
Expand All @@ -51,6 +53,7 @@ export function naturalLanguageToEsql<TToolOptions extends ToolOptions>({
outputApi: client.output,
messages,
system: systemMessage,
metadata,
toolOptions: {
tools,
toolChoice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
Message,
ToolOptions,
OutputCompleteEvent,
ChatCompleteMetadata,
} from '@kbn/inference-common';
import type { InferenceClient } from '../../inference_client';

Expand All @@ -30,5 +31,6 @@ export type NlToEsqlTaskParams<TToolOptions extends ToolOptions> = {
logger: Pick<Logger, 'debug'>;
functionCalling?: FunctionCallingMode;
system?: string;
metadata?: ChatCompleteMetadata;
} & TToolOptions &
({ input: string } | { messages: Message[] });
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ export const getEsqlKnowledgeBase: GetEsqlTranslatorToolParams =
client,
connectorId,
input,
logger: {
debug: (source) => {
logger.debug(typeof source === 'function' ? source() : source);
},
},
logger,
})
);
return content;
Expand Down

0 comments on commit da21c9f

Please sign in to comment.