JinaReaderConnector

JinaReaderConnector

Jina AI의 Reader API를 Haystack에서 바로 쓸 수 있게 해주는 커넥터예요. URL을 읽거나 웹을 검색하거나 사실 검증(grounding)을 수행하고, 그 결과를 문서로 내보내 줘요.

출처: JinaReaderConnector

본문

개요

JinaReaderConnector는 Jina AI의 Reader API와 상호작용해서 쿼리를 처리하고 문서를 만들어요. 컴포넌트를 초기화할 때 다음 모드 중 하나를 골라야 해요.

  • read: URL을 처리해서 텍스트 내용을 추출해요.
  • search: 웹을 검색해서 가장 관련성 높은 페이지에서 텍스트 내용을 가져와요.
  • ground: grounding 엔진을 사용해 사실 검증을 수행해요.

각 모드에 대한 자세한 설명은 Jina Reader 문서에서 확인할 수 있어요.

컴포넌트의 json_response 파라미터로 Jina Reader API의 응답 형식도 조절할 수 있어요.

  • True(기본값)는 구조화된 메타데이터가 담긴 문서를 위한 JSON 응답을 요청해요.
  • False는 원본(raw) 응답을 요청하며, 그 결과 최소한의 메타데이터만 가진 문서 하나가 나와요.

인증

컴포넌트는 기본적으로 JINA_API_KEY 환경 변수를 사용해요. 또는 초기화할 때 api_key로 Jina API 키를 직접 넘길 수도 있어요.

reader = JinaReaderConnector(mode="read", api_key=Secret.from_token("<your-api-key>"))

API 키를 얻으려면 Jina AI의 웹사이트를 방문하면 돼요.

설치

이 통합을 Haystack에서 쓰려면 패키지를 설치해요.

pip install jina-haystack

단독 사용

Read 모드:

from haystack_integrations.components.connectors.jina import JinaReaderConnector

reader = JinaReaderConnector(mode="read")
query = "https://example.com"
result = reader.run(query=query)

print(result)
# {'documents': [Document(id=fa3e51e4ca91828086dca4f359b6e1ea2881e358f83b41b53c84616cb0b2f7cf,
# content: 'This domain is for use in illustrative examples in documents. You may use this domain in literature ...',
# meta: {'title': 'Example Domain', 'description': '', 'url': 'https://example.com/', 'usage': {'tokens': 42}})]}

Search 모드:

from haystack_integrations.components.connectors.jina import JinaReaderConnector

reader = JinaReaderConnector(mode="search")
query = "UEFA Champions League 2024"
result = reader.run(query=query)

print(result)
# {'documents': [Document(id=6a71abf9955594232037321a476d39a835c0cb7bc575d886ee0087c973c95940,
# content: '2024/25 UEFA Champions League: Matches, draw, final, key dates | UEFA Champions League | UEFA.com...',
# meta: {'title': '2024/25 UEFA Champions League: Matches, draw, final, key dates',
# 'description': 'What are the match dates? Where is the 2025 final? How will the competition work?',
# 'url': 'https://www.uefa.com/uefachampionsleague/news/...',
# 'usage': {'tokens': 5581}}), ...]}

Ground 모드:

from haystack_integrations.components.connectors.jina import JinaReaderConnector

reader = JinaReaderConnector(mode="ground")
query = "ChatGPT was launched in 2017"
result = reader.run(query=query)

print(result)
# {'documents': [Document(id=f0c964dbc1ebb2d6584c8032b657150b9aa6e421f714cc1b9f8093a159127f0c,
# content: 'The statement that ChatGPT was launched in 2017 is incorrect. Multiple references confirm that ChatG...',
# meta: {'factuality': 0, 'result': False, 'references': [
# {'url': 'https://en.wikipedia.org/wiki/ChatGPT',
# 'keyQuote': 'ChatGPT is a generative artificial intelligence (AI) chatbot developed by OpenAI and launched in 2022.',
# 'isSupportive': False}, ...],
# 'usage': {'tokens': 10188}})]}

파이프라인 안에서

search 모드로 만든 쿼리 파이프라인

아래 예시에서는 JinaReaderConnector가 먼저 관련 문서를 검색하고, 그 문서들을 사용자 쿼리와 함께 프롬프트 템플릿에 넣은 뒤, 검색된 컨텍스트를 바탕으로 응답을 생성해요.

from haystack import Pipeline
from haystack.utils import Secret
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack_integrations.components.connectors.jina import JinaReaderConnector
from haystack.dataclasses import ChatMessage

reader_connector = JinaReaderConnector(mode="search")

prompt_template = [
    ChatMessage.from_system("You are a helpful assistant."),
    ChatMessage.from_user(
        "Given the information below:\n"
        "{% for document in documents %}{{ document.content }}{% endfor %}\n"
        "Answer question: {{ query }}.\nAnswer:",
    ),
]

prompt_builder = ChatPromptBuilder(
    template=prompt_template,
    required_variables={"query", "documents"},
)
llm = OpenAIChatGenerator(
    model="gpt-4o-mini",
    api_key=Secret.from_token("<your-api-key>"),
)

pipe = Pipeline()
pipe.add_component("reader_connector", reader_connector)
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("llm", llm)

pipe.connect("reader_connector.documents", "prompt_builder.documents")
pipe.connect("prompt_builder.prompt", "llm.messages")

query = "What is the most famous landmark in Berlin?"

result = pipe.run(
    data={"reader_connector": {"query": query}, "prompt_builder": {"query": query}},
)
print(result)

같은 컴포넌트를 search 모드로 인덱싱 파이프라인에도 쓸 수 있어요.

더 알아보기 (Learn more)