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.
Nc/llmchain functions (langchain-ai#1699)
* Refactor openai functions chains to use output parsers * Lint * Rename * Add docs
- Loading branch information
Showing
25 changed files
with
298 additions
and
152 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,24 @@ | ||
--- | ||
hide_table_of_contents: true | ||
sidebar_position: 4 | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Extraction from "@examples/chains/openai_functions_extraction.ts"; | ||
import Tagging from "@examples/chains/openai_functions_tagging.ts"; | ||
|
||
# OpenAI Functions Chains | ||
|
||
These chains are designed to be used with the [OpenAI Functions](https://platform.openai.com/docs/guides/gpt/function-calling) API. | ||
|
||
## Extraction | ||
|
||
This chain is designed to extract lists of objects from an input text and schema of desired info. | ||
|
||
<CodeBlock language="typescript">{Extraction}</CodeBlock> | ||
|
||
## Tagging | ||
|
||
This chain is designed to tag an input text according to properties defined in a schema. | ||
|
||
<CodeBlock language="typescript">{Tagging}</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,37 @@ | ||
import { z } from "zod"; | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { createExtractionChainFromZod } from "langchain/chains"; | ||
|
||
const chain = createExtractionChainFromZod( | ||
z.object({ | ||
"person-name": z.string().optional(), | ||
"person-age": z.number().optional(), | ||
"person-hair_color": z.string().optional(), | ||
"dog-name": z.string().optional(), | ||
"dog-breed": z.string().optional(), | ||
}), | ||
new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613", temperature: 0 }) | ||
); | ||
|
||
console.log( | ||
await chain.run(`Alex is 5 feet tall. Claudia is 4 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde. | ||
Alex's dog Frosty is a labrador and likes to play hide and seek.`) | ||
); | ||
/* | ||
[ | ||
{ | ||
'person-name': 'Alex', | ||
'person-age': 0, | ||
'person-hair_color': 'blonde', | ||
'dog-name': 'Frosty', | ||
'dog-breed': 'labrador' | ||
}, | ||
{ | ||
'person-name': 'Claudia', | ||
'person-age': 0, | ||
'person-hair_color': 'brunette', | ||
'dog-name': '', | ||
'dog-breed': '' | ||
} | ||
] | ||
*/ |
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,24 @@ | ||
import { createTaggingChain } from "langchain/chains"; | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
|
||
const chain = createTaggingChain( | ||
{ | ||
type: "object", | ||
properties: { | ||
sentiment: { type: "string" }, | ||
tone: { type: "string" }, | ||
language: { type: "string" }, | ||
}, | ||
required: ["tone"], | ||
}, | ||
new ChatOpenAI({ modelName: "gpt-4-0613", temperature: 0 }) | ||
); | ||
|
||
console.log( | ||
await chain.run( | ||
`Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!` | ||
) | ||
); | ||
/* | ||
{ tone: 'positive', language: 'Spanish' } | ||
*/ |
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
Oops, something went wrong.