マルチモーダル
マルチモーダル
Section titled “マルチモーダル”AI-Lib は、同じ統一 API でテキストと画像を組み合わせたマルチモーダル入力に対応しています。
| Capability | Providers |
|---|---|
| Vision (images) | OpenAI, Anthropic, Gemini, Qwen |
| Audio input | Limited (Gemini) |
use ai_lib::{AiClient, Message, ContentBlock};
let client = AiClient::new("openai/gpt-4o").await?;
let message = Message::user_with_content(vec![ ContentBlock::Text("What's in this image?".into()), ContentBlock::ImageUrl { url: "https://example.com/photo.jpg".into(), },]);
let response = client.chat() .messages(vec![message]) .execute() .await?;
println!("{}", response.content);Python
Section titled “Python”from ai_lib_python import AiClient, Message, ContentBlock
client = await AiClient.create("openai/gpt-4o")
message = Message.user_with_content([ ContentBlock.text("What's in this image?"), ContentBlock.image_url("https://example.com/photo.jpg"),])
response = await client.chat() \ .messages([message]) \ .execute()
print(response.content)Base64 画像
Section titled “Base64 画像”ローカル画像の場合は Base64 エンコーディングを使用してください:
let image_data = std::fs::read("photo.jpg")?;let base64 = base64::engine::general_purpose::STANDARD.encode(&image_data);
let message = Message::user_with_content(vec![ ContentBlock::Text("Describe this".into()), ContentBlock::ImageBase64 { data: base64, media_type: "image/jpeg".into(), },]);Python
Section titled “Python”import base64
with open("photo.jpg", "rb") as f: image_data = base64.b64encode(f.read()).decode()
message = Message.user_with_content([ ContentBlock.text("Describe this"), ContentBlock.image_base64(image_data, "image/jpeg"),])- ランタイムが混合コンテンツブロックを含むマルチモーダルメッセージを構築します
- プロトコルマニフェストがコンテンツブロックをプロバイダーのフォーマットにマッピングします
- プロバイダーによって異なる構造を使用します:
- OpenAI:
type: "image_url"オブジェクトを含むcontent配列 - Anthropic:
type: "image"オブジェクトを含むcontent配列 - Gemini:
inline_dataオブジェクトを含むparts配列
- OpenAI:
- プロトコルがすべてのフォーマットの違いを自動的に処理します
プロバイダー対応
Section titled “プロバイダー対応”画像を送信する前に、プロバイダーマニフェストで capabilities.vision: true を確認してください。
// The runtime checks capabilities before sending// If vision is not supported, you'll get a clear error