Skip to content

Commit

Permalink
partial format of prompt (langchain-ai#158)
Browse files Browse the repository at this point in the history
* partial format of prompt

* stash

* make format async

* cr

* make integration test

* cr

* cr
  • Loading branch information
hwchase17 authored Feb 28, 2023
1 parent 4a640bd commit b38919f
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 5 deletions.
70 changes: 70 additions & 0 deletions docs/docs/modules/prompts/partial_prompts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Partial Prompt Templates

A prompt template is a class with a `.format` method which takes in a key-value map and returns a string (a prompt) to pass to the language model. Like other methods, it can make sense to "partial" a prompt template - eg pass in a subset of the required values, as to create a new prompt template which expects only the remaining subset of values.

LangChain supports this in two ways: we allow for partially formatted prompts (1) with string values, (2) with functions that return string values. These two different ways support different use cases. In the documentation below we go over the motivations for both use cases as well as how to do it in LangChain.

## Partial With Strings

One common use case for wanting to partial a prompt template is if you get some of the variables before others. For example, suppose you have a prompt template that requires two variables, `foo` and `baz`. If you get the `foo` value early on in the chain, but the `baz` value later, it can be annoying to wait until you have both variables in the same place to pass them to the prompt template. Instead, you can partial the prompt template with the `foo` value, and then pass the partialed prompt template along and just use that. Below is an example of doing this:

```typescript
import { expect, test } from "@jest/globals";
import { PromptTemplate } from "../prompt.js";
test("Test partial with function", async () => {
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo", "bar"],
});
const partialPrompt = await prompt.partial({ foo: "foo" });
expect(await partialPrompt.format({ bar: "baz" })).toBe("foobaz");
});
```

You can also just initialize the prompt with the partialed variables.

```typescript
import { expect, test } from "@jest/globals";
import { PromptTemplate } from "../prompt.js";
test("Test partial with function", async () => {
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo"],
partialVariables: { bar: "baz" },
});
expect(await prompt.format({ foo: "foo" })).toBe("foobaz");
});
```

## Partial With Functions

The other common use is to partial with a function. The use case for this is when you have a variable you know that you always want to fetch in a common way. A prime example of this is with date or time. Imagine you have a prompt which you always want to have the current date. You can't hard code it in the prompt, and passing it along with the other input variables is a bit annoying. In this case, it's very handy to be able to partial the prompt with a function that always returns the current date.

```typescript
import { expect, test } from "@jest/globals";
import { PromptTemplate } from "../prompt.js";
test("Test partial with function", async () => {
const prompt = new PromptTemplate({
template: "Tell me a {adjective} joke about the day {date}",
inputVariables: ["adjective", "date"],
});
const partialPrompt = await prompt.partial({
date: () => dateFunction,
});
});
```

You can also just initialize the prompt with the partialed variables, which often makes more sense in this workflow.

```typescript
import { expect, test } from "@jest/globals";
import { PromptTemplate } from "../prompt.js";
test("Test partial with function", async () => {
const prompt = new PromptTemplate({
template: "Tell me a {adjective} joke about the day {date}",
inputVariables: ["adjective"],
partialVariables: { date: () => new Date().toLocaleDateString() },
});
expect(await prompt.format({ foo: "foo" })).toBe("foo<DATE>");
});
```
29 changes: 29 additions & 0 deletions langchain/src/prompts/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export type SerializedBasePromptTemplate = ReturnType<

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type InputValues = Record<string, any>;
export type PartialValues = Record<
string,
string | (() => Promise<string>) | (() => string)
>;

/**
* Input common to all prompt templates.
Expand All @@ -23,6 +27,9 @@ export interface BasePromptTemplateInput {
* How to parse the output of calling an LLM on this formatted prompt
*/
outputParser?: BaseOutputParser;

/** Partial variables */
partialVariables?: PartialValues;
}

/**
Expand All @@ -35,6 +42,8 @@ export abstract class BasePromptTemplate implements BasePromptTemplateInput {

outputParser?: BaseOutputParser;

partialVariables?: InputValues;

constructor(input: BasePromptTemplateInput) {
const { inputVariables } = input;
if (inputVariables.includes("stop")) {
Expand All @@ -45,6 +54,26 @@ export abstract class BasePromptTemplate implements BasePromptTemplateInput {
Object.assign(this, input);
}

abstract partial(values: PartialValues): Promise<BasePromptTemplate>;

async mergePartialAndUserVariables(
userVariables: InputValues
): Promise<InputValues> {
const partialVariables = this.partialVariables ?? {};
const partialValues: InputValues = {};
for (let i = 0; i < Object.keys(partialVariables).length; i += 1) {
const key = Object.keys(partialVariables)[i];
const value = partialVariables[key];
if (typeof value === "string") {
partialValues[key] = value;
} else {
partialValues[key] = await value();
}
}
const allKwargs = { ...partialValues, ...userVariables };
return allKwargs;
}

/**
* Format the prompt given the input values.
*
Expand Down
26 changes: 23 additions & 3 deletions langchain/src/prompts/few_shot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BasePromptTemplate,
InputValues,
BasePromptTemplateInput,
PartialValues,
} from "./index.js";
import {
TemplateFormat,
Expand Down Expand Up @@ -127,10 +128,16 @@ export class FewShotPromptTemplate
}

if (this.validateTemplate) {
let totalInputVariables = this.inputVariables;
if (this.partialVariables) {
totalInputVariables = totalInputVariables.concat(
Object.keys(this.partialVariables)
);
}
checkValidTemplate(
this.prefix + this.suffix,
this.templateFormat,
this.inputVariables
totalInputVariables
);
}
}
Expand All @@ -152,16 +159,29 @@ export class FewShotPromptTemplate
);
}

async partial(values: PartialValues): Promise<FewShotPromptTemplate> {
const promptDict: FewShotPromptTemplate = { ...this };
promptDict.inputVariables = this.inputVariables.filter(
(iv) => !(iv in values)
);
promptDict.partialVariables = {
...(this.partialVariables ?? {}),
...values,
};
return new FewShotPromptTemplate(promptDict);
}

async format(values: InputValues): Promise<string> {
const examples = this.getExamples(values);
const allValues = await this.mergePartialAndUserVariables(values);
const examples = this.getExamples(allValues);

const exampleStrings = examples.map((example) =>
this.examplePrompt.format(example)
);
const template = [this.prefix, ...exampleStrings, this.suffix].join(
this.exampleSeparator
);
return renderTemplate(template, this.templateFormat, values);
return renderTemplate(template, this.templateFormat, allValues);
}

serialize(): SerializedFewShotTemplate {
Expand Down
1 change: 1 addition & 0 deletions langchain/src/prompts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
BasePromptTemplateInput,
SerializedBasePromptTemplate,
InputValues,
PartialValues,
} from "./base.js";
export {
PromptTemplate,
Expand Down
24 changes: 22 additions & 2 deletions langchain/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BasePromptTemplate,
BasePromptTemplateInput,
InputValues,
PartialValues,
} from "./index.js";
import {
TemplateFormat,
Expand Down Expand Up @@ -76,10 +77,16 @@ export class PromptTemplate
Object.assign(this, input);

if (this.validateTemplate) {
let totalInputVariables = this.inputVariables;
if (this.partialVariables) {
totalInputVariables = totalInputVariables.concat(
Object.keys(this.partialVariables)
);
}
checkValidTemplate(
this.template,
this.templateFormat,
this.inputVariables
totalInputVariables
);
}
}
Expand All @@ -89,7 +96,8 @@ export class PromptTemplate
}

async format(values: InputValues): Promise<string> {
return renderTemplate(this.template, this.templateFormat, values);
const allValues = await this.mergePartialAndUserVariables(values);
return renderTemplate(this.template, this.templateFormat, allValues);
}

/**
Expand Down Expand Up @@ -136,6 +144,18 @@ export class PromptTemplate
});
}

async partial(values: PartialValues): Promise<PromptTemplate> {
const promptDict: PromptTemplateInput = { ...this };
promptDict.inputVariables = this.inputVariables.filter(
(iv) => !(iv in values)
);
promptDict.partialVariables = {
...(this.partialVariables ?? {}),
...values,
};
return new PromptTemplate(promptDict);
}

serialize(): SerializedPromptTemplate {
return {
_type: this._getPromptType(),
Expand Down
68 changes: 68 additions & 0 deletions langchain/src/prompts/tests/few_shot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expect, test } from "@jest/globals";
import { FewShotPromptTemplate } from "../few_shot.js";
import { PromptTemplate } from "../prompt.js";

test("Test using partial", async () => {
const examplePrompt = PromptTemplate.fromTemplate("{foo}{bar}");
const prompt = new FewShotPromptTemplate({
prefix: "{foo}{bar}",
examples: [],
suffix: "",
templateFormat: "f-string",
exampleSeparator: "\n",
examplePrompt,
inputVariables: ["foo"],
partialVariables: { bar: "baz" },
});
expect(await prompt.format({ foo: "foo" })).toBe("foobaz\n");
});

test("Test using full partial", async () => {
const examplePrompt = PromptTemplate.fromTemplate("{foo}{bar}");
const prompt = new FewShotPromptTemplate({
prefix: "{foo}{bar}",
examples: [],
suffix: "",
templateFormat: "f-string",
exampleSeparator: "\n",
examplePrompt,
inputVariables: [],
partialVariables: { bar: "baz", foo: "boo" },
});
expect(await prompt.format({})).toBe("boobaz\n");
});

test("Test partial with string", async () => {
const examplePrompt = PromptTemplate.fromTemplate("{foo}{bar}");
const prompt = new FewShotPromptTemplate({
prefix: "{foo}{bar}",
examples: [],
suffix: "",
templateFormat: "f-string",
exampleSeparator: "\n",
examplePrompt,
inputVariables: ["foo", "bar"],
});

const partialPrompt = await prompt.partial({ foo: "foo" });
expect(await partialPrompt.format({ bar: "baz" })).toBe("foobaz\n");
expect(prompt.inputVariables).toEqual(["foo", "bar"]);
});

test("Test partial with function", async () => {
const examplePrompt = PromptTemplate.fromTemplate("{foo}{bar}");
const prompt = new FewShotPromptTemplate({
prefix: "{foo}{bar}",
examples: [],
suffix: "",
templateFormat: "f-string",
exampleSeparator: "\n",
examplePrompt,
inputVariables: ["foo", "bar"],
});

const partialPrompt = await prompt.partial({
foo: () => Promise.resolve("boo"),
});
expect(await partialPrompt.format({ bar: "baz" })).toBe("boobaz\n");
});
41 changes: 41 additions & 0 deletions langchain/src/prompts/tests/prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, test } from "@jest/globals";
import { PromptTemplate } from "../prompt.js";

test("Test using partial", async () => {
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo"],
partialVariables: { bar: "baz" },
});
expect(await prompt.format({ foo: "foo" })).toBe("foobaz");
});

test("Test using full partial", async () => {
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: [],
partialVariables: { bar: "baz", foo: "boo" },
});
expect(await prompt.format({})).toBe("boobaz");
});

test("Test partial", async () => {
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo", "bar"],
});
const partialPrompt = await prompt.partial({ foo: "foo" });
expect(await partialPrompt.format({ bar: "baz" })).toBe("foobaz");
expect(prompt.inputVariables).toEqual(["foo", "bar"]);
});

test("Test partial with function", async () => {
const prompt = new PromptTemplate({
template: "{foo}{bar}",
inputVariables: ["foo", "bar"],
});
const partialPrompt = await prompt.partial({
foo: () => Promise.resolve("boo"),
});
expect(await partialPrompt.format({ bar: "baz" })).toBe("boobaz");
});

0 comments on commit b38919f

Please sign in to comment.