以编程方式使用文档

>Perigon 是一个全面的新闻 API,提供来自全球数千个来源的新闻文章、故事、元数据和维基百科页面的实时上下文信息访问。 >

安装和设置

Perigon 集成存在于其独立的 合作伙伴包中。您可以使用以下方式安装:

pip install -qU langchain-perigon

为了使用该包,您还需要设置 PERIGON_API_KEY 环境变量为您的 Perigon API 密钥。

检索器

Perigon 提供两种检索器:

ArticlesRetriever

此检索器根据给定的查询和可选过滤器检索文章。

查看 完整使用示例.

# Make sure PERIGON_API_KEY environment variable is set to your Perigon API key
from langchain_perigon import ArticlesRetriever, ArticlesFilter

# Create retriever with specific number of results
retriever = ArticlesRetriever(k=12)

# Configure filter options to exclude reprints and focus on US articles
options: ArticlesFilter = {
    "showReprints": False,  # Exclude duplicate/reprint articles
    "filter": {"country": "us"},  # Only US-based news
}

try:
    documents = retriever.invoke("Recent big tech layoffs", options=options)

    # Check if we got results before accessing
    if documents:
        print(f"First document: {documents[0].page_content[:200]}...")
    else:
        print("No articles found for the given query.")
except Exception as e:
    print(f"Error retrieving articles: {e}")

您可以将 ArticlesRetriever 用于标准检索管道:

WikipediaRetriever

此检索器根据给定的查询和可选过滤器检索维基百科页面。

查看 完整使用示例.

# Make sure PERIGON_API_KEY environment variable is set to your Perigon API key
from langchain_perigon import WikipediaRetriever

# Create retriever with specific number of results
retriever = WikipediaRetriever(k=12)

try:
    documents = retriever.invoke("machine learning")

    # Safely access results with error handling
    if documents:
        print(f"First document: {documents[0].page_content[:200]}...")
    else:
        print("No Wikipedia articles found for the given query.")
except Exception as e:
    print(f"Error retrieving Wikipedia articles: {e}")

您可以将 WikipediaRetriever 用于标准检索管道: