Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support combining multiple output parsers #618

Merged
merged 8 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions examples/src/prompts/combining_parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { OpenAI, PromptTemplate } from "langchain";
nfcampos marked this conversation as resolved.
Show resolved Hide resolved
import { StructuredOutputParser, RegexParser, CombiningOutputParser } from "langchain/output_parsers";

export const run = async () => {

const answerParser = StructuredOutputParser.fromNamesAndDescriptions({
answer: "answer to the user's question",
source: "source used to answer the user's question, should be a website.",
});

const confidenceParser = new RegexParser(/Confidence: (A|B|C), Explanation: (.*)/, ["confidence", "explanation"], "noConfidence");

const parser = new CombiningOutputParser(answerParser, confidenceParser);
const formatInstructions = parser.getFormatInstructions();

const prompt = new PromptTemplate({
template:
"Answer the users question as best as possible.\n{format_instructions}\n{question}",
inputVariables: ["question"],
partialVariables: { format_instructions: formatInstructions },
});

const model = new OpenAI({ temperature: 0 });

const input = await prompt.format({
question: "What is the capital of France?",
});
const response = await model.call(input);

console.log(input);
/*
Answer the users question as best as possible.
For your first output: The output should be a markdown code snippet formatted in the following schema:

```json
{
"answer": string // answer to the user's question
"source": string // source used to answer the user's question, should be a website.
}
```
Complete that output fully. Then produce another output: Your response should match the following regex: //Confidence: (A|B|C), Explanation: (.*)//

What is the capital of France?
*/

console.log(response);
/*
```json
{
"answer": "Paris",
"source": "https://en.wikipedia.org/wiki/France"
}
```
//Confidence: A, Explanation: Paris is the capital of France according to Wikipedia.//
*/

console.log(await parser.parse(response));
/*
{
answer: 'Paris',
source: 'https://en.wikipedia.org/wiki/France',
confidence: 'A',
explanation: 'Paris is the capital of France according to Wikipedia.//'
}
*/
};
29 changes: 29 additions & 0 deletions langchain/src/output_parsers/combining.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { BaseOutputParser } from "../schema/index.js";

/**
* Class to combine multiple output parsers
* @augments BaseOutputParser
*/
export class CombiningOutputParser extends BaseOutputParser {
parsers: BaseOutputParser[];

constructor(...parsers: BaseOutputParser[]) {
super();
this.parsers = parsers;
}

async parse(input: string): Promise<Record<string, any>> {
let ret = {};
for (const p of this.parsers) {
ret = { ...ret, ...((await p.parse(input)) as Record<string, any>) };
}
return ret;
}

getFormatInstructions(): string {
const initial = "For your first output: " + this?.parsers?.[0]?.getFormatInstructions();
const subsequent = this.parsers.slice(1).map((p) => "Complete that output fully. Then produce another output: " + p.getFormatInstructions()).join("\n");
return initial + "\n" + subsequent;
}
}

1 change: 1 addition & 0 deletions langchain/src/output_parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { ListOutputParser, CommaSeparatedListOutputParser } from "./list.js";
export { RegexParser } from "./regex.js";
export { StructuredOutputParser } from "./structured.js";
export { OutputFixingParser } from "./fix.js";
export { CombiningOutputParser } from "./combining.js";