Skip to content

Commit

Permalink
Add Support for Azure Content Filter Response
Browse files Browse the repository at this point in the history
  • Loading branch information
gh-abhay committed Jan 25, 2024
1 parent cfe885b commit 6246819
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.aallam.openai.api.chat;

import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.core.FinishReason
import com.aallam.openai.azure.api.chat.ChatFinishDetails
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForChoice
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -25,4 +26,17 @@ public data class ChatChoice(
* The reason why OpenAI stopped generating.
*/
@SerialName("finish_reason") public val finishReason: FinishReason? = null,

/**
* Information about the content filtering category (hate, sexual, violence,
* self_harm), if it has been detected, as well as the severity level (very_low, low, medium, high-scale that
* determines the intensity and risk level of harmful content) and if it has been filtered or not.
*/
@SerialName("content_filter_results") public val contentFilterResults: ContentFilterResultsForChoice? = null,

/**
* The reason the model stopped generating tokens, together with any applicable details.
* This structured representation replaces 'finish_reason' for some models.
*/
@SerialName("finish_details") public val finishDetails: ChatFinishDetails? = null,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.aallam.openai.api.chat;

import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.core.FinishReason
import com.aallam.openai.azure.api.chat.ChatFinishDetails
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForChoice
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -25,4 +26,17 @@ public data class ChatChunk(
* The reason why OpenAI stopped generating.
*/
@SerialName("finish_reason") public val finishReason: FinishReason?,

/**
* Information about the content filtering category (hate, sexual, violence,
* self_harm), if it has been detected, as well as the severity level (very_low, low, medium, high-scale that
* determines the intensity and risk level of harmful content) and if it has been filtered or not.
*/
@SerialName("content_filter_results") public val contentFilterResults: ContentFilterResultsForChoice? = null,

/**
* The reason the model stopped generating tokens, together with any applicable details.
* This structured representation replaces 'finish_reason' for some models.
*/
@SerialName("finish_details") public val finishDetails: ChatFinishDetails? = null,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.aallam.openai.api.chat

import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.core.Usage
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForPrompt
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down Expand Up @@ -44,4 +44,11 @@ public data class ChatCompletion(
* might impact determinism.
*/
@SerialName("system_fingerprint") public val systemFingerprint: String? = null,

/**
* Content filtering results for zero or more prompts in the request. In a streaming request,
* results for different prompts may arrive at different times or in different orders.
*/
@SerialName("prompt_filter_results")
val promptFilterResults: List<ContentFilterResultsForPrompt>? = null
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.aallam.openai.api.chat

import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.core.Usage
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.azure.api.filtering.ContentFilterResultsForPrompt
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand Down Expand Up @@ -42,4 +42,11 @@ public data class ChatCompletionChunk(
*/
@SerialName("usage")
public val usage: Usage? = null,

/**
* Content filtering results for zero or more prompts in the request. In a streaming request,
* results for different prompts may arrive at different times or in different orders.
*/
@SerialName("prompt_filter_results")
val promptFilterResults: List<ContentFilterResultsForPrompt>? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.aallam.openai.azure.api.chat

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* An abstract representation of structured information about why a chat completions response terminated.
*/
@Serializable
public sealed class ChatFinishDetails {

/**
* Represents the "stop" type of ChatFinishDetails.
*/
@Serializable
@SerialName("stop")
public data class StopFinishDetails(

/**
* The token sequence that the model terminated with.
*/
@SerialName("stop")
val stop: String
) : ChatFinishDetails()

/**
* A structured representation of a stop reason that signifies a token limit was reached before the model could
* naturally complete.
*/
@Serializable
@SerialName("max_tokens")
public class MaxTokensFinishDetails : ChatFinishDetails()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.aallam.openai.azure.api.core

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName

/**
* This class represents the error details of an HTTP response.
*/
@Serializable
public data class ResponseError(

/**
* The error code of this error.
*/
@SerialName("code")
val code: String,

/**
* The error message of this error.
*/
@SerialName("message")
val message: String,

/**
* The target of this error.
*/
@SerialName("target")
val target: String? = null,

/**
* The inner error information for this error.
*/
@SerialName("innererror")
val innerError: ResponseInnerError? = null,

/**
* A list of details about specific errors that led to this reported error.
*/
@SerialName("details")
val errorDetails: List<ResponseError>? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.aallam.openai.azure.api.core

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName


/**
* The inner error of a ResponseError.
*/
@Serializable
public data class ResponseInnerError(

/**
* The error code of the inner error.
*/
@SerialName("code")
val code: String? = null,

/**
* The nested inner error for this error.
*/
@SerialName("innererror")
val innerError: ResponseInnerError? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.aallam.openai.azure.api.filtering

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName


/**
* Represents the outcome of an evaluation against a custom blocklist as performed by content filtering.
*/
@Serializable
public data class ContentFilterBlocklistIdResult(

/**
* The ID of the custom blocklist evaluated.
*/
@SerialName("id")
val id: String,

/**
* A value indicating whether the content has been filtered.
*/
@SerialName("filtered")
val filtered: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.aallam.openai.azure.api.filtering

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* Represents the outcome of a detection operation against protected resources as performed by content filtering.
*/
@Serializable
public data class ContentFilterCitedDetectionResult(

/**
* A value indicating whether or not the content has been filtered.
*/
@SerialName("filtered")
val filtered: Boolean,

/**
* A value indicating whether detection occurred, irrespective of severity or whether the content was filtered.
*/
@SerialName("detected")
val detected: Boolean,

/**
* The internet location associated with the detection.
*/
@SerialName("URL")
val url: String,

/**
* The license description associated with the detection.
*/
@SerialName("license")
val license: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.aallam.openai.azure.api.filtering

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName

/**
* Represents the outcome of a detection operation performed by content filtering.
*/
@Serializable
public data class ContentFilterDetectionResult(

/**
* A value indicating whether the content has been filtered.
*/
@SerialName("filtered")
val filtered: Boolean,

/**
* A value indicating whether detection occurred, irrespective of severity or whether the content was filtered.
*/
@SerialName("detected")
val detected: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.aallam.openai.azure.api.filtering

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* Information about filtered content severity level and if it has been filtered or not.
*/
@Serializable
public data class ContentFilterResult(

/**
* Ratings for the intensity and risk level of filtered content.
*/
@SerialName("severity")
val severity: ContentFilterSeverity,

/**
* A value indicating whether the content has been filtered.
*/
@SerialName("filtered")
val filtered: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.aallam.openai.azure.api.filtering

import com.aallam.openai.azure.api.core.ResponseError
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* Information about content filtering evaluated against input data to Azure OpenAI.
*/
@Serializable
public data class ContentFilterResultDetailsForPrompt(

/**
* Describes language related to anatomical organs and genitals, romantic relationships,
* acts portrayed in erotic or affectionate terms, physical sexual acts, including
* those portrayed as an assault or a forced sexual violent act against one’s will,
* prostitution, pornography, and abuse.
*/
@SerialName("sexual")
val sexual: ContentFilterResult? = null,

/**
* Describes language related to physical actions intended to hurt, injure, damage, or
* kill someone or something; describes weapons, etc.
*/
@SerialName("violence")
val violence: ContentFilterResult? = null,

/**
* Describes language attacks or uses that include pejorative or discriminatory language
* with reference to a person or identity group on the basis of certain differentiating
* attributes of these groups including but not limited to race, ethnicity, nationality,
* gender identity and expression, sexual orientation, religion, immigration status, ability
* status, personal appearance, and body size.
*/
@SerialName("hate")
val hate: ContentFilterResult? = null,

/**
* Describes language related to physical actions intended to purposely hurt, injure,
* or damage one’s body, or kill oneself.
*/
@SerialName("self_harm")
val selfHarm: ContentFilterResult? = null,

/**
* Describes whether profanity was detected.
*/
@SerialName("profanity")
val profanity: ContentFilterDetectionResult? = null,

/**
* Describes detection results against configured custom blocklists.
*/
@SerialName("custom_blocklists")
val customBlocklists: List<ContentFilterBlocklistIdResult>? = null,

/**
* Describes an error returned if the content filtering system is
* down or otherwise unable to complete the operation in time.
*/
@SerialName("error")
val error: ResponseError? = null,

/**
* Whether a jailbreak attempt was detected in the prompt.
*/
@SerialName("jailbreak")
val jailbreak: ContentFilterDetectionResult? = null
)
Loading

0 comments on commit 6246819

Please sign in to comment.