ImageFileToImageContent
ImageFileToImageContent
ImageFileToImageContent는 로컬 이미지 파일을 읽어 ImageContent 객체로 변환해요. 이미지 캡셔닝, visual QA, 프롬프트 기반 생성 같은 작업을 포함한 멀티모달 AI 파이프라인에 바로 쓸 수 있도록 만든답니다.
출처: 문서
본문
ImageFileToImageContent는 이미지 소스 리스트를 처리해 ImageContent 객체로 변환해요. base64 인코딩 이미지 입력이 필요한 멀티모달 파이프라인에서 사용할 수 있어요. 각 소스는 다음 중 하나가 될 수 있어요:
- 파일 경로(string 또는
Path) ByteStream객체
선택적으로 meta 파라미터로 메타데이터를 제공할 수 있어요. 이것은 단일 딕셔너리(모든 이미지에 적용) 또는 sources 길이와 맞는 리스트일 수 있어요. size 파라미터로 가로세로 비율을 유지하면서 이미지 크기를 조절할 수 있어요. 이렇게 하면 원격 모델이나 리소스가 제한된 환경에서 작업할 때 유용한 메모리 사용량과 전송 크기를 줄여줘요.
이 컴포넌트는 쿼리 파이프라인에서 ChatPromptBuilder 바로 앞에 자주 사용돼요.
- 대표적인 파이프라인 위치: 쿼리 파이프라인에서
ChatPromptBuilder앞 - 필수 run 변수:
sources— 이미지 파일 경로 또는ByteStream리스트 - 출력 변수:
image_contents—ImageContent객체 리스트 - API reference: Image Converters
- 패키지명:
haystack-ai
Usage
On its own
from haystack.components.converters.image import ImageFileToImageContent
converter = ImageFileToImageContent(detail="high", size=(800, 600))
sources = ["cat.jpg", "scenery.png"]
result = converter.run(sources=sources)
image_contents = result["image_contents"]
print(image_contents)
# [
# ImageContent(
# base64_image="/9j/4A...", mime_type="image/jpeg", detail="high",
# meta={"file_path": "cat.jpg"}
# ),
# ImageContent(
# base64_image="iVBORw0KGgo...", mime_type="image/png", detail="high",
# meta={"file_path": "scenery.png"}
# )
# ]
In a pipeline
LLM으로 멀티모달 QA나 캡셔닝을 위해 ImageFileToImageContent로 ChatPromptBuilder에 이미지 데이터를 공급하세요.
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.converters.image import ImageFileToImageContent
# Query pipeline
pipeline = Pipeline()
pipeline.add_component("image_converter", ImageFileToImageContent(detail="auto"))
pipeline.add_component(
"chat_prompt_builder",
ChatPromptBuilder(
required_variables=["question"],
template="""{% message role="system" %}You are a helpful assistant that answers questions using the provided images.{% endmessage %}
{% message role="user" %}Question: {{ question }}
{% for img in image_contents %}{{ img | templatize_part }}{% endfor %}
{% endmessage %}""",
),
)
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini"))
pipeline.connect("image_converter", "chat_prompt_builder.image_contents")
pipeline.connect("chat_prompt_builder", "llm")
sources = ["apple.jpg", "haystack-logo.png"]
result = pipeline.run(
data={
"image_converter": {"sources": sources},
"chat_prompt_builder": {"question": "Describe the Haystack logo."},
},
)
print(result)
더 알아보기 (Learn more)
🧑🍳 Cookbook: Introduction to Multimodality