use crate::openai::misc::Usage;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Deserialize, Debug, Clone)]
pub struct Response {
pub id: Option<String>,
pub object: Option<String>,
pub created: Option<u64>,
pub model: Option<String>,
pub choices: Option<Vec<Choice>>,
pub usage: Option<Usage>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Choice {
pub message: Message,
pub finish_reason: String,
pub index: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StreamedReponse {
pub id: String,
pub object: String,
pub created: u64,
pub model: String,
pub choices: Vec<StreamedChoices>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StreamedChoices {
pub index: u64,
pub delta: Delta,
pub finish_reason: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Delta {
pub role: Option<String>,
pub content: Option<String>,
}
#[derive(Clone, Debug, Copy)]
pub enum MessageRole {
User,
Assistant,
System,
Function,
}
impl ToString for MessageRole {
fn to_string(&self) -> String {
match self {
Self::User => "user".to_string(),
Self::Assistant => "assistant".to_string(),
Self::System => "system".to_string(),
Self::Function => "function".to_string(),
}
}
}
impl<T: Into<String>> From<T> for MessageRole {
fn from(s: T) -> Self {
match s.into().as_str() {
"assistant" => Self::Assistant,
"system" => Self::System,
"function" => Self::Function,
_ => Self::User,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Message {
pub role: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub function_call: Option<FunctionCall>,
}
impl Message {
pub fn new<S: Into<String>>(role: &MessageRole, content: S) -> Self {
Self {
role: role.to_string(),
content: content.into(),
name: None,
function_call: None,
}
}
}
impl<T: Into<String>> From<T> for Message {
fn from(s: T) -> Self {
Self {
role: MessageRole::User.to_string(),
content: s.into(),
name: None,
function_call: None,
}
}
}
impl std::fmt::Display for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let role = match self.role.as_str() {
"assistant" => MessageRole::Assistant,
"system" => MessageRole::System,
"function" => MessageRole::Function,
_ => MessageRole::User,
};
write!(f, "{}: {}", role.to_string(), self.content)
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FunctionCall {
pub name: String,
pub arguments: String,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum Stop {
String(String),
Array(Vec<String>),
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Chat {
pub model: String,
pub messages: Vec<Message>,
#[serde(skip_serializing_if = "Option::is_none")]
pub functions: Option<Vec<Function>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub function_call: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub n: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stream: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stop: Option<Stop>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub presence_penalty: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub frequency_penalty: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub logit_bias: Option<HashMap<String, f32>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
}
impl Chat {
const DEFAULT_TEMPERATURE: f64 = 1.0;
const DEFAULT_MAX_TOKENS: u64 = 2048;
const DEFAULT_STREAM_RESPONSE: bool = true;
const DEFAULT_MODEL: &str = "gpt-3.5-turbo";
pub fn get_default_temperature() -> f64 {
Self::DEFAULT_TEMPERATURE
}
pub fn get_default_max_tokens() -> u64 {
Self::DEFAULT_MAX_TOKENS
}
pub fn get_default_stream() -> bool {
Self::DEFAULT_STREAM_RESPONSE
}
pub fn get_default_model() -> &'static str {
Self::DEFAULT_MODEL
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Function {
pub name: String,
pub description: Option<String>,
pub parameters: String,
}