Use patched anthropic-rs library to add Vision support to text conversations

Related to: https://github.com/AbdelStark/anthropic-rs/pull/11
This commit is contained in:
Slavi Pantaleev
2025-05-10 10:24:55 +03:00
parent de958208b2
commit 2f9c3dfce0
4 changed files with 15 additions and 30 deletions

3
Cargo.lock generated
View File

@@ -90,8 +90,7 @@ dependencies = [
[[package]] [[package]]
name = "anthropic" name = "anthropic"
version = "0.0.8" version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/etkecc/anthropic-rs.git?branch=fix-content-block-image#58d8492450304ca9c4d0fdbfd023668231862d2c"
checksum = "092086afb8afa8a2bf24820213e22a57662eace03bd6b3c2680f6baf022ef6ba"
dependencies = [ dependencies = [
"backoff", "backoff",
"config", "config",

View File

@@ -15,7 +15,7 @@ name = "baibot"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
anthropic = "=0.0.8" anthropic = { git = "https://github.com/etkecc/anthropic-rs.git", branch = "fix-content-block-image" }
anyhow = "1.0.*" anyhow = "1.0.*"
async-openai = { git = "https://github.com/etkecc/async-openai", branch = "async-openai-v0.28.1-patched" } async-openai = { git = "https://github.com/etkecc/async-openai", branch = "async-openai-v0.28.1-patched" }
base64 = "0.22.*" base64 = "0.22.*"

View File

@@ -176,15 +176,8 @@ impl ControllerTrait for Controller {
ContentBlock::Text { text } => { ContentBlock::Text { text } => {
text_parts.push(text); text_parts.push(text);
} }
ContentBlock::Image { ContentBlock::Image { .. } => {
source, text_parts.push("The model responded with an image".to_string());
media_type,
data: _,
} => {
text_parts.push(format!(
"The model responded with an image of type {}: {}",
media_type, source
));
} }
} }
} }

View File

@@ -1,4 +1,4 @@
use anthropic::types::{ContentBlock, Message, MessagesRequest, MessagesRequestBuilder, Role}; use anthropic::types::{ContentBlock, ImageSource, Message, MessagesRequest, MessagesRequestBuilder, Role};
use crate::conversation::llm::{Author as LLMAuthor, Message as LLMMessage, MessageContent as LLMMessageContent}; use crate::conversation::llm::{Author as LLMAuthor, Message as LLMMessage, MessageContent as LLMMessageContent};
@@ -15,28 +15,21 @@ pub(super) fn create_anthropic_message_request(llm_messages: Vec<LLMMessage>) ->
}; };
let content = match &message.content { let content = match &message.content {
LLMMessageContent::Text(text) => Some(vec![ContentBlock::Text { text: text.clone() }]), LLMMessageContent::Text(text) => vec![ContentBlock::Text { text: text.clone() }],
LLMMessageContent::Image(_image_details) => { LLMMessageContent::Image(image_details) => {
// This cannot be implemented yet, because the Anthropic library does not support it. vec![ContentBlock::Image {
// The code below requires the library to be forked and changed a bit. source: ImageSource::Base64 {
// vec![ContentBlock::Image { media_type: image_details.mime.to_string(),
// source: ImageSource { data: crate::utils::base64::base64_encode(&image_details.data),
// r#type: "base64".to_string(), },
// media_type: mime_type.to_string(), }]
// data: crate::utils::base64::base64_encode(image_details.data),
// },
// }]
tracing::warn!("Image content is not supported by the Anthropic library yet. Skipping it.");
None
} }
}; };
if let Some(content) = content {
let message = Message { role, content }; let message = Message { role, content };
messages.push(message); messages.push(message);
} }
}
MessagesRequestBuilder::default() MessagesRequestBuilder::default()
.messages(messages) .messages(messages)