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

output detection for chat completions #288

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/clients/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::{create_http_client, http::HttpClientExt, Client, Error, HttpClient};
use crate::{
config::ServiceConfig,
health::HealthCheckResult,
models::{DetectorParams, GuardrailDetection, InputWarningReason},
models::{DetectionWarningReason, DetectorParams, GuardrailDetection},
};

const DEFAULT_PORT: u16 = 8080;
Expand Down Expand Up @@ -724,20 +724,28 @@ pub struct InputDetectionResult {
/// Guardrails detection result for application output.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputDetectionResult {
choice_index: usize,
pub choice_index: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
results: Vec<GuardrailDetection>,
pub results: Vec<GuardrailDetection>,
}

/// Represents the input and output of detection results following processing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectionResult {
pub index: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub results: Vec<GuardrailDetection>,
}

/// Warnings generated by guardrails.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrchestratorWarning {
r#type: InputWarningReason,
r#type: DetectionWarningReason,
message: String,
}

impl OrchestratorWarning {
pub fn new(warning_type: InputWarningReason, message: &str) -> Self {
pub fn new(warning_type: DetectionWarningReason, message: &str) -> Self {
Self {
r#type: warning_type,
message: message.to_string(),
Expand Down
14 changes: 9 additions & 5 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ pub struct ClassifiedGeneratedTextResult {

/// Vector of warnings on input detection
#[serde(skip_serializing_if = "Option::is_none")]
pub warnings: Option<Vec<InputWarning>>,
pub warnings: Option<Vec<DetectionWarning>>,

/// Individual generated tokens and associated details, if requested
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -406,7 +406,7 @@ pub struct ClassifiedGeneratedTextStreamResult {

/// Vector of warnings on input detection
#[serde(skip_serializing_if = "Option::is_none")]
pub warnings: Option<Vec<InputWarning>>,
pub warnings: Option<Vec<DetectionWarning>>,

/// Individual generated tokens and associated details, if requested
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -498,10 +498,10 @@ pub enum FinishReason {

/// Warning reason and message on input detection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InputWarning {
pub struct DetectionWarning {
/// Warning reason
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<InputWarningReason>,
pub id: Option<DetectionWarningReason>,

/// Warning message
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -513,10 +513,14 @@ pub struct InputWarning {
/// which helps with FFI.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum InputWarningReason {
pub enum DetectionWarningReason {
/// Unsuitable text detected on input
resoluteCoder marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename = "UNSUITABLE_INPUT")]
UnsuitableInput,

/// Unsuitable text detected on output
#[serde(rename = "UNSUITABLE_OUTPUT")]
UnsuitableOutput,
}

/// Generated token information
Expand Down
2 changes: 2 additions & 0 deletions src/orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const UNSUITABLE_INPUT_MESSAGE: &str = "Unsuitable input detected. \
Please check the detected entities on your input and try again \
with the unsuitable input removed.";

const UNSUITABLE_OUTPUT_MESSAGE: &str = "Unsuitable output detected.";

#[cfg_attr(test, derive(Default))]
pub struct Context {
config: OrchestratorConfig,
Expand Down
Loading