-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Obs AI Assistant] Add ES function API test #187465
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
37a52d7
[Obs AI Assistant] Add ES function API test
sorenlouv 0ade4f3
Use constant
sorenlouv 838a5ac
Move wait for call
sorenlouv 6e72a29
Add test for summarize
sorenlouv 7e078bd
minor refactor
sorenlouv 29d17da
Address feedback
sorenlouv f396cf0
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine f5f1d74
Merge branch 'main' into add-api-test-es-function
sorenlouv efbd404
Fix tsc
sorenlouv 03f3732
Merge branch 'main' into add-api-test-es-function
sorenlouv bc1039e
Fix typescript error
sorenlouv 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 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 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 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 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 |
---|---|---|
|
@@ -11,7 +11,7 @@ import type { ElasticsearchClient, IUiSettingsClient } from '@kbn/core/server'; | |
import type { Logger } from '@kbn/logging'; | ||
import type { PublicMethodsOf } from '@kbn/utility-types'; | ||
import { SpanKind, context } from '@opentelemetry/api'; | ||
import { merge, omit } from 'lodash'; | ||
import { last, merge, omit } from 'lodash'; | ||
import { | ||
catchError, | ||
combineLatest, | ||
|
@@ -334,13 +334,12 @@ export class ObservabilityAIAssistantClient { | |
const initialMessagesWithAddedMessages = | ||
messagesWithUpdatedSystemMessage.concat(addedMessages); | ||
|
||
const lastMessage = | ||
initialMessagesWithAddedMessages[initialMessagesWithAddedMessages.length - 1]; | ||
const lastMessage = last(initialMessagesWithAddedMessages); | ||
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. Use utility for improved readability |
||
|
||
// if a function request is at the very end, close the stream to consumer | ||
// without persisting or updating the conversation. we need to wait | ||
// on the function response to have a valid conversation | ||
const isFunctionRequest = lastMessage.message.function_call?.name; | ||
const isFunctionRequest = !!lastMessage?.message.function_call?.name; | ||
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. cast to bool |
||
|
||
if (!persist || isFunctionRequest) { | ||
return of(); | ||
|
This file contains 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 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 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 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 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
97 changes: 97 additions & 0 deletions
97
...observability_ai_assistant_api_integration/tests/complete/functions/elasticsearch.spec.ts
This file contains 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,97 @@ | ||
/* | ||
* 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 { MessageAddEvent, MessageRole } from '@kbn/observability-ai-assistant-plugin/common'; | ||
import expect from '@kbn/expect'; | ||
import { apm, timerange } from '@kbn/apm-synthtrace-client'; | ||
import { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace'; | ||
import { ELASTICSEARCH_FUNCTION_NAME } from '@kbn/observability-ai-assistant-plugin/server/functions/elasticsearch'; | ||
import { LlmProxy } from '../../../common/create_llm_proxy'; | ||
import { FtrProviderContext } from '../../../common/ftr_provider_context'; | ||
import { | ||
createLLMProxyConnector, | ||
deleteLLMProxyConnector, | ||
getMessageAddedEvents, | ||
invokeChatCompleteWithFunctionRequest, | ||
} from './helpers'; | ||
|
||
export default function ApiTest({ getService }: FtrProviderContext) { | ||
const supertest = getService('supertest'); | ||
const log = getService('log'); | ||
const apmSynthtraceEsClient = getService('apmSynthtraceEsClient'); | ||
const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantAPIClient'); | ||
|
||
describe('when calling elasticsearch', () => { | ||
let proxy: LlmProxy; | ||
let connectorId: string; | ||
let events: MessageAddEvent[]; | ||
|
||
before(async () => { | ||
({ connectorId, proxy } = await createLLMProxyConnector({ log, supertest })); | ||
await generateApmData(apmSynthtraceEsClient); | ||
|
||
const responseBody = await invokeChatCompleteWithFunctionRequest({ | ||
connectorId, | ||
observabilityAIAssistantAPIClient, | ||
functionCall: { | ||
name: ELASTICSEARCH_FUNCTION_NAME, | ||
trigger: MessageRole.User, | ||
arguments: JSON.stringify({ | ||
method: 'POST', | ||
path: 'traces*/_search', | ||
body: { | ||
size: 0, | ||
aggs: { | ||
services: { | ||
terms: { | ||
field: 'service.name', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}); | ||
|
||
await proxy.waitForAllInterceptorsSettled(); | ||
|
||
events = getMessageAddedEvents(responseBody); | ||
}); | ||
|
||
after(async () => { | ||
await deleteLLMProxyConnector({ supertest, connectorId, proxy }); | ||
await apmSynthtraceEsClient.clean(); | ||
}); | ||
|
||
it('returns elasticsearch function response', async () => { | ||
const esFunctionResponse = events[0]; | ||
const parsedEsResponse = JSON.parse(esFunctionResponse.message.message.content!).response; | ||
|
||
expect(esFunctionResponse.message.message.name).to.be('elasticsearch'); | ||
expect(parsedEsResponse.hits.total.value).to.be(15); | ||
expect(parsedEsResponse.aggregations.services.buckets).to.eql([ | ||
{ key: 'java-backend', doc_count: 15 }, | ||
]); | ||
expect(events.length).to.be(2); | ||
}); | ||
}); | ||
} | ||
|
||
export async function generateApmData(apmSynthtraceEsClient: ApmSynthtraceEsClient) { | ||
const serviceA = apm | ||
.service({ name: 'java-backend', environment: 'production', agentName: 'java' }) | ||
.instance('a'); | ||
|
||
const events = timerange('now-15m', 'now') | ||
.interval('1m') | ||
.rate(1) | ||
.generator((timestamp) => { | ||
return serviceA.transaction({ transactionName: 'tx' }).timestamp(timestamp).duration(1000); | ||
}); | ||
|
||
return apmSynthtraceEsClient.index(events); | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dgieselaar Does this change look ok to you? Before we'd inject the context request immediately after other function requests effectively overwriting them. This changes fixes that but I'm not sure if it causes problems to context function request injection in some cases