!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

You are viewing the **preview** v0.2 docs. View the **stable** v0.1 docs [here](/v0.1/docs/get_started/introduction/). Leave feedback on the v0.2 docs [here](https://github.com/langchain-ai/langchainjs/discussions/5386).

[

![🦜️🔗 Langchain](/v0.2/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.2/img/brand/wordmark-dark.png)

](/v0.2/)[Integrations](/v0.2/docs/integrations/platforms/)[API Reference](https://v02.api.js.langchain.com)

[More](#)

*   [People](/v0.2/docs/people/)
*   [Community](/v0.2/docs/community)
*   [Tutorials](/v0.2/docs/additional_resources/tutorials)
*   [Contributing](/v0.2/docs/contributing)

[v0.2](#)

*   [v0.2](/v0.2/docs/introduction)
*   [v0.1](https://js.langchain.com/v0.1/docs/get_started/introduction)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.2/docs/integrations/platforms/)
    
    *   [Providers](/v0.2/docs/integrations/platforms/)
    *   [Anthropic](/v0.2/docs/integrations/platforms/anthropic)
    *   [AWS](/v0.2/docs/integrations/platforms/aws)
    *   [Google](/v0.2/docs/integrations/platforms/google)
    *   [Microsoft](/v0.2/docs/integrations/platforms/microsoft)
    *   [OpenAI](/v0.2/docs/integrations/platforms/openai)
*   [Components](/v0.2/docs/integrations/components)
    
    *   [Chat models](/v0.2/docs/integrations/chat/)
        
    *   [LLMs](/v0.2/docs/integrations/llms/)
        
    *   [Embedding models](/v0.2/docs/integrations/text_embedding)
        
    *   [Document loaders](/v0.2/docs/integrations/document_loaders)
        
    *   [Document transformers](/v0.2/docs/integrations/document_transformers)
        
    *   [Vector stores](/v0.2/docs/integrations/vectorstores)
        
    *   [Retrievers](/v0.2/docs/integrations/retrievers)
        
    *   [Tools](/v0.2/docs/integrations/tools)
        
        *   [ChatGPT Plugins](/v0.2/docs/integrations/tools/aiplugin-tool)
        *   [Connery Action Tool](/v0.2/docs/integrations/tools/connery)
        *   [Dall-E Tool](/v0.2/docs/integrations/tools/dalle)
        *   [Discord Tool](/v0.2/docs/integrations/tools/discord)
        *   [DuckDuckGoSearch](/v0.2/docs/integrations/tools/duckduckgo_search)
        *   [Exa Search](/v0.2/docs/integrations/tools/exa_search)
        *   [Gmail Tool](/v0.2/docs/integrations/tools/gmail)
        *   [Google Calendar Tool](/v0.2/docs/integrations/tools/google_calendar)
        *   [Google Places Tool](/v0.2/docs/integrations/tools/google_places)
        *   [Agent with AWS Lambda](/v0.2/docs/integrations/tools/lambda_agent)
        *   [Python interpreter tool](/v0.2/docs/integrations/tools/pyinterpreter)
        *   [SearchApi tool](/v0.2/docs/integrations/tools/searchapi)
        *   [Searxng Search tool](/v0.2/docs/integrations/tools/searxng)
        *   [StackExchange Tool](/v0.2/docs/integrations/tools/stackexchange)
        *   [Tavily Search](/v0.2/docs/integrations/tools/tavily_search)
        *   [Web Browser Tool](/v0.2/docs/integrations/tools/webbrowser)
        *   [Wikipedia tool](/v0.2/docs/integrations/tools/wikipedia)
        *   [WolframAlpha Tool](/v0.2/docs/integrations/tools/wolframalpha)
        *   [Agent with Zapier NLA Integration](/v0.2/docs/integrations/tools/zapier_agent)
    *   [Toolkits](/v0.2/docs/integrations/toolkits)
        
    *   [Stores](/v0.2/docs/integrations/stores/)
        

*   [](/v0.2/)
*   [Components](/v0.2/docs/integrations/components)
*   [Tools](/v0.2/docs/integrations/tools)
*   Exa Search

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.](https://dashboard.exa.ai/login)

Usage[​](#usage "Direct link to Usage")
---------------------------------------

First, install the LangChain integration package for Exa:

tip

See [this section for general instructions on installing integration packages](/v0.2/docs/how_to/installation#installing-integration-packages).

*   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[​](#usage-1 "Direct link to 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-agentconst 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](https://v02.api.js.langchain.com/classes/langchain_exa.ExaSearchResults.html) from `@langchain/exa`
*   [ChatOpenAI](https://v02.api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [ChatPromptTemplate](https://v02.api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [pull](https://v02.api.js.langchain.com/functions/langchain_hub.pull.html) from `langchain/hub`
*   [AgentExecutor](https://v02.api.js.langchain.com/classes/langchain_agents.AgentExecutor.html) from `langchain/agents`
*   [createOpenAIFunctionsAgent](https://v02.api.js.langchain.com/functions/langchain_agents.createOpenAIFunctionsAgent.html) from `langchain/agents`

tip

You can see a LangSmith trace for this example [here](https://smith.langchain.com/public/775ea9a8-d54c-405c-9126-a012405d9099/r).

Using the Exa SDK as LangChain Agent Tools[​](#using-the-exa-sdk-as-langchain-agent-tools "Direct link to Using the Exa SDK as LangChain Agent Tools")
------------------------------------------------------------------------------------------------------------------------------------------------------

We can create LangChain tools which use the [`ExaRetriever`](/v0.2/docs/integrations/retrievers/exa) and the [`createRetrieverTool`](https://v02.api.js.langchain.com/functions/langchain_tools_retriever.createRetrieverTool.html) 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 toolconst 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](https://v02.api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [MessagesPlaceholder](https://v02.api.js.langchain.com/classes/langchain_core_prompts.MessagesPlaceholder.html) from `@langchain/core/prompts`
*   [ChatOpenAI](https://v02.api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [AgentExecutor](https://v02.api.js.langchain.com/classes/langchain_agents.AgentExecutor.html) from `langchain/agents`
*   [createOpenAIFunctionsAgent](https://v02.api.js.langchain.com/functions/langchain_agents.createOpenAIFunctionsAgent.html) from `langchain/agents`
*   [createRetrieverTool](https://v02.api.js.langchain.com/functions/langchain_tools_retriever.createRetrieverTool.html) from `langchain/tools/retriever`
*   [ExaRetriever](https://v02.api.js.langchain.com/classes/langchain_exa.ExaRetriever.html) 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/))'}

tip

You can see a LangSmith trace for this example [here](https://smith.langchain.com/public/d123ba5f-8535-4669-9e43-ac7ab3c6735e/r).

* * *

#### Was this page helpful?

  

#### You can leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

DuckDuckGoSearch

](/v0.2/docs/integrations/tools/duckduckgo_search)[

Next

Gmail Tool

](/v0.2/docs/integrations/tools/gmail)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.