Skip to content

Commit

Permalink
Merge pull request #859 from jameshfisher/fix-regex-parser-double-sla…
Browse files Browse the repository at this point in the history
…shes

Fix RegexParser including double slashes in prompt
  • Loading branch information
nfcampos authored Apr 18, 2023
2 parents 182419f + bb07992 commit d8bd20f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
20 changes: 11 additions & 9 deletions examples/src/prompts/combining_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,35 @@ export const run = async () => {
```json
{
"answer": string // answer to the user's question
"source": string // source used to answer the user's question, should be a website.
"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: (.*)//
Including the leading and trailing "```json" and "```"
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"
"answer": "Paris",
"source": "https://en.wikipedia.org/wiki/Paris"
}
```
//Confidence: A, Explanation: Paris is the capital of France according to Wikipedia.//
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',
source: 'https://en.wikipedia.org/wiki/Paris',
confidence: 'A',
explanation: 'Paris is the capital of France according to Wikipedia.//'
}
explanation: 'Paris is the capital of France, according to Wikipedia.'
}
*/
};
38 changes: 38 additions & 0 deletions examples/src/prompts/regex_parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { OpenAI } from "langchain/llms/openai";
import { RegexParser } from "langchain/output_parsers";
import { PromptTemplate } from "langchain/prompts";

export const run = async () => {
const parser = new RegexParser(
/Humor: ([0-9]+), Sophistication: (A|B|C|D|E)/,
["mark", "grade"],
"noConfidence"
);
const formatInstructions = parser.getFormatInstructions();

const prompt = new PromptTemplate({
template: "Grade the joke.\n\n{format_instructions}\n\nJoke: {joke}",
inputVariables: ["joke"],
partialVariables: { format_instructions: formatInstructions },
});

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

const input = await prompt.format({
joke: "What time is the appointment? Tooth hurt-y.",
});
console.log(input);
/*
Grade the joke.
Your response should match the following regex: /Humor: ([0-9]+), Sophistication: (A|B|C|D|E)/
Joke: What time is the appointment? Tooth hurt-y.
*/

const response = await model.call(input);
console.log(response);
/*
Humor: 8, Sophistication: D
*/
};
4 changes: 2 additions & 2 deletions langchain/src/output_parsers/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class RegexParser extends BaseOutputParser<Record<string, string>> {
defaultOutputKey?: string
) {
super();
this.regex = regex;
this.regex = typeof regex === "string" ? new RegExp(regex) : regex;
this.outputKeys = outputKeys;
this.defaultOutputKey = defaultOutputKey;
}
Expand Down Expand Up @@ -46,6 +46,6 @@ export class RegexParser extends BaseOutputParser<Record<string, string>> {
}

getFormatInstructions(): string {
return `Your response should match the following regex: /${this.regex}/`;
return `Your response should match the following regex: ${this.regex}`;
}
}

0 comments on commit d8bd20f

Please sign in to comment.