Skip to content

Commit

Permalink
chore:add test
Browse files Browse the repository at this point in the history
  • Loading branch information
KavithaSiva committed Jul 26, 2024
1 parent 11bc217 commit fdd7d5f
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 21 deletions.
59 changes: 48 additions & 11 deletions packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,67 @@ describe('GenAiHubClient', () => {
},
prompt: {
template: [{ role: 'user', content: 'Hello!' }]
}
};

const mockResponse = parseMockResponse<CompletionPostResponse>(
'orchestration',
'genaihub-chat-completion-success-response.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 and parses response', 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: {
AzureContentSafety: {
azureContentSafety: {
Hate: 0,
SelfHarm: 2
}
},
output: [
{
AzureContentSafety: {
Hate: 0,
SelfHarm: 2,
Sexual: 4,
Violence: 6
}
output: {
azureContentSafety: {
Hate: 0,
SelfHarm: 2,
Sexual: 4,
Violence: 6
}
]
}
}
};

const mockResponse = parseMockResponse<CompletionPostResponse>(
'orchestration',
'genaihub-chat-completion-success-response.json'
'genaihub-chat-completion-filter-config.json'
);

mockInference(
Expand Down
32 changes: 31 additions & 1 deletion packages/gen-ai-hub/src/orchestration/orchestration-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { executeRequest, CustomRequestConfig } from '../core/index.js';
import { CompletionPostRequest } from './api/schema/index.js';
import { CompletionPostRequest, Filter } from './api/schema/index.js';
import {
FilterConfig,
FilterServiceProvider,
filterServiceProviders,
GenAiHubCompletionParameters,
GenAiHubCompletionResponse
} from './orchestration-types.js';
Expand Down Expand Up @@ -47,6 +50,10 @@ export function constructCompletionPostRequest(
},
llm_module_config: input.llmConfig
},
...(input.filterConfig &&
input.filterConfig?.input &&
input.filterConfig?.output &&
buildFilterConfig(input.filterConfig)),
...(input.prompt.template_params && {
input_params: input.prompt.template_params
}),
Expand All @@ -56,3 +63,26 @@ export function constructCompletionPostRequest(
}
};
}

function buildFilterConfig(config: FilterConfig) {
const inputFilters = createFilters(config?.input);
const outputFilters = createFilters(config?.output);
if (inputFilters.length === 0 && outputFilters.length === 0) {
return;
}
return {
filtering_module_config: {
...(inputFilters.length > 0 && { input: { filters: inputFilters } }),
...(outputFilters.length > 0 && { output: { filters: outputFilters } })
}
};
}

function createFilters(config: FilterServiceProvider | undefined): Filter[] {
return config
? Object.entries(config).map(([key, value]) => ({
type: filterServiceProviders[key],
config: value
}))
: [];
}
25 changes: 16 additions & 9 deletions packages/gen-ai-hub/src/orchestration/orchestration-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
ChatMessages,
CompletionPostResponse,
InputParamsEntry,
LLMModuleConfig
LLMModuleConfig,
ProviderType
} from './api/index.js';

/**
Expand Down Expand Up @@ -47,21 +48,27 @@ export interface FilterConfig {
/**
* Input configuration for filtering provider.
*/
input?: FilterServiceProvider | FilterServiceProvider[];
input?: FilterServiceProvider;
/**
* Output configuration for filtering provider.
*/
output?: FilterServiceProvider | FilterServiceProvider[];
output?: FilterServiceProvider;
}
/**
* Wrapper object to configure the filter service provider.
*/
export type FilterServiceProvider =
| { AzureContentSafety: AzureContentSafety; SomeOtherServiceProvider?: never }
| {
SomeOtherServiceProvider: AzureContentSafety;
AzureContentSafety?: never;
};
export interface FilterServiceProvider {
/**
* Azure content safery filter service provider.
*/
azureContentSafety?: AzureContentSafety;
}
/**
* Map of filter service providers.
*/
export const filterServiceProviders: { [key: string]: ProviderType } = {
azureContentSafety: 'azure_content_safety'
};
/**
* Wrapper object to encompass Orchestration options.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"request_id": "request-id",
"module_results": {
"templating": [
{
"role": "user",
"content": "Create {number} paraphrases of {phrase}"
}
],
"input_filtering": {
"message": "Input filter passed successfully.",
"data": {
"original_service_response": {
"Hate": 0,
"SelfHarm": 0
}
}
},
"llm": {
"id": "chatcmpl-request-id",
"object": "chat.completion",
"created": 1722006045,
"model": "gpt-35-turbo-16k",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I cannot generate a specific number of paraphrases without knowing the phrase you would like me to paraphrase. Could you please provide the phrase you would like me to work with?"
},
"finish_reason": "stop"
}
],
"usage": {
"completion_tokens": 40,
"prompt_tokens": 17,
"total_tokens": 57
}
},
"output_filtering": {
"message": "Output filter passed successfully.",
"data": {
"original_service_response": {
"Hate": 0,
"SelfHarm": 0,
"Sexual": 0,
"Violence": 0
}
}
}
},
"orchestration_result": {
"id": "chatcmpl-request-id",
"object": "chat.completion",
"created": 1722006045,
"model": "gpt-35-turbo-16k",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I cannot generate a specific number of paraphrases without knowing the phrase you would like me to paraphrase. Could you please provide the phrase you would like me to work with?"
},
"finish_reason": "stop"
}
],
"usage": {
"completion_tokens": 40,
"prompt_tokens": 17,
"total_tokens": 57
}
}
}

0 comments on commit fdd7d5f

Please sign in to comment.