Skip to content
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

[AI Assistant] Add scopes to AI assistant #192611

Merged
merged 12 commits into from
Sep 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ export function ErrorSampleContextualInsight({
instructions: `I'm an SRE. I am looking at an exception and trying to understand what it means.

Your task is to describe what the error means and what it could be caused by.

The error occurred on a service called ${serviceName}, which is a ${runtimeName} service written in ${languageName}. The
runtime version is ${runtimeVersion}.

The request it occurred for is called ${transactionName}.

${
logStacktrace
? `The log stacktrace:
${logStacktrace}`
: ''
}

${
exceptionStacktrace
? `The exception stacktrace:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export function registerGetApmDatasetInfoFunction({
`,
},
};
}
},
['observability']
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export function registerGetApmDownstreamDependenciesFunction({
registerFunction(
{
name: 'get_apm_downstream_dependencies',
description: `Get the downstream dependencies (services or uninstrumented backends) for a
service. This allows you to map the downstream dependency name to a service, by
returning both span.destination.service.resource and service.name. Use this to
description: `Get the downstream dependencies (services or uninstrumented backends) for a
service. This allows you to map the downstream dependency name to a service, by
returning both span.destination.service.resource and service.name. Use this to
drilldown further if needed.`,
descriptionForUser: i18n.translate(
'xpack.apm.observabilityAiAssistant.functions.registerGetApmDownstreamDependencies.descriptionForUser',
{
defaultMessage: `Get the downstream dependencies (services or uninstrumented backends) for a
service. This allows you to map the dowstream dependency name to a service, by
returning both span.destination.service.resource and service.name. Use this to
defaultMessage: `Get the downstream dependencies (services or uninstrumented backends) for a
service. This allows you to map the dowstream dependency name to a service, by
returning both span.destination.service.resource and service.name. Use this to
drilldown further if needed.`,
}
),
Expand Down Expand Up @@ -67,6 +67,7 @@ export function registerGetApmDownstreamDependenciesFunction({
randomSampler,
}),
};
}
},
['observability']
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function registerGetApmServicesListFunction({
arguments: args,
}),
};
}
},
['observability']
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export function registerGetApmTimeseriesFunction({
content: timeseries.map((series): Omit<ApmTimeseries, 'data'> => omit(series, 'data')),
data: timeseries,
};
}
},
['observability']
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const ContextualInsightProcessRow = ({ command }: { command: string }) =>
with the arguments to the process you should then explain its arguments and how they influence the behaviour
of the process. If I do not provide any arguments then explain the behaviour of the process when no arguments are
provided.

Here is an example with arguments.
Process: metricbeat -c /etc/metricbeat.yml -d autodiscover,kafka -e -system.hostfs=/hostfs
Explanation: Metricbeat is part of the Elastic Stack. It is a lightweight shipper that you can install on your
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { JSONSchema7TypeName } from 'json-schema';
import type { Observable } from 'rxjs';
import { ChatCompletionChunkEvent, MessageAddEvent } from '../conversation_complete';
import { FunctionVisibility } from './function_visibility';
import { AssistantScope } from '../types';
export { FunctionVisibility };

type JSONSchemaOrPrimitive = CompatibleJSONSchema | string | number | boolean;
Expand Down Expand Up @@ -41,6 +42,7 @@ export interface FunctionDefinition<TParameters extends CompatibleJSONSchema = a
visibility?: FunctionVisibility;
descriptionForUser?: string;
parameters?: TParameters;
scopes?: AssistantScope[];
}

export type FunctionRegistry = Map<string, FunctionDefinition>;
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,5 @@ export interface ObservabilityAIAssistantScreenContext {
actions?: Array<ScreenContextActionDefinition<any>>;
starterPrompts?: StarterPrompt[];
}

export type AssistantScope = 'observability' | 'search' | 'all';
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function ChatContent({
}) {
const service = useObservabilityAIAssistant();
const chatService = useObservabilityAIAssistantChatService();
const { scope } = service;

const initialMessagesRef = useRef(initialMessages);

Expand All @@ -68,6 +69,7 @@ function ChatContent({
initialMessages,
persist: false,
disableFunctions: true,
scope,
});

const lastAssistantResponse = getLastMessageOfType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('useChat', () => {
service: {
getScreenContexts: () => [],
} as unknown as ObservabilityAIAssistantService,
scope: 'observability',
} as UseChatProps,
});
});
Expand Down Expand Up @@ -109,6 +110,7 @@ describe('useChat', () => {
service: {
getScreenContexts: () => [],
} as unknown as ObservabilityAIAssistantService,
scope: 'observability',
} as UseChatProps,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { merge } from 'lodash';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { AbortError } from '@kbn/kibana-utils-plugin/common';
import type { NotificationsStart } from '@kbn/core/public';
import { AssistantScope } from '../../common/types';
import {
MessageRole,
type Message,
Expand Down Expand Up @@ -55,6 +56,7 @@ interface UseChatPropsWithoutContext {
disableFunctions?: boolean;
onConversationUpdate?: (event: ConversationCreateEvent | ConversationUpdateEvent) => void;
onChatComplete?: (messages: Message[]) => void;
scope: AssistantScope;
}

export type UseChatProps = Omit<UseChatPropsWithoutContext, 'notifications'>;
Expand All @@ -70,6 +72,7 @@ function useChatWithoutContext({
onChatComplete,
persist,
disableFunctions,
scope,
}: UseChatPropsWithoutContext): UseChatResult {
const [chatState, setChatState] = useState(ChatState.Ready);
const systemMessage = useMemo(() => {
Expand Down Expand Up @@ -161,6 +164,7 @@ function useChatWithoutContext({
disableFunctions: disableFunctions ?? false,
signal: abortControllerRef.current.signal,
conversationId,
scope,
});

function getPendingMessages() {
Expand Down Expand Up @@ -259,6 +263,7 @@ function useChatWithoutContext({
disableFunctions,
service,
systemMessage,
scope,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const mockService: ObservabilityAIAssistantService = {
predefinedConversation$: new Observable(),
},
navigate: async () => of(),
scope: 'all',
};

function createSetupContract(): ObservabilityAIAssistantPublicSetup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class ObservabilityAIAssistantPlugin
coreStart.application.capabilities.observabilityAIAssistant[
aiAssistantCapabilities.show
] === true,
scope: 'observability',
}));

const withProviders = <P extends {}, R = {}>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('complete', () => {
disableFunctions: false,
signal: new AbortController().signal,
...params,
scope: 'all',
},
requestCallback
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function complete(
disableFunctions,
signal,
instructions,
scope,
}: {
client: Pick<ObservabilityAIAssistantChatService, 'chat' | 'complete'>;
getScreenContexts: () => ObservabilityAIAssistantScreenContext[];
Expand All @@ -65,6 +66,7 @@ export function complete(
screenContexts,
conversationId,
instructions,
scope,
},
},
}).pipe(shareReplay());
Expand Down Expand Up @@ -131,6 +133,7 @@ export function complete(
persist,
disableFunctions,
instructions,
scope,
},
requestCallback
).subscribe(subscriber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('createChatService', () => {
apiClient: clientSpy,
registrations: [],
signal: new AbortController().signal,
scope: 'observability',
});
});

Expand All @@ -79,7 +80,12 @@ describe('createChatService', () => {

describe('chat', () => {
function chat({ signal }: { signal: AbortSignal } = { signal: new AbortController().signal }) {
return service.chat('my_test', { signal, messages: [], connectorId: '' });
return service.chat('my_test', {
signal,
messages: [],
connectorId: '',
scope: 'observability',
});
}

it('correctly parses a stream of JSON lines', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
throwError,
timestamp,
} from 'rxjs';
import { AssistantScope } from '../../common/types';
import { ChatCompletionChunkEvent, Message, MessageRole } from '../../common';
import {
StreamingChatResponseEventType,
Expand Down Expand Up @@ -137,19 +138,26 @@ export async function createChatService({
signal: setupAbortSignal,
registrations,
apiClient,
scope,
}: {
analytics: AnalyticsServiceStart;
signal: AbortSignal;
registrations: ChatRegistrationRenderFunction[];
apiClient: ObservabilityAIAssistantAPIClient;
scope: AssistantScope;
}): Promise<ObservabilityAIAssistantChatService> {
const functionRegistry: FunctionRegistry = new Map();

const renderFunctionRegistry: Map<string, RenderFunction<unknown, FunctionResponse>> = new Map();

const [{ functionDefinitions, systemMessage }] = await Promise.all([
apiClient('GET /internal/observability_ai_assistant/functions', {
apiClient('GET /internal/observability_ai_assistant/{scope}/functions', {
signal: setupAbortSignal,
params: {
path: {
scope,
},
},
}),
...registrations.map((registration) => {
return registration({
Expand Down Expand Up @@ -196,6 +204,7 @@ export async function createChatService({
connectorId,
functionCall,
functions: functions ?? [],
scope,
},
},
signal,
Expand Down Expand Up @@ -228,6 +237,7 @@ export async function createChatService({
signal,
client,
instructions,
scope,
},
({ params }) => {
return callStreamingApi('POST /internal/observability_ai_assistant/chat/complete', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import type { AnalyticsServiceStart, CoreStart } from '@kbn/core/public';
import { compact, without } from 'lodash';
import { BehaviorSubject, debounceTime, filter, lastValueFrom, of, Subject, take } from 'rxjs';
import type { Message, ObservabilityAIAssistantScreenContext } from '../../common/types';
import type {
AssistantScope,
Message,
ObservabilityAIAssistantScreenContext,
} from '../../common/types';
import { createFunctionRequestMessage } from '../../common/utils/create_function_request_message';
import { createFunctionResponseMessage } from '../../common/utils/create_function_response_message';
import { createCallObservabilityAIAssistantAPI } from '../api';
Expand All @@ -19,10 +23,12 @@ export function createService({
analytics,
coreStart,
enabled,
scope,
}: {
analytics: AnalyticsServiceStart;
coreStart: CoreStart;
enabled: boolean;
scope: AssistantScope;
}): ObservabilityAIAssistantService {
const apiClient = createCallObservabilityAIAssistantAPI(coreStart);

Expand All @@ -42,7 +48,7 @@ export function createService({
},
start: async ({ signal }) => {
const mod = await import('./create_chat_service');
return await mod.createChatService({ analytics, apiClient, signal, registrations });
return await mod.createChatService({ analytics, apiClient, signal, registrations, scope });
},
callApi: apiClient,
getScreenContexts() {
Expand Down Expand Up @@ -89,5 +95,6 @@ export function createService({
},
predefinedConversation$: predefinedConversation$.asObservable(),
},
scope,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export const createStorybookService = (): ObservabilityAIAssistantService => ({
predefinedConversation$: new Observable(),
},
navigate: async () => of(),
scope: 'observability',
});
Loading