feat!: add client.chat_stream() method
BREAKING CHANGE: You can't set the `stream` option for `client.chat*()`. Either use `client.chat_stream()` if you want to use streams or use `client.chat()` / `client.chat_async()` otherwise.
This commit is contained in:
@@ -2,6 +2,25 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::v1::{common, constants};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Definitions
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ChatMessage {
|
||||
pub role: ChatMessageRole,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum ChatMessageRole {
|
||||
assistant,
|
||||
user,
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Request
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ChatCompletionParams {
|
||||
pub tools: Option<String>,
|
||||
@@ -9,7 +28,6 @@ pub struct ChatCompletionParams {
|
||||
pub max_tokens: Option<u32>,
|
||||
pub top_p: Option<f32>,
|
||||
pub random_seed: Option<u32>,
|
||||
pub stream: Option<bool>,
|
||||
pub safe_prompt: Option<bool>,
|
||||
}
|
||||
impl Default for ChatCompletionParams {
|
||||
@@ -20,7 +38,6 @@ impl Default for ChatCompletionParams {
|
||||
max_tokens: None,
|
||||
top_p: None,
|
||||
random_seed: None,
|
||||
stream: None,
|
||||
safe_prompt: None,
|
||||
}
|
||||
}
|
||||
@@ -28,7 +45,7 @@ impl Default for ChatCompletionParams {
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ChatCompletionRequest {
|
||||
pub messages: Vec<ChatCompletionMessage>,
|
||||
pub messages: Vec<ChatMessage>,
|
||||
pub model: constants::Model,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tools: Option<String>,
|
||||
@@ -40,8 +57,7 @@ pub struct ChatCompletionRequest {
|
||||
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>,
|
||||
pub stream: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub safe_prompt: Option<bool>,
|
||||
// TODO Check this prop (seen in official Python client but not in API doc).
|
||||
@@ -52,7 +68,8 @@ pub struct ChatCompletionRequest {
|
||||
impl ChatCompletionRequest {
|
||||
pub fn new(
|
||||
model: constants::Model,
|
||||
messages: Vec<ChatCompletionMessage>,
|
||||
messages: Vec<ChatMessage>,
|
||||
stream: bool,
|
||||
options: Option<ChatCompletionParams>,
|
||||
) -> Self {
|
||||
let ChatCompletionParams {
|
||||
@@ -61,7 +78,6 @@ impl ChatCompletionRequest {
|
||||
max_tokens,
|
||||
top_p,
|
||||
random_seed,
|
||||
stream,
|
||||
safe_prompt,
|
||||
} = options.unwrap_or_default();
|
||||
|
||||
@@ -79,6 +95,9 @@ impl ChatCompletionRequest {
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Response
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ChatCompletionResponse {
|
||||
pub id: String,
|
||||
@@ -86,28 +105,45 @@ pub struct ChatCompletionResponse {
|
||||
/// Unix timestamp (in seconds).
|
||||
pub created: u32,
|
||||
pub model: constants::Model,
|
||||
pub choices: Vec<ChatCompletionChoice>,
|
||||
pub choices: Vec<ChatCompletionResponseChoice>,
|
||||
pub usage: common::ResponseUsage,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ChatCompletionChoice {
|
||||
pub struct ChatCompletionResponseChoice {
|
||||
pub index: u32,
|
||||
pub message: ChatCompletionMessage,
|
||||
pub message: ChatMessage,
|
||||
pub finish_reason: String,
|
||||
// TODO Check this prop (seen in API responses but undocumented).
|
||||
// pub logprobs: ???
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ChatCompletionMessage {
|
||||
pub role: ChatCompletionMessageRole,
|
||||
pub content: String,
|
||||
// -----------------------------------------------------------------------------
|
||||
// Stream
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ChatCompletionStreamChunk {
|
||||
pub id: String,
|
||||
pub object: String,
|
||||
/// Unix timestamp (in seconds).
|
||||
pub created: u32,
|
||||
pub model: constants::Model,
|
||||
pub choices: Vec<ChatCompletionStreamChunkChoice>,
|
||||
// TODO Check this prop (seen in API responses but undocumented).
|
||||
// pub usage: ???,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum ChatCompletionMessageRole {
|
||||
assistant,
|
||||
user,
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ChatCompletionStreamChunkChoice {
|
||||
pub index: u32,
|
||||
pub delta: ChatCompletionStreamChunkChoiceDelta,
|
||||
pub finish_reason: Option<String>,
|
||||
// TODO Check this prop (seen in API responses but undocumented).
|
||||
// pub logprobs: ???,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ChatCompletionStreamChunkChoiceDelta {
|
||||
pub role: Option<ChatMessageRole>,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user