Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Mar 30, 2024
1 parent 534c00b commit 1a84b7c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
45 changes: 7 additions & 38 deletions langchain-core/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export type MessageType =
| "generic"
| "system"
| "function"
| "tool"
| "placeholder";
| "tool";

type ImageDetail = "auto" | "low" | "high";

Expand Down Expand Up @@ -406,39 +405,6 @@ export class AIMessageChunk extends BaseMessageChunk {
}
}

export class PlaceholderMessage extends BaseMessage {
static lc_name() {
return "PlaceholderMessage";
}

_getType(): MessageType {
return "placeholder";
}
}

export class PlaceholderMessageChunk extends BaseMessageChunk {
static lc_name() {
return "PlaceholderMessageChunk";
}

_getType(): MessageType {
return "placeholder";
}

concat(chunk: PlaceholderMessageChunk) {
return new PlaceholderMessageChunk({
content: mergeContent(this.content, chunk.content),
additional_kwargs: _mergeDicts(
this.additional_kwargs,
chunk.additional_kwargs
),
response_metadata: _mergeDicts(
this.response_metadata,
chunk.response_metadata
),
});
}
}
/**
* Represents a system message in a conversation.
*/
Expand Down Expand Up @@ -664,7 +630,12 @@ export class ChatMessage

export type BaseMessageLike =
| BaseMessage
| [StringWithAutocomplete<MessageType | "user" | "assistant">, MessageContent]
| [
StringWithAutocomplete<
MessageType | "user" | "assistant" | "placeholder"
>,
MessageContent
]
| string;

export function isBaseMessage(
Expand Down Expand Up @@ -697,8 +668,6 @@ export function coerceMessageLikeToMessage(
return new AIMessage({ content });
} else if (type === "system") {
return new SystemMessage({ content });
} else if (type === "placeholder") {
return new PlaceholderMessage({ content });
} else {
throw new Error(
`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.`
Expand Down
36 changes: 18 additions & 18 deletions langchain-core/src/prompts/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AIMessage,
HumanMessage,
SystemMessage,
PlaceholderMessage,
BaseMessage,
ChatMessage,
type BaseMessageLike,
Expand Down Expand Up @@ -348,8 +347,7 @@ interface _ImageTemplateParam {
type MessageClass =
| typeof HumanMessage
| typeof AIMessage
| typeof SystemMessage
| typeof PlaceholderMessage;
| typeof SystemMessage;

type ChatMessageClass = typeof ChatMessage;

Expand Down Expand Up @@ -644,19 +642,6 @@ export class SystemMessagePromptTemplate<
}
}

export class MessagesPlaceholderPromptTemplate<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunInput extends InputValues = any
> extends MessagesPlaceholder<RunInput> {
static _messageClass(): typeof PlaceholderMessage {
return PlaceholderMessage;
}

static lc_name() {
return "MessagesPlaceholderPromptTemplate";
}
}

/**
* Interface for the input of a ChatPromptTemplate.
*/
Expand Down Expand Up @@ -701,15 +686,30 @@ function _coerceMessagePromptTemplateLike(
) {
return messagePromptTemplateLike;
}
if (
Array.isArray(messagePromptTemplateLike) &&
messagePromptTemplateLike[0] === "placeholder"
) {
const messageContent = messagePromptTemplateLike[1];
if (
typeof messageContent !== "string" ||
messageContent[0] !== "{" ||
messageContent[messageContent.length - 1] !== "}"
) {
throw new Error(
`Invalid placeholder template: "${messagePromptTemplateLike[1]}". Expected a variable name surrounded by curly braces.`
);
}
const variableName = messageContent.slice(1, -1);
return new MessagesPlaceholder({ variableName, optional: true });
}
const message = coerceMessageLikeToMessage(messagePromptTemplateLike);
if (message._getType() === "human") {
return HumanMessagePromptTemplate.fromTemplate(message.content);
} else if (message._getType() === "ai") {
return AIMessagePromptTemplate.fromTemplate(message.content);
} else if (message._getType() === "system") {
return SystemMessagePromptTemplate.fromTemplate(message.content);
} else if (message._getType() === "placeholder") {
return new MessagesPlaceholderPromptTemplate(message.content as string);
} else if (ChatMessage.isInstance(message)) {
return ChatMessagePromptTemplate.fromTemplate(
message.content as string,
Expand Down
17 changes: 17 additions & 0 deletions langchain-core/src/prompts/tests/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ test("Test MessagesPlaceholder not optional", async () => {
);
});

test.only("Test MessagesPlaceholder shorthand in a chat prompt template should throw for invalid syntax", async () => {
expect(() =>
ChatPromptTemplate.fromMessages([["placeholder", "foo"]])
).toThrow();
});

test.only("Test MessagesPlaceholder shorthand in a chat prompt template", async () => {
const prompt = ChatPromptTemplate.fromMessages([["placeholder", "{foo}"]]);
const messages = await prompt.formatMessages({
foo: [new HumanMessage("Hi there!"), new AIMessage("how r u")],
});
expect(messages).toEqual([
new HumanMessage("Hi there!"),
new AIMessage("how r u"),
]);
});

test("Test using partial", async () => {
const userPrompt = new PromptTemplate({
template: "{foo}{bar}",
Expand Down

0 comments on commit 1a84b7c

Please sign in to comment.