-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25dd410
commit 105ff11
Showing
3 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
js/packages/phoenix-client/test/promptMessageFormatter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { promptMessageFormatter } from "../src/utils/promptMessageFormatter"; | ||
import { PromptChatMessage } from "../src/types/prompts"; | ||
import { TextPart } from "../src/schemas/llm/promptSchemas"; | ||
|
||
describe("promptMessageFormatter", () => { | ||
it("should only format TextPart content", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [ | ||
{ type: "text", text: { text: "Hello {{name}}" } } as TextPart, | ||
{ type: "image", image: { url: "test.jpg" } }, | ||
], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("MUSTACHE", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello World" }, | ||
}); | ||
expect(formatted?.[0]?.content?.[1]).toEqual({ | ||
type: "image", | ||
image: { url: "test.jpg" }, | ||
}); | ||
}); | ||
|
||
describe("MUSTACHE format", () => { | ||
it("should replace single variable", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [{ type: "text", text: { text: "Hello {{name}}" } }], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("MUSTACHE", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello World" }, | ||
}); | ||
}); | ||
|
||
it("should replace multiple variables", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [ | ||
{ | ||
type: "text", | ||
text: { text: "{{greeting}} there, {{name}}!" }, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("MUSTACHE", messages, { | ||
greeting: "Hello", | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello there, World!" }, | ||
}); | ||
}); | ||
|
||
it("should replace multiple instances of same variable", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [{ type: "text", text: { text: "{{name}}, {{name}}!" } }], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("MUSTACHE", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "World, World!" }, | ||
}); | ||
}); | ||
|
||
it("should handle escaped mustache syntax", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [ | ||
{ | ||
type: "text", | ||
text: { text: "Hello {{name}}, use {{{escaped}}} {{name}}" }, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("MUSTACHE", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello World, use {{{escaped}}} World" }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("FSTRING format", () => { | ||
it("should replace single variable", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [{ type: "text", text: { text: "Hello {name}" } }], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("FSTRING", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello World" }, | ||
}); | ||
}); | ||
|
||
it("should replace multiple variables", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [ | ||
{ type: "text", text: { text: "{greeting} there, {name}!" } }, | ||
], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("FSTRING", messages, { | ||
greeting: "Hello", | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello there, World!" }, | ||
}); | ||
}); | ||
|
||
it("should replace multiple instances of same variable", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [{ type: "text", text: { text: "{name}, {name}!" } }], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("FSTRING", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "World, World!" }, | ||
}); | ||
}); | ||
|
||
it("should handle escaped fstring syntax", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [ | ||
{ | ||
type: "text", | ||
text: { text: "Hello {name}, use {{escaped}} {name}" }, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("FSTRING", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello World, use {{escaped}} World" }, | ||
}); | ||
}); | ||
}); | ||
|
||
it("should not modify text when format is NONE", () => { | ||
const messages: PromptChatMessage[] = [ | ||
{ | ||
role: "USER", | ||
content: [{ type: "text", text: { text: "Hello {name} {{name}}" } }], | ||
}, | ||
{ | ||
role: "AI", | ||
content: [{ type: "text", text: { text: "Hello {name} {{name}}" } }], | ||
}, | ||
]; | ||
|
||
const formatted = promptMessageFormatter("NONE", messages, { | ||
name: "World", | ||
}); | ||
expect(formatted?.[0]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello {name} {{name}}" }, | ||
}); | ||
expect(formatted?.[1]?.content?.[0]).toEqual({ | ||
type: "text", | ||
text: { text: "Hello {name} {{name}}" }, | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.