Skip to content

Commit 6246819

Browse files
committed
Add Support for Azure Content Filter Response
1 parent cfe885b commit 6246819

15 files changed

+462
-4
lines changed

openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/ChatChoice.kt

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.aallam.openai.api.chat;
22

3-
import com.aallam.openai.api.BetaOpenAI
43
import com.aallam.openai.api.core.FinishReason
4+
import com.aallam.openai.azure.api.chat.ChatFinishDetails
5+
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForChoice
56
import kotlinx.serialization.SerialName
67
import kotlinx.serialization.Serializable
78

@@ -25,4 +26,17 @@ public data class ChatChoice(
2526
* The reason why OpenAI stopped generating.
2627
*/
2728
@SerialName("finish_reason") public val finishReason: FinishReason? = null,
29+
30+
/**
31+
* Information about the content filtering category (hate, sexual, violence,
32+
* self_harm), if it has been detected, as well as the severity level (very_low, low, medium, high-scale that
33+
* determines the intensity and risk level of harmful content) and if it has been filtered or not.
34+
*/
35+
@SerialName("content_filter_results") public val contentFilterResults: ContentFilterResultsForChoice? = null,
36+
37+
/**
38+
* The reason the model stopped generating tokens, together with any applicable details.
39+
* This structured representation replaces 'finish_reason' for some models.
40+
*/
41+
@SerialName("finish_details") public val finishDetails: ChatFinishDetails? = null,
2842
)

openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/ChatChunk.kt

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.aallam.openai.api.chat;
22

3-
import com.aallam.openai.api.BetaOpenAI
43
import com.aallam.openai.api.core.FinishReason
4+
import com.aallam.openai.azure.api.chat.ChatFinishDetails
5+
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForChoice
56
import kotlinx.serialization.SerialName
67
import kotlinx.serialization.Serializable
78

@@ -25,4 +26,17 @@ public data class ChatChunk(
2526
* The reason why OpenAI stopped generating.
2627
*/
2728
@SerialName("finish_reason") public val finishReason: FinishReason?,
29+
30+
/**
31+
* Information about the content filtering category (hate, sexual, violence,
32+
* self_harm), if it has been detected, as well as the severity level (very_low, low, medium, high-scale that
33+
* determines the intensity and risk level of harmful content) and if it has been filtered or not.
34+
*/
35+
@SerialName("content_filter_results") public val contentFilterResults: ContentFilterResultsForChoice? = null,
36+
37+
/**
38+
* The reason the model stopped generating tokens, together with any applicable details.
39+
* This structured representation replaces 'finish_reason' for some models.
40+
*/
41+
@SerialName("finish_details") public val finishDetails: ChatFinishDetails? = null,
2842
)

openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/ChatCompletion.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.aallam.openai.api.chat
22

3-
import com.aallam.openai.api.BetaOpenAI
43
import com.aallam.openai.api.core.Usage
54
import com.aallam.openai.api.model.ModelId
5+
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForPrompt
66
import kotlinx.serialization.SerialName
77
import kotlinx.serialization.Serializable
88

@@ -44,4 +44,11 @@ public data class ChatCompletion(
4444
* might impact determinism.
4545
*/
4646
@SerialName("system_fingerprint") public val systemFingerprint: String? = null,
47+
48+
/**
49+
* Content filtering results for zero or more prompts in the request. In a streaming request,
50+
* results for different prompts may arrive at different times or in different orders.
51+
*/
52+
@SerialName("prompt_filter_results")
53+
val promptFilterResults: List<ContentFilterResultsForPrompt>? = null
4754
)

openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/ChatCompletionChunk.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.aallam.openai.api.chat
22

3-
import com.aallam.openai.api.BetaOpenAI
43
import com.aallam.openai.api.core.Usage
54
import com.aallam.openai.api.model.ModelId
5+
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForPrompt
66
import kotlinx.serialization.SerialName
77
import kotlinx.serialization.Serializable
88

@@ -42,4 +42,11 @@ public data class ChatCompletionChunk(
4242
*/
4343
@SerialName("usage")
4444
public val usage: Usage? = null,
45+
46+
/**
47+
* Content filtering results for zero or more prompts in the request. In a streaming request,
48+
* results for different prompts may arrive at different times or in different orders.
49+
*/
50+
@SerialName("prompt_filter_results")
51+
val promptFilterResults: List<ContentFilterResultsForPrompt>? = null
4552
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.aallam.openai.azure.api.chat
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* An abstract representation of structured information about why a chat completions response terminated.
8+
*/
9+
@Serializable
10+
public sealed class ChatFinishDetails {
11+
12+
/**
13+
* Represents the "stop" type of ChatFinishDetails.
14+
*/
15+
@Serializable
16+
@SerialName("stop")
17+
public data class StopFinishDetails(
18+
19+
/**
20+
* The token sequence that the model terminated with.
21+
*/
22+
@SerialName("stop")
23+
val stop: String
24+
) : ChatFinishDetails()
25+
26+
/**
27+
* A structured representation of a stop reason that signifies a token limit was reached before the model could
28+
* naturally complete.
29+
*/
30+
@Serializable
31+
@SerialName("max_tokens")
32+
public class MaxTokensFinishDetails : ChatFinishDetails()
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.aallam.openai.azure.api.core
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.SerialName
5+
6+
/**
7+
* This class represents the error details of an HTTP response.
8+
*/
9+
@Serializable
10+
public data class ResponseError(
11+
12+
/**
13+
* The error code of this error.
14+
*/
15+
@SerialName("code")
16+
val code: String,
17+
18+
/**
19+
* The error message of this error.
20+
*/
21+
@SerialName("message")
22+
val message: String,
23+
24+
/**
25+
* The target of this error.
26+
*/
27+
@SerialName("target")
28+
val target: String? = null,
29+
30+
/**
31+
* The inner error information for this error.
32+
*/
33+
@SerialName("innererror")
34+
val innerError: ResponseInnerError? = null,
35+
36+
/**
37+
* A list of details about specific errors that led to this reported error.
38+
*/
39+
@SerialName("details")
40+
val errorDetails: List<ResponseError>? = null
41+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.aallam.openai.azure.api.core
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.SerialName
5+
6+
7+
/**
8+
* The inner error of a ResponseError.
9+
*/
10+
@Serializable
11+
public data class ResponseInnerError(
12+
13+
/**
14+
* The error code of the inner error.
15+
*/
16+
@SerialName("code")
17+
val code: String? = null,
18+
19+
/**
20+
* The nested inner error for this error.
21+
*/
22+
@SerialName("innererror")
23+
val innerError: ResponseInnerError? = null
24+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.aallam.openai.azure.api.filtering
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.SerialName
5+
6+
7+
/**
8+
* Represents the outcome of an evaluation against a custom blocklist as performed by content filtering.
9+
*/
10+
@Serializable
11+
public data class ContentFilterBlocklistIdResult(
12+
13+
/**
14+
* The ID of the custom blocklist evaluated.
15+
*/
16+
@SerialName("id")
17+
val id: String,
18+
19+
/**
20+
* A value indicating whether the content has been filtered.
21+
*/
22+
@SerialName("filtered")
23+
val filtered: Boolean
24+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.aallam.openai.azure.api.filtering
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* Represents the outcome of a detection operation against protected resources as performed by content filtering.
8+
*/
9+
@Serializable
10+
public data class ContentFilterCitedDetectionResult(
11+
12+
/**
13+
* A value indicating whether or not the content has been filtered.
14+
*/
15+
@SerialName("filtered")
16+
val filtered: Boolean,
17+
18+
/**
19+
* A value indicating whether detection occurred, irrespective of severity or whether the content was filtered.
20+
*/
21+
@SerialName("detected")
22+
val detected: Boolean,
23+
24+
/**
25+
* The internet location associated with the detection.
26+
*/
27+
@SerialName("URL")
28+
val url: String,
29+
30+
/**
31+
* The license description associated with the detection.
32+
*/
33+
@SerialName("license")
34+
val license: String
35+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.aallam.openai.azure.api.filtering
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.SerialName
5+
6+
/**
7+
* Represents the outcome of a detection operation performed by content filtering.
8+
*/
9+
@Serializable
10+
public data class ContentFilterDetectionResult(
11+
12+
/**
13+
* A value indicating whether the content has been filtered.
14+
*/
15+
@SerialName("filtered")
16+
val filtered: Boolean,
17+
18+
/**
19+
* A value indicating whether detection occurred, irrespective of severity or whether the content was filtered.
20+
*/
21+
@SerialName("detected")
22+
val detected: Boolean
23+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.aallam.openai.azure.api.filtering
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* Information about filtered content severity level and if it has been filtered or not.
8+
*/
9+
@Serializable
10+
public data class ContentFilterResult(
11+
12+
/**
13+
* Ratings for the intensity and risk level of filtered content.
14+
*/
15+
@SerialName("severity")
16+
val severity: ContentFilterSeverity,
17+
18+
/**
19+
* A value indicating whether the content has been filtered.
20+
*/
21+
@SerialName("filtered")
22+
val filtered: Boolean
23+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.aallam.openai.azure.api.filtering
2+
3+
import com.aallam.openai.azure.api.core.ResponseError
4+
import kotlinx.serialization.SerialName
5+
import kotlinx.serialization.Serializable
6+
7+
/**
8+
* Information about content filtering evaluated against input data to Azure OpenAI.
9+
*/
10+
@Serializable
11+
public data class ContentFilterResultDetailsForPrompt(
12+
13+
/**
14+
* Describes language related to anatomical organs and genitals, romantic relationships,
15+
* acts portrayed in erotic or affectionate terms, physical sexual acts, including
16+
* those portrayed as an assault or a forced sexual violent act against one’s will,
17+
* prostitution, pornography, and abuse.
18+
*/
19+
@SerialName("sexual")
20+
val sexual: ContentFilterResult? = null,
21+
22+
/**
23+
* Describes language related to physical actions intended to hurt, injure, damage, or
24+
* kill someone or something; describes weapons, etc.
25+
*/
26+
@SerialName("violence")
27+
val violence: ContentFilterResult? = null,
28+
29+
/**
30+
* Describes language attacks or uses that include pejorative or discriminatory language
31+
* with reference to a person or identity group on the basis of certain differentiating
32+
* attributes of these groups including but not limited to race, ethnicity, nationality,
33+
* gender identity and expression, sexual orientation, religion, immigration status, ability
34+
* status, personal appearance, and body size.
35+
*/
36+
@SerialName("hate")
37+
val hate: ContentFilterResult? = null,
38+
39+
/**
40+
* Describes language related to physical actions intended to purposely hurt, injure,
41+
* or damage one’s body, or kill oneself.
42+
*/
43+
@SerialName("self_harm")
44+
val selfHarm: ContentFilterResult? = null,
45+
46+
/**
47+
* Describes whether profanity was detected.
48+
*/
49+
@SerialName("profanity")
50+
val profanity: ContentFilterDetectionResult? = null,
51+
52+
/**
53+
* Describes detection results against configured custom blocklists.
54+
*/
55+
@SerialName("custom_blocklists")
56+
val customBlocklists: List<ContentFilterBlocklistIdResult>? = null,
57+
58+
/**
59+
* Describes an error returned if the content filtering system is
60+
* down or otherwise unable to complete the operation in time.
61+
*/
62+
@SerialName("error")
63+
val error: ResponseError? = null,
64+
65+
/**
66+
* Whether a jailbreak attempt was detected in the prompt.
67+
*/
68+
@SerialName("jailbreak")
69+
val jailbreak: ContentFilterDetectionResult? = null
70+
)

0 commit comments

Comments
 (0)