Skip to content

Commit

Permalink
Fix guest chat trees (#956)
Browse files Browse the repository at this point in the history
- Fix ChatML end marker
- Fix empty chat load caused by deleted message edge case
- Add more recommendations to presets
- Add missing placeholders to prompt help modal
Co-authored-by: Sceuick <[email protected]>
  • Loading branch information
sceuick authored Jun 29, 2024
1 parent 2b878b7 commit 9f83e54
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 68 deletions.
16 changes: 14 additions & 2 deletions common/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function toChatGraph(messages: AppSchema.ChatMessage[]): { tree: ChatTree
return { tree, root: messages[0]?._id || '' }
}

export function updateChatTree(tree: ChatTree, msg: AppSchema.ChatMessage) {
export function updateChatTreeNode(tree: ChatTree, msg: AppSchema.ChatMessage) {
tree[msg._id] = {
msg,
children: new Set(),
Expand All @@ -56,7 +56,19 @@ export function updateChatTree(tree: ChatTree, msg: AppSchema.ChatMessage) {
parent.children.add(msg._id)
}

return tree
return Object.assign({}, tree)
}

export function removeChatTreeNodes(tree: ChatTree, ids: string[]) {
for (const id of ids) {
const parent = tree[id]
if (parent) {
parent.children.delete(id)
}
delete tree[id]
}

return Object.assign({}, tree)
}

export function getChatDepths(tree: ChatTree) {
Expand Down
2 changes: 1 addition & 1 deletion common/presets/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const BUILTIN_FORMATS: { [key in ModelFormat]: FormatTags } = {
},
ChatML: {
openUser: '<|im_start|>user\n',
closeUser: '<|im_end>',
closeUser: '<|im_end|>',
openBot: '<|im_start|>assistant\n',
closeBot: '<|im_end|>',
openSystem: '<|im_start|>system\n',
Expand Down
2 changes: 2 additions & 0 deletions common/prompt-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const formatHolders: Record<string, Record<string, string>> = {
{{/each}}`,
post: `### Response:\n{{post}}`,
system_prompt: `{{#if system_prompt}}### Instruction:\n{{system_prompt}}{{/if}}`,
ujb: `{{#if ujb}}({{ujb}}){{/if}}`,
},
Vicuna: {
preamble: neat`Below is an instruction that describes a task. Write a response that appropriately completes the request.\n
Expand All @@ -49,6 +50,7 @@ export const formatHolders: Record<string, Record<string, string>> = {
{{/each}}`,
post: `ASSISTANT: {{post}}`,
system_prompt: `{{#if system_prompt}}SYSTEM: {{system_prompt}}{{/if}}`,
ujb: `{{#if ujb}}({{ujb}}){{/if}}`,
},
Mistral: {
preamble: neat`Below is an instruction that describes a task. Write a response that appropriately completes the request.\n
Expand Down
2 changes: 1 addition & 1 deletion common/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export namespace AppSchema {
level: number
service: AIAdapter
guidance: boolean
preset: GenSettings
preset: GenSettings & Pick<SubscriptionPreset, 'allowGuestUsage'>
}

export interface AppConfig {
Expand Down
5 changes: 5 additions & 0 deletions common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export function replace<T extends { _id: string }>(id: string, list: T[], item:
return list.map((li) => (li._id === id ? { ...li, ...item } : li))
}

export function exclude<T extends { _id: string }>(list: T[], ids: string[]) {
const set = new Set(ids)
return list.filter((item) => !set.has(item._id))
}

export function findOne<T extends { _id: string }>(id: string, list: T[]): T | void {
for (const item of list) {
if (item._id === id) return item
Expand Down
34 changes: 31 additions & 3 deletions srv/api/chat/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ export const generateMessageV2 = handle(async (req, res) => {
}

const responseText = body.kind === 'continue' ? `${body.continuing.msg} ${generated}` : generated
const parent = getNewMessageParent(body, userMsg)
const actions: AppSchema.ChatAction[] = []
let treeLeafId = ''

Expand Down Expand Up @@ -363,7 +364,7 @@ export const generateMessageV2 = handle(async (req, res) => {
meta,
retries,
event: undefined,
parent: userMsg?._id,
parent,
})

sendMany(members, {
Expand All @@ -387,7 +388,7 @@ export const generateMessageV2 = handle(async (req, res) => {

await store.msgs.editMessage(body.replacing._id, {
msg: responseText,
parent: body.parent,
parent,
actions,
adapter,
meta,
Expand Down Expand Up @@ -419,7 +420,7 @@ export const generateMessageV2 = handle(async (req, res) => {
meta,
retries,
event: undefined,
parent: body.parent,
parent,
})
treeLeafId = requestId
sendMany(members, {
Expand Down Expand Up @@ -491,12 +492,14 @@ async function handleGuestGenerate(body: GenRequest, req: AppRequest, res: Respo
characterId: body.impersonate?._id,
ooc: body.kind === 'ooc',
event: undefined,
parent: body.parent,
})
} else if (body.kind.startsWith('send-event:')) {
newMsg = newMessage(v4(), chatId, body.text!, {
characterId: replyAs?._id,
ooc: false,
event: getScenarioEventType(body.kind),
parent: body.parent,
})
}

Expand Down Expand Up @@ -571,6 +574,7 @@ async function handleGuestGenerate(body: GenRequest, req: AppRequest, res: Respo

const characterId = body.kind === 'self' ? undefined : body.replyAs?._id || body.char?._id
const senderId = body.kind === 'self' ? 'anon' : undefined
const parent = getNewMessageParent(body, newMsg)

if (body.kind === 'retry' && body.replacing) {
retries = [body.replacing.msg].concat(retries).concat(body.replacing.retries || [])
Expand All @@ -583,6 +587,7 @@ async function handleGuestGenerate(body: GenRequest, req: AppRequest, res: Respo
meta,
event: undefined,
retries,
parent,
})

switch (body.kind) {
Expand Down Expand Up @@ -623,6 +628,7 @@ function newMessage(
meta?: any
event: undefined | AppSchema.ScenarioEventType
retries?: string[]
parent?: string
}
) {
const userMsg: AppSchema.ChatMessage = {
Expand Down Expand Up @@ -674,3 +680,25 @@ async function ensureBotMembership(
update.characters = characters
await store.chats.update(chat._id, update)
}

function getNewMessageParent(body: GenRequest, userMsg: AppSchema.ChatMessage | undefined): string {
switch (body.kind) {
case 'summary':
case 'chat-query':
return ''

case 'retry':
case 'continue':
return body.parent || ''

case 'request':
case 'ooc':
case 'self':
case 'send':
case 'send-event:character':
case 'send-event:hidden':
case 'send-event:ooc':
case 'send-event:world':
return userMsg?._id || ''
}
}
7 changes: 6 additions & 1 deletion srv/api/chat/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sendMany } from '../ws'

export const deleteMessages = handle(async ({ body, params, userId }) => {
const chatId = params.id
assertValid({ ids: ['string'] }, body)
assertValid({ ids: ['string'], leafId: 'string?' }, body)

const chat = await store.chats.getChatOnly(chatId)
if (!chat) {
Expand All @@ -17,6 +17,11 @@ export const deleteMessages = handle(async ({ body, params, userId }) => {
}

await store.msgs.deleteMessages(body.ids)

if (body.leafId) {
await store.chats.update(chatId, { treeLeafId: body.leafId })
}

sendMany(chat.memberIds.concat(chat.userId), { type: 'messages-deleted', ids: body.ids })
return { success: true }
})
Expand Down
12 changes: 12 additions & 0 deletions srv/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { setupSockets } from './api/ws'
import { config } from './config'
import { createServer } from './server'
import pipeline from './api/pipeline'
import { getDb } from './db/client'

export function createApp() {
const upload = multer({
Expand Down Expand Up @@ -41,6 +42,17 @@ export function createApp() {
app.use('/v1', keyedRouter)
app.use('/api', api)

if (config.db.host || config.db.uri) {
app.get('/healthcheck', (_, res) => {
try {
getDb()
res.status(200).json({ message: 'okay', status: true })
} catch (ex) {
res.status(503).json({ message: 'Database not ready', status: false })
}
})
}

if (config.pipelineProxy) {
app.use('/pipeline', pipeline)
}
Expand Down
10 changes: 10 additions & 0 deletions srv/db/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function getCachedSubscriptions(user?: AppSchema.User | null) {
guidance: !!sub.guidanceCapable,
preset: {
name: sub.name,

maxContextLength: sub.maxContextLength,
maxTokens: sub.maxTokens,
frequencyPenalty: sub.frequencyPenalty,
Expand All @@ -110,6 +111,15 @@ export function getCachedSubscriptions(user?: AppSchema.User | null) {
encoderRepitionPenalty: sub.encoderRepitionPenalty,
mirostatLR: sub.mirostatLR,
mirostatTau: sub.mirostatTau,
allowGuestUsage: sub.allowGuestUsage,
dynatemp_exponent: sub.dynatemp_exponent,
dynatemp_range: sub.dynatemp_range,
modelFormat: sub.modelFormat,
penaltyAlpha: sub.penaltyAlpha,
smoothingCurve: sub.smoothingCurve,
smoothingFactor: sub.smoothingFactor,
skipSpecialTokens: sub.skipSpecialTokens,
tempLast: sub.tempLast,
},
}))
.sort((l, r) => (l.level === r.level ? l.name.localeCompare(r.name) : l.level - r.level))
Expand Down
2 changes: 1 addition & 1 deletion web/pages/Chat/ChatDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ const ChatDetail: Component = () => {
</div>
</Show>
<Show when={chats.loaded && chatMsgs().length === 0 && !msgs.waiting}>
<div class="flex justify-center">
<div class="flex justify-center gap-2">
<Button onClick={generateFirst}>Generate Message</Button>
</div>
</Show>
Expand Down
4 changes: 2 additions & 2 deletions web/pages/Chat/components/GraphModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const ChatGraphModal: Component<{
footer={
<>
<Button onClick={() => setShort(short() === 'full' ? 'short' : 'full')}>
<Show when={short() === 'short'} fallback={'Detailed'}>
Summary
<Show when={short() === 'short'} fallback={'Collapse Graph'}>
Expand Graph
</Show>
</Button>
<Button onClick={reset}>Reset View</Button>
Expand Down
1 change: 1 addition & 0 deletions web/pages/Settings/Agnaistic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const AgnaisticSettings: Component<{

return config.subs
.filter((sub) => sub.level <= level)
.filter((sub) => (level === -1 ? !!sub.preset.allowGuestUsage : true))
.map((sub) => ({ label: sub.name, value: sub._id, level: sub.level }))
.sort((l, r) => r.level - l.level)
})
Expand Down
Loading

0 comments on commit 9f83e54

Please sign in to comment.