Commit d09f63a 1 parent a2ded31 commit d09f63a Copy full SHA for d09f63a
File tree 2 files changed +25
-1
lines changed
langchain-core/src/prompts
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -85,7 +85,9 @@ const mustacheTemplateToNodes = (
85
85
if ( temp [ 0 ] === "name" ) {
86
86
const name = temp [ 1 ] . includes ( "." ) ? temp [ 1 ] . split ( "." ) [ 0 ] : temp [ 1 ] ;
87
87
return { type : "variable" , name } ;
88
- } else if ( temp [ 0 ] === "#" ) {
88
+ } else if ( [ "#" , "&" ] . includes ( temp [ 0 ] ) ) {
89
+ // # represents a section, "&" represents an unescaped variable.
90
+ // These should both be considered variables.
89
91
return { type : "variable" , name : temp [ 1 ] } ;
90
92
} else {
91
93
return { type : "literal" , text : temp [ 1 ] } ;
Original file line number Diff line number Diff line change 1
1
import { test , expect } from "@jest/globals" ;
2
2
import { PromptTemplate } from "../prompt.js" ;
3
+ import { parseTemplate } from "../template.js" ;
3
4
4
5
test ( "Single input variable." , async ( ) => {
5
6
const template = "This is a {{foo}} test." ;
91
92
is a test.` ) ;
92
93
expect ( promptWithRepeats . inputVariables ) . toEqual ( [ "foo" ] ) ;
93
94
} ) ;
95
+
96
+ test ( "Escaped variables" , async ( ) => {
97
+ const template = `test: {{{text}}}` ;
98
+ const parsed = parseTemplate ( template , "mustache" ) ;
99
+ expect ( parsed [ 0 ] ) . toStrictEqual ( {
100
+ type : "literal" ,
101
+ text : "test: " ,
102
+ } ) ;
103
+ expect ( parsed [ 1 ] ) . toStrictEqual ( {
104
+ type : "variable" ,
105
+ name : "text" ,
106
+ } ) ;
107
+
108
+ const promptTemplate = PromptTemplate . fromTemplate ( template , {
109
+ templateFormat : "mustache" ,
110
+ } ) ;
111
+ const result = await promptTemplate . invoke ( {
112
+ text : `hello i have a "quote` ,
113
+ } ) ;
114
+ expect ( result . value ) . toBe ( `test: hello i have a "quote` ) ;
115
+ } ) ;
You can’t perform that action at this time.
0 commit comments