How to extract metadata using langchain and openai?

51 Views Asked by At

I am trying to extract name and age in years only, when I run this. It's not actually giving me the correct data or sometimes fails abruptly.

const { z } = require("zod");
    const {
      createMetadataTaggerFromZod,
    } = require("langchain/document_transformers/openai_functions");
    const { ChatOpenAI } = require("@langchain/openai");
    const { Document } = require("@langchain/core/documents");
    const { PromptTemplate } = require("@langchain/core/prompts");
    
    const zodSchema = z.object({
      name: z.string().default(["Default"]),
      age: z.string().default([15]),
    });
    const taggingChainTemplate = `Extract age and name. Age should always be converted in years.
    
    Passage:
    {input}
    `;
    const metadataTagger = createMetadataTaggerFromZod(zodSchema, {
      llm: new ChatOpenAI({
        modelName: "gpt-4",
        prompt: PromptTemplate.fromTemplate(taggingChainTemplate),
      }),
    });
    
    const documents = [
      new Document({
        pageContent: "name is sahil, 30 year old",
      }),
      new Document({
        pageContent: "name is akash , 365 days old",
      }),
    ];
    
    metadataTagger.transformDocuments(documents).then((x) => {
      console.log(x);
    });

Sometimes I get this output. But you can see days are not converted into year:

[
  Document {
    pageContent: 'name is sahil, 30 year old',
    metadata: { name: 'sahil', age: '30' }
  },
  Document {
    pageContent: 'name is akash , 365 days old',
    metadata: { name: 'akash', age: '365' }
  }
]

Sometimes I get this error:

node_modules/langchain/dist/output_parsers/openai_functions.cjs:48
            throw new Error(`No function_call in message ${JSON.stringify(generations)}`);
0

There are 0 best solutions below