You.com Retriever

You.com Retriever

You.com의 Search API를 LlamaIndex에서 리트리버로 사용하는 방법을 보여드려요. 검색 결과가 NodeWithScore 형식으로 변환되어 쿼리 엔진·에이전트와 자연스럽게 통합되는 걸 함께 볼게요.

출처: 문서

본문

이 노트북은 You.com의 Search API를 LlamaIndex에서 리트리버로 사용하는 방법을 보여줍니다. 이 API는 쿼리에 기반해 관련 웹 및/또는 뉴스 결과를 자동으로 반환합니다. Search 및 기타 API에 대한 자세한 내용은 문서를 참고하세요: https://docs.you.com/

리트리버는 You.com의 검색 결과를 LlamaIndex의 표준 형식(NodeWithScore)으로 변환해 다음을 가능하게 합니다:

  • LLM 쿼리의 컨텍스트로 검색 결과 사용
  • 다른 리트리버(벡터 스토어, 데이터베이스)와 결합
  • 쿼리 엔진과 에이전트와의 원활한 통합

시작하려면 llama-index-retrievers-you 패키지를 설치합니다.

%pip install llama-index-retrievers-you

Setup

You.com 플랫폼에서 API 키를 받습니다.

import os
from getpass import getpass


# Set your API key
you_api_key = os.environ.get("YDC_API_KEY") or getpass(
    "Enter your You.com API key: "
)

기본 사용법 (Basic usage)

먼저 리트리버를 설정하고 어떤 데이터를 반환하는지 확인해 봅시다:

from llama_index.retrievers.you import YouRetriever


retriever = YouRetriever(api_key=you_api_key)
retrieved_results = retriever.retrieve("national parks in the US")


print(f"Retrieved {len(retrieved_results)} results")


for i, result in enumerate(retrieved_results):
    print(f"\nResult {i+1}:")
    print(f"  Text: {result.node.text}...")
    print("Metadata:")
    for key, value in result.node.metadata.items():
        print(f"  {key}: {value}")

기본 설정에서는 쿼리 "national parks in the US"에 대해 10개의 결과를 반환합니다. 각 결과는 NodeWithScore 형식으로, 노드의 text에 스니펫을 담고 metadata 딕셔너리에 url, title, description, page_age, thumbnail_url, favicon_url, source_type(web/news) 같은 메타데이터를 포함합니다. 예를 들어 첫 번째 결과는 Wikipedia의 "List of national parks of the United States" 페이지로, url이 https://en.wikipedia.org/wiki/List_of_national_parks_of_the_United_States이고 source_type이 web입니다.

쿼리를 바꾸면 관계없는 페이지 대신 해당 주제의 검색 결과가 반환됩니다. 예를 들어 재생 에너지 관련 쿼리를 하면 Nature 저널 기사, Wikipedia의 Renewable energy 문서 등 관련 웹·뉴스 결과가 돌아옵니다.

Query Engine에서 사용하기

이제 가져오고 싶은 웹 데이터를 어떻게 커스터마이즈하는지 보았으니, LLM을 사용해 검색 결과로부터 자연어 답변을 합성해 봅시다. 이 예시에서는 Anthropic의 모델을 사용합니다.

%pip install llama-index-llms-anthropic
import os
from getpass import getpass


# Set your Anthropic API key
anthropic_api_key = os.environ.get("ANTHROPIC_API_KEY") or getpass(
    "Enter your Anthropic API key: "
)
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.llms.anthropic import Anthropic
from llama_index.core import Settings
from llama_index.retrievers.you import YouRetriever


# Configure Anthropic as your LLM
llm = Anthropic(model="claude-haiku-4-5-20251001", api_key=anthropic_api_key)


# Create a query engine that uses You.com search results as context
retriever = YouRetriever(api_key=you_api_key)
query_engine = RetrieverQueryEngine.from_args(retriever, llm)
# The query engine:
# 1. Uses the retriever to fetch relevant search results from You.com
# 2. Passes those results as context to the LLM
# 3. Returns a synthesized answer


response = query_engine.query(
    "What are the most visited national parks in the US and why? keep it brief."
)


# Try a different query
# response = query_engine.query("What are the latest geopolitical updates from India")


print(str(response))

쿼리 엔진은 리트리버가 가져온 You.com 검색 결과를 LLM 컨텍스트로 넘겨 합성 응답을 반환합니다. 예를 들어 미국에서 가장 많이 방문하는 국립공원을 물으면 Great Smoky Mountains(1,330만 명), Zion(490만 명), Grand Canyon(470만 명)을 방문 이유와 함께 정리해 답해 줍니다.

이 형식이 필요한 이유 (Why this format?)

리트리버는 You.com의 JSON 응답을 LlamaIndex 표준 NodeWithScore 형식으로 변환합니다. 이렇게 하면 다음과 같은 이점이 있습니다:

장점 (Benefits):

  • 소스 무관(Source-agnostic): You.com, 벡터 DB, 기타 소스에서 검색하든 동일한 인터페이스 사용
  • 구성 가능(Composability): 여러 리트리버를 쉽게 결합하거나 교체 가능
  • 통합(Integration): LlamaIndex 쿼리 엔진, 에이전트 및 기타 컴포넌트와 원활하게 동작

보존되는 것 (What's preserved):

  • 텍스트 콘텐츠: 웹 결과의 스니펫 또는 뉴스 기사 설명
  • 메타데이터: metadata 딕셔너리에 저장된 URL, 제목, page_age
  • 점수(Score): 관련성 점수(You.com이 점수를 제공하지 않으므로 기본적으로 1.0)

이 추상화 덕분에 API별 응답 형식을 다루는 대신 애플리케이션 구축에 집중할 수 있습니다.

더 알아보기 (Learn more)