이 노트북은 langchain_pinecone/libs/pinecone/rerank.py에서 보여주는 것처럼 Pinecone의 호스팅 reranking API를 사용하여 2단계 벡터 검색 reranking을 위해 PineconeRerank를 사용하는 방법을 보여줍니다.

Setup

langchain-pinecone 패키지를 설치합니다.
pip install -qU "langchain-pinecone"

Credentials

reranking API를 사용하기 위해 Pinecone API key를 설정합니다.
import os
from getpass import getpass

os.environ["PINECONE_API_KEY"] = os.getenv("PINECONE_API_KEY") or getpass(
    "Enter your Pinecone API key: "
)

Instantiation

PineconeRerank를 사용하여 쿼리와의 관련성에 따라 문서 목록을 rerank합니다.
from langchain_core.documents import Document
from langchain_pinecone import PineconeRerank

# Initialize reranker
reranker = PineconeRerank(model="bge-reranker-v2-m3")

# Sample documents
documents = [
    Document(page_content="Paris is the capital of France."),
    Document(page_content="Berlin is the capital of Germany."),
    Document(page_content="The Eiffel Tower is in Paris."),
]

# Rerank documents
query = "What is the capital of France?"
reranked_docs = reranker.compress_documents(documents, query)

# Print results
for doc in reranked_docs:
    score = doc.metadata.get("relevance_score")
    print(f"Score: {score:.4f} | Content: {doc.page_content}")
/Users/jakit/customers/aurelio/langchain-pinecone/libs/pinecone/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
Score: 0.9998 | Content: Paris is the capital of France.
Score: 0.1950 | Content: The Eiffel Tower is in Paris.
Score: 0.0042 | Content: Berlin is the capital of Germany.

Usage

Top-N으로 Reranking하기

반환되는 문서 수를 제한하려면 top_n을 지정합니다.
# Return only top-1 result
reranker_top1 = PineconeRerank(model="bge-reranker-v2-m3", top_n=1)
top1_docs = reranker_top1.compress_documents(documents, query)
print("Top-1 Result:")
for doc in top1_docs:
    print(f"Score: {doc.metadata['relevance_score']:.4f} | Content: {doc.page_content}")
Top-1 Result:
Score: 0.9998 | Content: Paris is the capital of France.

사용자 정의 Rank Fields로 Reranking하기

문서가 dictionary이거나 사용자 정의 필드를 가지고 있는 경우, rank_fields를 사용하여 rank할 필드를 지정합니다.
# Sample dictionary documents with 'text' field
docs_dict = [
    {
        "id": "doc1",
        "text": "Article about renewable energy.",
        "title": "Renewable Energy",
    },
    {"id": "doc2", "text": "Report on economic growth.", "title": "Economic Growth"},
    {
        "id": "doc3",
        "text": "News on climate policy changes.",
        "title": "Climate Policy",
    },
]

# Initialize reranker with rank_fields
reranker_text = PineconeRerank(model="bge-reranker-v2-m3", rank_fields=["text"])
climate_docs = reranker_text.rerank(docs_dict, "Latest news on climate change.")

# Show IDs and scores
for res in climate_docs:
    print(f"ID: {res['id']} | Score: {res['score']:.4f}")
ID: doc3 | Score: 0.9892
ID: doc1 | Score: 0.0006
ID: doc2 | Score: 0.0000
title 필드를 기준으로 rerank할 수 있습니다
economic_docs = reranker_text.rerank(docs_dict, "Economic forecast.")

# Show IDs and scores
for res in economic_docs:
    print(
        f"ID: {res['id']} | Score: {res['score']:.4f} | Title: {res['document']['title']}"
    )
ID: doc2 | Score: 0.8918 | Title: Economic Growth
ID: doc3 | Score: 0.0002 | Title: Climate Policy
ID: doc1 | Score: 0.0000 | Title: Renewable Energy

추가 Parameters로 Reranking하기

model별 parameters(예: truncate)를 .rerank()에 직접 전달할 수 있습니다. 모델이 지원하는 것보다 긴 입력을 처리하는 방법입니다. 허용되는 값: END 또는 NONE. END는 입력 token 제한에서 입력 시퀀스를 잘라냅니다. NONE은 입력이 입력 token 제한을 초과할 때 오류를 반환합니다.
# Rerank with custom truncate parameter
docs_simple = [
    {"id": "docA", "text": "Quantum entanglement is a physical phenomenon..."},
    {"id": "docB", "text": "Classical mechanics describes motion..."},
]

reranked = reranker.rerank(
        documents=docs_simple,
        query="Explain the concept of quantum entanglement.",
        truncate="END",
)
# Print reranked IDs and scores
for res in reranked:
    print(f"ID: {res['id']} | Score: {res['score']:.4f}")
ID: docA | Score: 0.6950
ID: docB | Score: 0.0001

Chain 내에서 사용하기

API reference

  • PineconeRerank(model, top_n, rank_fields, return_documents)
  • .rerank(documents, query, rank_fields=None, model=None, top_n=None, truncate="END")
  • .compress_documents(documents, query) (metadata에 relevance_score가 포함된 Document 객체를 반환합니다)

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I