Skip to content

Commit 9c63daf

Browse files
committed
feat: refactor base64 encoding to use prelude for image handling
1 parent 59f5d0e commit 9c63daf

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

src/llm/anthropic.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::llm::{
44
};
55
use anyhow::{Context, Result, bail};
66
use async_trait::async_trait;
7-
use base64::Engine;
7+
use base64::prelude::*;
88
use json5;
99
use serde_json::Value;
1010

@@ -42,8 +42,7 @@ impl LlmGenerationClient for Client {
4242

4343
// Add image part if present
4444
if let Some(image_bytes) = &request.image {
45-
let base64_image =
46-
base64::engine::general_purpose::STANDARD.encode(image_bytes.as_ref());
45+
let base64_image = BASE64_STANDARD.encode(image_bytes.as_ref());
4746
let mime_type = detect_image_mime_type(image_bytes.as_ref())?;
4847
user_content_parts.push(serde_json::json!({
4948
"type": "image",

src/llm/gemini.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::llm::{
44
LlmEmbeddingClient, LlmGenerateRequest, LlmGenerateResponse, LlmGenerationClient, OutputFormat,
55
ToJsonSchemaOptions, detect_image_mime_type,
66
};
7-
use base64::Engine;
7+
use base64::prelude::*;
88
use phf::phf_map;
99
use serde_json::Value;
1010
use urlencoding::encode;
@@ -78,8 +78,7 @@ impl LlmGenerationClient for Client {
7878

7979
// Add image part if present
8080
if let Some(image_bytes) = &request.image {
81-
let base64_image =
82-
base64::engine::general_purpose::STANDARD.encode(image_bytes.as_ref());
81+
let base64_image = BASE64_STANDARD.encode(image_bytes.as_ref());
8382
let mime_type = detect_image_mime_type(image_bytes.as_ref())?;
8483
user_parts.push(serde_json::json!({
8584
"inlineData": {

src/llm/openai.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use async_openai::{
1515
},
1616
};
1717
use async_trait::async_trait;
18+
use base64::prelude::*;
1819
use phf::phf_map;
1920

2021
static DEFAULT_EMBEDDING_DIMENSIONS: phf::Map<&str, u32> = phf_map! {
@@ -68,8 +69,7 @@ impl LlmGenerationClient for Client {
6869
// Add user message
6970
let user_message_content = match request.image {
7071
Some(img_bytes) => {
71-
use base64::{Engine as _, engine::general_purpose::STANDARD};
72-
let base64_image = STANDARD.encode(img_bytes.as_ref());
72+
let base64_image = BASE64_STANDARD.encode(img_bytes.as_ref());
7373
let mime_type = detect_image_mime_type(img_bytes.as_ref())?;
7474
let image_url = format!("data:{};base64,{}", mime_type, base64_image);
7575
ChatCompletionRequestUserMessageContent::Array(vec![

0 commit comments

Comments
 (0)