Skip to content

Commit

Permalink
fix: cut prompt to history's last message
Browse files Browse the repository at this point in the history
  • Loading branch information
codedealer committed Apr 8, 2024
1 parent d29eb45 commit ce433fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion common/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ function createPostPrompt(

if (opts.kind === 'chat-query') {
post.push(`Query Response:`)
} else if (opts.kind !== 'continue') {
} else {
post.push(`${opts.replyAs.name}:`)
}

Expand Down
30 changes: 20 additions & 10 deletions common/template-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,22 @@ export async function parseTemplate(
parts.ujb = render(parts.ujb, opts)
}

const ast = parser.parse(template, {}) as PNode[]
let ast = parser.parse(template, {}) as PNode[]

// hack: when we continue, remove the post along with the last newline from tree
if (opts.continue && ast.length > 1) {
const last = ast[ast.length - 1]
if (typeof last !== 'string' && last.kind === 'placeholder' && last.value === 'post') {
ast.pop()
}
if (ast[ast.length - 1] === '\n') {
ast.pop()
/**
* Continuing the previous message:
* In this case our goal is to end the prompt as close to the
* last message as possible.
*/
if (opts.continue) {
const historyIndex = ast.findIndex(
(node) =>
typeof node !== 'string' &&
((node.kind === 'placeholder' && node.value === 'history') ||
(node.kind === 'each' && node.value === 'history'))
)
if (historyIndex !== -1) {
ast = ast.slice(0, historyIndex + 1)
}
}

Expand Down Expand Up @@ -442,7 +448,7 @@ function renderIterator(holder: IterableHolder, children: CNode[], opts: Templat
let i = 0
for (const entity of entities) {
let curr = ''
for (const child of children) {
children_loop: for (const child of children) {
if (typeof child === 'string') {
curr += child
continue
Expand All @@ -468,6 +474,8 @@ function renderIterator(holder: IterableHolder, children: CNode[], opts: Templat
case 'history-prop': {
const result = renderProp(child, opts, entity, i)
if (result) curr += result
// when continuing, cut the first node (last response) to its message
if (opts.continue && i === 0 && isHistory && child.prop === 'message') break children_loop
break
}

Expand All @@ -477,6 +485,8 @@ function renderIterator(holder: IterableHolder, children: CNode[], opts: Templat
if (!prop) break
const result = renderEntityCondition(child.children, opts, entity, i)
curr += result
// when continuing, cut the first node (last response) to its message
if (opts.continue && i === 0 && isHistory) break children_loop
break
}
}
Expand Down

0 comments on commit ce433fc

Please sign in to comment.