forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds document transformer abstraction, metadata tagger (langchain-ai#…
…1945) * Adds document transformers, add OpenAIFunctions metadata tagger * Adds docstrings for base document transformer abstract class * Adds docs and entrypoint for document transformers * Type tagging chain inputs better * Formatting
- Loading branch information
1 parent
c360d53
commit 37dfc77
Showing
22 changed files
with
364 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
sidebar_label: Document Transformers | ||
sidebar_position: 2 | ||
--- | ||
|
||
# Document Transformers | ||
|
||
Once you've loaded documents, you'll often want to transform them to better suit your application. One example of this is | ||
to automatically tag the loaded documents with metadata extracted from their content. | ||
|
||
## 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 | ||
|
||
For example, let's say you wanted to index a set of movie reviews. You could initialize the document transformer as follows: | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/document_transformers/metadata_tagger.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> | ||
|
||
There is an additional `createMetadataTagger` method that accepts a valid JSON Schema object as well. | ||
|
||
### 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 CustomExample from "@examples/document_transformers/metadata_tagger_custom_prompt.ts"; | ||
|
||
<CodeBlock language="typescript">{CustomExample}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { z } from "zod"; | ||
import { createMetadataTaggerFromZod } from "langchain/document_transformers/openai_functions"; | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { Document } from "langchain/document"; | ||
|
||
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({ modelName: "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 | ||
} | ||
} | ||
] | ||
*/ |
70 changes: 70 additions & 0 deletions
70
examples/src/document_transformers/metadata_tagger_custom_prompt.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { z } from "zod"; | ||
import { createMetadataTaggerFromZod } from "langchain/document_transformers/openai_functions"; | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { Document } from "langchain/document"; | ||
import { PromptTemplate } from "langchain/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({ modelName: "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 | ||
} | ||
} | ||
] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { z } from "zod"; | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
import type { JsonSchema7ObjectType } from "zod-to-json-schema/src/parsers/object.js"; | ||
|
||
import { Document } from "../document.js"; | ||
import { BaseChain } from "../chains/base.js"; | ||
import { BaseDocumentTransformer } from "../schema/document.js"; | ||
import { | ||
TaggingChainOptions, | ||
createTaggingChain, | ||
} from "../chains/openai_functions/index.js"; | ||
import { ChatOpenAI } from "../chat_models/openai.js"; | ||
|
||
export class MetadataTagger extends BaseDocumentTransformer { | ||
protected taggingChain: BaseChain; | ||
|
||
constructor(fields: { taggingChain: BaseChain }) { | ||
super(); | ||
this.taggingChain = fields.taggingChain; | ||
if (this.taggingChain.inputKeys.length !== 1) { | ||
throw new Error( | ||
"Invalid input chain. The input chain must have exactly one input." | ||
); | ||
} | ||
if (this.taggingChain.outputKeys.length !== 1) { | ||
throw new Error( | ||
"Invalid input chain. The input chain must have exactly one output." | ||
); | ||
} | ||
} | ||
|
||
async transformDocuments(documents: Document[]): Promise<Document[]> { | ||
const newDocuments = []; | ||
for (const document of documents) { | ||
const taggingChainResponse = await this.taggingChain.call({ | ||
[this.taggingChain.inputKeys[0]]: document.pageContent, | ||
}); | ||
const extractedMetadata = | ||
taggingChainResponse[this.taggingChain.outputKeys[0]]; | ||
const newDocument = new Document({ | ||
pageContent: document.pageContent, | ||
metadata: { ...extractedMetadata, ...document.metadata }, | ||
}); | ||
newDocuments.push(newDocument); | ||
} | ||
return newDocuments; | ||
} | ||
} | ||
|
||
export function createMetadataTagger( | ||
schema: JsonSchema7ObjectType, | ||
options: TaggingChainOptions & { llm?: ChatOpenAI } | ||
) { | ||
const { llm = new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613" }), ...rest } = | ||
options; | ||
const taggingChain = createTaggingChain(schema, llm, rest); | ||
return new MetadataTagger({ taggingChain }); | ||
} | ||
|
||
export function createMetadataTaggerFromZod( | ||
schema: z.AnyZodObject, | ||
options: TaggingChainOptions & { llm?: ChatOpenAI } | ||
) { | ||
return createMetadataTagger( | ||
zodToJsonSchema(schema) as JsonSchema7ObjectType, | ||
options | ||
); | ||
} |
Oops, something went wrong.