🎯 Mục tiêu bài học
Multimodal pipelines kết hợp text và image processing để tạo ứng dụng phức tạp: từ content creation đến analysis automation.
Sau bài này, bạn sẽ:
✅ Xây dựng multimodal pipeline kết hợp text và image ✅ Tạo blog posts tự động với AI-generated images ✅ Build product catalog và social media content generator ✅ Implement multimodal analysis report
🔍 Multimodal Architecture
Checkpoint
Bạn đã hiểu cách multimodal pipeline kết hợp text và image processing chưa?
📝 Blog Post với Auto-generated Images
1from langchain_openai import ChatOpenAI2from langchain_core.prompts import ChatPromptTemplate3from langchain_core.output_parsers import StrOutputParser4from openai import OpenAI5from pydantic import BaseModel, Field6from typing import List78llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)9image_client = OpenAI()1011class BlogOutline(BaseModel):12 title: str13 sections: List[str]14 image_prompts: List[str] = Field(description="DALL-E prompts cho moi section")1516# Step 1: Generate outline voi image prompts17outline_chain = (18 ChatPromptTemplate.from_messages([19 ("system", """Tao blog outline voi 4 sections.20 Cho moi section, tao image prompt chi tiet cho DALL-E."""),21 ("human", "Topic: {topic}")22 ])23 | llm.with_structured_output(BlogOutline)24)2526# Step 2: Generate text27write_chain = (28 ChatPromptTemplate.from_messages([29 ("system", "Viet section chi tiet, 200 tu."),30 ("human", "Section: {section}\nContext: {context}")31 ])32 | llm33 | StrOutputParser()34)3536# Step 3: Generate images37def generate_image(prompt):38 response = image_client.images.generate(39 model="dall-e-3",40 prompt=prompt,41 size="1792x1024",42 quality="standard"43 )44 return response.data[0].url4546# Full pipeline47async def create_blog_with_images(topic):48 outline = outline_chain.invoke({"topic": topic})49 50 blog = {"title": outline.title, "sections": []}51 52 for section, img_prompt in zip(outline.sections, outline.image_prompts):53 text = write_chain.invoke({54 "section": section,55 "context": topic56 })57 image_url = generate_image(img_prompt)58 59 blog["sections"].append({60 "heading": section,61 "text": text,62 "image": image_url63 })64 65 return blogCheckpoint
Bạn đã hiểu cách xây dựng blog generator tự động tạo text + images chưa?
🛠️ Product Catalog Generator
1import base6423class ProductListing(BaseModel):4 title: str5 description: str6 features: List[str]7 seo_tags: List[str]89# Analyze product image -> generate listing10def create_product_listing(image_path):11 with open(image_path, "rb") as f:12 b64 = base64.b64encode(f.read()).decode()13 14 # Step 1: Analyze image15 listing_llm = llm.with_structured_output(ProductListing)16 17 listing = listing_llm.invoke([{18 "role": "user",19 "content": [20 {"type": "text", "text": "Tao product listing tu hinh san pham nay."},21 {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}22 ]23 }])24 25 # Step 2: Generate variations26 variations = image_client.images.generate(27 model="dall-e-3",28 prompt=f"Product photo of {listing.title}, white background, studio lighting",29 n=1,30 size="1024x1024"31 )32 33 return {34 "listing": listing,35 "additional_images": [v.url for v in variations.data]36 }Checkpoint
Bạn đã biết cách tạo product listing tự động từ product photos chưa?
📱 Social Media Content Pack
1class SocialPack(BaseModel):2 caption: str3 hashtags: List[str]4 image_prompt: str56# Image -> complete social media pack7def create_social_pack(image_path, platform="instagram"):8 with open(image_path, "rb") as f:9 b64 = base64.b64encode(f.read()).decode()10 11 pack_llm = llm.with_structured_output(SocialPack)12 13 pack = pack_llm.invoke([{14 "role": "user",15 "content": [16 {"type": "text", "text": f"Tao {platform} post tu image. Include caption, hashtags, va suggested image prompt for additional content."},17 {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}18 ]19 }])20 21 return packCheckpoint
Bạn đã hiểu cách tạo social media content pack từ một hình ảnh đầu vào chưa?
📊 Image + Text Analysis Report
1class AnalysisReport(BaseModel):2 image_description: str3 text_summary: str4 combined_insights: List[str]5 recommendations: List[str]67def multimodal_report(image_path, text_data):8 with open(image_path, "rb") as f:9 b64 = base64.b64encode(f.read()).decode()10 11 report_llm = llm.with_structured_output(AnalysisReport)12 13 return report_llm.invoke([{14 "role": "user",15 "content": [16 {"type": "text", "text": f"Phan tich ket hop image va text data:\n\n{text_data}\n\nTao insights va recommendations."},17 {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}18 ]19 }])Checkpoint
Bạn đã biết cách kết hợp insights từ ảnh và text data để tạo analysis report chưa?
🎯 Tổng kết
Bài tập thực hành
- Build blog generator với auto-images
- Tạo product listing từ product photos
- Build social media content pack generator
- Implement multimodal analysis report
Challenge: E-commerce pipeline: photo -> listing + variants + social posts
Câu hỏi tự kiểm tra
- Multimodal pipeline kết hợp text và image processing như thế nào để tạo ứng dụng phức tạp?
- Làm sao xây dựng auto blog generator với AI-generated images từ nội dung bài viết?
- Social media content pack generator hoạt động như thế nào từ một hình ảnh đầu vào?
- Multimodal analysis report kết hợp insights từ ảnh và text data để tạo recommendations ra sao?
🎉 Tuyệt vời! Bạn đã hoàn thành bài học Multimodal Pipelines!
Tiếp theo: Hãy áp dụng tất cả kiến thức đã học vào Capstone Project - xây dựng Image Processing Platform hoàn chỉnh!
