Skip to content

Commit

Permalink
Make format_instructions an optional argument in chat agent (#2931)
Browse files Browse the repository at this point in the history
* Make format_instructions an optional argument in chat agent

This simple change allows users to fully overwrite the prompts when creating a chat agent, and optimize them for their use case. This minimal change makes the agent code much more generally useful. Previously, they could overwrite everything except for FORMAT_INSTRUCTIONS, which is essential to customization.

* Add formatInstructions to docstrings and ChatCreatePromptArgs type

* Fix typo in docstring

* Fix typo

* Fix format

* Fix typo

---------

Co-authored-by: Jacob Lee <jacoblee93@gmail.com>
  • Loading branch information
EKMeyerson and jacoblee93 authored Oct 17, 2023
1 parent aa9b618 commit 737d986
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion langchain/src/agents/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface ChatCreatePromptArgs {
prefix?: string;
/** String to use directly as the human message template. */
humanMessageTemplate?: string;
/** Formattable string to use as the instructions template. */
formatInstructions?: string;
/** List of input variables the final prompt will expect. */
inputVariables?: string[];
}
Expand Down Expand Up @@ -117,17 +119,19 @@ export class ChatAgent extends Agent {
* @param args.suffix - String to put after the list of tools.
* @param args.prefix - String to put before the list of tools.
* @param args.humanMessageTemplate - String to use directly as the human message template
* @param args.formatInstructions - Formattable string to use as the instructions template
*/
static createPrompt(tools: Tool[], args?: ChatCreatePromptArgs) {
const {
prefix = PREFIX,
suffix = SUFFIX,
humanMessageTemplate = DEFAULT_HUMAN_MESSAGE_TEMPLATE,
formatInstructions = FORMAT_INSTRUCTIONS,
} = args ?? {};
const toolStrings = tools
.map((tool) => `${tool.name}: ${tool.description}`)
.join("\n");
const template = [prefix, toolStrings, FORMAT_INSTRUCTIONS, suffix].join(
const template = [prefix, toolStrings, formatInstructions, suffix].join(
"\n\n"
);
const messages = [
Expand Down

0 comments on commit 737d986

Please sign in to comment.