use std::collections::HashMap;
use serde_json::Value;
use crate::audio::{AudioOutput, AudioResponse, TranscriptionResponse};
use crate::embeddings::{Embedding, EmbeddingsResponse};
use crate::images::{GeneratedImage, ImagesResponse};
use crate::moderation::{ModerationResponse, ModerationResult};
use crate::rerank::{RankedDocument, RerankResponse};
use crate::structured::StructuredResponse;
use crate::text::Step;
use crate::value_objects::{EmbeddingsUsage, FinishReason, MediaData, Meta, ToolCall, Usage};
pub struct FakeTextResponse {
step: Step,
}
impl FakeTextResponse {
pub fn new(text: impl Into<String>) -> Self {
let text = text.into();
Self {
step: Step {
text: if text.is_empty() { None } else { Some(text) },
tool_calls: Vec::new(),
finish_reason: FinishReason::Stop,
usage: Usage::default(),
meta: Meta::default(),
},
}
}
pub fn with_tool_call(
mut self,
id: impl Into<String>,
name: impl Into<String>,
arguments: Value,
) -> Self {
self.step.tool_calls.push(ToolCall {
id: id.into(),
name: name.into(),
arguments,
});
self.step.finish_reason = FinishReason::ToolCalls;
self
}
pub fn with_usage(mut self, usage: Usage) -> Self {
self.step.usage = usage;
self
}
pub fn with_finish_reason(mut self, finish_reason: FinishReason) -> Self {
self.step.finish_reason = finish_reason;
self
}
pub fn build(self) -> Step {
self.step
}
}
impl From<FakeTextResponse> for Step {
fn from(fixture: FakeTextResponse) -> Self {
fixture.build()
}
}
pub struct FakeStructuredResponse {
response: StructuredResponse,
}
impl FakeStructuredResponse {
pub fn new(data: Value) -> Self {
Self {
response: StructuredResponse {
data,
finish_reason: FinishReason::Stop,
usage: Usage::default(),
meta: Meta::default(),
},
}
}
pub fn with_usage(mut self, usage: Usage) -> Self {
self.response.usage = usage;
self
}
pub fn with_finish_reason(mut self, finish_reason: FinishReason) -> Self {
self.response.finish_reason = finish_reason;
self
}
pub fn build(self) -> StructuredResponse {
self.response
}
}
impl From<FakeStructuredResponse> for StructuredResponse {
fn from(fixture: FakeStructuredResponse) -> Self {
fixture.build()
}
}
pub struct FakeModerationResponse {
response: ModerationResponse,
}
impl FakeModerationResponse {
pub fn new() -> Self {
Self {
response: ModerationResponse {
results: Vec::new(),
meta: Meta::default(),
},
}
}
pub fn flagged(mut self, flagged: bool) -> Self {
self.response.results.push(ModerationResult {
flagged,
categories: HashMap::new(),
category_scores: HashMap::new(),
});
self
}
pub fn with_result(mut self, result: ModerationResult) -> Self {
self.response.results.push(result);
self
}
pub fn build(self) -> ModerationResponse {
self.response
}
}
impl Default for FakeModerationResponse {
fn default() -> Self {
Self::new()
}
}
impl From<FakeModerationResponse> for ModerationResponse {
fn from(fixture: FakeModerationResponse) -> Self {
fixture.build()
}
}
pub struct FakeEmbeddingsResponse {
response: EmbeddingsResponse,
}
impl FakeEmbeddingsResponse {
pub fn new() -> Self {
Self {
response: EmbeddingsResponse {
embeddings: Vec::new(),
usage: EmbeddingsUsage::default(),
meta: Meta::default(),
},
}
}
pub fn with_embedding(mut self, vector: Vec<f32>) -> Self {
self.response.embeddings.push(Embedding { vector });
self
}
pub fn with_usage(mut self, usage: EmbeddingsUsage) -> Self {
self.response.usage = usage;
self
}
pub fn build(self) -> EmbeddingsResponse {
self.response
}
}
impl Default for FakeEmbeddingsResponse {
fn default() -> Self {
Self::new()
}
}
impl From<FakeEmbeddingsResponse> for EmbeddingsResponse {
fn from(fixture: FakeEmbeddingsResponse) -> Self {
fixture.build()
}
}
pub struct FakeRerankResponse {
response: RerankResponse,
}
impl FakeRerankResponse {
pub fn new() -> Self {
Self {
response: RerankResponse {
results: Vec::new(),
usage: EmbeddingsUsage::default(),
meta: Meta::default(),
},
}
}
pub fn with_result(mut self, index: usize, relevance_score: f32) -> Self {
self.response.results.push(RankedDocument {
index,
relevance_score,
document: None,
});
self
}
pub fn with_usage(mut self, usage: EmbeddingsUsage) -> Self {
self.response.usage = usage;
self
}
pub fn build(self) -> RerankResponse {
self.response
}
}
impl Default for FakeRerankResponse {
fn default() -> Self {
Self::new()
}
}
impl From<FakeRerankResponse> for RerankResponse {
fn from(fixture: FakeRerankResponse) -> Self {
fixture.build()
}
}
pub struct FakeImagesResponse {
response: ImagesResponse,
}
impl FakeImagesResponse {
pub fn new() -> Self {
Self {
response: ImagesResponse {
images: Vec::new(),
meta: Meta::default(),
},
}
}
pub fn with_url(mut self, url: impl Into<String>) -> Self {
self.response.images.push(GeneratedImage {
data: MediaData::Url(url.into()),
revised_prompt: None,
});
self
}
pub fn with_base64(mut self, data: impl Into<String>) -> Self {
self.response.images.push(GeneratedImage {
data: MediaData::Base64(data.into()),
revised_prompt: None,
});
self
}
pub fn build(self) -> ImagesResponse {
self.response
}
}
impl Default for FakeImagesResponse {
fn default() -> Self {
Self::new()
}
}
impl From<FakeImagesResponse> for ImagesResponse {
fn from(fixture: FakeImagesResponse) -> Self {
fixture.build()
}
}
pub struct FakeAudioResponse {
response: AudioResponse,
}
impl FakeAudioResponse {
pub fn new(data: Vec<u8>, mime_type: impl Into<String>) -> Self {
Self {
response: AudioResponse {
audio: AudioOutput {
data,
mime_type: mime_type.into(),
},
meta: Meta::default(),
},
}
}
pub fn build(self) -> AudioResponse {
self.response
}
}
impl From<FakeAudioResponse> for AudioResponse {
fn from(fixture: FakeAudioResponse) -> Self {
fixture.build()
}
}
pub struct FakeTranscriptionResponse {
response: TranscriptionResponse,
}
impl FakeTranscriptionResponse {
pub fn new(text: impl Into<String>) -> Self {
Self {
response: TranscriptionResponse {
text: text.into(),
meta: Meta::default(),
},
}
}
pub fn build(self) -> TranscriptionResponse {
self.response
}
}
impl From<FakeTranscriptionResponse> for TranscriptionResponse {
fn from(fixture: FakeTranscriptionResponse) -> Self {
fixture.build()
}
}