-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Agent builder] Builtin Product documentation tool #242598
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
32 commits
Select commit
Hold shift + click to select a range
0e45c52
wip
stephmilovic 461686c
fix tool description and schema
stephmilovic 5996a99
rm
stephmilovic 779e84c
fixes
stephmilovic 771f19e
Merge branch 'main' into product_docs_ab_tool
stephmilovic 8252602
Changes from node scripts/lint_ts_projects --fix
kibanamachine f2ee19c
Merge branch 'product_docs_ab_tool' of github.com:stephmilovic/kibana…
stephmilovic b1d415c
Merge branch 'main' into product_docs_ab_tool
stephmilovic 15f004b
shorter description
stephmilovic 67e00dd
product documentation registration only when available
stephmilovic 99b53ec
update tool services tests
stephmilovic b356eef
promises for types
stephmilovic caa6709
Changes from node scripts/eslint_all_files --no-cache --fix
kibanamachine ccf5860
Merge branch 'main' into product_docs_ab_tool
stephmilovic 85f22ee
refactor
stephmilovic 6be7a9f
more fixing
stephmilovic 2856fb6
Changes from node scripts/lint_ts_projects --fix
kibanamachine 789b159
rm async
stephmilovic 6f4bea1
Merge branch 'product_docs_ab_tool' of github.com:stephmilovic/kibana…
stephmilovic 5afd16c
revert plugin
stephmilovic 24b046d
dynamic inference id
stephmilovic e71f2dd
Merge branch 'main' into product_docs_ab_tool
elasticmachine 7382505
rm connector inferenceId code
stephmilovic 414df60
Merge branch 'main' into product_docs_ab_tool
stephmilovic 0911d2a
Merge remote-tracking branch 'upstream/main' into product_docs_ab_tool
stephmilovic 63b605a
Changes from node scripts/regenerate_moon_projects.js --update
kibanamachine d9a976f
Merge remote-tracking branch 'upstream/main' into product_docs_ab_tool
stephmilovic bad8112
move to new plugin
stephmilovic c52a35e
productDocumentationTool
stephmilovic 5eaae5c
Changes from node scripts/lint_ts_projects --fix
kibanamachine 6409546
Changes from node scripts/regenerate_moon_projects.js --update
kibanamachine 3282c73
Changes from node scripts/eslint_all_files --no-cache --fix
kibanamachine 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
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
148 changes: 148 additions & 0 deletions
148
x-pack/platform/plugins/shared/agent_builder_platform/server/tools/product_documentation.ts
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 |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import { z } from '@kbn/zod'; | ||
| import { platformCoreTools, ToolType } from '@kbn/onechat-common'; | ||
| import { defaultInferenceEndpoints } from '@kbn/inference-common'; | ||
| import type { BuiltinToolDefinition } from '@kbn/onechat-server'; | ||
| import { createErrorResult } from '@kbn/onechat-server'; | ||
| import { ToolResultType } from '@kbn/onechat-common/tools/tool_result'; | ||
| import type { CoreSetup } from '@kbn/core/server'; | ||
| import type { RetrieveDocumentationResultDoc } from '@kbn/llm-tasks-plugin/server'; | ||
| import type { AgentBuilderPlatformPluginStart, PluginStartDependencies } from '../types'; | ||
|
|
||
| const productDocumentationSchema = z.object({ | ||
| query: z.string().describe('Search query to retrieve documentation about Elastic products'), | ||
| product: z | ||
| .enum(['kibana', 'elasticsearch', 'observability', 'security']) | ||
| .optional() | ||
| .describe('Product to filter by: "kibana", "elasticsearch", "observability", or "security"'), | ||
| max: z | ||
| .number() | ||
| .optional() | ||
| .default(3) | ||
| .describe('Maximum number of documents to return. Defaults to 3.'), | ||
| }); | ||
|
|
||
| // TODO make this configurable, we need a platform level setting for the embedding model | ||
| const inferenceId = defaultInferenceEndpoints.ELSER; | ||
|
|
||
| export const productDocumentationTool = ( | ||
| coreSetup: CoreSetup<PluginStartDependencies, AgentBuilderPlatformPluginStart> | ||
| ): BuiltinToolDefinition<typeof productDocumentationSchema> => { | ||
| // Create a closure that will resolve llmTasks when the handler is called | ||
| const getLlmTasks = async () => { | ||
| const [, plugins] = await coreSetup.getStartServices(); | ||
| return plugins.llmTasks; | ||
| }; | ||
|
|
||
| const baseTool: BuiltinToolDefinition<typeof productDocumentationSchema> = { | ||
| id: platformCoreTools.productDocumentation, | ||
| type: ToolType.builtin, | ||
| description: `Search and retrieve documentation about Elastic products (Kibana, Elasticsearch, Elastic Security, Elastic Observability).`, | ||
| schema: productDocumentationSchema, | ||
| handler: async ({ query, product, max = 3 }, { modelProvider, logger, request }) => { | ||
| const llmTasks = await getLlmTasks(); | ||
| if (!llmTasks) { | ||
| return { | ||
| results: [ | ||
| createErrorResult({ | ||
| message: | ||
| 'Product documentation tool is not available. LlmTasks plugin is not available.', | ||
| }), | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| // Get the default model to extract the connector | ||
| const model = await modelProvider.getDefaultModel(); | ||
| const connector = model.connector; | ||
|
|
||
| // Retrieve documentation | ||
| const result = await llmTasks.retrieveDocumentation({ | ||
| searchTerm: query, | ||
| products: product ? [product as any] : undefined, | ||
| max, | ||
| connectorId: connector.connectorId, | ||
| request, | ||
| inferenceId, | ||
| }); | ||
|
|
||
| if (!result.success || result.documents.length === 0) { | ||
| return { | ||
| results: [ | ||
| createErrorResult({ | ||
| message: 'No documentation found for the given query.', | ||
| metadata: { | ||
| query, | ||
| product: product || 'all', | ||
| }, | ||
| }), | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| // Return documentation results | ||
| return { | ||
| results: result.documents.map((doc: RetrieveDocumentationResultDoc) => ({ | ||
| type: ToolResultType.resource, | ||
| data: { | ||
| reference: { | ||
| url: doc.url, | ||
| title: doc.title, | ||
| }, | ||
| partial: doc.summarized, | ||
| content: { | ||
| title: doc.title, | ||
| url: doc.url, | ||
| content: doc.content, | ||
| summarized: doc.summarized, | ||
| }, | ||
| }, | ||
| })), | ||
| }; | ||
| } catch (error) { | ||
| logger.error(`Error retrieving product documentation: ${error.message}`); | ||
| return { | ||
| results: [ | ||
| createErrorResult({ | ||
| message: `Failed to retrieve product documentation: ${error.message}`, | ||
| }), | ||
| ], | ||
| }; | ||
| } | ||
| }, | ||
| tags: [], | ||
| availability: { | ||
| cacheMode: 'space', | ||
| handler: async () => { | ||
| try { | ||
| const [, plugins] = await coreSetup.getStartServices(); | ||
| const llmTasks = plugins.llmTasks; | ||
|
|
||
| if (!llmTasks) { | ||
| return { status: 'unavailable' }; | ||
| } | ||
|
|
||
| const isAvailable = | ||
| (await llmTasks.retrieveDocumentationAvailable({ | ||
| inferenceId, | ||
| })) ?? false; | ||
|
|
||
| return { | ||
| status: isAvailable ? 'available' : 'unavailable', | ||
| }; | ||
| } catch (error) { | ||
| return { status: 'unavailable' }; | ||
| } | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| return baseTool; | ||
| }; | ||
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
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
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.