FileToFileContent
FileToFileContent
FileToFileContent는 로컬 파일을 읽어서 FileContent 객체로 변환해요. PDF나 다른 파일 유형을 LLM에 전달해야 하는 멀티모달 AI 파이프라인에 바로 쓸 수 있도록 만들어 주죠.
출처: 문서
본문
FileToFileContent는 파일 소스 목록을 처리해 FileContent 객체로 변환해요. 이 객체는 ChatMessage에 임베딩되어 언어 모델에 전달될 수 있어요. 각 소스는 다음 중 하나가 될 수 있어요:
- 파일 경로(string 또는
Path) ByteStream객체
선택적으로 extra 파라미터로 제공자별 추가 정보를 줄 수 있어요. 이 값은 딕셔너리 하나(모든 파일에 적용) 또는 sources 길이와 맞는 리스트일 수 있어요. 파일을 LLM에 전달하는 지원은 제공자마다 달라요. 어떤 제공자는 파일 입력을 지원하지 않고, 어떤 제공자는 PDF만 지원하며, 또 어떤 제공자는 더 넓은 범위의 파일 유형을 받아들여요.
- 대표적인 파이프라인 위치: 쿼리 파이프라인에서
ChatPromptBuilder앞 - 필수 run 변수:
sources— 파일 경로 또는ByteStream의 리스트 - 출력 변수:
file_contents—FileContent객체의 리스트 - 패키지명:
haystack-ai
Usage
On its own
from haystack.components.converters import FileToFileContent
converter = FileToFileContent()
sources = ["document.pdf", "recording.mp3"]
result = converter.run(sources=sources)
file_contents = result["file_contents"]
print(file_contents)
# [
# FileContent(
# base64_data='JVBERi0x...', mime_type='application/pdf',
# filename='document.pdf', extra={}
# ),
# FileContent(
# base64_data='SUQzBA...', mime_type='audio/mpeg',
# filename='recording.mp3', extra={}
# )
# ]
In a pipeline
FileToFileContent를 LinkContentFetcher 및 ChatPromptBuilder와 함께 사용해 원격 파일을 가져오고, 변환하고, LLM에 전달하는 파이프라인을 만들 수 있어요.
from haystack.components.converters import FileToFileContent
from haystack.components.fetchers import LinkContentFetcher
from haystack.components.generators.chat.openai import OpenAIChatGenerator
from haystack.components.builders import ChatPromptBuilder
from haystack import Pipeline
template = """{% message role="user"%}
{% for file in files %}{{ file | templatize_part }}{% endfor %}
What's the main takeaway of the following document? Just one sentence.
{% endmessage %}"""
pipeline = Pipeline()
pipeline.add_component("fetcher", LinkContentFetcher())
pipeline.add_component("converter", FileToFileContent())
pipeline.add_component("prompt_builder", ChatPromptBuilder(template=template))
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4.1-mini"))
pipeline.connect("fetcher", "converter")
pipeline.connect("converter", "prompt_builder")
pipeline.connect("prompt_builder", "llm")
results = pipeline.run({"fetcher": {"urls": ["https://arxiv.org/pdf/2309.08632"]}})
print(results["llm"]["replies"][0].text)
# The document is a satirical paper humorously claiming that pretraining a
# small language model exclusively on evaluation benchmark test sets can achieve
# perfect performance, highlighting issues of data contamination in model
# evaluation.
더 알아보기 (Learn more)
- FileToFileContent API reference를 확인하세요.