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

refactor: Content filter helper function #441

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ZhongpinWang
Copy link
Contributor

Context

SAP/ai-sdk-js-backlog#202.

What this PR does and why it is needed

Deprecate buildAzureContentFilter() because it restricts filtering to have only one filter.

Implement ContentFilters.azure(). New filters can be added to ContentFilters constant.

'@sap-ai-sdk/orchestration': minor
---

[Compatibility Note] Deprecate `buildAzureContentFilter()` since it restricts filtering to have only one filter, use `ContentFilters.azure()` instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Compatibility Note] Deprecate `buildAzureContentFilter()` since it restricts filtering to have only one filter, use `ContentFilters.azure()` instead.
[Compatibility Note] Deprecate `buildAzureContentFilter()` function since it restricts filtering to have only one filter. Use `ContentFilters.azure()` instead.

} from '@sap-ai-sdk/orchestration';
#### Azure Content Filter

Use `ContentFilters.azure()` to build an Azure content filter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use `ContentFilters.azure()` to build an Azure content filter.
Use `ContentFilters.azure()` function to build an Azure content filter.

Follow style guide

When the chat completion request fails with a `400` status code, the caught error will be an `Axios` error.
The property `error.response.data.message` may provide additional details about the failure's cause.

- **output content filtered**:
- **Output Content Filtered**:
The method `getContent()` can throw an error if the output filter filters the model output.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The method `getContent()` can throw an error if the output filter filters the model output.
The `getContent()` method can throw an error if the output filter filters the model output.

@@ -1,4 +1,4 @@
export * from './orchestration-client.js';
export * from './orchestration-utils.js';
export * from './orchestration-filtering.js';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're splitting the grounding and filtering util files, please put them in a separate util folder

* @param config - Filtering configuration for Azure filter. If skipped, the default Azure content filter configuration is used.
* @returns Filter config object.
*/
function azure(config?: AzureContentSafety): FilterConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type here should be AzureContentSafetyFilterConfig, no?


#### Output Filtering

`GET /orchestration/outputFiltering`

Get chat completion response with Azure content filter for the output.
Use `buildAzureContentFilter()` to build the content filter.
For example, use `ContentFilters.azure()` to build Azure content filter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[req] style guide

export { buildAzureContentFilter } from './orchestration-utils.js';
export {
buildAzureContentFilter,
ContentFilters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[req] The azure function should also be exported here.

Copy link
Member

@tomfrenken tomfrenken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept looks good and even allows for adding streaming options.

LGTM, after making the adjustments Deeksha has requested.

@KavithaSiva
Copy link
Contributor

KavithaSiva commented Jan 17, 2025

Proposed API looks good to me too, although I didn't like it that the user has to explicitly wrap the calls inside an array even for using a single filter.

I feel typing { filters: [ContentFilters.azure({ Hate: 4, SelfHarm: 2 })] } to use a single filter is redundant.

An alternate I could think of was:

filtering: {
  input: ContentFilters.azure({ Hate: 4, SelfHarm: 0 }),
  // or
  input: ContentFilters.llamaGuard({ ... }),
  // or
  input: ContentFilters.addMultipleFilters(
    ContentFilters.azure({ Hate: 4, SelfHarm: 0 }),
    ContentFilters.llamaGuard({ ... })
  )
}

WDY think?
But yeah, the behaviour would then be similar to buildAzureContentFilter() and I would then think ContentFilters then only helps with discoverability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants