Exa Search
Exa (formerly Metaphor Search) 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.
First, get an Exa API key and add it as an environment variable. Get 1000 free searches/month by signing up here.
Usageβ
First, install the LangChain integration package for Exa:
- npm
- Yarn
- pnpm
npm install @langchain/exa @langchain/openai langchain
yarn add @langchain/exa @langchain/openai langchain
pnpm add @langchain/exa @langchain/openai langchain
You'll need to set your API key as an environment variable.
The Exa
class defaults to EXASEARCH_API_KEY
when searching for your API key.
Usageβ
import { ExaSearchResults } from "@langchain/exa";
import { ChatOpenAI } from "@langchain/openai";
import type { ChatPromptTemplate } from "@langchain/core/prompts";
import Exa from "exa-js";
import { pull } from "langchain/hub";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
// Define the tools the agent will have access to.
const tools = [
new ExaSearchResults({
// @ts-expect-error Some TS Config's will cause this to give a TypeScript error, even though it works.
client: new Exa(process.env.EXASEARCH_API_KEY),
}),
];
// Get the prompt to use - you can modify this!
// If you want to see the prompt in full, you can at:
// https://smith.langchain.com/hub/hwchase17/openai-functions-agent
const prompt = await pull<ChatPromptTemplate>(
"hwchase17/openai-functions-agent"
);
const llm = new ChatOpenAI({
model: "gpt-3.5-turbo-1106",
temperature: 0,
});
const agent = await createOpenAIFunctionsAgent({
llm,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
});
const result = await agentExecutor.invoke({
input: "what is the weather in wailea?",
});
console.log(result);
/*
{
input: 'what is the weather in wailea?',
output: 'I found a weather forecast for Wailea-Makena on Windfinder.com. You can check the forecast [here](https://www.windfinder.com/forecast/wailea-makena).'
}
*/
API Reference:
- ExaSearchResults from
@langchain/exa
- ChatOpenAI from
@langchain/openai
- ChatPromptTemplate from
@langchain/core/prompts
- pull from
langchain/hub
- AgentExecutor from
langchain/agents
- createOpenAIFunctionsAgent from
langchain/agents
You can see a LangSmith trace for this example here.
Using the Exa SDK as LangChain Agent Toolsβ
We can create LangChain tools which use the ExaRetriever
and the createRetrieverTool
Using these tools we can construct a simple search agent that can answer questions about any topic.
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
import Exa from "exa-js";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
import { createRetrieverTool } from "langchain/tools/retriever";
import { ExaRetriever } from "@langchain/exa";
// @ts-expect-error Some TS Config's will cause this to give a TypeScript error, even though it works.
const client: Exa.default = new Exa(process.env.EXASEARCH_API_KEY);
const exaRetriever = new ExaRetriever({
client,
searchArgs: {
numResults: 2,
},
});
// Convert the ExaRetriever into a tool
const searchTool = createRetrieverTool(exaRetriever, {
name: "search",
description: "Get the contents of a webpage given a string search query.",
});
const tools = [searchTool];
const llm = new ChatOpenAI({ model: "gpt-4", temperature: 0 });
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
`You are a web researcher who answers user questions by looking up information on the internet and retrieving contents of helpful documents. Cite your sources.`,
],
["human", "{input}"],
new MessagesPlaceholder("agent_scratchpad"),
]);
const agentExecutor = new AgentExecutor({
agent: await createOpenAIFunctionsAgent({
llm,
tools,
prompt,
}),
tools,
});
console.log(
await agentExecutor.invoke({
input: "Summarize for me a fascinating article about cats.",
})
);
API Reference:
- ChatPromptTemplate from
@langchain/core/prompts
- MessagesPlaceholder from
@langchain/core/prompts
- ChatOpenAI from
@langchain/openai
- AgentExecutor from
langchain/agents
- createOpenAIFunctionsAgent from
langchain/agents
- createRetrieverTool from
langchain/tools/retriever
- ExaRetriever from
@langchain/exa
{
input: 'Summarize for me a fascinating article about cats.',
output: 'The article discusses the research of biologist Jaroslav Flegr, who has been investigating the effects of a single-celled parasite called Toxoplasma gondii (T. gondii or Toxo), which is excreted by cats in their feces. Flegr began to suspect in the early 1990s that this parasite was subtly manipulating his personality, causing him to behave in strange, often self-destructive ways. He reasoned that if it was affecting him, it was probably doing the same to others.\n' +
'\n' +
"T. gondii is the microbe that causes toxoplasmosis, a disease that can be transmitted from a pregnant woman to her fetus, potentially resulting in severe brain damage or death. It's also a major threat to people with weakened immunity. However, healthy children and adults usually experience nothing worse than brief flu-like symptoms before quickly fighting off the protozoan, which then lies dormant inside brain cells.\n" +
'\n' +
"Flegr's research is unconventional and suggests that these tiny organisms carried by house cats could be creeping into our brains, causing everything from car wrecks to schizophrenia.\n" +
'\n' +
'(Source: [The Atlantic](https://www.theatlantic.com/magazine/archive/2012/03/how-your-cat-is-making-you-crazy/308873/))'
}
You can see a LangSmith trace for this example here.