Skip to content

Commit

Permalink
Merge branch 'main' into feat-streaming-azure-openai
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongpinWang committed Nov 15, 2024
2 parents d4ef790 + 67bdc68 commit 63db0c4
Show file tree
Hide file tree
Showing 30 changed files with 141 additions and 118 deletions.
6 changes: 0 additions & 6 deletions .changeset/filter.md

This file was deleted.

6 changes: 0 additions & 6 deletions .changeset/interface-vs-type.md

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sap-ai-sdk",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"keywords": [],
"license": "Apache-2.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/ai-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sap-ai-sdk/ai-api

## 1.3.0

### Patch Changes

- @sap-ai-sdk/core@1.3.0

## 1.2.0

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sap-ai-sdk/ai-api",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"license": "Apache-2.0",
"keywords": [
Expand Down
2 changes: 2 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# @sap-ai-sdk/core

## 1.3.0

## 1.2.0

## 1.1.0
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sap-ai-sdk/core",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"license": "Apache-2.0",
"keywords": [
Expand Down
8 changes: 8 additions & 0 deletions packages/foundation-models/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @sap-ai-sdk/foundation-models

## 1.3.0

### Patch Changes

- 91df549: [Fixed Issue] Get choice via index by comparing the `index` property instead of using array index.
- @sap-ai-sdk/ai-api@1.3.0
- @sap-ai-sdk/core@1.3.0

## 1.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/foundation-models/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sap-ai-sdk/foundation-models",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"license": "Apache-2.0",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { createLogger } from '@sap-cloud-sdk/util';
import { jest } from '@jest/globals';
import { parseMockResponse } from '../../../../test-util/mock-http.js';
import { AzureOpenAiChatCompletionResponse } from './azure-openai-chat-completion-response.js';
import type { HttpResponse } from '@sap-cloud-sdk/http-client';
Expand Down Expand Up @@ -51,14 +49,7 @@ describe('OpenAI chat completion response', () => {
});

it('should return undefined when convenience function is called with incorrect index', () => {
const logger = createLogger({
package: 'foundation-models',
messageContext: 'azure-openai-chat-completion-response'
});
const errorSpy = jest.spyOn(logger, 'error');
expect(azureOpenAiChatResponse.getFinishReason(1)).toBeUndefined();
expect(errorSpy).toHaveBeenCalledWith('Choice index 1 is out of bounds.');
expect(azureOpenAiChatResponse.getContent(1)).toBeUndefined();
expect(errorSpy).toHaveBeenCalledTimes(2);
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { createLogger } from '@sap-cloud-sdk/util';
import type { HttpResponse } from '@sap-cloud-sdk/http-client';
import type { AzureOpenAiCreateChatCompletionResponse } from './client/inference/schema/index.js';

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

/**
* Azure OpenAI chat completion response.
*/
Expand All @@ -32,9 +26,8 @@ export class AzureOpenAiChatCompletionResponse {
* @param choiceIndex - The index of the choice to parse.
* @returns The finish reason.
*/
getFinishReason(choiceIndex = 0): string | undefined | null {
this.logInvalidChoiceIndex(choiceIndex);
return this.data.choices[choiceIndex]?.finish_reason;
getFinishReason(choiceIndex = 0): string | undefined {
return this.data.choices.find(c => c.index === choiceIndex)?.finish_reason;
}

/**
Expand All @@ -43,13 +36,7 @@ export class AzureOpenAiChatCompletionResponse {
* @returns The message content.
*/
getContent(choiceIndex = 0): string | undefined | null {
this.logInvalidChoiceIndex(choiceIndex);
return this.data.choices[choiceIndex]?.message?.content;
}

private logInvalidChoiceIndex(choiceIndex: number): void {
if (choiceIndex < 0 || choiceIndex >= this.data.choices.length) {
logger.error(`Choice index ${choiceIndex} is out of bounds.`);
}
return this.data.choices.find(c => c.index === choiceIndex)?.message
?.content;
}
}
9 changes: 9 additions & 0 deletions packages/langchain/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @sap-ai-sdk/langchain

## 1.3.0

### Patch Changes

- Updated dependencies [91df549]
- @sap-ai-sdk/foundation-models@1.3.0
- @sap-ai-sdk/ai-api@1.3.0
- @sap-ai-sdk/core@1.3.0

## 1.2.0

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/langchain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sap-ai-sdk/langchain",
"version": "1.2.0",
"version": "1.3.0",
"description": "LangChain clients based on the @sap-ai-sdk",
"license": "Apache-2.0",
"keywords": [
Expand Down
18 changes: 18 additions & 0 deletions packages/orchestration/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# @sap-ai-sdk/orchestration

## 1.3.0

### Minor Changes

- bf17e17: [Fixed Issue] Fix input and output filters to disallow additional properties as part of the filter config.

[Compatibility Note] `FilteringConfig` type has been renamed to `InputFilteringConfig` and `OutputFilteringConfig` for future extensibility.

- 6fe32b8: [Compatibility Note] Switch some of the orchestration interfaces to types, as they were introduced by accident.

[Compatibility Note] Remove `grounding` key from the type `ModuleResults`.

### Patch Changes

- 91df549: [Fixed Issue] Get choice via index by comparing the `index` property instead of using array index.
- @sap-ai-sdk/ai-api@1.3.0
- @sap-ai-sdk/core@1.3.0

## 1.2.0

### Patch Changes
Expand Down
5 changes: 2 additions & 3 deletions packages/orchestration/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sap-ai-sdk/orchestration",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"license": "Apache-2.0",
"keywords": [
Expand Down Expand Up @@ -33,7 +33,6 @@
"dependencies": {
"@sap-ai-sdk/core": "workspace:^",
"@sap-ai-sdk/ai-api": "workspace:^",
"@sap-cloud-sdk/http-client": "^3.22.2",
"@sap-cloud-sdk/util": "^3.22.2"
"@sap-cloud-sdk/http-client": "^3.22.2"
}
}
9 changes: 0 additions & 9 deletions packages/orchestration/src/orchestration-response.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { createLogger } from '@sap-cloud-sdk/util';
import { jest } from '@jest/globals';
import { parseMockResponse } from '../../../test-util/mock-http.js';
import { OrchestrationResponse } from './orchestration-response.js';
import type { HttpResponse } from '@sap-cloud-sdk/http-client';
Expand Down Expand Up @@ -48,15 +46,8 @@ describe('OrchestrationResponse', () => {
});

it('should return undefined when convenience function is called with incorrect index', () => {
const logger = createLogger({
package: 'orchestration',
messageContext: 'orchestration-response'
});
const errorSpy = jest.spyOn(logger, 'error');
expect(orchestrationResponse.getFinishReason(1)).toBeUndefined();
expect(errorSpy).toHaveBeenCalledWith('Choice index 1 is out of bounds.');
expect(orchestrationResponse.getContent(1)).toBeUndefined();
expect(errorSpy).toHaveBeenCalledTimes(2);
});

it('should throw if content that was filtered is accessed', () => {
Expand Down
23 changes: 5 additions & 18 deletions packages/orchestration/src/orchestration-response.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { createLogger } from '@sap-cloud-sdk/util';
import type { HttpResponse } from '@sap-cloud-sdk/http-client';
import type {
CompletionPostResponse,
TokenUsage
} from './client/api/schema/index.js';

const logger = createLogger({
package: 'orchestration',
messageContext: 'orchestration-response'
});

/**
* Representation of an orchestration response.
*/
Expand All @@ -35,8 +29,7 @@ export class OrchestrationResponse {
* @returns The finish reason.
*/
getFinishReason(choiceIndex = 0): string | undefined {
this.logInvalidChoiceIndex(choiceIndex);
return this.getChoices()[choiceIndex]?.finish_reason;
return this.getChoices().find(c => c.index === choiceIndex)?.finish_reason;
}

/**
Expand All @@ -46,25 +39,19 @@ export class OrchestrationResponse {
* @returns The message content.
*/
getContent(choiceIndex = 0): string | undefined {
this.logInvalidChoiceIndex(choiceIndex);
const choice = this.getChoices().find(c => c.index === choiceIndex);
if (
this.getChoices()[choiceIndex]?.message?.content === '' &&
this.getChoices()[choiceIndex]?.finish_reason === 'content_filter'
choice?.message?.content === '' &&
choice?.finish_reason === 'content_filter'
) {
throw new Error(
'Content generated by the LLM was filtered by the output filter. Please try again with a different prompt or filter configuration.'
);
}
return this.getChoices()[choiceIndex]?.message?.content;
return choice?.message?.content;
}

private getChoices() {
return this.data.orchestration_result.choices;
}

private logInvalidChoiceIndex(choiceIndex: number): void {
if (choiceIndex < 0 || choiceIndex >= this.getChoices().length) {
logger.error(`Choice index ${choiceIndex} is out of bounds.`);
}
}
}
Loading

0 comments on commit 63db0c4

Please sign in to comment.