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

chore: Update Azure OpenAI types #141

Merged
merged 17 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@sap-cloud-sdk/generator-common": "^3.21.0",
"@sap-cloud-sdk/http-client": "^3.21.0",
"@sap-cloud-sdk/util": "^3.21.0",
"@sap-cloud-sdk/openapi-generator": "^3.21.0",
"@types/jest": "^29.5.13",
"@types/jsonwebtoken": "^9.0.7",
"@types/mock-fs": "^4.13.4",
Expand Down
3 changes: 1 addition & 2 deletions packages/ai-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@sap-ai-sdk/core": "workspace:^"
},
"devDependencies": {
"typescript": "^5.6.2",
"@sap-cloud-sdk/openapi-generator": "^3.21.0"
"typescript": "^5.6.2"
}
}
9 changes: 5 additions & 4 deletions packages/foundation-models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
"internal.d.ts"
],
"scripts": {
"compile": "pnpm run generate-zod && tsc",
"compile": "tsc",
"compile:cjs": "tsc -p tsconfig.cjs.json",
"test": "pnpm run generate-zod && NODE_OPTIONS=--experimental-vm-modules jest",
"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",
"generate-zod": "ts-to-zod src/azure-openai/azure-openai-types.ts src/azure-openai/azure-openai-types-schema.ts",
"generate:azure-openai": "openapi-generator --generateESM --clearOutputDir -i ./src/azure-openai/spec/inference.yaml -o ./src/azure-openai/client --schemaPrefix AzureOpenAi",
"postgenerate:azure-openai": "rm ./src/azure-openai/client/inference/*.ts && pnpm lint:fix",
"generate-zod": "ts-to-zod src/azure-openai/client/inference/schema/*.ts src/azure-openai/azure-openai-types-schema.ts",
"postgenerate-zod": "sed -i'' -e \"s|export const|\\/**\\n * @internal\\n *\\/\\nexport const|\" src/azure-openai/azure-openai-types-schema.ts"
},
"dependencies": {
Expand All @@ -38,7 +40,6 @@
"@sap-cloud-sdk/util": "^3.21.0"
},
"devDependencies": {
"@sap-cloud-sdk/openapi-generator": "^3.21.0",
"nock": "^13.5.5",
"ts-to-zod": "^3.13.0",
"typescript": "^5.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
mockInference,
parseMockResponse
} from '../../../../test-util/mock-http.js';
import {
AzureOpenAiChatCompletionOutput,
AzureOpenAiChatMessage
} from './azure-openai-types.js';
import { AzureOpenAiChatClient } from './azure-openai-chat-client.js';
import type {
AzureOpenAiChatCompletionRequestMessageUser,
AzureOpenAiCreateChatCompletionResponse
} from './client/inference/schema/index.js';

describe('Azure OpenAI chat client', () => {
const chatCompletionEndpoint = {
Expand All @@ -33,13 +33,14 @@ describe('Azure OpenAI chat client', () => {
role: 'user',
content: 'Where is the deepest place on earth located'
}
] as AzureOpenAiChatMessage[]
] as AzureOpenAiChatCompletionRequestMessageUser[]
};

const mockResponse = parseMockResponse<AzureOpenAiChatCompletionOutput>(
'foundation-models',
'azure-openai-chat-completion-success-response.json'
);
const mockResponse =
parseMockResponse<AzureOpenAiCreateChatCompletionResponse>(
'foundation-models',
'azure-openai-chat-completion-success-response.json'
);

mockInference(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
getDeploymentId,
type ModelDeployment
} from '@sap-ai-sdk/ai-api/internal.js';
import type { AzureOpenAiCreateChatCompletionRequest } from './client/inference/schema/index.js';
import type { AzureOpenAiChatModel } from './model-types.js';
import type { AzureOpenAiChatCompletionParameters } from './azure-openai-types.js';
import { AzureOpenAiChatCompletionResponse } from './azure-openai-chat-completion-response.js';

const apiVersion = '2024-02-01';
const apiVersion = '2024-06-01';

/**
* Azure OpenAI client for chat completion.
Expand All @@ -26,7 +26,7 @@ export class AzureOpenAiChatClient {
* @returns The completion result.
*/
async run(
data: AzureOpenAiChatCompletionParameters,
data: AzureOpenAiCreateChatCompletionRequest,
requestConfig?: CustomRequestConfig
): Promise<AzureOpenAiChatCompletionResponse> {
const deploymentId = await getDeploymentId(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { HttpResponse } from '@sap-cloud-sdk/http-client';
import { createLogger } from '@sap-cloud-sdk/util';
import {
AzureOpenAiChatCompletionOutput,
AzureOpenAiUsage
} from './azure-openai-types.js';
import type { AzureOpenAiCreateChatCompletionResponse } from './client/inference/schema/index.js';

const logger = createLogger({
package: 'gen-ai-hub',
Expand All @@ -17,8 +14,7 @@ export class AzureOpenAiChatCompletionResponse {
/**
* The chat completion response.
*/
public readonly data: AzureOpenAiChatCompletionOutput;

public readonly data: AzureOpenAiCreateChatCompletionResponse;
constructor(public readonly rawResponse: HttpResponse) {
this.data = rawResponse.data;
}
Expand All @@ -27,7 +23,7 @@ export class AzureOpenAiChatCompletionResponse {
* Usage of tokens in the response.
* @returns Token usage.
*/
getTokenUsage(): AzureOpenAiUsage {
getTokenUsage(): this['data']['usage'] {
MatKuhr marked this conversation as resolved.
Show resolved Hide resolved
return this.data.usage;
}

Expand All @@ -36,7 +32,9 @@ export class AzureOpenAiChatCompletionResponse {
* @param choiceIndex - The index of the choice to parse.
* @returns The finish reason.
*/
getFinishReason(choiceIndex = 0): string | undefined {
getFinishReason(
choiceIndex = 0
): this['data']['choices'][0]['finish_reason'] {
this.logInvalidChoiceIndex(choiceIndex);
return this.data.choices[choiceIndex]?.finish_reason;
}
Expand All @@ -46,7 +44,7 @@ export class AzureOpenAiChatCompletionResponse {
* @param choiceIndex - The index of the choice to parse.
* @returns The message content.
*/
getContent(choiceIndex = 0): string | undefined {
getContent(choiceIndex = 0): string | undefined | null {
this.logInvalidChoiceIndex(choiceIndex);
return this.data.choices[choiceIndex]?.message?.content;
}
Expand Down
Loading
Loading