Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Apply schema check for Azure OpenAI client response #130

Merged
merged 19 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
],
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",
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
"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/http-client": "^3.20.0",
"@sap-cloud-sdk/openapi": "^3.20.0",
"@sap-cloud-sdk/util": "^3.20.0",
"@sap-cloud-sdk/openapi": "^3.20.0"
"zod": "^3.23.8"
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@sap-cloud-sdk/openapi-generator": "^3.20.0",
"typescript": "^5.5.4",
"nock": "^13.5.5"
"nock": "^13.5.5",
"ts-to-zod": "^3.13.0",
"typescript": "^5.5.4"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type ModelDeployment
} from '@sap-ai-sdk/ai-api/internal.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);
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -7,10 +7,8 @@ import {
getDeploymentId,
type ModelDeployment
} from '@sap-ai-sdk/ai-api/internal.js';
import type {
AzureOpenAiEmbeddingParameters,
AzureOpenAiEmbeddingOutput
} from './azure-openai-types.js';
import { AzureOpenAiEmbeddingResponse } from './azure-openai-embedding-response.js';
import type { AzureOpenAiEmbeddingParameters } from './azure-openai-types.js';

const apiVersion = '2024-02-01';

Expand All @@ -22,6 +20,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 @@ -35,7 +34,7 @@ export class AzureOpenAiEmbeddingClient {
async run(
data: AzureOpenAiEmbeddingParameters,
requestConfig?: CustomRequestConfig
): Promise<AzureOpenAiEmbeddingOutput> {
): Promise<AzureOpenAiEmbeddingResponse> {
const deploymentId = await getDeploymentId(
this.modelDeployment,
'azure-openai'
Expand All @@ -45,6 +44,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