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 11 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.2",
"@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
17 changes: 10 additions & 7 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",
"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/openai/openai-types.ts src/openai/openai-types-schema.ts && sed -i 's|export const|\\/**\\n * @internal\\n *\\/\\nexport const|' src/openai/openai-types-schema.ts"
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
},
"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"
}
}
3 changes: 2 additions & 1 deletion packages/foundation-models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export type {
export {
OpenAiChatClient,
OpenAiEmbeddingClient,
OpenAiChatCompletionResponse
OpenAiChatCompletionResponse,
OpenAiEmbeddingResponse
} from './openai/index.js';
3 changes: 2 additions & 1 deletion packages/foundation-models/src/openai/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './openai-types.js';
export * from './openai-chat-client.js';
export * from './openai-embedding-client.js';
export * from './openai-response.js';
export * from './openai-chat-completion-response.js';
export * from './openai-embedding-response.js';
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 { OpenAiChatCompletionParameters } from './openai-types.js';
import { OpenAiChatCompletionResponse } from './openai-response.js';
import { OpenAiChatCompletionResponse } from './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,21 @@
import { parseMockResponse } from '../../../../test-util/mock-http.js';
import { OpenAiChatCompletionResponse } from './openai-chat-completion-response.js';
import { OpenAiChatCompletionOutput } from './openai-types.js';

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

it('should return the completion response', () => {
expect(response.data).toStrictEqual(mockResponse);
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HttpResponse } from '@sap-cloud-sdk/http-client';
import { createLogger } from '@sap-cloud-sdk/util';
import { OpenAiChatCompletionOutput, OpenAiUsage } from './openai-types.js';
import { openAiChatCompletionOutputSchema } from './openai-types-schema.js';

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

/**
Expand All @@ -15,8 +16,9 @@ export class OpenAiChatCompletionResponse {
* The chat completion response.
*/
public readonly data: OpenAiChatCompletionOutput;

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('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
11 changes: 5 additions & 6 deletions packages/foundation-models/src/openai/openai-embedding-client.ts
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 {
OpenAiEmbeddingParameters,
OpenAiEmbeddingOutput
} from './openai-types.js';
import type { OpenAiEmbeddingParameters } from './openai-types.js';
import { OpenAiEmbeddingResponse } from './openai-embedding-response.js';

const apiVersion = '2024-02-01';

Expand All @@ -22,6 +20,7 @@ export class OpenAiEmbeddingClient {
* Creates an instance of the 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 OpenAiEmbeddingClient {
async run(
data: OpenAiEmbeddingParameters,
requestConfig?: CustomRequestConfig
): Promise<OpenAiEmbeddingOutput> {
): Promise<OpenAiEmbeddingResponse> {
const deploymentId = await getDeploymentId(
this.modelDeployment,
'azure-openai'
Expand All @@ -45,6 +44,6 @@ export class OpenAiEmbeddingClient {
data,
requestConfig
);
return response.data;
return new OpenAiEmbeddingResponse(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parseMockResponse } from '../../../../test-util/mock-http.js';
import { OpenAiChatCompletionOutput } from './openai-types.js';
import { OpenAiEmbeddingResponse } from './openai-embedding-response.js';

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

it('should return the embedding response', () => {
expect(response.data).toStrictEqual(mockResponse);
});
});
38 changes: 38 additions & 0 deletions packages/foundation-models/src/openai/openai-embedding-response.ts
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 { OpenAiEmbeddingOutput } from './openai-types.js';
import { openAiEmbeddingOutputSchema } from './openai-types-schema.js';

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

/**
* OpenAI embedding response.
*/
export class OpenAiEmbeddingResponse {
/**
* The embedding response.
*/
public readonly data: OpenAiEmbeddingOutput;
constructor(public readonly rawResponse: HttpResponse) {
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
this.data = openAiEmbeddingOutputSchema.parse(rawResponse.data);
}

/**
* Parses the Open AI 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.`);
}
}
}
47 changes: 0 additions & 47 deletions packages/foundation-models/src/openai/openai-response.test.ts

This file was deleted.

Loading