Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions srv/adapter/chat-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,23 @@ export async function toChatCompletionPayload(

const line = all[i]

const obj: CompletionItem = {
role: 'assistant',
content: line.trim().replace(BOT_REPLACE, replyAs.name).replace(SELF_REPLACE, handle),
}

const isSystem = line.startsWith('System:')
const isUser = line.startsWith(handle)
const isBot = !isUser && !isSystem

const name = isBot ? replyAs.name : isUser ? handle : undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is handle by this point accounting for impersonated characters? I'd assume so

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this was breaking past impersonations (registering them as the main character in assistant message), and example messages (incorrect splitting). This is fixed in c4eefd5


const content = line
.trim()
.replace(BOT_REPLACE, replyAs.name)
.replace(SELF_REPLACE, handle)
.replace(name ? `${name}: ` : '', '')

const obj: CompletionItem = {
role: 'assistant',
content,
name,
}
const insert = inserts.get(distanceFromBottom)
if (insert) history.push({ role: 'system', content: insert })

Expand Down Expand Up @@ -291,7 +299,7 @@ export async function toChatCompletionPayload(
obj.role = 'user'
}

const length = await encoder()(obj.content)
const length = (await encoder()(obj.content)) + (obj.name ? 1 + (await encoder()(obj.name)) : 0)
if (tokens + length > maxBudget) {
--i
break
Expand Down Expand Up @@ -339,12 +347,15 @@ export async function splitSampleChat(opts: SplitSampleChatProps) {
? 'user'
: 'system'

const msg: CompletionItem = {
role: role,
content: sample.replace(BOT_REPLACE, char).replace(SELF_REPLACE, sender),
}
const name = role === 'assistant' ? char : role === 'user' ? sender : undefined

const content = sample
.replace(BOT_REPLACE, char)
.replace(SELF_REPLACE, sender)
.replace(name ? `${name}: ` : '', '')

const length = await encoder()(msg.content)
const msg: CompletionItem = { role, content, name }
const length = (await encoder()(msg.content)) + (msg.name ? 1 + (await encoder()(msg.name)) : 0)
if (budget && tokens + length > budget) break

additions.push(msg)
Expand Down