!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

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
        *   [html-to-text](/v0.1/docs/integrations/document_transformers/html-to-text/)
        *   [@mozilla/readability](/v0.1/docs/integrations/document_transformers/mozilla_readability/)
        *   [OpenAI functions metadata tagger](/v0.1/docs/integrations/document_transformers/openai_metadata_tagger/)
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   [Document transformers](/v0.1/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.1/docs/get_started/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://api.js.langchain.com/functions/langchain_document_transformers_openai_functions.createMetadataTaggerFromZod.html) from `langchain/document_transformers/openai_functions`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [Document](https://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://api.js.langchain.com/functions/langchain_document_transformers_openai_functions.createMetadataTaggerFromZod.html) from `langchain/document_transformers/openai_functions`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [Document](https://api.js.langchain.com/classes/langchain_core_documents.Document.html) from `@langchain/core/documents`
*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

* * *

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

[

Previous

@mozilla/readability

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

Next

Document compressors

](/v0.1/docs/integrations/document_compressors/)

*   [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.