!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)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

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

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

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

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/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

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
        *   [Quickstart](/v0.1/docs/use_cases/question_answering/quickstart/)
        *   [Per-User Retrieval](/v0.1/docs/use_cases/question_answering/per_user/)
        *   [Add chat history](/v0.1/docs/use_cases/question_answering/chat_history/)
        *   [Citations](/v0.1/docs/use_cases/question_answering/citations/)
        *   [Using agents](/v0.1/docs/use_cases/question_answering/conversational_retrieval_agents/)
        *   [Using local models](/v0.1/docs/use_cases/question_answering/local_retrieval_qa/)
        *   [Returning sources](/v0.1/docs/use_cases/question_answering/sources/)
        *   [Streaming](/v0.1/docs/use_cases/question_answering/streaming/)
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
*   Using agents

On this page

Using agents
============

This is an agent specifically optimized for doing retrieval when necessary and also holding a conversation.

To start, we will set up the retriever we want to use, and then turn it into a retriever tool. Next, we will use the high level constructor for this type of agent. Finally, we will walk through how to construct a conversational retrieval agent from components.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

### Dependencies[​](#dependencies "Direct link to Dependencies")

We’ll use an OpenAI chat model and embeddings and a Memory vector store in this walkthrough, but everything shown here works with any [ChatModel](/v0.1/docs/modules/model_io/chat/) or [LLM](/v0.1/docs/modules/model_io/llms/), [Embeddings](/v0.1/docs/modules/data_connection/text_embedding/), and [VectorStore](/v0.1/docs/modules/data_connection/vectorstores/) or [Retriever](/v0.1/docs/modules/data_connection/retrievers/).

We’ll use the following packages:

    npm install --save langchain @langchain/openai

We need to set our environment variable for OpenAI:

    export OPENAI_API_KEY=YOUR_KEY

### LangSmith[​](#langsmith "Direct link to LangSmith")

Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls. As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent. The best way to do this is with [LangSmith](https://smith.langchain.com/).

Note that LangSmith is not needed, but it is helpful. If you do want to use LangSmith, after you sign up at the link above, make sure to set your environment variables to start logging traces:

    export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=YOUR_KEY

The Retriever[​](#the-retriever "Direct link to The Retriever")
---------------------------------------------------------------

To start, we need a retriever to use! The code here is mostly just example code. Feel free to use your own retriever and skip to the section on creating a retriever tool.

    import { TextLoader } from "langchain/document_loaders/fs/text";const loader = new TextLoader("../../../../../examples/state_of_the_union.txt");const documents = await loader.load();

    import { CharacterTextSplitter } from "langchain/text_splitter";import { MemoryVectorStore } from "langchain/vectorstores/memory";import { OpenAIEmbeddings } from "@langchain/openai";const textSplitter = new CharacterTextSplitter({  chunkSize: 1000,  chunkOverlap: 0,});const texts = await textSplitter.splitDocuments(documents);console.log("texts.length", texts.length);const embeddings = new OpenAIEmbeddings();const db = await MemoryVectorStore.fromDocuments(texts, embeddings);

    texts.length 41

    const retriever = db.asRetriever();

Retriever Tool[​](#retriever-tool "Direct link to Retriever Tool")
------------------------------------------------------------------

Now we need to create a tool for our retriever. The main things we need to pass in are a name for the retriever as well as a description. These will both be used by the language model, so they should be informative.

    import { createRetrieverTool } from "langchain/tools/retriever";const tool = createRetrieverTool(retriever, {  name: "search_state_of_union",  description:    "Searches and returns excerpts from the 2022 State of the Union.",});const tools = [tool];

Agent Constructor[​](#agent-constructor "Direct link to Agent Constructor")
---------------------------------------------------------------------------

Here, we will use the high level `createOpenaiToolsAgent` API to construct the agent.

Notice that beside the list of tools, the only thing we need to pass in is a language model to use. Under the hood, this agent is using the OpenAI tool-calling capabilities, so we need to use a ChatOpenAI model.

    import { pull } from "langchain/hub";import { ChatPromptTemplate } from "@langchain/core/prompts";const prompt = await pull<ChatPromptTemplate>("hwchase17/openai-tools-agent");prompt.promptMessages;

    [  SystemMessagePromptTemplate {    lc_serializable: true,    lc_kwargs: {      prompt: PromptTemplate {        lc_serializable: true,        lc_kwargs: {          template: "You are a helpful assistant",          inputVariables: [],          templateFormat: "f-string",          partialVariables: {}        },        lc_runnable: true,        name: undefined,        lc_namespace: [ "langchain_core", "prompts", "prompt" ],        inputVariables: [],        outputParser: undefined,        partialVariables: {},        template: "You are a helpful assistant",        templateFormat: "f-string",        validateTemplate: true      }    },    lc_runnable: true,    name: undefined,    lc_namespace: [ "langchain_core", "prompts", "chat" ],    prompt: PromptTemplate {      lc_serializable: true,      lc_kwargs: {        template: "You are a helpful assistant",        inputVariables: [],        templateFormat: "f-string",        partialVariables: {}      },      lc_runnable: true,      name: undefined,      lc_namespace: [ "langchain_core", "prompts", "prompt" ],      inputVariables: [],      outputParser: undefined,      partialVariables: {},      template: "You are a helpful assistant",      templateFormat: "f-string",      validateTemplate: true    }  },  MessagesPlaceholder {    lc_serializable: true,    lc_kwargs: { optional: true, variableName: "chat_history" },    lc_runnable: true,    name: undefined,    lc_namespace: [ "langchain_core", "prompts", "chat" ],    variableName: "chat_history",    optional: true  },  HumanMessagePromptTemplate {    lc_serializable: true,    lc_kwargs: {      prompt: PromptTemplate {        lc_serializable: true,        lc_kwargs: {          template: "{input}",          inputVariables: [Array],          templateFormat: "f-string",          partialVariables: {}        },        lc_runnable: true,        name: undefined,        lc_namespace: [ "langchain_core", "prompts", "prompt" ],        inputVariables: [ "input" ],        outputParser: undefined,        partialVariables: {},        template: "{input}",        templateFormat: "f-string",        validateTemplate: true      }    },    lc_runnable: true,    name: undefined,    lc_namespace: [ "langchain_core", "prompts", "chat" ],    prompt: PromptTemplate {      lc_serializable: true,      lc_kwargs: {        template: "{input}",        inputVariables: [ "input" ],        templateFormat: "f-string",        partialVariables: {}      },      lc_runnable: true,      name: undefined,      lc_namespace: [ "langchain_core", "prompts", "prompt" ],      inputVariables: [ "input" ],      outputParser: undefined,      partialVariables: {},      template: "{input}",      templateFormat: "f-string",      validateTemplate: true    }  },  MessagesPlaceholder {    lc_serializable: true,    lc_kwargs: { optional: false, variableName: "agent_scratchpad" },    lc_runnable: true,    name: undefined,    lc_namespace: [ "langchain_core", "prompts", "chat" ],    variableName: "agent_scratchpad",    optional: false  }]

    import { ChatOpenAI } from "@langchain/openai";const llm = new ChatOpenAI({ temperature: 0 });

    import { createOpenAIToolsAgent, AgentExecutor } from "langchain/agents";const agent = await createOpenAIToolsAgent({  llm,  tools,  prompt,});const agentExecutor = new AgentExecutor({  agent,  tools,});

We can now try it out!

    const result1 = await agentExecutor.invoke({ input: "hi im bob" });

    result1.output;

    "Hello Bob! How can I assist you today?"

Notice that it now does retrieval

    const result2 = await agentExecutor.invoke({  input: `what did the president say about ketanji brown jackson in the most recent state of the union? The current date is ${new Date().toDateString()}`,});

    result2.output;

    "In the most recent State of the Union, the President mentioned Ketanji Brown Jackson as his nominee "... 176 more characters

See a LangSmith trace for the run above [here](https://smith.langchain.com/public/02281666-7124-402e-bd12-722fb58976e5/r)

Notice that the follow up question asks about information previously retrieved, so no need to do another retrieval

    const result3 = await agentExecutor.invoke({  input:    "how long ago did the president nominate ketanji brown jackson? Use all the tools to find the answer.",});

    result3.output;

    "The president nominated Ketanji Brown Jackson 4 days ago."

See a LangSmith trace for the run above [here](https://smith.langchain.com/public/2b9ade9d-1f7e-4ae6-bb28-567f96a669f0/r)

For more on how to use agents with retrievers and other tools, head to the [Agents](/v0.1/docs/modules/agents/) section.

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Citations

](/v0.1/docs/use_cases/question_answering/citations/)[

Next

Using local models

](/v0.1/docs/use_cases/question_answering/local_retrieval_qa/)

*   [Setup](#setup)
    *   [Dependencies](#dependencies)
    *   [LangSmith](#langsmith)
*   [The Retriever](#the-retriever)
*   [Retriever Tool](#retriever-tool)
*   [Agent Constructor](#agent-constructor)

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.