2024-03-03 15:20:30 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
use crate::v1::common;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-03-03 19:10:25 +01:00
|
|
|
pub struct ChatCompletionParams {
|
2024-03-03 15:20:30 +01:00
|
|
|
pub tools: Option<String>,
|
|
|
|
|
pub temperature: Option<f32>,
|
|
|
|
|
pub max_tokens: Option<u32>,
|
|
|
|
|
pub top_p: Option<f32>,
|
|
|
|
|
pub random_seed: Option<u32>,
|
|
|
|
|
pub stream: Option<bool>,
|
|
|
|
|
pub safe_prompt: Option<bool>,
|
|
|
|
|
}
|
2024-03-03 19:10:25 +01:00
|
|
|
impl Default for ChatCompletionParams {
|
2024-03-03 15:20:30 +01:00
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
tools: None,
|
|
|
|
|
temperature: None,
|
|
|
|
|
max_tokens: None,
|
|
|
|
|
top_p: None,
|
|
|
|
|
random_seed: None,
|
|
|
|
|
stream: None,
|
|
|
|
|
safe_prompt: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct ChatCompletionRequest {
|
|
|
|
|
pub messages: Vec<ChatCompletionMessage>,
|
|
|
|
|
pub model: String,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub tools: Option<String>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub temperature: Option<f32>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub max_tokens: Option<u32>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub top_p: Option<f32>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub random_seed: Option<u32>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub stream: Option<bool>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub safe_prompt: Option<bool>,
|
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>,
|
|
|
|
|
}
|
|
|
|
|
impl ChatCompletionRequest {
|
|
|
|
|
pub fn new(
|
|
|
|
|
model: String,
|
|
|
|
|
messages: Vec<ChatCompletionMessage>,
|
2024-03-03 19:10:25 +01:00
|
|
|
options: Option<ChatCompletionParams>,
|
2024-03-03 15:20:30 +01:00
|
|
|
) -> Self {
|
2024-03-03 19:10:25 +01:00
|
|
|
let ChatCompletionParams {
|
2024-03-03 15:20:30 +01:00
|
|
|
tools,
|
|
|
|
|
temperature,
|
|
|
|
|
max_tokens,
|
|
|
|
|
top_p,
|
|
|
|
|
random_seed,
|
|
|
|
|
stream,
|
|
|
|
|
safe_prompt,
|
|
|
|
|
} = options.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
messages,
|
|
|
|
|
model,
|
|
|
|
|
tools,
|
|
|
|
|
temperature,
|
|
|
|
|
max_tokens,
|
|
|
|
|
top_p,
|
|
|
|
|
random_seed,
|
|
|
|
|
stream,
|
|
|
|
|
safe_prompt,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
pub struct ChatCompletionResponse {
|
|
|
|
|
pub id: String,
|
|
|
|
|
pub object: String,
|
|
|
|
|
/// Unix timestamp (in seconds).
|
|
|
|
|
pub created: u32,
|
|
|
|
|
pub model: String,
|
|
|
|
|
pub choices: Vec<ChatCompletionChoice>,
|
|
|
|
|
pub usage: common::ResponseUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
pub struct ChatCompletionChoice {
|
|
|
|
|
pub index: u32,
|
|
|
|
|
pub message: ChatCompletionMessage,
|
|
|
|
|
pub finish_reason: String,
|
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: ???
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
pub struct ChatCompletionMessage {
|
|
|
|
|
pub role: ChatCompletionMessageRole,
|
|
|
|
|
pub content: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
|
pub enum ChatCompletionMessageRole {
|
|
|
|
|
assistant,
|
|
|
|
|
user,
|
|
|
|
|
}
|