use std::collections::BTreeMap;
use http::HeaderMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct Response {
pub model: String,
pub answers: BTreeMap<String, Answer>,
#[serde(default)]
pub usage: Option<Usage>,
#[serde(skip)]
pub request_id: Option<String>,
#[serde(skip)]
pub headers: HeaderMap,
#[serde(skip)]
pub raw_body: Vec<u8>,
}
impl Response {
pub fn answer(&self, id: &str) -> Option<&Answer> {
self.answers.get(id)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Usage {
#[serde(default)]
pub input_tokens: Option<u64>,
#[serde(default)]
pub output_tokens: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Answer {
Noul {
noul: f64,
},
Choice {
choice: String,
probabilities: BTreeMap<String, f64>,
confidence: f64,
},
Score {
score: f64,
legend: BTreeMap<String, Value>,
probabilities: BTreeMap<String, f64>,
confidence: f64,
},
}
impl Answer {
pub fn noul(&self) -> Option<f64> {
match self {
Self::Noul { noul } => Some(*noul),
_ => None,
}
}
pub fn choice(&self) -> Option<&str> {
match self {
Self::Choice { choice, .. } => Some(choice),
_ => None,
}
}
pub fn score(&self) -> Option<f64> {
match self {
Self::Score { score, .. } => Some(*score),
_ => None,
}
}
pub fn confidence(&self) -> Option<f64> {
match self {
Self::Choice { confidence, .. } | Self::Score { confidence, .. } => Some(*confidence),
Self::Noul { .. } => None,
}
}
pub fn probabilities(&self) -> Option<&BTreeMap<String, f64>> {
match self {
Self::Choice { probabilities, .. } | Self::Score { probabilities, .. } => {
Some(probabilities)
}
Self::Noul { .. } => None,
}
}
}