use std::fmt::Display;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericMessage {
pub content: Option<String>,
pub role: GenericRole,
pub name: Option<String>,
pub tool_calls: Option<Vec<GenericFunctionCallIntent>>,
pub tool_call_id: Option<String>,
}
impl GenericMessage {
pub fn new(message: String, role: GenericRole) -> Self {
Self {
content: Some(message),
role,
name: None,
tool_call_id: None,
tool_calls: None,
}
}
pub fn new_tool_call(tool_call_id: String, tool_calls: Vec<GenericFunctionCallIntent>) -> Self {
Self {
content: None,
role: GenericRole::Assistant,
name: None,
tool_calls: Some(tool_calls),
tool_call_id: Some(tool_call_id),
}
}
pub fn with_name(mut self, name: impl ToString) -> Self {
self.name = Some(name.to_string());
self
}
pub fn with_tool_call_id(mut self, tool_call_id: impl ToString) -> Self {
self.tool_call_id = Some(tool_call_id.to_string());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GenericRole {
System,
Assistant,
User,
Tool,
}
impl Display for GenericRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GenericRole::System => write!(f, "system"),
GenericRole::Assistant => write!(f, "assistant"),
GenericRole::User => write!(f, "user"),
GenericRole::Tool => write!(f, "tool"),
}
}
}
#[derive(Debug)]
pub struct GenericChatCompletionResponse<T> {
pub content: ResponseContent<T>,
pub usage: Option<GenericUsageReport>,
}
#[derive(Debug)]
pub enum ResponseContent<T> {
Finished(T),
ToolCalls(GenericMessage),
}
#[derive(Debug, Clone)]
pub struct GenericUsageReport {
pub prompt_tokens: i64,
pub completion_tokens: i64,
pub total_tokens: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericFunctionCallIntent {
pub id: String,
pub function: GenericFunctionCall,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericFunctionCall {
pub name: String,
pub arguments: serde_json::Value,
}
#[derive(Debug, Clone, Copy)]
pub enum GenericStramingChatChunk {
Created,
Completed,
Failed,
OutputTextDelta,
OutputTextDone,
}
#[derive(Debug, Clone)]
pub struct GenericFunctionSpec {
pub name: String,
pub description: String,
pub parameters: serde_json::Value,
}
#[derive(Debug, Clone)]
pub enum StreamEvent {
TextDelta(String),
ToolCallStart {
index: usize,
id: Option<String>,
name: Option<String>,
},
ToolCallArgumentsDelta {
index: usize,
arguments_fragment: String,
},
ToolCallComplete {
index: usize,
intent: GenericFunctionCallIntent,
},
MessageEnd,
Usage(GenericUsageReport),
}
pub trait StreamingEventsProvider: crate::provider::ChatCompletionProvider {
type EventStream<'s>: futures_core::stream::Stream<Item = crate::error::Result<StreamEvent>>
+ Send
+ 's
where
Self: 's;
fn chat_complete_events_stream<'p, M>(
&self,
params: crate::provider::ChatCompleteParameters<M>,
) -> Self::EventStream<'p>
where
M: Into<Self::Message> + Send + Sync + 'p;
}