Skip to content
Merged
1 change: 0 additions & 1 deletion api_docs/gen_ai_settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ Contact [@elastic/appex-ai-infra](https://github.com/orgs/elastic/teams/appex-ai

### Consts, variables and types
<DocDefinitionList data={genAiSettingsObj.server.misc}/>

Comment thread
viduni94 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ import {
isChatCompletionMessageEvent,
MessageRole,
OutputEventType,
ToolChoiceType,
} from '@kbn/inference-common';
import { correctCommonEsqlMistakes, generateFakeToolCallId } from '../../../../common';
import { INLINE_ESQL_QUERY_REGEX } from '../../../../common/tasks/nl_to_esql/constants';
import type { EsqlDocumentBase } from '../doc_base';
import { requestDocumentationSchema } from './shared';
import type { NlToEsqlTaskEvent } from '../types';

const MAX_CALLS = 5;

export const generateEsqlTask = <TToolOptions extends ToolOptions>({
chatCompleteApi,
connectorId,
Expand All @@ -42,6 +45,7 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({
logger,
system,
metadata,
maxCallsAllowed = MAX_CALLS,
}: {
connectorId: string;
systemMessage: string;
Expand All @@ -52,12 +56,16 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({
logger: Pick<Logger, 'debug'>;
metadata?: ChatCompleteMetadata;
system?: string;
maxCallsAllowed?: number;
} & Pick<ChatCompleteOptions, 'maxRetries' | 'retryConfiguration' | 'functionCalling'>) => {
return function askLlmToRespond({
documentationRequest: { commands, functions },
callCount = 0,
}: {
documentationRequest: { commands?: string[]; functions?: string[] };
callCount?: number;
}): Observable<NlToEsqlTaskEvent<TToolOptions>> {
const functionLimitReached = callCount >= maxCallsAllowed;
const keywords = [...(commands ?? []), ...(functions ?? [])];
const requestedDocumentation = docBase.getDocumentation(keywords);
const fakeRequestDocsToolCall = createFakeTooCall(commands, functions);
Expand Down Expand Up @@ -126,14 +134,16 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({
toolCallId: fakeRequestDocsToolCall.toolCallId,
},
],
toolChoice,
tools: {
...tools,
request_documentation: {
description: 'Request additional ES|QL documentation if needed',
schema: requestDocumentationSchema,
},
},
toolChoice: !functionLimitReached ? toolChoice : ToolChoiceType.none,
tools: functionLimitReached
? {}
: {
...tools,
request_documentation: {
description: 'Request additional ES|QL documentation if needed',
schema: requestDocumentationSchema,
},
},
}).pipe(
withoutTokenCountEvents(),
map((generateEvent) => {
Expand All @@ -150,18 +160,32 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({
}),
switchMap((generateEvent) => {
if (isChatCompletionMessageEvent(generateEvent)) {
const onlyToolCall =
generateEvent.toolCalls.length === 1 ? generateEvent.toolCalls[0] : undefined;

if (onlyToolCall?.function.name === 'request_documentation') {
const args = onlyToolCall.function.arguments;

return askLlmToRespond({
documentationRequest: {
commands: args.commands,
functions: args.functions,
},
});
const toolCalls = generateEvent.toolCalls as ToolCall[];
const onlyToolCall = toolCalls.length === 1 ? toolCalls[0] : undefined;

if (onlyToolCall && onlyToolCall.function.name === 'request_documentation') {
if (functionLimitReached) {
return of({
...generateEvent,
content: `You have reached the maximum number of documentation requests. Do not try to request documentation again for commands ${commands?.join(
', '
)} and functions ${functions?.join(
', '
)}. Try to answer the user's question using currently available information.`,
});
}

const args =
'arguments' in onlyToolCall.function ? onlyToolCall.function.arguments : undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably need some better type defaults here to prevent having to do this much work to get some props from an object, but we can figure that out later

if (args && (args.commands?.length || args.functions?.length)) {
return askLlmToRespond({
documentationRequest: {
commands: args.commands ?? [],
functions: args.functions ?? [],
},
callCount: callCount + 1,
});
}
}
}

Expand Down