Skip to content

Commit

Permalink
Fixed test and not mapped provider issue
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Nov 8, 2024
1 parent 69ee6dd commit 8b58919
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useState, useEffect, useCallback } from 'react';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import {
EuiFormRow,
EuiSpacer,
Expand Down Expand Up @@ -309,6 +309,22 @@ const InferenceAPIConnectorFields: React.FunctionComponent<ActionConnectorFields
setFieldValue('config.provider', '');
}, [onProviderChange, setFieldValue]);

const providerIcon = useMemo(
() =>
Object.keys(SERVICE_PROVIDERS).includes(config?.provider)
? SERVICE_PROVIDERS[config?.provider as ServiceProviderKeys].icon
: undefined,
[config?.provider]
);

const providerName = useMemo(
() =>
Object.keys(SERVICE_PROVIDERS).includes(config?.provider)
? SERVICE_PROVIDERS[config?.provider as ServiceProviderKeys].name
: config?.provider,
[config?.provider]
);

const providerSuperSelect = useCallback(
(isInvalid: boolean) => (
<EuiFormControlLayout
Expand All @@ -317,21 +333,15 @@ const InferenceAPIConnectorFields: React.FunctionComponent<ActionConnectorFields
isDisabled={isEdit || readOnly}
isInvalid={isInvalid}
fullWidth
icon={
!config?.provider
? { type: 'sparkles', side: 'left' }
: SERVICE_PROVIDERS[config?.provider as ServiceProviderKeys].icon
}
icon={!config?.provider ? { type: 'sparkles', side: 'left' } : providerIcon}
>
<EuiFieldText
onClick={handleProviderPopover}
data-test-subj="provider-select"
isInvalid={isInvalid}
disabled={isEdit || readOnly}
onKeyDown={handleProviderKeyboardOpen}
value={
config?.provider ? SERVICE_PROVIDERS[config?.provider as ServiceProviderKeys].name : ''
}
value={config?.provider ? providerName : ''}
fullWidth
placeholder={i18n.SELECT_PROVIDER}
icon={{ type: 'arrowDown', side: 'right' }}
Expand All @@ -345,8 +355,10 @@ const InferenceAPIConnectorFields: React.FunctionComponent<ActionConnectorFields
readOnly,
onClearProvider,
config?.provider,
providerIcon,
handleProviderPopover,
handleProviderKeyboardOpen,
providerName,
isProviderPopoverOpen,
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ export const ServiceProviderIcon: React.FC<ServiceProviderProps> = ({ providerKe

return provider ? (
<EuiIcon data-test-subj={`icon-service-provider-${providerKey}`} type={provider.icon} />
) : (
<span>{providerKey}</span>
);
) : null;
};

export const ServiceProviderName: React.FC<ServiceProviderProps> = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,123 +6,128 @@
*/

import { httpServiceMock, httpServerMock } from '@kbn/core/server/mocks';
import { coreMock } from '@kbn/core/server/mocks';
import { getInferenceServicesRoute } from './get_inference_services';
import { DisplayType, FieldType } from '../../common/dynamic_config/types';

describe('getInferenceServicesRoute', () => {
it('returns available service providers', async () => {
const router = httpServiceMock.createRouter();
const core = coreMock.createRequestHandlerContext();

const mockResult = [
{
provider: 'openai',
task_types: [
{
task_type: 'completion',
configuration: {
user: {
display: DisplayType.TEXTBOX,
label: 'User',
order: 1,
required: false,
sensitive: false,
tooltip: 'Specifies the user issuing the request.',
type: FieldType.STRING,
validations: [],
value: '',
ui_restrictions: [],
default_value: null,
depends_on: [],
},
},
},
],
configuration: {
api_key: {
display: DisplayType.TEXTBOX,
label: 'API Key',
order: 3,
required: true,
sensitive: true,
tooltip: `The OpenAI API authentication key. For more details about generating OpenAI API keys, refer to the https://platform.openai.com/account/api-keys.`,
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
model_id: {
display: DisplayType.TEXTBOX,
label: 'Model ID',
order: 2,
required: true,
sensitive: false,
tooltip: 'The name of the model to use for the inference task.',
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
organization_id: {
display: DisplayType.TEXTBOX,
label: 'Organization ID',
order: 4,
required: false,
sensitive: false,
tooltip: 'The unique identifier of your organization.',
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
url: {
display: DisplayType.TEXTBOX,
label: 'URL',
order: 1,
required: true,
sensitive: false,
tooltip:
'The OpenAI API endpoint URL. For more information on the URL, refer to the https://platform.openai.com/docs/api-reference.',
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: 'https://api.openai.com/v1/chat/completions',
depends_on: [],
},
'rate_limit.requests_per_minute': {
display: DisplayType.NUMERIC,
label: 'Rate limit',
order: 5,
required: false,
sensitive: false,
tooltip:
'Default number of requests allowed per minute. For text_embedding is 3000. For completion is 500.',
type: FieldType.INTEGER,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
},
},
];
core.elasticsearch.client.asInternalUser.transport.request.mockResolvedValue(mockResult);

getInferenceServicesRoute(router);

const [config, handler] = router.get.mock.calls[0];
expect(config.path).toMatchInlineSnapshot('/internal/stack_connectors/_inference/_services');
expect(config.path).toMatchInlineSnapshot(`"/internal/stack_connectors/_inference/_services"`);

const mockResponse = httpServerMock.createResponseFactory();
const mockRequest = httpServerMock.createKibanaRequest();
await handler({}, mockRequest, mockResponse);
await handler({ core }, mockRequest, mockResponse);

expect(mockResponse.ok).toHaveBeenCalledWith({
body: [
{
provider: 'openai',
task_types: [
{
task_type: 'completion',
configuration: {
user: {
display: DisplayType.TEXTBOX,
label: 'User',
order: 1,
required: false,
sensitive: false,
tooltip: 'Specifies the user issuing the request.',
type: FieldType.STRING,
validations: [],
value: '',
ui_restrictions: [],
default_value: null,
depends_on: [],
},
},
},
],
configuration: {
api_key: {
display: DisplayType.TEXTBOX,
label: 'API Key',
order: 3,
required: true,
sensitive: true,
tooltip: `The OpenAI API authentication key. For more details about generating OpenAI API keys, refer to the https://platform.openai.com/account/api-keys.`,
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
model_id: {
display: DisplayType.TEXTBOX,
label: 'Model ID',
order: 2,
required: true,
sensitive: false,
tooltip: 'The name of the model to use for the inference task.',
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
organization_id: {
display: DisplayType.TEXTBOX,
label: 'Organization ID',
order: 4,
required: false,
sensitive: false,
tooltip: 'The unique identifier of your organization.',
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
url: {
display: DisplayType.TEXTBOX,
label: 'URL',
order: 1,
required: true,
sensitive: false,
tooltip:
'The OpenAI API endpoint URL. For more information on the URL, refer to the https://platform.openai.com/docs/api-reference.',
type: FieldType.STRING,
validations: [],
value: null,
ui_restrictions: [],
default_value: 'https://api.openai.com/v1/chat/completions',
depends_on: [],
},
'rate_limit.requests_per_minute': {
display: DisplayType.NUMERIC,
label: 'Rate limit',
order: 5,
required: false,
sensitive: false,
tooltip:
'Default number of requests allowed per minute. For text_embedding is 3000. For completion is 500.',
type: FieldType.INTEGER,
validations: [],
value: null,
ui_restrictions: [],
default_value: null,
depends_on: [],
},
},
},
],
body: mockResult,
});
});
});

0 comments on commit 8b58919

Please sign in to comment.