Skip to content

Commit

Permalink
feat: Add content filtering with convenience functions (#58)
Browse files Browse the repository at this point in the history
* chore:add generated content filtering schema

* chore: Initial API design

* chore:minor API adjustments

* chore:add test

* feat:initial implementation

* chore: fix build failure

* chore: cleanup

* chore: fix build

* feat: switch to adding filter config in OrchestrationCompletionParameters

* chore:simplify implementation

* fix: pipeline

* chore:switch to varargs

* chore: cleanup API proposal

Co-authored-by: Kuhr, Matthias <[email protected]>

* chore: enhance test

* chore: fix build issues

* chore:add additional tests

* chore: cleanup

* chore: minor fixes

* fix: Changes from lint

* chore: address review comments

* chore: address review comments

* fix: Changes from lint

* chore: address review comments

* chore: regenerate client

* chore: remove non-relevant generated files

* chore: cleanup function

* chore: improve JSDoc

* chore: make input_params optional again

* Update packages/gen-ai-hub/src/orchestration/orchestration-client.ts

Co-authored-by: Marika Marszalkowski <[email protected]>

* Update packages/gen-ai-hub/src/orchestration/orchestration-filter-utility.test.ts

Co-authored-by: Marika Marszalkowski <[email protected]>

* fix: Changes from lint

* Update packages/gen-ai-hub/src/orchestration/orchestration-filter-utility.ts

Co-authored-by: Marika Marszalkowski <[email protected]>

* Update packages/gen-ai-hub/src/orchestration/orchestration-filter-utility.ts

Co-authored-by: Marika Marszalkowski <[email protected]>

* Update packages/gen-ai-hub/src/orchestration/orchestration-filter-utility.ts

Co-authored-by: Marika Marszalkowski <[email protected]>

* fix: typo

* Update packages/gen-ai-hub/src/orchestration/orchestration-filter-utility.test.ts

Co-authored-by: Marika Marszalkowski <[email protected]>

* Update packages/gen-ai-hub/src/orchestration/orchestration-filter-utility.test.ts

Co-authored-by: Marika Marszalkowski <[email protected]>

* fix: test

* chore: address review comments

---------

Co-authored-by: Kuhr, Matthias <[email protected]>
Co-authored-by: cloud-sdk-js <[email protected]>
Co-authored-by: Marika Marszalkowski <[email protected]>
  • Loading branch information
4 people authored Aug 13, 2024
1 parent 9541e91 commit 5bf34b5
Show file tree
Hide file tree
Showing 17 changed files with 452 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved.
*
* This is a generated file powered by the SAP Cloud SDK for JavaScript.
*/
import type { AzureContentSafety } from './azure-content-safety.js';
/**
* Representation of the 'AzureContentSafetyFilterConfig' schema.
*/
export type AzureContentSafetyFilterConfig = {
/**
* String represents name of the filter provider.
*/
type: 'azure_content_safety';
/**
* Filter configuration for Azure Content Azure Content Safety.
*/
config?: AzureContentSafety;
} & Record<string, any>;
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type { AzureThreshold } from './azure-threshold.js';
/**
* Representation of the 'AzureContentSafety' schema.
*/
export type AzureContentSafety = {
export interface AzureContentSafety {
Hate?: AzureThreshold;
SelfHarm?: AzureThreshold;
Sexual?: AzureThreshold;
Violence?: AzureThreshold;
} & Record<string, any>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import type { ChatMessages } from './chat-messages.js';
*/
export type CompletionPostRequest = {
orchestration_config: OrchestrationConfig;
input_params: Record<string, InputParamsEntry>;
input_params?: Record<string, InputParamsEntry>;
messages_history?: ChatMessages;
} & Record<string, any>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved.
*
* This is a generated file powered by the SAP Cloud SDK for JavaScript.
*/
import type { AzureContentSafetyFilterConfig } from './azure-content-safety-filter-config.js';
/**
* Representation of the 'FilterConfig' schema.
*/
export type FilterConfig = AzureContentSafetyFilterConfig;
16 changes: 0 additions & 16 deletions packages/gen-ai-hub/src/orchestration/client/api/schema/filter.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
*
* This is a generated file powered by the SAP Cloud SDK for JavaScript.
*/
import type { Filter } from './filter.js';
import type { FilterConfig } from './filter-config.js';
/**
* Representation of the 'FilteringConfig' schema.
*/
export type FilteringConfig = {
/**
* Filters to be used.
* Configuration for content filtering services that should be used for the given filtering step (input filtering or output filtering).
* Min Items: 1.
*/
filters: Filter[];
filters: FilterConfig[];
} & Record<string, any>;
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export * from './token-usage.js';
export * from './templating-module-config.js';
export * from './filtering-module-config.js';
export * from './filtering-config.js';
export * from './filter.js';
export * from './filter-config.js';
export * from './azure-content-safety-filter-config.js';
export * from './azure-content-safety.js';
export * from './provider-type.js';
export * from './azure-threshold.js';
export * from './masking-module-config.js';
export * from './masking-config.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type LLMModuleConfig = {
model_params: Record<string, any>;
/**
* Version of the model to use.
* @example "2024-08-08T14:36:04.617Z"
* @example "2024-08-12T10:11:17.042Z"
* Default: "latest".
*/
model_version?: string;
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions packages/gen-ai-hub/src/orchestration/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './client/api/index.js';
export * from './orchestration-client.js';
export * from './orchestration-types.js';
export * from './orchestration-filter-utility.js';
107 changes: 106 additions & 1 deletion packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { mockInference, parseMockResponse } from '../test-util/mock-http.js';
import { BaseLlmParametersWithDeploymentId } from '../core/index.js';
import { CompletionPostResponse } from './client/api/index.js';
import { GenAiHubCompletionParameters } from './orchestration-types.js';
import { azureContentFilter } from './orchestration-filter-utility.js';
jest.unstable_mockModule('../core/context.js', () => ({
getAiCoreDestination: jest.fn(() =>
Promise.resolve(mockGetAiCoreDestination())
Expand Down Expand Up @@ -34,7 +35,7 @@ describe('GenAiHubClient', () => {
jest.restoreAllMocks();
});

it('calls chatCompletion with minimum configuration and parses response', async () => {
it('calls chatCompletion with minimum configuration', async () => {
const request: GenAiHubCompletionParameters = {
deploymentConfiguration,
llmConfig: {
Expand Down Expand Up @@ -70,6 +71,110 @@ describe('GenAiHubClient', () => {
expect(client.chatCompletion(request)).resolves.toEqual(mockResponse);
});

it('calls chatCompletion with filter configuration supplied using convenience function', async () => {
const request: GenAiHubCompletionParameters = {
deploymentConfiguration,
llmConfig: {
model_name: 'gpt-35-turbo-16k',
model_params: { max_tokens: 50, temperature: 0.1 }
},
prompt: {
template: [
{ role: 'user', content: 'Create {number} paraphrases of {phrase}' }
],
template_params: { phrase: 'I hate you.', number: 3 }
},
filterConfig: {
input: azureContentFilter({ Hate: 4, SelfHarm: 2 }),
output: azureContentFilter({ Sexual: 0, Violence: 4 })
}
};
const mockResponse = parseMockResponse<CompletionPostResponse>(
'orchestration',
'genaihub-chat-completion-filter-config.json'
);

mockInference(
{
data: {
deploymentConfiguration,
...constructCompletionPostRequest(request)
}
},
{
data: mockResponse,
status: 200
},
destination,
{
url: 'completion'
}
);
expect(client.chatCompletion(request)).resolves.toEqual(mockResponse);
});

it('calls chatCompletion with filtering configuration', async () => {
const request: GenAiHubCompletionParameters = {
deploymentConfiguration,
llmConfig: {
model_name: 'gpt-35-turbo-16k',
model_params: { max_tokens: 50, temperature: 0.1 }
},
prompt: {
template: [
{ role: 'user', content: 'Create {number} paraphrases of {phrase}' }
],
template_params: { phrase: 'I hate you.', number: 3 }
},
filterConfig: {
input: {
filters: [
{
type: 'azure_content_safety',
config: {
Hate: 4,
SelfHarm: 2
}
}
]
},
output: {
filters: [
{
type: 'azure_content_safety',
config: {
Sexual: 0,
Violence: 4
}
}
]
}
}
};
const mockResponse = parseMockResponse<CompletionPostResponse>(
'orchestration',
'genaihub-chat-completion-filter-config.json'
);

mockInference(
{
data: {
deploymentConfiguration,
...constructCompletionPostRequest(request)
}
},
{
data: mockResponse,
status: 200
},
destination,
{
url: 'completion'
}
);
expect(client.chatCompletion(request)).resolves.toEqual(mockResponse);
});

it('sends message history together with templating config', async () => {
const request: GenAiHubCompletionParameters = {
deploymentConfiguration,
Expand Down
26 changes: 14 additions & 12 deletions packages/gen-ai-hub/src/orchestration/orchestration-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ export class GenAiHubClient {
export function constructCompletionPostRequest(
input: GenAiHubCompletionParameters
): CompletionPostRequest {
return {
// TODO: Properly implement input params
input_params: {},
const result: CompletionPostRequest = {
orchestration_config: {
module_configurations: {
templating_module_config: {
template: input.prompt.template
},
llm_module_config: input.llmConfig
},
...(input.prompt.template_params && {
input_params: input.prompt.template_params
}),
...(input.prompt.messages_history && {
messages_history: input.prompt.messages_history
})
}
llm_module_config: input.llmConfig,
...(Object.keys(input?.filterConfig || {}).length && {
filtering_module_config: input.filterConfig
})
}
},
...(input.prompt.template_params && {
input_params: input.prompt.template_params
}),
...(input.prompt.messages_history && {
messages_history: input.prompt.messages_history
})
};
return result;
}
Loading

0 comments on commit 5bf34b5

Please sign in to comment.