2024-03-03 15:20:30 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2024-03-09 11:28:50 +01:00
|
|
|
use crate::v1::{common, constants, tool};
|
2024-03-03 15:20:30 +01:00
|
|
|
|
2024-03-04 08:16:06 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Definitions
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
pub struct ChatMessage {
|
|
|
|
|
pub role: ChatMessageRole,
|
|
|
|
|
pub content: String,
|
2024-03-09 11:28:50 +01:00
|
|
|
pub tool_calls: Option<Vec<tool::ToolCall>>,
|
|
|
|
|
}
|
|
|
|
|
impl ChatMessage {
|
|
|
|
|
pub fn new_assistant_message(content: &str, tool_calls: Option<Vec<tool::ToolCall>>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
role: ChatMessageRole::Assistant,
|
|
|
|
|
content: content.to_string(),
|
|
|
|
|
tool_calls,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_user_message(content: &str) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
role: ChatMessageRole::User,
|
|
|
|
|
content: content.to_string(),
|
|
|
|
|
tool_calls: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-04 08:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-09 11:28:50 +01:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
2024-03-04 08:16:06 +01:00
|
|
|
pub enum ChatMessageRole {
|
2024-03-09 11:28:50 +01:00
|
|
|
#[serde(rename = "assistant")]
|
|
|
|
|
Assistant,
|
|
|
|
|
#[serde(rename = "user")]
|
|
|
|
|
User,
|
2024-03-04 08:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Request
|
|
|
|
|
|
2024-03-03 15:20:30 +01:00
|
|
|
#[derive(Debug)]
|
2024-03-09 11:28:50 +01:00
|
|
|
pub struct ChatParams {
|
2024-03-03 15:20:30 +01:00
|
|
|
pub max_tokens: Option<u32>,
|
|
|
|
|
pub random_seed: Option<u32>,
|
|
|
|
|
pub safe_prompt: Option<bool>,
|
2024-03-09 11:28:50 +01:00
|
|
|
pub temperature: Option<f32>,
|
|
|
|
|
pub tool_choice: Option<tool::ToolChoice>,
|
|
|
|
|
pub tools: Option<Vec<tool::Tool>>,
|
|
|
|
|
pub top_p: Option<f32>,
|
2024-03-03 15:20:30 +01:00
|
|
|
}
|
2024-03-09 11:28:50 +01:00
|
|
|
impl Default for ChatParams {
|
2024-03-03 15:20:30 +01:00
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
max_tokens: None,
|
|
|
|
|
random_seed: None,
|
|
|
|
|
safe_prompt: None,
|
2024-03-09 11:28:50 +01:00
|
|
|
temperature: None,
|
|
|
|
|
tool_choice: None,
|
|
|
|
|
tools: None,
|
|
|
|
|
top_p: None,
|
2024-03-03 15:20:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2024-03-09 11:28:50 +01:00
|
|
|
pub struct ChatRequest {
|
2024-03-04 08:16:06 +01:00
|
|
|
pub messages: Vec<ChatMessage>,
|
2024-03-04 03:14:23 +01:00
|
|
|
pub model: constants::Model,
|
2024-03-09 11:28:50 +01:00
|
|
|
|
2024-03-03 15:20:30 +01:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub max_tokens: Option<u32>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub random_seed: Option<u32>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub safe_prompt: Option<bool>,
|
2024-03-09 11:28:50 +01:00
|
|
|
pub stream: bool,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub temperature: Option<f32>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub tool_choice: Option<tool::ToolChoice>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub tools: Option<Vec<tool::Tool>>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub top_p: Option<f32>,
|
2024-03-03 19:38:34 +01:00
|
|
|
// TODO Check this prop (seen in official Python client but not in API doc).
|
2024-03-03 15:20:30 +01:00
|
|
|
// pub tool_choice: Option<String>,
|
2024-03-03 19:38:34 +01:00
|
|
|
// TODO Check this prop (seen in official Python client but not in API doc).
|
2024-03-03 15:20:30 +01:00
|
|
|
// pub response_format: Option<String>,
|
|
|
|
|
}
|
2024-03-09 11:28:50 +01:00
|
|
|
impl ChatRequest {
|
2024-03-03 15:20:30 +01:00
|
|
|
pub fn new(
|
2024-03-04 03:14:23 +01:00
|
|
|
model: constants::Model,
|
2024-03-04 08:16:06 +01:00
|
|
|
messages: Vec<ChatMessage>,
|
|
|
|
|
stream: bool,
|
2024-03-09 11:28:50 +01:00
|
|
|
options: Option<ChatParams>,
|
2024-03-03 15:20:30 +01:00
|
|
|
) -> Self {
|
2024-03-09 11:28:50 +01:00
|
|
|
let ChatParams {
|
2024-03-03 15:20:30 +01:00
|
|
|
max_tokens,
|
|
|
|
|
random_seed,
|
|
|
|
|
safe_prompt,
|
2024-03-09 11:28:50 +01:00
|
|
|
temperature,
|
|
|
|
|
tool_choice,
|
|
|
|
|
tools,
|
|
|
|
|
top_p,
|
2024-03-03 15:20:30 +01:00
|
|
|
} = options.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
messages,
|
|
|
|
|
model,
|
2024-03-09 11:28:50 +01:00
|
|
|
|
2024-03-03 15:20:30 +01:00
|
|
|
max_tokens,
|
|
|
|
|
random_seed,
|
|
|
|
|
safe_prompt,
|
2024-03-09 11:28:50 +01:00
|
|
|
stream,
|
|
|
|
|
temperature,
|
|
|
|
|
tool_choice,
|
|
|
|
|
tools,
|
|
|
|
|
top_p,
|
2024-03-03 15:20:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 08:16:06 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Response
|
|
|
|
|
|
2024-03-03 15:20:30 +01:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2024-03-09 11:28:50 +01:00
|
|
|
pub struct ChatResponse {
|
2024-03-03 15:20:30 +01:00
|
|
|
pub id: String,
|
|
|
|
|
pub object: String,
|
|
|
|
|
/// Unix timestamp (in seconds).
|
|
|
|
|
pub created: u32,
|
2024-03-04 03:14:23 +01:00
|
|
|
pub model: constants::Model,
|
2024-03-09 11:28:50 +01:00
|
|
|
pub choices: Vec<ChatResponseChoice>,
|
2024-03-03 15:20:30 +01:00
|
|
|
pub usage: common::ResponseUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2024-03-09 11:28:50 +01:00
|
|
|
pub struct ChatResponseChoice {
|
2024-03-03 15:20:30 +01:00
|
|
|
pub index: u32,
|
2024-03-04 08:16:06 +01:00
|
|
|
pub message: ChatMessage,
|
2024-03-09 11:28:50 +01:00
|
|
|
pub finish_reason: ChatResponseChoiceFinishReason,
|
2024-03-03 19:38:34 +01:00
|
|
|
// TODO Check this prop (seen in API responses but undocumented).
|
2024-03-03 15:20:30 +01:00
|
|
|
// pub logprobs: ???
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 11:28:50 +01:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
|
|
|
|
pub enum ChatResponseChoiceFinishReason {
|
|
|
|
|
#[serde(rename = "stop")]
|
|
|
|
|
Stop,
|
|
|
|
|
#[serde(rename = "tool_calls")]
|
|
|
|
|
ToolCalls,
|
2024-03-03 15:20:30 +01:00
|
|
|
}
|