-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[NL2ESQL] Adds max function calls allowed to prevent tool being called recursively #231719
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7c59ff0
Add max function calls to function to prevent tool being called over …
qn895 cd43b33
Add check for previous inferenceIds to update
qn895 5097b10
Set toolChoice to non
qn895 963551d
Fix tools
qn895 3a2bb43
Merge upstream/main into branch
qn895 c30dbc7
Merge upstream/main into branch 2
qn895 a7c619b
Remove isPopulatedObject
qn895 1034ba5
Merge remote-tracking branch 'upstream/main' into ai-esql-loop
qn895 c2504b8
Fix types
qn895 35e7923
Fix weird merge
qn895 b0a6939
Merge remote-tracking branch 'upstream/main' into ai-esql-loop
qn895 9c5e7c5
Remove role
qn895 8628f89
Merge remote-tracking branch 'upstream/main' into ai-esql-loop
qn895 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -42,6 +45,7 @@ export const generateEsqlTask = <TToolOptions extends ToolOptions>({ | |
| logger, | ||
| system, | ||
| metadata, | ||
| maxCallsAllowed = MAX_CALLS, | ||
| }: { | ||
| connectorId: string; | ||
| systemMessage: string; | ||
|
|
@@ -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); | ||
|
|
@@ -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) => { | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.