Skip to content

Commit

Permalink
api test
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Nov 7, 2024
1 parent c9d008c commit db31e60
Show file tree
Hide file tree
Showing 19 changed files with 255 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function WelcomeMessageKnowledgeBaseSetupErrorPanel({
onClick={onRetryInstall}
>
{i18n.translate(
'xpack.observabilityAiAssistant.welcomeMessageKnowledgeBaseSetupErrorPanel.retryInstallingLinkLabel',
'xpack.aiAssistant.welcomeMessageKnowledgeBaseSetupErrorPanel.retryInstallingLinkLabel',
{ defaultMessage: 'Retry install knowledge base' }
)}
</EuiLink>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { registerFunctions } from './functions';
import { recallRankingEvent } from './analytics/recall_ranking';
import { initLangtrace } from './service/client/instrumentation/init_langtrace';
import { aiAssistantCapabilities } from '../common/capabilities';
import { registerMigrateKnowledgeBaseEntriesTask } from './service/task_manager_definitions/register_migrate_knowledge_base_entries_task';

export class ObservabilityAIAssistantPlugin
implements
Expand Down Expand Up @@ -156,11 +157,16 @@ export class ObservabilityAIAssistantPlugin
const service = (this.service = new ObservabilityAIAssistantService({
logger: this.logger.get('service'),
core,
taskManager: plugins.taskManager,
getSearchConnectorModelId,
enableKnowledgeBase: this.config.enableKnowledgeBase,
}));

registerMigrateKnowledgeBaseEntriesTask({
core,
taskManager: plugins.taskManager,
logger: this.logger,
});

service.register(registerFunctions);

registerServerRoutes({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const getKnowledgeBaseStatus = createObservabilityAIAssistantServerRoute({
service,
request,
}): Promise<
Partial<InferenceEndpointResponse['endpoints'][0]> & {
ready: boolean;
}
{ ready: boolean; enabled: boolean } & Partial<InferenceEndpointResponse['endpoints'][0]>
> => {
const client = await service.getClient({ request });

Expand All @@ -44,7 +42,7 @@ const setupKnowledgeBase = createObservabilityAIAssistantServerRoute({
idleSocket: 20 * 60 * 1000, // 20 minutes
},
},
handler: async (resources): Promise<unknown> => {
handler: async (resources): Promise<any> => {
const client = await resources.service.getClient({ request: resources.request });

if (!client) {
Expand Down Expand Up @@ -252,8 +250,8 @@ const importKnowledgeBaseEntries = createObservabilityAIAssistantServerRoute({
throw notImplemented();
}

const status = await client.getKnowledgeBaseStatus();
if (!status.ready) {
const { ready } = await client.getKnowledgeBaseStatus();
if (!ready) {
throw new Error('Knowledge base is not ready');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import type { Logger } from '@kbn/logging';
import { registerRoutes } from '@kbn/server-route-repository';
import { getGlobalObservabilityAIAssistantServerRouteRepository } from './get_global_observability_ai_assistant_route_repository';
import type { ObservabilityAIAssistantRouteHandlerResources } from './types';
import { ObservabilityAIAssistantPluginStartDependencies } from '../types';

export function registerServerRoutes({
core,
logger,
dependencies,
}: {
core: CoreSetup;
core: CoreSetup<ObservabilityAIAssistantPluginStartDependencies>;
logger: Logger;
dependencies: Omit<
ObservabilityAIAssistantRouteHandlerResources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ export class ObservabilityAIAssistantClient {
};

getKnowledgeBaseStatus = () => {
return this.dependencies.knowledgeBaseService.getElserModelStatus();
return this.dependencies.knowledgeBaseService.getStatus();
};

setupKnowledgeBase = () => {
Expand All @@ -737,9 +737,8 @@ export class ObservabilityAIAssistantClient {

migrateKnowledgeBaseToSemanticText = () => {
return runSemanticTextKnowledgeBaseMigration({
esClient: this.dependencies.esClient.asInternalUser,
esClient: this.dependencies.esClient,
logger: this.dependencies.logger,
kbService: this.dependencies.knowledgeBaseService,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { errors } from '@elastic/elasticsearch';
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import { Logger } from '@kbn/logging';

Expand Down Expand Up @@ -102,3 +103,30 @@ export async function getInferenceEndpoint({
throw e;
}
}

export async function getInferenceEndpointStatus({
esClient,
logger,
}: {
esClient: { asInternalUser: ElasticsearchClient };
logger: Logger;
}) {
try {
const endpoint = await getInferenceEndpoint({ esClient, logger });
const ready = endpoint !== undefined;
return { ...endpoint, ready };
} catch (error) {
if (isInferenceEndpointMissingOrUnavailable(error)) {
return { ready: false };
}
throw error;
}
}

export function isInferenceEndpointMissingOrUnavailable(error: Error) {
return (
error instanceof errors.ResponseError &&
(error.body?.error?.type === 'resource_not_found_exception' ||
error.body?.error?.type === 'status_exception')
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { createConcreteWriteIndex, getDataStreamAdapter } from '@kbn/alerting-pl
import type { CoreSetup, CoreStart, KibanaRequest, Logger } from '@kbn/core/server';
import type { SecurityPluginStart } from '@kbn/security-plugin/server';
import { getSpaceIdFromPath } from '@kbn/spaces-plugin/common';
import type { TaskManagerSetupContract } from '@kbn/task-manager-plugin/server';
import { once } from 'lodash';
import type { AssistantScope } from '@kbn/ai-assistant-common';
import { ObservabilityAIAssistantScreenContextRequest } from '../../common/types';
Expand Down Expand Up @@ -57,32 +56,20 @@ export class ObservabilityAIAssistantService {
constructor({
logger,
core,
taskManager,
getSearchConnectorModelId,
enableKnowledgeBase,
}: {
logger: Logger;
core: CoreSetup<ObservabilityAIAssistantPluginStartDependencies>;
taskManager: TaskManagerSetupContract;
getSearchConnectorModelId: () => Promise<string>;
enableKnowledgeBase: boolean;
}) {
this.core = core;
this.logger = logger;
this.getSearchConnectorModelId = getSearchConnectorModelId;
this.enableKnowledgeBase = enableKnowledgeBase;

this.registerInit();

registerMigrateKnowledgeBaseEntriesTask({
taskManager,
logger,
getKbService: () => this.kbService,
getTaskManagerStart,
getEsClient: async () => {
const [coreStart] = await core.getStartServices();
return coreStart.elasticsearch.client.asInternalUser;
},
});
}

init = async () => {};
Expand All @@ -99,7 +86,7 @@ export class ObservabilityAIAssistantService {
private doInit = async () => {
try {
this.logger.info('Setting up index assets');
const [coreStart, pluginsStart] = await this.core.getStartServices();
const [coreStart] = await this.core.getStartServices();

const esClient = {
asInternalUser: coreStart.elasticsearch.client.asInternalUser,
Expand Down Expand Up @@ -183,7 +170,6 @@ export class ObservabilityAIAssistantService {
this.kbService = new KnowledgeBaseService({
logger: this.logger.get('kb'),
esClient,
taskManagerStart: pluginsStart.taskManager,
getSearchConnectorModelId: this.getSearchConnectorModelId,
enabled: this.enableKnowledgeBase,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export const kbComponentTemplate: ClusterComponentTemplate['component_template']
namespace: keyword,
text,
semantic_text: {
// @ts-expect-error: @elastic/elasticsearch does not have this type yet
type: 'semantic_text',
inference_id: AI_ASSISTANT_KB_INFERENCE_ID,
// @ts-expect-error: @elastic/elasticsearch does not have this type yet
model_settings: {
task_type: 'sparse_embedding',
},
Expand Down
Loading

0 comments on commit db31e60

Please sign in to comment.