Skip to content

Commit

Permalink
^ image - added convenient ContentPart constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychone committed Jan 1, 2025
1 parent a759517 commit 27013c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
10 changes: 3 additions & 7 deletions examples/c07-image.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This example demonstrates how to properly attach image to the conversations
use genai::chat::printer::print_chat_stream;
use genai::chat::{ChatMessage, ChatRequest, ContentPart, ImageSource};
use genai::chat::{ChatMessage, ChatRequest, ContentPart};
use genai::Client;

const MODEL: &str = "gpt-4o-mini";
Expand All @@ -16,12 +16,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut chat_req = ChatRequest::default().with_system("Answer in one sentence");
// This is similar to sending initial system chat messages (which will be cumulative with system chat messages)
chat_req = chat_req.append_message(ChatMessage::user(vec![
ContentPart::Text(question.to_string()),
ContentPart::Image {
content: IMAGE_URL.to_string(),
content_type: "image/jpg".to_string(),
source: ImageSource::Url,
},
ContentPart::from_text(question),
ContentPart::from_image_url("image/jpg", IMAGE_URL),
]));

println!("\n--- Question:\n{question}");
Expand Down
23 changes: 23 additions & 0 deletions src/chat/message_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ pub enum ContentPart {
},
}

/// Constructors
impl ContentPart {
pub fn from_text(text: impl Into<String>) -> ContentPart {
ContentPart::Text(text.into())
}

pub fn from_image_b64(content_type: impl Into<String>, content: impl Into<String>) -> ContentPart {
ContentPart::Image {
content: content.into(),
content_type: content_type.into(),
source: ImageSource::Base64,
}
}

pub fn from_image_url(content_type: impl Into<String>, url: impl Into<String>) -> ContentPart {
ContentPart::Image {
content: url.into(),
content_type: content_type.into(),
source: ImageSource::Url,
}
}
}

// region: --- Froms

impl<'a> From<&'a str> for ContentPart {
Expand Down
16 changes: 4 additions & 12 deletions tests/support/common_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,8 @@ pub async fn common_test_chat_image_url_ok(model: &str) -> Result<()> {
let mut chat_req = ChatRequest::default().with_system("Answer in one sentence");
// This is similar to sending initial system chat messages (which will be cumulative with system chat messages)
chat_req = chat_req.append_message(ChatMessage::user(vec![
ContentPart::Text("What is in this picture?".to_string()),
ContentPart::Image {
content: IMAGE_URL_JPG_DUCK.to_string(),
content_type: "image/jpeg".to_string(),
source: ImageSource::Url,
},
ContentPart::from_text("What is in this picture?"),
ContentPart::from_image_url("image/jpeg", IMAGE_URL_JPG_DUCK),
]));
let chat_res = client.exec_chat(model, chat_req, None).await?;

Expand All @@ -355,12 +351,8 @@ pub async fn common_test_chat_image_b64_ok(model: &str) -> Result<()> {
let mut chat_req = ChatRequest::default().with_system("Answer in one sentence");
// This is similar to sending initial system chat messages (which will be cumulative with system chat messages)
chat_req = chat_req.append_message(ChatMessage::user(vec![
ContentPart::Text("What is in this picture?".to_string()),
ContentPart::Image {
content: get_b64_duck()?,
content_type: "image/jpeg".to_string(),
source: ImageSource::Base64,
},
ContentPart::from_text("What is in this picture?"),
ContentPart::from_image_b64("image/jpeg", get_b64_duck()?),
]));
let chat_res = client.exec_chat(model, chat_req, None).await?;

Expand Down

0 comments on commit 27013c4

Please sign in to comment.