Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions common/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export const adapterSettings: {
swipesPerGeneration: ['aphrodite'],
epsilonCutoff: ['aphrodite'],
etaCutoff: ['aphrodite'],
prependAuthorNameToMessages: ['openai'],

prefill: ['claude'],

Expand Down
1 change: 1 addition & 0 deletions common/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const presetValidator = {
streamResponse: 'boolean?',
ultimeJailbreak: 'string?',
prefixNameAppend: 'boolean?',
prependAuthorNameToMessages: 'boolean?',
prefill: 'string?',
antiBond: 'boolean?',

Expand Down
3 changes: 3 additions & 0 deletions common/presets/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Facts: {{memory}}

Relevant Information: {{user_embed}}
`,
prependAuthorNameToMessages: true,
},
openaiAlt: {
name: 'Turbo (#2)',
Expand All @@ -52,6 +53,7 @@ Facts: {{memory}}

Relevant Information: {{user_embed}}
`,
prependAuthorNameToMessages: true,
},
openaiTurbo: {
name: 'DaVinci',
Expand All @@ -74,5 +76,6 @@ Description of {{char}}: {{personality}}
Circumstances and context of the dialogue: {{scenario}}

Facts: {{memory}}`,
prependAuthorNameToMessages: true,
},
} satisfies Record<string, Partial<AppSchema.GenSettings>>
1 change: 1 addition & 0 deletions common/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ export namespace AppSchema {
promptOrder?: Array<{ placeholder: string; enabled: boolean }>
ultimeJailbreak?: string
prefixNameAppend?: boolean
prependAuthorNameToMessages?: boolean
prefill?: string
ignoreCharacterUjb?: boolean
antiBond?: boolean
Expand Down
42 changes: 32 additions & 10 deletions srv/adapter/chat-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type SplitSampleChatProps = {
char: string
sender: string
budget?: number
prependAuthorNameToMessages: boolean
}

type CompletionContent<T> = Array<{ finish_reason: string; index: number } & ({ text: string } | T)>
Expand Down Expand Up @@ -249,15 +250,28 @@ 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 lineWithPholdersReplaced = line
.trim()
.replace(BOT_REPLACE, replyAs.name)
.replace(SELF_REPLACE, handle)

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

const prependAuthorNameToMessages = opts.gen.prependAuthorNameToMessages ?? true

const content = prependAuthorNameToMessages
? lineWithPholdersReplaced
: lineWithPholdersReplaced.replace(author ? `${author}: ` : '', '')

const obj: CompletionItem = {
role: 'assistant',
content,
}

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

Expand All @@ -270,6 +284,7 @@ export async function toChatCompletionPayload(
sampleChat: obj.content,
char: replyAs.name,
sender: handle,
prependAuthorNameToMessages: opts.gen.prependAuthorNameToMessages ?? true,
})

if (tokens + consumed > maxBudget) {
Expand Down Expand Up @@ -307,7 +322,7 @@ export async function toChatCompletionPayload(
}

export async function splitSampleChat(opts: SplitSampleChatProps) {
const { sampleChat, char, sender, budget } = opts
const { sampleChat, char, sender, budget, prependAuthorNameToMessages } = opts
const regex = new RegExp(
`(?<=\\n)(?=${escapeRegex(char)}:|${escapeRegex(sender)}:|System:|<start>)`,
'gi'
Expand Down Expand Up @@ -339,10 +354,17 @@ export async function splitSampleChat(opts: SplitSampleChatProps) {
? 'user'
: 'system'

const msg: CompletionItem = {
role: role,
content: sample.replace(BOT_REPLACE, char).replace(SELF_REPLACE, sender),
}
const sampleWithPholdersReplaced = sample
.replace(BOT_REPLACE, char)
.replace(SELF_REPLACE, sender)

const author = role === 'assistant' ? char : role === 'user' ? sender : undefined

const content = prependAuthorNameToMessages
? sampleWithPholdersReplaced
: sampleWithPholdersReplaced.replace(author ? `${author}: ` : '', '')

const msg: CompletionItem = { role, content }

const length = await encoder()(msg.content)
if (budget && tokens + length > budget) break
Expand Down
1 change: 1 addition & 0 deletions tests/chat-model-sample-chat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async function testInput(input: string, budget?: number) {
char: TEST_CHARACTER_NAME,
sender: TEST_USER_NAME,
budget,
prependAuthorNameToMessages: true,
})

return JSON.stringify(result.additions, null, 2)
Expand Down
15 changes: 15 additions & 0 deletions web/shared/GenerationSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,21 @@ const PromptSettings: Component<
hide={useAdvanced() === 'basic'}
showTemplates
/>
<Toggle
fieldName="prependAuthorNameToMessages"
label="Prepend messages with author name"
helperText={
<>
For OpenAI Chat Completion. Prepends messages with the name of their author.
Recommended for multi-character and multi-user chats.
</>
}
value={props.inherit?.prependAuthorNameToMessages ?? true}
disabled={props.disabled}
service={props.service}
format={props.format}
aiSetting={'prependAuthorNameToMessages'}
/>

<FormLabel
label="System Prompt"
Expand Down