-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Azure OpenAI Content Filter Support
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":""}
- Loading branch information
Showing
13 changed files
with
224 additions
and
5 deletions.
There are no files selected for viewing
File renamed without changes.
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
25 changes: 25 additions & 0 deletions
25
openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestChatCompletionChunk.kt
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,25 @@ | ||
package com.aallam.openai.client | ||
|
||
import com.aallam.openai.api.chat.ChatCompletionChunk | ||
import com.aallam.openai.api.file.FileSource | ||
import com.aallam.openai.client.internal.JsonLenient | ||
import com.aallam.openai.client.internal.TestFileSystem | ||
import com.aallam.openai.client.internal.testFilePath | ||
import kotlin.test.Test | ||
import okio.buffer | ||
|
||
class TestChatCompletionChunk { | ||
@Test | ||
fun testContentFilterDeserialization() { | ||
val json = FileSource(path = testFilePath("json/azureContentFilterChunk.json"), fileSystem = TestFileSystem) | ||
val actualJson = json.source.buffer().readByteArray().decodeToString() | ||
JsonLenient.decodeFromString<ChatCompletionChunk>(actualJson) | ||
} | ||
|
||
@Test | ||
fun testDeserialization() { | ||
val json = FileSource(path = testFilePath("json/chatChunk.json"), fileSystem = TestFileSystem) | ||
val actualJson = json.source.buffer().readByteArray().decodeToString() | ||
JsonLenient.decodeFromString<ChatCompletionChunk>(actualJson) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
openai-client/src/commonTest/resources/json/azureContentFilterChunk.json
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,35 @@ | ||
{ | ||
"choices": [ | ||
{ | ||
"content_filter_offsets": { | ||
"check_offset": 33188, | ||
"start_offset": 33188, | ||
"end_offset": 33557 | ||
}, | ||
"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": "" | ||
} |
16 changes: 16 additions & 0 deletions
16
openai-client/src/commonTest/resources/json/chatChunk.json
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,16 @@ | ||
{ | ||
"choices": [ | ||
{ | ||
"delta": { | ||
"content": " engineering" | ||
}, | ||
"finish_reason": null, | ||
"index": 0 | ||
} | ||
], | ||
"created": 1716855566, | ||
"id": "chatcmpl-9TeqkT3BJs5zXQq12b204deXcY5nj", | ||
"model": "gpt-4o-2024-05-13", | ||
"object": "chat.completion.chunk", | ||
"system_fingerprint": "fp_5f4bad809a" | ||
} |
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
Oops, something went wrong.