微信扫码
添加专属顾问
我要投稿
ColPali 是一种多模态检索器,直接对图像进行处理,无需OCR。
对数据建立索引后,使用 Qwen2-VL-7B 完成 RAG 的生成部分。
from pdf2image import convert_from_path
images = convert_from_path("/content/climate_youth_magazine.pdf")
images[5]
byaldi 是 answer.ai 开源的工具包,可轻松使用 ColPali
from byaldi import RAGMultiModalModel
RAG = RAGMultiModalModel.from_pretrained("vidore/colpali")
建立索引
RAG.index(
    input_path="/content/climate_youth_magazine.pdf",
    index_name="image_index", # index will be saved at index_root/index_name/
    store_collection_with_index=False,
    overwrite=True
)
然后就可以搜索了
text_query = "How much did the world temperature change so far?"
results = RAG.search(text_query, k=1)
results
[{'doc_id': 0, 'page_num': 6, 'score': 17.25, 'metadata': {}, 'base64': None}]
答案确实是在第6页,就是上面展示的那页pdf。现在我们可以构建一个 RAG 管道了。使用 Qwen2-VL-7B 模型。
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
import torch
model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct",
                                                        trust_remote_code=True, torch_dtype=torch.bfloat16).cuda().eval()
                                                        
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True)
image_index = results[0]["page_num"] - 1
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": images[image_index],
            },
            {"type": "text", "text": text_query},
        ],
    }
]
text = processor.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=[text],
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt",
)
inputs = inputs.to("cuda")
generated_ids = model.generate(**inputs, max_new_tokens=50)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
["The Earth's average global temperature has increased by around 1.1°C since the late 19th century, according to the information provided in the image."] 答案正确!
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-11-04
AI Infra:POINTS-Reader,腾讯开源的文档解析和OCR工具
2025-11-03
震惊,Github开源,真正让程序员效率提升 90%的AI辅助工具来啦!!!
2025-11-03
Dify迎来最强开源对手!这个本地Agent内置微调+超细权限控制~
2025-11-03
我们大胆做了个决定,大会所有音乐bgm由AI生成,这部分预算可以省了!|Jinqiu Scan
2025-11-03
LongCat-Flash-Omni 正式发布并开源:开启全模态实时交互时代
2025-11-03
科大讯飞“王炸”开源!企业级智能体平台 Astron Agent:原生集成 RPA,Apache 2.0 商业友好!
2025-11-03
DeepSeek-OCR到底厉害在哪?
2025-11-02
刚刚,OpenAI开源了两个大模型~
            2025-08-20
2025-09-07
2025-08-20
2025-08-26
2025-08-22
2025-09-06
2025-10-20
2025-08-22
2025-09-08
2025-08-12
2025-11-03
2025-10-29
2025-10-28
2025-10-13
2025-09-29
2025-09-17
2025-09-09
2025-09-08