use crate::openai::misc::Usage;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum InputType {
SingleString(String),
MultipleStrings(Vec<String>),
MultipleTokens(Vec<u64>),
}
impl InputType {
pub fn is_single_string(&self) -> bool {
matches!(self, Self::SingleString(_))
}
pub fn is_multiple_strings(&self) -> bool {
matches!(self, Self::MultipleStrings(_))
}
pub fn is_multiple_tokens(&self) -> bool {
matches!(self, Self::MultipleTokens(_))
}
pub fn new_single_string(input: String) -> Self {
Self::SingleString(input)
}
pub fn new_multiple_strings(input: Vec<String>) -> Self {
Self::MultipleStrings(input)
}
pub fn new_multiple_tokens(input: Vec<u64>) -> Self {
Self::MultipleTokens(input)
}
}
impl From<String> for InputType {
fn from(input: String) -> Self {
Self::SingleString(input)
}
}
impl From<&[u64]> for InputType {
fn from(input: &[u64]) -> Self {
Self::MultipleTokens(input.to_vec())
}
}
impl From<Vec<String>> for InputType {
fn from(input: Vec<String>) -> Self {
Self::MultipleStrings(input)
}
}
impl From<Vec<u64>> for InputType {
fn from(input: Vec<u64>) -> Self {
Self::MultipleTokens(input)
}
}
impl From<&str> for InputType {
fn from(input: &str) -> Self {
Self::SingleString(input.to_string())
}
}
#[derive(Deserialize, Debug, Clone)]
pub struct Response {
pub object: String,
pub data: Vec<Data>,
pub model: String,
pub usage: Usage,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Data {
pub object: String,
pub embedding: Vec<f64>,
pub index: u64,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Embedding {
pub model: String,
pub input: InputType,
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
}
impl Embedding {
const DEFAULT_MODEL: &'static str = "text-embedding-ada-002";
pub fn get_default_model() -> &'static str {
Self::DEFAULT_MODEL
}
}