generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Apply schema check for Azure OpenAI client response (#130)
* 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
1 parent
8c28ef9
commit 1f4617d
Showing
19 changed files
with
832 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/foundation-models/src/azure-openai/azure-openai-chat-completion-response.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/foundation-models/src/azure-openai/azure-openai-embedding-response.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/foundation-models/src/azure-openai/azure-openai-embedding-response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
} | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
packages/foundation-models/src/azure-openai/azure-openai-response.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.