Skip to content

Commit

Permalink
chore: Make API more consistent (#136)
Browse files Browse the repository at this point in the history
* Remove unused base config

* Shorten type names to “Config”

* Remove old deploymentconfig from type tests

* simplify orchestration properties

* add todo

* fix: Changes from lint

* fix tests

* add type

* fix more tests

* fix type tests

* typo

* remove comment to not make check fail

* more fixes

---------

Co-authored-by: cloud-sdk-js <[email protected]>
  • Loading branch information
marikaner and cloud-sdk-js authored Sep 17, 2024
1 parent 4068a88 commit 8c28ef9
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 140 deletions.
6 changes: 3 additions & 3 deletions packages/ai-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from './client/AI_CORE_API/index.js';

export type {
ModelDeployment,
DeploymentIdConfiguration,
ModelConfiguration,
ResourceGroupConfiguration
DeploymentIdConfig,
ModelConfig,
ResourceGroupConfig
} from './utils/index.js';
19 changes: 8 additions & 11 deletions packages/ai-api/src/utils/deployment-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { extractModel, type FoundationModel } from './model.js';
* The model deployment configuration when using a model.
* @typeParam ModelNameT - String literal type representing the name of the model.
*/
export interface ModelConfiguration<ModelNameT = string> {
export interface ModelConfig<ModelNameT = string> {
/**
* The name of the model.
*/
Expand All @@ -23,7 +23,7 @@ export interface ModelConfiguration<ModelNameT = string> {
/**
* The deployment configuration when using a deployment ID.
*/
export interface DeploymentIdConfiguration {
export interface DeploymentIdConfig {
/**
* The deployment ID.
*/
Expand All @@ -33,7 +33,7 @@ export interface DeploymentIdConfiguration {
/**
* The deployment configuration when using a resource group.
*/
export interface ResourceGroupConfiguration {
export interface ResourceGroupConfig {
/**
* The resource group of the deployment.
*/
Expand All @@ -46,17 +46,16 @@ export interface ResourceGroupConfiguration {
*/
export type ModelDeployment<ModelNameT = string> =
| ModelNameT
| ((ModelConfiguration<ModelNameT> | DeploymentIdConfiguration) &
ResourceGroupConfiguration);
| ((ModelConfig<ModelNameT> | DeploymentIdConfig) & ResourceGroupConfig);

/**
* Type guard to check if the given deployment configuration is a deployment ID configuration.
* @param modelDeployment - Configuration to check.
* @returns `true` if the configuration is a deployment ID configuration, `false` otherwise.
*/
function isDeploymentIdConfiguration(
function isDeploymentIdConfig(
modelDeployment: ModelDeployment
): modelDeployment is DeploymentIdConfiguration {
): modelDeployment is DeploymentIdConfig {
return (
typeof modelDeployment === 'object' && 'deploymentId' in modelDeployment
);
Expand Down Expand Up @@ -156,7 +155,7 @@ export async function getDeploymentId(
modelDeployment: ModelDeployment,
executableId: string
): Promise<string> {
if (isDeploymentIdConfiguration(modelDeployment)) {
if (isDeploymentIdConfig(modelDeployment)) {
return modelDeployment.deploymentId;
}

Expand All @@ -173,9 +172,7 @@ export async function getDeploymentId(
});
}

function translateToFoundationModel(
modelConfig: ModelConfiguration
): FoundationModel {
function translateToFoundationModel(modelConfig: ModelConfig): FoundationModel {
if (typeof modelConfig === 'string') {
return { name: modelConfig };
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ describe('http-client', () => {
beforeEach(() => {
mockClientCredentialsGrantCall();
});

afterEach(() => {
nock.cleanAll();
});

it('should execute a request to the AI Core service', async () => {
const mockPrompt = { prompt: 'some test prompt' };
const mockPromptResponse = { completion: 'some test completion' };
Expand Down
19 changes: 0 additions & 19 deletions packages/core/src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ import {
} from '@sap-cloud-sdk/http-client';
import { getAiCoreDestination } from './context.js';

/**
* Input parameters with Deployment ID.
*/
export interface BaseLlmParametersWithDeploymentId {
/**
* Deployment ID of the model to use.
*/
deploymentId: string;
}
/**
* Base LLM Input Parameters.
*/
export interface BaseLlmParameters {
/**
* Deployment configuration.
*/
deploymentConfiguration: BaseLlmParametersWithDeploymentId;
}

/**
* The type for parameters in custom request configuration.
*/
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export type {
BaseLlmParametersWithDeploymentId,
BaseLlmParameters,
CustomRequestConfig,
EndpointOptions
} from './http-client.js';
export type { CustomRequestConfig, EndpointOptions } from './http-client.js';
export { executeRequest } from './http-client.js';
export { getAiCoreDestination } from './context.js';
export { OpenApiRequestBuilder } from './openapi-request-builder.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestration/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type {

export type {
OrchestrationModuleConfig,
LlmConfig,
LlmModuleConfig,
Prompt
} from './orchestration-types.js';

Expand Down
29 changes: 15 additions & 14 deletions packages/orchestration/src/orchestration-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './orchestration-client.js';
import { azureContentFilter } from './orchestration-filter-utility.js';
import { OrchestrationResponse } from './orchestration-response.js';
import { OrchestrationModuleConfig } from './orchestration-types.js';

describe('orchestration service client', () => {
beforeEach(() => {
Expand All @@ -24,12 +25,12 @@ describe('orchestration service client', () => {
});

it('calls chatCompletion with minimum configuration', async () => {
const config = {
llmConfig: {
const config: OrchestrationModuleConfig = {
llm: {
model_name: 'gpt-35-turbo-16k',
model_params: { max_tokens: 50, temperature: 0.1 }
},
templatingConfig: {
templating: {
template: [{ role: 'user', content: 'Hello!' }]
}
};
Expand Down Expand Up @@ -61,20 +62,20 @@ describe('orchestration service client', () => {
});

it('calls chatCompletion with filter configuration supplied using convenience function', async () => {
const config = {
llmConfig: {
const config: OrchestrationModuleConfig = {
llm: {
model_name: 'gpt-35-turbo-16k',
model_params: { max_tokens: 50, temperature: 0.1 }
},
templatingConfig: {
templating: {
template: [
{
role: 'user',
content: 'Create {{?number}} paraphrases of {{?phrase}}'
}
]
},
filterConfig: {
filtering: {
input: azureContentFilter({ Hate: 4, SelfHarm: 2 }),
output: azureContentFilter({ Sexual: 0, Violence: 4 })
}
Expand Down Expand Up @@ -106,20 +107,20 @@ describe('orchestration service client', () => {
});

it('calls chatCompletion with filtering configuration', async () => {
const config = {
llmConfig: {
const config: OrchestrationModuleConfig = {
llm: {
model_name: 'gpt-35-turbo-16k',
model_params: { max_tokens: 50, temperature: 0.1 }
},
templatingConfig: {
templating: {
template: [
{
role: 'user',
content: 'Create {{?number}} paraphrases of {{?phrase}}'
}
]
},
filterConfig: {
filtering: {
input: {
filters: [
{
Expand Down Expand Up @@ -169,12 +170,12 @@ describe('orchestration service client', () => {
});

it('sends message history together with templating config', async () => {
const config = {
llmConfig: {
const config: OrchestrationModuleConfig = {
llm: {
model_name: 'gpt-35-turbo-16k',
model_params: { max_tokens: 50, temperature: 0.1 }
},
templatingConfig: {
templating: {
template: [{ role: 'user', content: "What's my name?" }],
messages_history: [
{
Expand Down
12 changes: 6 additions & 6 deletions packages/orchestration/src/orchestration-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { executeRequest, CustomRequestConfig } from '@sap-ai-sdk/core';
import {
resolveDeploymentId,
ResourceGroupConfiguration
ResourceGroupConfig
} from '@sap-ai-sdk/ai-api/internal.js';
import { CompletionPostRequest } from './client/api/schema/index.js';
import { OrchestrationModuleConfig, Prompt } from './orchestration-types.js';
Expand All @@ -18,7 +18,7 @@ export class OrchestrationClient {
*/
constructor(
private config: OrchestrationModuleConfig,
private deploymentConfig?: ResourceGroupConfiguration
private deploymentConfig?: ResourceGroupConfig
) {}

/**
Expand Down Expand Up @@ -60,11 +60,11 @@ export function constructCompletionPostRequest(
orchestration_config: {
module_configurations: {
templating_module_config: {
template: config.templatingConfig.template
template: config.templating.template
},
llm_module_config: config.llmConfig,
...(Object.keys(config?.filterConfig || {}).length && {
filtering_module_config: config.filterConfig
llm_module_config: config.llm,
...(Object.keys(config?.filtering || {}).length && {
filtering_module_config: config.filtering
})
}
},
Expand Down
Loading

0 comments on commit 8c28ef9

Please sign in to comment.