A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python.langchain.com/docs/integrations/tools/exa_search below:

Exa Search | 🦜️🔗 LangChain

Exa Search

Exa is a search engine fully designed for use by LLMs. Search for documents on the internet using natural language queries, then retrieve cleaned HTML content from desired documents.

Unlike keyword-based search (Google), Exa's neural search capabilities allow it to semantically understand queries and return relevant documents. For example, we could search "fascinating article about cats" and compare the search results from Google and Exa. Google gives us SEO-optimized listicles based on the keyword "fascinating". Exa just works.

This notebook goes over how to use Exa Search with LangChain.

Setup Installation

Install the LangChain Exa integration package:

%pip install --upgrade --quiet langchain-exa 


%pip install --upgrade --quiet langchain langchain-openai langchain-community
Credentials

You'll need an Exa API key to use this integration. Get $10 free credit (plus more by completing certain actions like making your first search) by signing up here.

import getpass
import os

if not os.environ.get("EXA_API_KEY"):
os.environ["EXA_API_KEY"] = getpass.getpass("Exa API key:\n")

ExaSearchResults is a tool that can be used with LangChain agents to perform Exa searches. It provides a more structured interface for search operations:

from langchain_exa import ExaSearchResults


search_tool = ExaSearchResults(exa_api_key=os.environ["EXA_API_KEY"])


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)
Advanced Features for ExaSearchResults

You can use advanced search options like controlling search type, live crawling, and content filtering:


search_results = search_tool._run(
query="Latest AI research papers",
num_results=10,
type="auto",
livecrawl="always",
text_contents_options={"max_characters": 2000},
summary={"query": "generate one liner"},
)

print("Advanced Search Results:")
print(search_results)

ExaFindSimilarResults allows you to find webpages similar to a given URL. This is useful for finding related content or competitive analysis:

from langchain_exa import ExaFindSimilarResults


find_similar_tool = ExaFindSimilarResults(exa_api_key=os.environ["EXA_API_KEY"])


similar_results = find_similar_tool._run(
url="http://espn.com", num_results=5, text_contents_options=True, highlights=True
)

print("Similar Results:", similar_results)
Use within an Agent

We can use the ExaSearchResults and ExaFindSimilarResults tools with a LangGraph agent. This gives the agent the ability to dynamically search for information and find similar content based on the user's queries.

First, let's set up the language model. You'll need to provide your OpenAI API key:

import getpass

if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API key:\n")

We will need to install langgraph:

%pip install -qU langgraph
from langchain.chat_models import init_chat_model
from langchain_exa import ExaFindSimilarResults, ExaSearchResults
from langgraph.prebuilt import create_react_agent


llm = init_chat_model(model="gpt-4o", model_provider="openai", temperature=0)


exa_search = ExaSearchResults(
exa_api_key=os.environ["EXA_API_KEY"],
max_results=5,
)

exa_find_similar = ExaFindSimilarResults(
exa_api_key=os.environ["EXA_API_KEY"],
max_results=5,
)


agent = create_react_agent(llm, [exa_search, exa_find_similar])


user_input = "What are the latest developments in quantum computing?"

for step in agent.stream(
{"messages": user_input},
stream_mode="values",
):
step["messages"][-1].pretty_print()
Using ExaSearchRetriever

ExaSearchRetriever is a retriever that uses Exa Search to retrieve relevant documents.

note

The max_characters parameter for TextContentsOptions used to be called max_length which is now deprecated. Make sure to use max_characters instead.

Basic Usage

Here's a simple example of using ExaSearchRetriever:

from langchain_exa import ExaSearchRetriever


exa = ExaSearchRetriever(exa_api_key=os.environ["EXA_API_KEY"])


results = exa.invoke("What is the capital of France?")


print(results)
Advanced Features

You can use advanced features like controlling the number of results, search type, live crawling, summaries, and text content options:

from langchain_exa import ExaSearchRetriever


exa = ExaSearchRetriever(
exa_api_key=os.environ["EXA_API_KEY"],
k=20,
type="auto",
livecrawl="always",
text_contents_options={"max_characters": 3000},

summary={"query": "generate one line summary in simple words."},
)


results = exa.invoke("Latest developments in quantum computing")
print(f"Found {len(results)} results")
for result in results[:3]:
print(f"Title: {result.metadata.get('title', 'N/A')}")
print(f"URL: {result.metadata.get('url', 'N/A')}")
print(f"Summary: {result.metadata.get('summary', 'N/A')}")
print("-" * 80)
API Reference

For detailed documentation of all Exa API features and configurations, visit the Exa API documentation.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4