Skip to content

Commit

Permalink
feat: Apply schema check for Azure OpenAI client response (#130)
Browse files Browse the repository at this point in the history
* feat: use zod and ts-to-zod for openai response schema check

* fix: schema issues

* feat: add schema parse to openai responses

* fix: Changes from lint

* fix: create getEmbedding method

* fix: type test and exports

* refactor: remove parseHttpResponse from core

* feat: auto generate schema during compilation

* fix: auto add @internal to generated zod schema

* feat: put check in tests

* fix: Changes from lint

* fix: remove reduntant files and auto generate zod

* fix: move zod to dev deps

* fix: lock file

---------

Co-authored-by: cloud-sdk-js <[email protected]>
  • Loading branch information
ZhongpinWang and cloud-sdk-js authored Sep 17, 2024
1 parent 8c28ef9 commit 1f4617d
Show file tree
Hide file tree
Showing 19 changed files with 832 additions and 81 deletions.
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export default [
{
files: [
'**/test-util/**/*.ts',
'**/packages/orchestration/src/client/**/*'
'**/packages/orchestration/src/client/**/*',
'packages/foundation-models/src/openai/openai-types-schema.ts'
],
rules: {
'jsdoc/require-jsdoc': 'off'
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
"devDependencies": {
"@changesets/cli": "^2.27.8",
"@jest/globals": "^29.5.12",
"@sap-cloud-sdk/eslint-config": "^3.20.0",
"@sap-cloud-sdk/connectivity": "^3.20.0",
"@sap-cloud-sdk/generator-common": "^3.20.0",
"@sap-cloud-sdk/http-client": "^3.20.0",
"@sap-cloud-sdk/util": "^3.20.0",
"@sap-ai-sdk/ai-api": "workspace:^",
"@sap-ai-sdk/core": "workspace:^",
"@sap-ai-sdk/foundation-models": "workspace:^",
"@sap-ai-sdk/orchestration": "workspace:^",
"@sap-cloud-sdk/connectivity": "^3.20.0",
"@sap-cloud-sdk/eslint-config": "^3.20.0",
"@sap-cloud-sdk/generator-common": "^3.20.0",
"@sap-cloud-sdk/http-client": "^3.20.0",
"@sap-cloud-sdk/util": "^3.20.0",
"@types/jest": "^29.5.13",
"@types/jsonwebtoken": "^9.0.7",
"@types/mock-fs": "^4.13.4",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"check:public-api": "node --loader ts-node/esm ../../scripts/check-public-api-cli.ts"
},
"dependencies": {
"@sap-cloud-sdk/http-client": "^3.20.0",
"@sap-cloud-sdk/connectivity": "^3.20.0",
"@sap-cloud-sdk/util": "^3.20.0",
"@sap-cloud-sdk/openapi": "^3.20.0"
"@sap-cloud-sdk/http-client": "^3.20.0",
"@sap-cloud-sdk/openapi": "^3.20.0",
"@sap-cloud-sdk/util": "^3.20.0"
},
"devDependencies": {
"typescript": "^5.5.4"
Expand Down
19 changes: 11 additions & 8 deletions packages/foundation-models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@
"internal.d.ts"
],
"scripts": {
"compile": "tsc",
"compile": "pnpm run generate-zod && tsc",
"compile:cjs": "tsc -p tsconfig.cjs.json",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": "pnpm run generate-zod && NODE_OPTIONS=--experimental-vm-modules jest",
"lint": "eslint \"**/*.ts\" && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c",
"lint:fix": "eslint \"**/*.ts\" --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error",
"check:public-api": "node --loader ts-node/esm ../../scripts/check-public-api-cli.ts"
"check:public-api": "node --loader ts-node/esm ../../scripts/check-public-api-cli.ts",
"generate-zod": "ts-to-zod src/azure-openai/azure-openai-types.ts src/azure-openai/azure-openai-types-schema.ts && sed -i 's|export const|\\/**\\n * @internal\\n *\\/\\nexport const|' src/azure-openai/azure-openai-types-schema.ts"
},
"dependencies": {
"@sap-ai-sdk/core": "workspace:^",
"@sap-ai-sdk/ai-api": "workspace:^",
"@sap-cloud-sdk/http-client": "^3.20.0",
"@sap-ai-sdk/core": "workspace:^",
"@sap-cloud-sdk/connectivity": "^3.20.0",
"@sap-cloud-sdk/util": "^3.20.0",
"@sap-cloud-sdk/openapi": "^3.20.0"
"@sap-cloud-sdk/http-client": "^3.20.0",
"@sap-cloud-sdk/openapi": "^3.20.0",
"@sap-cloud-sdk/util": "^3.20.0"
},
"devDependencies": {
"@sap-cloud-sdk/openapi-generator": "^3.20.0",
"nock": "^13.5.5",
"ts-to-zod": "^3.13.0",
"typescript": "^5.5.4",
"nock": "^13.5.5"
"zod": "^3.23.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@sap-ai-sdk/ai-api/internal.js';
import type { AzureOpenAiChatModel } from './model-types.js';
import type { AzureOpenAiChatCompletionParameters } from './azure-openai-types.js';
import { AzureOpenAiChatCompletionResponse } from './azure-openai-response.js';
import { AzureOpenAiChatCompletionResponse } from './azure-openai-chat-completion-response.js';

const apiVersion = '2024-02-01';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { parseMockResponse } from '../../../../test-util/mock-http.js';
import { AzureOpenAiChatCompletionResponse } from './azure-openai-chat-completion-response.js';
import { AzureOpenAiChatCompletionOutput } from './azure-openai-types.js';
import { azureOpenAiChatCompletionOutputSchema } from './azure-openai-types-schema.js';

describe('OpenAI chat completion response', () => {
const mockResponse = parseMockResponse<AzureOpenAiChatCompletionOutput>(
'foundation-models',
'azure-openai-chat-completion-success-response.json'
);
const rawResponse = {
data: mockResponse,
status: 200,
headers: {},
request: {}
};
const response = new AzureOpenAiChatCompletionResponse(rawResponse);

it('should return the completion response', () => {
const data = azureOpenAiChatCompletionOutputSchema.parse(response.data);
expect(data).toStrictEqual(mockResponse);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

const logger = createLogger({
package: 'gen-ai-hub',
messageContext: 'azure-openai-response'
messageContext: 'azure-openai-chat-completion-response'
});

/**
Expand All @@ -18,6 +18,7 @@ export class AzureOpenAiChatCompletionResponse {
* The chat completion response.
*/
public readonly data: AzureOpenAiChatCompletionOutput;

constructor(public readonly rawResponse: HttpResponse) {
this.data = rawResponse.data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Azure OpenAI embedding client', () => {
embeddingsEndpoint
);
const response = await client.run(prompt);
expect(response).toEqual(mockResponse);
expect(response.data).toEqual(mockResponse);
});

it('throws on bad request', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import {
getDeploymentId,
type ModelDeployment
} from '@sap-ai-sdk/ai-api/internal.js';
import { AzureOpenAiEmbeddingResponse } from './azure-openai-embedding-response.js';
import type { AzureOpenAiEmbeddingParameters } from './azure-openai-types.js';
import type { AzureOpenAiEmbeddingModel } from './model-types.js';
import type {
AzureOpenAiEmbeddingParameters,
AzureOpenAiEmbeddingOutput
} from './azure-openai-types.js';

const apiVersion = '2024-02-01';

Expand All @@ -19,6 +17,7 @@ export class AzureOpenAiEmbeddingClient {
* Creates an instance of the Azure OpenAI embedding client.
* @param modelDeployment - This configuration is used to retrieve a deployment. Depending on the configuration use either the given deployment ID or the model name to retrieve matching deployments. If model and deployment ID are given, the model is verified against the deployment.
*/

constructor(
private modelDeployment: ModelDeployment<AzureOpenAiEmbeddingModel>
) {}
Expand All @@ -32,7 +31,7 @@ export class AzureOpenAiEmbeddingClient {
async run(
data: AzureOpenAiEmbeddingParameters,
requestConfig?: CustomRequestConfig
): Promise<AzureOpenAiEmbeddingOutput> {
): Promise<AzureOpenAiEmbeddingResponse> {
const deploymentId = await getDeploymentId(
this.modelDeployment,
'azure-openai'
Expand All @@ -42,6 +41,6 @@ export class AzureOpenAiEmbeddingClient {
data,
requestConfig
);
return response.data;
return new AzureOpenAiEmbeddingResponse(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { parseMockResponse } from '../../../../test-util/mock-http.js';
import { AzureOpenAiEmbeddingResponse } from './azure-openai-embedding-response.js';
import { azureOpenAiEmbeddingOutputSchema } from './azure-openai-types-schema.js';

describe('Azure OpenAI embedding response', () => {
const mockResponse = parseMockResponse<AzureOpenAiEmbeddingResponse>(
'foundation-models',
'azure-openai-embeddings-success-response.json'
);
const rawResponse = {
data: mockResponse,
status: 200,
headers: {},
request: {}
};
const response = new AzureOpenAiEmbeddingResponse(rawResponse);

it('should return the embedding response', () => {
const data = azureOpenAiEmbeddingOutputSchema.parse(response.data);
expect(data).toStrictEqual(mockResponse);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { HttpResponse } from '@sap-cloud-sdk/http-client';
import { createLogger } from '@sap-cloud-sdk/util';
import { AzureOpenAiEmbeddingOutput } from './azure-openai-types.js';

const logger = createLogger({
package: 'gen-ai-hub',
messageContext: 'azure-openai-embedding-response'
});

/**
* Azure OpenAI embedding response.
*/
export class AzureOpenAiEmbeddingResponse {
/**
* The embedding response.
*/
public readonly data: AzureOpenAiEmbeddingOutput;

constructor(public readonly rawResponse: HttpResponse) {
this.data = rawResponse.data;
}

/**
* Parses the Azure OpenAI response and returns the embedding.
* @param dataIndex - The index of the data to parse.
* @returns The message content.
*/
getEmbedding(dataIndex = 0): number[] | undefined {
this.logInvalidDataIndex(dataIndex);
return this.data.data[0]?.embedding;
}

private logInvalidDataIndex(dataIndex: number): void {
if (dataIndex < 0 || dataIndex >= this.data.data.length) {
logger.error(`Data index ${dataIndex} is out of bounds.`);
}
}
}

This file was deleted.

Loading

0 comments on commit 1f4617d

Please sign in to comment.