!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)
        
        *   [html-to-text](/v0.2/docs/integrations/document_transformers/html-to-text)
        *   [@mozilla/readability](/v0.2/docs/integrations/document_transformers/mozilla_readability)
        *   [OpenAI functions metadata tagger](/v0.2/docs/integrations/document_transformers/openai_metadata_tagger)
    *   [Vector stores](/v0.2/docs/integrations/vectorstores)
        
    *   [Retrievers](/v0.2/docs/integrations/retrievers)
        
    *   [Tools](/v0.2/docs/integrations/tools)
        
    *   [Toolkits](/v0.2/docs/integrations/toolkits)
        
    *   [Stores](/v0.2/docs/integrations/stores/)
        

*   [](/v0.2/)
*   [Components](/v0.2/docs/integrations/components)
*   [Document transformers](/v0.2/docs/integrations/document_transformers)
*   OpenAI functions metadata tagger

On this page

OpenAI functions metadata tagger
================================

It can often be useful to tag ingested documents with structured metadata, such as the title, tone, or length of a document, to allow for more targeted similarity search later. However, for large numbers of documents, performing this labelling process manually can be tedious.

The `MetadataTagger` document transformer automates this process by extracting metadata from each provided document according to a provided schema. It uses a configurable OpenAI Functions-powered chain under the hood, so if you pass a custom LLM instance, it must be an OpenAI model with functions support.

**Note:** This document transformer works best with complete documents, so it's best to run it first with whole documents before doing any other splitting or processing!

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

For example, let's say you wanted to index a set of movie reviews. You could initialize the document transformer as follows:

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/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { z } from "zod";import { createMetadataTaggerFromZod } from "langchain/document_transformers/openai_functions";import { ChatOpenAI } from "@langchain/openai";import { Document } from "@langchain/core/documents";const zodSchema = z.object({  movie_title: z.string(),  critic: z.string(),  tone: z.enum(["positive", "negative"]),  rating: z    .optional(z.number())    .describe("The number of stars the critic rated the movie"),});const metadataTagger = createMetadataTaggerFromZod(zodSchema, {  llm: new ChatOpenAI({ model: "gpt-3.5-turbo" }),});const documents = [  new Document({    pageContent:      "Review of The Bee Movie\nBy Roger Ebert\nThis is the greatest movie ever made. 4 out of 5 stars.",  }),  new Document({    pageContent:      "Review of The Godfather\nBy Anonymous\n\nThis movie was super boring. 1 out of 5 stars.",    metadata: { reliable: false },  }),];const taggedDocuments = await metadataTagger.transformDocuments(documents);console.log(taggedDocuments);/*  [    Document {      pageContent: 'Review of The Bee Movie\n' +        'By Roger Ebert\n' +        'This is the greatest movie ever made. 4 out of 5 stars.',      metadata: {        movie_title: 'The Bee Movie',        critic: 'Roger Ebert',        tone: 'positive',        rating: 4      }    },    Document {      pageContent: 'Review of The Godfather\n' +        'By Anonymous\n' +        '\n' +        'This movie was super boring. 1 out of 5 stars.',      metadata: {        movie_title: 'The Godfather',        critic: 'Anonymous',        tone: 'negative',        rating: 1,        reliable: false      }    }  ]*/

#### API Reference:

*   [createMetadataTaggerFromZod](https://v02.api.js.langchain.com/functions/langchain_document_transformers_openai_functions.createMetadataTaggerFromZod.html) from `langchain/document_transformers/openai_functions`
*   [ChatOpenAI](https://v02.api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [Document](https://v02.api.js.langchain.com/classes/langchain_core_documents.Document.html) from `@langchain/core/documents`

There is an additional `createMetadataTagger` method that accepts a valid JSON Schema object as well.

### Customization[​](#customization "Direct link to Customization")

You can pass the underlying tagging chain the standard LLMChain arguments in the second options parameter. For example, if you wanted to ask the LLM to focus specific details in the input documents, or extract metadata in a certain style, you could pass in a custom prompt:

    import { z } from "zod";import { createMetadataTaggerFromZod } from "langchain/document_transformers/openai_functions";import { ChatOpenAI } from "@langchain/openai";import { Document } from "@langchain/core/documents";import { PromptTemplate } from "@langchain/core/prompts";const taggingChainTemplate = `Extract the desired information from the following passage.Anonymous critics are actually Roger Ebert.Passage:{input}`;const zodSchema = z.object({  movie_title: z.string(),  critic: z.string(),  tone: z.enum(["positive", "negative"]),  rating: z    .optional(z.number())    .describe("The number of stars the critic rated the movie"),});const metadataTagger = createMetadataTaggerFromZod(zodSchema, {  llm: new ChatOpenAI({ model: "gpt-3.5-turbo" }),  prompt: PromptTemplate.fromTemplate(taggingChainTemplate),});const documents = [  new Document({    pageContent:      "Review of The Bee Movie\nBy Roger Ebert\nThis is the greatest movie ever made. 4 out of 5 stars.",  }),  new Document({    pageContent:      "Review of The Godfather\nBy Anonymous\n\nThis movie was super boring. 1 out of 5 stars.",    metadata: { reliable: false },  }),];const taggedDocuments = await metadataTagger.transformDocuments(documents);console.log(taggedDocuments);/*  [    Document {      pageContent: 'Review of The Bee Movie\n' +        'By Roger Ebert\n' +        'This is the greatest movie ever made. 4 out of 5 stars.',      metadata: {        movie_title: 'The Bee Movie',        critic: 'Roger Ebert',        tone: 'positive',        rating: 4      }    },    Document {      pageContent: 'Review of The Godfather\n' +        'By Anonymous\n' +        '\n' +        'This movie was super boring. 1 out of 5 stars.',      metadata: {        movie_title: 'The Godfather',        critic: 'Roger Ebert',        tone: 'negative',        rating: 1,        reliable: false      }    }  ]*/

#### API Reference:

*   [createMetadataTaggerFromZod](https://v02.api.js.langchain.com/functions/langchain_document_transformers_openai_functions.createMetadataTaggerFromZod.html) from `langchain/document_transformers/openai_functions`
*   [ChatOpenAI](https://v02.api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [Document](https://v02.api.js.langchain.com/classes/langchain_core_documents.Document.html) from `@langchain/core/documents`
*   [PromptTemplate](https://v02.api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

* * *

#### 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

@mozilla/readability

](/v0.2/docs/integrations/document_transformers/mozilla_readability)[

Next

Vector stores

](/v0.2/docs/integrations/vectorstores)

*   [Usage](#usage)
*   [Customization](#customization)

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.