langchain/libs/partners/voyageai/tests/integration_tests/test_rerank.py
fzowl aea2be5bf3
voyageai[patch]: VoyageAI rerank (#19521)
Adding VoyageAI reranking

---------

Co-authored-by: fodizoltan <zoltan@conway.expert>
Co-authored-by: Yujie Qian <thomasq0809@gmail.com>
2024-03-26 17:07:23 -07:00

69 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Test the voyageai reranker."""
import os
from langchain_core.documents import Document
from langchain_voyageai.rerank import VoyageAIRerank
def test_voyageai_reranker_init() -> None:
"""Test the voyageai reranker initializes correctly."""
VoyageAIRerank(voyage_api_key="foo", model="foo")
def test_sync() -> None:
rerank = VoyageAIRerank(
voyage_api_key=os.environ["VOYAGE_API_KEY"],
model="rerank-lite-1",
)
doc_list = [
"The Mediterranean diet emphasizes fish, olive oil, and vegetables"
", believed to reduce chronic diseases.",
"Photosynthesis in plants converts light energy into glucose and "
"produces essential oxygen.",
"20th-century innovations, from radios to smartphones, centered "
"on electronic advancements.",
"Rivers provide water, irrigation, and habitat for aquatic species, "
"vital for ecosystems.",
"Apples conference call to discuss fourth fiscal quarter results and "
"business updates is scheduled for Thursday, November 2, 2023 at 2:00 "
"p.m. PT / 5:00 p.m. ET.",
"Shakespeare's works, like 'Hamlet' and 'A Midsummer Night's Dream,' "
"endure in literature.",
]
documents = [Document(page_content=x) for x in doc_list]
result = rerank.compress_documents(
query="When is the Apple's conference call scheduled?", documents=documents
)
assert len(doc_list) == len(result)
async def test_async() -> None:
rerank = VoyageAIRerank(
voyage_api_key=os.environ["VOYAGE_API_KEY"],
model="rerank-lite-1",
)
doc_list = [
"The Mediterranean diet emphasizes fish, olive oil, and vegetables"
", believed to reduce chronic diseases.",
"Photosynthesis in plants converts light energy into glucose and "
"produces essential oxygen.",
"20th-century innovations, from radios to smartphones, centered "
"on electronic advancements.",
"Rivers provide water, irrigation, and habitat for aquatic species, "
"vital for ecosystems.",
"Apples conference call to discuss fourth fiscal quarter results and "
"business updates is scheduled for Thursday, November 2, 2023 at 2:00 "
"p.m. PT / 5:00 p.m. ET.",
"Shakespeare's works, like 'Hamlet' and 'A Midsummer Night's Dream,' "
"endure in literature.",
]
documents = [Document(page_content=x) for x in doc_list]
result = await rerank.acompress_documents(
query="When is the Apple's conference call scheduled?", documents=documents
)
assert len(doc_list) == len(result)