Skip to content

Commit

Permalink
[Obs AI Assistant] Make resource names static (elastic#187489)
Browse files Browse the repository at this point in the history
This changes the resource names from being stateful to being static.
This makes it easier to import them since they don't have to be passed
around, and "go-to-definition" actually takes you to the implementation
point instead of the types.

(cherry picked from commit 1040bae)
  • Loading branch information
sorenlouv committed Jul 5, 2024
1 parent e306ae6 commit 45c8223
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { Subject } from 'rxjs';
import { EventEmitter, PassThrough, type Readable } from 'stream';
import { finished } from 'stream/promises';
import { ObservabilityAIAssistantClient } from '.';
import { createResourceNamesMap } from '..';
import { MessageRole, type Message } from '../../../common';
import { ObservabilityAIAssistantConnectorType } from '../../../common/connectors';
import {
Expand Down Expand Up @@ -186,7 +185,6 @@ describe('Observability AI Assistant client', () => {
knowledgeBaseService: knowledgeBaseServiceMock,
logger: loggerMock,
namespace: 'default',
resources: createResourceNamesMap(),
user: {
name: 'johndoe',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from 'rxjs';
import { Readable } from 'stream';
import { v4 } from 'uuid';
import { resourceNames } from '..';
import { ObservabilityAIAssistantConnectorType } from '../../../common/connectors';
import {
ChatCompletionChunkEvent,
Expand Down Expand Up @@ -60,7 +61,6 @@ import {
KnowledgeBaseService,
RecalledEntry,
} from '../knowledge_base_service';
import type { ObservabilityAIAssistantResourceNames } from '../types';
import { getAccessQuery } from '../util/get_access_query';
import { getSystemMessageFromInstructions } from '../util/get_system_message_from_instructions';
import { replaceSystemMessage } from '../util/replace_system_message';
Expand Down Expand Up @@ -93,7 +93,6 @@ export class ObservabilityAIAssistantClient {
asInternalUser: ElasticsearchClient;
asCurrentUser: ElasticsearchClient;
};
resources: ObservabilityAIAssistantResourceNames;
logger: Logger;
user?: {
id?: string;
Expand All @@ -107,7 +106,7 @@ export class ObservabilityAIAssistantClient {
conversationId: string
): Promise<SearchHit<Conversation> | undefined> => {
const response = await this.dependencies.esClient.asInternalUser.search<Conversation>({
index: this.dependencies.resources.aliases.conversations,
index: resourceNames.aliases.conversations,
query: {
bool: {
filter: [
Expand Down Expand Up @@ -594,7 +593,7 @@ export class ObservabilityAIAssistantClient {

find = async (options?: { query?: string }): Promise<{ conversations: Conversation[] }> => {
const response = await this.dependencies.esClient.asInternalUser.search<Conversation>({
index: this.dependencies.resources.aliases.conversations,
index: resourceNames.aliases.conversations,
allow_no_indices: true,
query: {
bool: {
Expand Down Expand Up @@ -686,7 +685,7 @@ export class ObservabilityAIAssistantClient {
);

await this.dependencies.esClient.asInternalUser.index({
index: this.dependencies.resources.aliases.conversations,
index: resourceNames.aliases.conversations,
document: createdConversation,
refresh: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,34 @@ import { ObservabilityAIAssistantClient } from './client';
import { conversationComponentTemplate } from './conversation_component_template';
import { kbComponentTemplate } from './kb_component_template';
import { KnowledgeBaseEntryOperationType, KnowledgeBaseService } from './knowledge_base_service';
import type {
RegistrationCallback,
ObservabilityAIAssistantResourceNames,
RespondFunctionResources,
} from './types';
import type { RegistrationCallback, RespondFunctionResources } from './types';
import { splitKbText } from './util/split_kb_text';

function getResourceName(resource: string) {
return `.kibana-observability-ai-assistant-${resource}`;
}

export function createResourceNamesMap() {
return {
componentTemplate: {
conversations: getResourceName('component-template-conversations'),
kb: getResourceName('component-template-kb'),
},
aliases: {
conversations: getResourceName('conversations'),
kb: getResourceName('kb'),
},
indexPatterns: {
conversations: getResourceName('conversations*'),
kb: getResourceName('kb*'),
},
indexTemplate: {
conversations: getResourceName('index-template-conversations'),
kb: getResourceName('index-template-kb'),
},
pipelines: {
kb: getResourceName('kb-ingest-pipeline'),
},
};
}
export const resourceNames = {
componentTemplate: {
conversations: getResourceName('component-template-conversations'),
kb: getResourceName('component-template-kb'),
},
aliases: {
conversations: getResourceName('conversations'),
kb: getResourceName('kb'),
},
indexPatterns: {
conversations: getResourceName('conversations*'),
kb: getResourceName('kb*'),
},
indexTemplate: {
conversations: getResourceName('index-template-conversations'),
kb: getResourceName('index-template-kb'),
},
pipelines: {
kb: getResourceName('kb-ingest-pipeline'),
},
};

export const INDEX_QUEUED_DOCUMENTS_TASK_ID = 'observabilityAIAssistant:indexQueuedDocumentsTask';

Expand All @@ -76,8 +70,6 @@ export class ObservabilityAIAssistantService {
private readonly getModelId: () => Promise<string>;
private kbService?: KnowledgeBaseService;

private readonly resourceNames: ObservabilityAIAssistantResourceNames = createResourceNamesMap();

private readonly registrations: RegistrationCallback[] = [];

constructor({
Expand Down Expand Up @@ -143,15 +135,15 @@ export class ObservabilityAIAssistantService {
const esClient = coreStart.elasticsearch.client;
await esClient.asInternalUser.cluster.putComponentTemplate({
create: false,
name: this.resourceNames.componentTemplate.conversations,
name: resourceNames.componentTemplate.conversations,
template: conversationComponentTemplate,
});

await esClient.asInternalUser.indices.putIndexTemplate({
name: this.resourceNames.indexTemplate.conversations,
composed_of: [this.resourceNames.componentTemplate.conversations],
name: resourceNames.indexTemplate.conversations,
composed_of: [resourceNames.componentTemplate.conversations],
create: false,
index_patterns: [this.resourceNames.indexPatterns.conversations],
index_patterns: [resourceNames.indexPatterns.conversations],
template: {
settings: {
number_of_shards: 1,
Expand All @@ -166,7 +158,7 @@ export class ObservabilityAIAssistantService {
},
});

const conversationAliasName = this.resourceNames.aliases.conversations;
const conversationAliasName = resourceNames.aliases.conversations;

await createConcreteWriteIndex({
esClient: esClient.asInternalUser,
Expand All @@ -177,19 +169,19 @@ export class ObservabilityAIAssistantService {
pattern: `${conversationAliasName}*`,
basePattern: `${conversationAliasName}*`,
name: `${conversationAliasName}-000001`,
template: this.resourceNames.indexTemplate.conversations,
template: resourceNames.indexTemplate.conversations,
},
dataStreamAdapter: getDataStreamAdapter({ useDataStreamForAlerts: false }),
});

await esClient.asInternalUser.cluster.putComponentTemplate({
create: false,
name: this.resourceNames.componentTemplate.kb,
name: resourceNames.componentTemplate.kb,
template: kbComponentTemplate,
});

await esClient.asInternalUser.ingest.putPipeline({
id: this.resourceNames.pipelines.kb,
id: resourceNames.pipelines.kb,
processors: [
{
inference: {
Expand All @@ -210,10 +202,10 @@ export class ObservabilityAIAssistantService {
});

await esClient.asInternalUser.indices.putIndexTemplate({
name: this.resourceNames.indexTemplate.kb,
composed_of: [this.resourceNames.componentTemplate.kb],
name: resourceNames.indexTemplate.kb,
composed_of: [resourceNames.componentTemplate.kb],
create: false,
index_patterns: [this.resourceNames.indexPatterns.kb],
index_patterns: [resourceNames.indexPatterns.kb],
template: {
settings: {
number_of_shards: 1,
Expand All @@ -223,7 +215,7 @@ export class ObservabilityAIAssistantService {
},
});

const kbAliasName = this.resourceNames.aliases.kb;
const kbAliasName = resourceNames.aliases.kb;

await createConcreteWriteIndex({
esClient: esClient.asInternalUser,
Expand All @@ -234,15 +226,14 @@ export class ObservabilityAIAssistantService {
pattern: `${kbAliasName}*`,
basePattern: `${kbAliasName}*`,
name: `${kbAliasName}-000001`,
template: this.resourceNames.indexTemplate.kb,
template: resourceNames.indexTemplate.kb,
},
dataStreamAdapter: getDataStreamAdapter({ useDataStreamForAlerts: false }),
});

this.kbService = new KnowledgeBaseService({
logger: this.logger.get('kb'),
esClient,
resources: this.resourceNames,
taskManagerStart: pluginsStart.taskManager,
getModelId: this.getModelId,
});
Expand Down Expand Up @@ -289,7 +280,6 @@ export class ObservabilityAIAssistantService {
asInternalUser: coreStart.elasticsearch.client.asInternalUser,
asCurrentUser: coreStart.elasticsearch.client.asScoped(request).asCurrentUser,
},
resources: this.resourceNames,
logger: this.logger,
user: user
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ import pRetry from 'p-retry';
import { map, orderBy } from 'lodash';
import { encode } from 'gpt-tokenizer';
import { MlTrainedModelDeploymentNodesStats } from '@elastic/elasticsearch/lib/api/types';
import { INDEX_QUEUED_DOCUMENTS_TASK_ID, INDEX_QUEUED_DOCUMENTS_TASK_TYPE } from '..';
import {
INDEX_QUEUED_DOCUMENTS_TASK_ID,
INDEX_QUEUED_DOCUMENTS_TASK_TYPE,
resourceNames,
} from '..';
import { KnowledgeBaseEntry, KnowledgeBaseEntryRole, UserInstruction } from '../../../common/types';
import type { ObservabilityAIAssistantResourceNames } from '../types';
import { getAccessQuery } from '../util/get_access_query';
import { getCategoryQuery } from '../util/get_category_query';
import { recallFromConnectors } from './recall_from_connectors';

interface Dependencies {
esClient: { asInternalUser: ElasticsearchClient };
resources: ObservabilityAIAssistantResourceNames;
logger: Logger;
taskManagerStart: TaskManagerStartContract;
getModelId: () => Promise<string>;
Expand Down Expand Up @@ -194,7 +196,7 @@ export class KnowledgeBaseService {
private async processOperation(operation: KnowledgeBaseEntryOperation) {
if (operation.type === KnowledgeBaseEntryOperationType.Delete) {
await this.dependencies.esClient.asInternalUser.deleteByQuery({
index: this.dependencies.resources.aliases.kb,
index: resourceNames.aliases.kb,
query: {
bool: {
filter: [
Expand Down Expand Up @@ -333,7 +335,7 @@ export class KnowledgeBaseService {
const response = await this.dependencies.esClient.asInternalUser.search<
Pick<KnowledgeBaseEntry, 'text' | 'is_correction' | 'labels'>
>({
index: [this.dependencies.resources.aliases.kb],
index: [resourceNames.aliases.kb],
query: esQuery,
size: 20,
_source: {
Expand Down Expand Up @@ -431,7 +433,7 @@ export class KnowledgeBaseService {
): Promise<UserInstruction[]> => {
try {
const response = await this.dependencies.esClient.asInternalUser.search<KnowledgeBaseEntry>({
index: this.dependencies.resources.aliases.kb,
index: resourceNames.aliases.kb,
query: {
bool: {
must: [
Expand Down Expand Up @@ -475,7 +477,7 @@ export class KnowledgeBaseService {
}): Promise<{ entries: KnowledgeBaseEntry[] }> => {
try {
const response = await this.dependencies.esClient.asInternalUser.search<KnowledgeBaseEntry>({
index: this.dependencies.resources.aliases.kb,
index: resourceNames.aliases.kb,
...(query
? {
query: {
Expand Down Expand Up @@ -537,15 +539,15 @@ export class KnowledgeBaseService {
}): Promise<void> => {
try {
await this.dependencies.esClient.asInternalUser.index({
index: this.dependencies.resources.aliases.kb,
index: resourceNames.aliases.kb,
id,
document: {
'@timestamp': new Date().toISOString(),
...document,
user,
namespace,
},
pipeline: this.dependencies.resources.pipelines.kb,
pipeline: resourceNames.pipelines.kb,
refresh: 'wait_for',
});
} catch (error) {
Expand Down Expand Up @@ -579,7 +581,7 @@ export class KnowledgeBaseService {
deleteEntry = async ({ id }: { id: string }): Promise<void> => {
try {
await this.dependencies.esClient.asInternalUser.delete({
index: this.dependencies.resources.aliases.kb,
index: resourceNames.aliases.kb,
id,
refresh: 'wait_for',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,3 @@ export type RegistrationCallback = ({}: {
client: ObservabilityAIAssistantClient;
functions: ChatFunctionClient;
}) => Promise<void>;

export interface ObservabilityAIAssistantResourceNames {
componentTemplate: {
conversations: string;
kb: string;
};
indexTemplate: {
conversations: string;
kb: string;
};
aliases: {
conversations: string;
kb: string;
};
indexPatterns: {
conversations: string;
kb: string;
};
pipelines: {
kb: string;
};
}

0 comments on commit 45c8223

Please sign in to comment.