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

Add Azure OpenAI Content Filter Streaming Support #340

Merged

Conversation

rasharab
Copy link
Contributor

@rasharab rasharab commented May 28, 2024

ISSUE: #339

Azure enforces content filtering on all completion requests.
To reduce the overhead of content filtering, they’ve added asychronous mode, which basically outputs specialized bodies at the end of streaming output.

https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter?tabs=warning%2Cpython-new#annotation-message

The basic structure is this while streaming:
{"choices":[{"content_filter_offsets":{"check_offset":33188,"start_offset":33188,"end_offset":33546},"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"finish_reason":null,"index":0}],"created":0,"id":"","model":"","object":""}

This is related to #300, but that PR has a bug with the streaming case.

TESTING:

  1. I've added test resources with json bodies for what azure returns and added deserialization tests for what we've observed in the wild.

@@ -19,7 +18,17 @@ public data class ChatChunk(
/**
* The generated chat message.
*/
@SerialName("delta") public val delta: ChatDelta,
@SerialName("delta") public val delta: ChatDelta? = null,
Copy link
Contributor Author

@rasharab rasharab May 28, 2024

Choose a reason for hiding this comment

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

Delta must be nullable for content filtering.

Azure enforces content filtering on all completion requests.
To reduce the overhead of content filtering, they’ve added asychronous mode, which basically outputs specialized bodies at the end of streaming output.

https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter?tabs=warning%2Cpython-new#annotation-message

The basic structure is this:
data: {"choices":[{"content_filter_offsets":{"check_offset":33188,"start_offset":33188,"end_offset":33546},"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"finish_reason":null,"index":0}],"created":0,"id":"","model":"","object":""}
@rasharab rasharab force-pushed the AddAzureContentFilterSupport branch from 0deb69a to 5883420 Compare May 28, 2024 22:54
@rasharab rasharab changed the title Add Azure OpenAI Content Filter Support Add Azure OpenAI Content Filter Streaming Support May 28, 2024
@aallam aallam merged commit 61f5759 into aallam:main Jun 17, 2024
2 of 16 checks passed
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.

2 participants