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:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,27 +15,20 @@ 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 };
|
||||
let message = Message { role, content };
|
||||
|
||||
messages.push(message);
|
||||
}
|
||||
messages.push(message);
|
||||
}
|
||||
|
||||
MessagesRequestBuilder::default()
|
||||
|
||||
Reference in New Issue
Block a user