-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openai): support streaming (#10)
- Loading branch information
Showing
20 changed files
with
555 additions
and
408 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use std::env; | ||
|
||
pub fn stream_buffer_size_bytes() -> usize { | ||
env::var("STREAM_BUFFER_SIZE_BYTES") | ||
.unwrap_or_else(|_| "1000".to_string()) | ||
.parse::<usize>() | ||
.unwrap_or(1000) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod constants; | ||
pub mod lib; | ||
pub mod models; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Clone)] | ||
#[serde(untagged)] | ||
pub enum ChatMessageContent { | ||
String(String), | ||
Array(Vec<ChatMessageContentPart>), | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone)] | ||
pub struct ChatMessageContentPart { | ||
#[serde(rename = "type")] | ||
pub r#type: String, | ||
pub text: String, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone)] | ||
pub struct ChatCompletionMessage { | ||
pub role: String, | ||
pub content: ChatMessageContent, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub name: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct LogProbs { | ||
pub content: Vec<LogProbContent>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct LogProbContent { | ||
pub token: String, | ||
pub logprob: f32, | ||
pub bytes: Vec<u8>, | ||
pub top_logprobs: Vec<TopLogprob>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct TopLogprob { | ||
pub token: String, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub bytes: Option<Vec<i32>>, | ||
pub logprob: f64, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct ChatCompletionTokenLogprob { | ||
pub token: String, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub bytes: Option<Vec<i32>>, | ||
pub logprob: f64, | ||
pub top_logprobs: Vec<TopLogprob>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct ChoiceLogprobs { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub content: Option<Vec<ChatCompletionTokenLogprob>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub refusal: Option<Vec<ChatCompletionTokenLogprob>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
pub mod chat; | ||
pub mod common; | ||
pub mod completion; | ||
pub mod content; | ||
pub mod embeddings; | ||
pub mod logprob; | ||
pub mod streaming; | ||
pub mod tool_calls; | ||
pub mod usage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::logprob::ChoiceLogprobs; | ||
use super::tool_calls::ChoiceDeltaToolCall; | ||
use super::usage::Usage; | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct ChoiceDelta { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub content: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub role: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub tool_calls: Option<Vec<ChoiceDeltaToolCall>>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct Choice { | ||
pub delta: ChoiceDelta, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub finish_reason: Option<String>, | ||
pub index: u32, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub logprobs: Option<ChoiceLogprobs>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct ChatCompletionChunk { | ||
pub id: String, | ||
pub choices: Vec<Choice>, | ||
pub created: i64, | ||
pub model: String, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub service_tier: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub system_fingerprint: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub usage: Option<Usage>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct ChoiceDeltaFunctionCall { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub arguments: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub name: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct ChoiceDeltaToolCallFunction { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub arguments: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub name: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct ChoiceDeltaToolCall { | ||
pub index: i32, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub id: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub function: Option<ChoiceDeltaToolCallFunction>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub r#type: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct CompletionTokensDetails { | ||
pub accepted_prediction_tokens: Option<u32>, | ||
pub audio_tokens: Option<u32>, | ||
pub reasoning_tokens: Option<u32>, | ||
pub rejected_prediction_tokens: Option<u32>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct PromptTokensDetails { | ||
pub audio_tokens: Option<u32>, | ||
pub cached_tokens: Option<u32>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug, Default)] | ||
pub struct Usage { | ||
pub prompt_tokens: u32, | ||
pub completion_tokens: u32, | ||
pub total_tokens: u32, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub completion_tokens_details: Option<CompletionTokensDetails>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub prompt_tokens_details: Option<PromptTokensDetails>, | ||
} |
Oops, something went wrong.