以编程方式使用文档

>Exa 是一个面向 AI 和开发者的知识 API。 >

安装和设置

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

pip install -qU langchain-exa

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

检索器

您可以在标准检索管道中使用 ExaSearchRetriever 。您可以按如下方式导入。

请参阅 使用示例.

from langchain_exa import ExaSearchRetriever

工具

您可以将 Exa 用作代理工具,如 Exa 工具调用文档.

请参阅 使用示例.

ExaFindSimilarResults

一个查询 Metaphor Search API 并返回 JSON 的工具。

from langchain_exa.tools import ExaFindSimilarResults

ExaSearchResults

Exa 搜索工具。

from langchain_exa.tools import ExaSearchResults

Exa 搜索检索器

您可以按以下方式检索搜索结果

from langchain_exa import ExaSearchRetriever

exa_api_key = "YOUR API KEY"

# Create a new instance of the ExaSearchRetriever
exa = ExaSearchRetriever(exa_api_key=exa_api_key)

# Search for a query and save the results
results  = exa.invoke("What is the capital of France?")

# Print the results
print(results)

高级功能

您可以使用高级功能,如文本限制、摘要和实时爬取:

from langchain_exa import ExaSearchRetriever, TextContentsOptions

# Create a new instance with advanced options
exa = ExaSearchRetriever(
    exa_api_key="YOUR API KEY",
    k=20,  # Number of results (1-100)
    type="auto",  # Can be "neural", "keyword", or "auto"
    livecrawl="always",  # Can be "always", "fallback", or "never"
    summary=True,  # Get an AI-generated summary of each result
    text_contents_options={"max_characters": 3000}  # Limit text length
)

# Search for a query with custom summary prompt
exa_with_custom_summary = ExaSearchRetriever(
    exa_api_key="YOUR API KEY",
    summary={"query": "generate one line summary in simple words."}  # Custom summary prompt
)

Exa 搜索结果

您可以按以下方式运行 ExaSearchResults 模块

from langchain_exa import ExaSearchResults

# Initialize the ExaSearchResults tool
search_tool = ExaSearchResults(exa_api_key="YOUR API KEY")

# Perform a search query
search_results = search_tool._run(
    query="When was the last time the New York Knicks won the NBA Championship?",
    num_results=5,
    text_contents_options=True,
    highlights=True
)

print("Search Results:", search_results)

Exa 查找相似结果

您可以按以下方式运行 ExaFindSimilarResults 模块

from langchain_exa import ExaFindSimilarResults

# Initialize the ExaFindSimilarResults tool
find_similar_tool = ExaFindSimilarResults(exa_api_key="YOUR API KEY")

# Find similar results based on a URL
similar_results = find_similar_tool._run(
    url="http://espn.com",
    num_results=5,
    text_contents_options=True,
    highlights=True
)

print("Similar Results:", similar_results)

配置选项

所有 Exa 工具支持以下通用参数:

  • - num_results (1-100):要返回的搜索结果数量
  • - type:搜索类型 - "neural"、"keyword" 或 "auto"
  • - livecrawl:实时爬取模式 - "always"、"fallback" 或 "never"
  • - summary: Get AI-generated summaries (True/False or custom prompt dict)
  • - text_contents_options:用于限制文本长度的字典(例如 {"max_characters": 2000})
  • - highlights: Include highlighted text snippets (True/False)