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

View File

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

View File

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

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};
@@ -15,28 +15,21 @@ pub(super) fn create_anthropic_message_request(llm_messages: Vec<LLMMessage>) ->
};
let content = match &message.content {
LLMMessageContent::Text(text) => Some(vec![ContentBlock::Text { text: text.clone() }]),
LLMMessageContent::Image(_image_details) => {
// This cannot be implemented yet, because the Anthropic library does not support it.
// The code below requires the library to be forked and changed a bit.
// vec![ContentBlock::Image {
// source: ImageSource {
// 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
LLMMessageContent::Text(text) => vec![ContentBlock::Text { text: text.clone() }],
LLMMessageContent::Image(image_details) => {
vec![ContentBlock::Image {
source: ImageSource::Base64 {
media_type: image_details.mime.to_string(),
data: crate::utils::base64::base64_encode(&image_details.data),
},
}]
}
};
if let Some(content) = content {
let message = Message { role, content };
messages.push(message);
}
}
MessagesRequestBuilder::default()
.messages(messages)