feat: add SSE streaming, thinking models, and modernized Part struct
Some checks failed
Rust / build (push) Has been cancelled
Some checks failed
Rust / build (push) Has been cancelled
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::{collections::HashMap, fmt::Display, str::FromStr, vec};
|
||||
use std::{fmt::Display, str::FromStr, vec};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
|
||||
pub struct Content {
|
||||
@@ -13,10 +14,7 @@ impl Content {
|
||||
self.parts.as_ref().map(|parts| {
|
||||
parts
|
||||
.iter()
|
||||
.filter_map(|part| match part {
|
||||
Part::Text(text) => Some(text.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.filter_map(|part| part.text.clone())
|
||||
.collect::<String>()
|
||||
})
|
||||
}
|
||||
@@ -26,6 +24,27 @@ impl Content {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SystemPrompt {
|
||||
content: Content,
|
||||
}
|
||||
|
||||
impl<T: AsRef<str>> From<T> for SystemPrompt {
|
||||
fn from(value: T) -> Self {
|
||||
SystemPrompt {
|
||||
content: Content {
|
||||
parts: Some(vec![Part::text(value.as_ref())]),
|
||||
role: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SystemPrompt> for Content {
|
||||
fn from(value: SystemPrompt) -> Self {
|
||||
value.content
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ContentBuilder {
|
||||
content: Content,
|
||||
@@ -33,7 +52,7 @@ pub struct ContentBuilder {
|
||||
|
||||
impl ContentBuilder {
|
||||
pub fn add_text_part<T: Into<String>>(self, text: T) -> Self {
|
||||
self.add_part(Part::Text(text.into()))
|
||||
self.add_part(Part::text(text))
|
||||
}
|
||||
|
||||
pub fn add_part(mut self, part: Part) -> Self {
|
||||
@@ -54,7 +73,7 @@ impl ContentBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Role {
|
||||
User,
|
||||
@@ -83,83 +102,83 @@ impl FromStr for Role {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum Part {
|
||||
Text(String),
|
||||
InlineData {
|
||||
mime_type: String,
|
||||
data: String,
|
||||
},
|
||||
FileData {
|
||||
mime_type: String,
|
||||
file_uri: String,
|
||||
},
|
||||
FunctionCall {
|
||||
name: String,
|
||||
args: HashMap<String, String>,
|
||||
},
|
||||
pub struct Part {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub text: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub inline_data: Option<InlineData>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub file_data: Option<FileData>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub function_call: Option<FunctionCallData>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub function_response: Option<FunctionResponseData>,
|
||||
/// Opaque signature emitted by Gemini 2.5+ thinking models on function-call
|
||||
/// parts. Must be echoed back on the same part — and on the corresponding
|
||||
/// `functionResponse` part — in subsequent turns, otherwise Vertex rejects
|
||||
/// the request with "function call X is missing a thought_signature".
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub thought_signature: Option<String>,
|
||||
/// `Some(true)` on a response part marks it as a reasoning/thinking token
|
||||
/// (only emitted when `thinkingConfig.includeThoughts` is set on the
|
||||
/// request). Absent on regular text and on outgoing requests.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub thought: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct PartHelper {
|
||||
text: Option<String>,
|
||||
inline_data: Option<InlineDataHelper>,
|
||||
file_data: Option<FileDataHelper>,
|
||||
function_call: Option<FunctionCallHelper>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct InlineDataHelper {
|
||||
mime_type: String,
|
||||
data: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct FileDataHelper {
|
||||
mime_type: String,
|
||||
file_uri: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct FunctionCallHelper {
|
||||
name: String,
|
||||
args: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Part {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let helper = PartHelper::deserialize(deserializer)?;
|
||||
if let Some(text) = helper.text {
|
||||
return Ok(Part::Text(text));
|
||||
impl Part {
|
||||
pub fn text(text: impl Into<String>) -> Self {
|
||||
Part {
|
||||
text: Some(text.into()),
|
||||
..Default::default()
|
||||
}
|
||||
if let Some(inline_data) = helper.inline_data {
|
||||
return Ok(Part::InlineData {
|
||||
mime_type: inline_data.mime_type,
|
||||
data: inline_data.data,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn function_call(name: impl Into<String>, args: Value) -> Self {
|
||||
Part {
|
||||
function_call: Some(FunctionCallData {
|
||||
name: name.into(),
|
||||
args,
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
if let Some(file_data) = helper.file_data {
|
||||
return Ok(Part::FileData {
|
||||
mime_type: file_data.mime_type,
|
||||
file_uri: file_data.file_uri,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn function_response(name: impl Into<String>, response: Value) -> Self {
|
||||
Part {
|
||||
function_response: Some(FunctionResponseData {
|
||||
name: name.into(),
|
||||
response,
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
if let Some(function_call) = helper.function_call {
|
||||
return Ok(Part::FunctionCall {
|
||||
name: function_call.name,
|
||||
args: function_call.args,
|
||||
});
|
||||
}
|
||||
Err(serde::de::Error::custom(
|
||||
"Part does not contain any recognizable variant",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct FunctionCallData {
|
||||
pub name: String,
|
||||
pub args: Value,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct FunctionResponseData {
|
||||
pub name: String,
|
||||
pub response: Value,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct InlineData {
|
||||
pub mime_type: String,
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileData {
|
||||
pub mime_type: String,
|
||||
pub file_uri: String,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user