Skip to content

Commit

Permalink
langchain[patch],docs[patch]: Deprecate createStructuredOutputRunnable (
Browse files Browse the repository at this point in the history
#5199)

* langchain[patch],docs[patch]: Deprecate createStructuredOutputRunnable

* chore: lint files
  • Loading branch information
bracesproul committed Apr 24, 2024
1 parent b9d86b1 commit 3823c26
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
8 changes: 1 addition & 7 deletions docs/core_docs/docs/use_cases/graph/mapping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
"metadata": {},
"outputs": [],
"source": [
"import { createStructuredOutputRunnable } from \"langchain/chains/openai_functions\";\n",
"import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"import { z } from \"zod\";\n",
Expand All @@ -166,12 +165,7 @@
" ]\n",
")\n",
"\n",
"\n",
"const entityChain = createStructuredOutputRunnable({\n",
" outputSchema: entities,\n",
" prompt,\n",
" llm,\n",
" });"
"const entityChain = prompt.pipe(llm.withStructuredOutput(entities));"
]
},
{
Expand Down
11 changes: 4 additions & 7 deletions examples/src/chains/openai_functions_json_schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
import { createStructuredOutputRunnable } from "langchain/chains/openai_functions";
import { JsonOutputFunctionsParser } from "langchain/output_parsers";

const jsonSchema = {
Expand All @@ -25,12 +24,10 @@ const prompt = ChatPromptTemplate.fromMessages([
]);
const outputParser = new JsonOutputFunctionsParser();

const runnable = createStructuredOutputRunnable({
outputSchema: jsonSchema,
llm: model,
prompt,
outputParser,
});
const runnable = prompt
.pipe(model.withStructuredOutput(jsonSchema))
.pipe(outputParser);

const response = await runnable.invoke({
description:
"My name's John Doe and I'm 30 years old. My favorite kind of food are chocolate chip cookies.",
Expand Down
43 changes: 18 additions & 25 deletions examples/src/use_cases/sql/large_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
RunnableSequence,
} from "@langchain/core/runnables";
import { ChatOpenAI } from "@langchain/openai";
import { createStructuredOutputRunnable } from "langchain/chains/openai_functions";
import { createSqlQueryChain } from "langchain/chains/sql_db";
import { SqlDatabase } from "langchain/sql_db";
import { DataSource } from "typeorm";
Expand All @@ -31,14 +30,12 @@ ${tableNames}
Remember to include ALL POTENTIALLY RELEVANT tables, even if you're not sure that they're needed.`;

const tableChain = createStructuredOutputRunnable({
llm,
outputSchema: Table,
prompt: ChatPromptTemplate.fromMessages([
["system", system],
["human", "{input}"],
]),
});
const prompt = ChatPromptTemplate.fromMessages([
["system", system],
["human", "{input}"],
]);
const tableChain = prompt.pipe(llm.withStructuredOutput(Table));

console.log(
await tableChain.invoke({
input: "What are all the genres of Alanis Morisette songs?",
Expand All @@ -62,22 +59,18 @@ In this case, we might think to simplify our model’s job by grouping the table
We’ll just ask the model to choose between categories “Music” and “Business”, and then take care of selecting all the relevant tables from there:
*/

const system2 = `Return the names of the SQL tables that are relevant to the user question.
The tables are:
Music
Business`;
const categoryChain = createStructuredOutputRunnable<
{ input: string },
z.infer<typeof Table>
>({
llm,
outputSchema: Table,
prompt: ChatPromptTemplate.fromMessages([
["system", system2],
["human", "{input}"],
]),
});
const prompt2 = ChatPromptTemplate.fromMessages([
[
"system",
`Return the names of the SQL tables that are relevant to the user question.
The tables are:
Music
Business`,
],
["human", "{input}"],
]);
const categoryChain = prompt2.pipe(llm.withStructuredOutput(Table));
console.log(
await categoryChain.invoke({
input: "What are all the genres of Alanis Morisette songs?",
Expand Down
2 changes: 2 additions & 0 deletions langchain/src/chains/openai_functions/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export type CreateStructuredOutputRunnableConfig<
};

/**
* @deprecated Prefer the `.withStructuredOutput` method on chat model classes.
*
* Create a runnable that uses an OpenAI function to get a structured output.
* @param config Params required to create the runnable.
* @returns A runnable sequence that will pass the given function to the model when run.
Expand Down

0 comments on commit 3823c26

Please sign in to comment.