Skip to content

Commit

Permalink
Update mdp to support inline images/rich data
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat committed May 31, 2024
1 parent 3b28a52 commit 8359380
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ window.lxl = {
tools: {
stripping,
tokenizer,
loadPrompt: mdp.loadPrompt,
_segmentPromptByRoles: mdp.segmentByRoles,
...misc
}
Expand Down
15 changes: 13 additions & 2 deletions src/tools/mdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,16 @@ function preMarkdown (text, vars = {}, roles) {
for (let i = 0; i < tokens.length; i++) {
if (tokens[i][1] === 'var') {
const varName = tokens[i][0].slice(TOKEN_VAR_START.length, -TOKEN_VAR_END.length)
if (typeof vars[varName] === 'object') {
if (varName.startsWith('{') && varName.endsWith('}')) {
// inline JSON object var
const json = varName.slice(1, -1)
try {
const replacement = JSON.parse(json)
tokens[i] = [replacement, 'part']
} catch (e) {
throw new Error(`Failed to parse JSON object in variable insertion token: ${varName}`)
}
} else if (typeof vars[varName] === 'object') {
tokens[i] = [vars[varName], 'part']
} else {
const replacement = vars[varName] || ''
Expand Down Expand Up @@ -257,7 +266,9 @@ function preMarkdown (text, vars = {}, roles) {
if (parts.length === 1) {
return parts[0]
} else {
return parts.map(part => typeof part === 'string' ? ({ text: part }) : part)
return parts
.map(part => typeof part === 'string' ? ({ text: part }) : part)
.filter(x => x.trim ? (x.trim() !== '') : true)
}
}

Expand Down

0 comments on commit 8359380

Please sign in to comment.