Skip to content

Commit

Permalink
log probs and chat porting fixes
Browse files Browse the repository at this point in the history
* fix logprobs ui
* Fix json schema import
* fix char schema editor
* bugfix: unhide sliders
- fix chatml order template
- fix chat export/imports
- fix ios chat import file select
  • Loading branch information
sceuick authored Nov 10, 2024
1 parent 54a91e6 commit 0ecf249
Show file tree
Hide file tree
Showing 24 changed files with 176 additions and 89 deletions.
2 changes: 1 addition & 1 deletion common/prompt-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const formatHolders: Record<string, Record<string, string>> = {
example_dialogue: neat`{{#if example_dialogue}}How "{{char}}" speaks:\n{{example_dialogue}}\n{{/if}}`,
history: neat`Then the roleplay chat between {{user}} and {{char}} begins.
{{#each msg}}<|im_start|>{{#if .isbot}}assistant{{/if}}{{#if .isuser}}user{{/if}}{{.name}}: {{.msg}}<|im_end|>{{/each}}`,
{{#each msg}}<|im_start|>{{#if .isbot}}assistant{{/if}}{{#if .isuser}}user{{/if}}\n{{.name}}: {{.msg}}<|im_end|>{{/each}}`,
post: neat`<|im_start|>assistant
{{#if ujb}}({{value}}) {{/if}}{{post}}`,
},
Expand Down
18 changes: 15 additions & 3 deletions srv/api/chat/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,23 @@ export const importChat = handle(async ({ body, userId }) => {
greeting: 'string?',
scenario: 'string?',
scenarioId: 'string?',
treeLeafId: 'string?',
messages: [
{
_id: 'string?',
msg: 'string',
parent: 'string?',
characterId: 'string?',
userId: 'string?',
handle: 'string?',
ooc: 'boolean?',
retries: ['string?'],
createdAt: 'string?',
json: 'any?',
values: 'any?',
state: 'string?',
name: 'string?',
extras: 'any?',
},
],
},
Expand All @@ -91,12 +100,12 @@ export const importChat = handle(async ({ body, userId }) => {
body.characterId,
{
name: body.name,
greeting: body.greeting ?? character.greeting,
scenario: body.scenario,
overrides: character.persona,
sampleChat: '',
userId,
scenarioIds: body.scenarioId ? [body.scenarioId] : [],
treeLeafId: body.treeLeafId,
},
profile!
)
Expand All @@ -105,13 +114,16 @@ export const importChat = handle(async ({ body, userId }) => {
chatId: chat._id,
message: msg.msg,
adapter: 'import',
characterId: msg.characterId ? character._id : undefined,
characterId: msg.characterId === 'imported' ? character._id : msg.characterId,
senderId: msg.userId ? msg.userId : undefined,
handle: msg.handle,
ooc: msg.ooc ?? false,
parent: msg.parent,
retries: character.alternateGreetings,
event: undefined,
name: character.name,
name: msg.name,
json: msg.json,
values: msg.values,
}))

await store.msgs.importMessages(userId, messages)
Expand Down
3 changes: 3 additions & 0 deletions srv/db/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function getChat(id: string) {
if (!chat) return

const charIds = Object.keys(chat.characters || {})

const characters = await db('character')
.find({ _id: { $in: charIds.concat(chat.characterId) } })
.toArray()
Expand Down Expand Up @@ -60,6 +61,7 @@ export async function create(
| 'genPreset'
| 'mode'
| 'imageSource'
| 'treeLeafId'
>,
profile: AppSchema.Profile,
impersonating?: AppSchema.Character
Expand Down Expand Up @@ -89,6 +91,7 @@ export async function create(
messageCount: props.greeting ? 1 : 0,
tempCharacters: {},
imageSource: props.imageSource,
treeLeafId: props.treeLeafId,
}

await db('chat').insertOne(doc)
Expand Down
66 changes: 45 additions & 21 deletions srv/db/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type NewMessage = {
retries?: string[]
parent?: string
json?: AppSchema.ChatMessage['json']
values?: any
name: string | undefined
}

Expand Down Expand Up @@ -60,27 +61,50 @@ export async function createChatMessage(creating: NewMessage, ephemeral?: boolea

export async function importMessages(userId: string, messages: NewMessage[]) {
const start = Date.now()
const docs: AppSchema.ChatMessage[] = messages.map((msg, i) => ({
_id: v4(),
kind: 'chat-message',
rating: 'none',
chatId: msg.chatId,
characterId: msg.characterId,
/**
* We will use this soon to retain the original handles.
* This needs further consideration for how it'll be handled in the front-end
* and how ancestors of this chat will retain this information when subsequent exports occur.
*/
// userId: msg.senderId === 'anon' ? userId : msg.senderId,
// handle: msg.handle ? { name: msg.handle, userId: msg.senderId } : undefined,
handle: msg.handle || undefined,
userId: msg.senderId ? msg.senderId : undefined,
msg: msg.message,
adapter: msg.adapter,
createdAt: new Date(start + i).toISOString(),
updatedAt: new Date(start + i).toISOString(),
retries: [],
}))

const parents = new Map<string, AppSchema.ChatMessage>()

const docs: AppSchema.ChatMessage[] = messages.map((msg, i) => {
const id = v4()

const mapped = {
_id: id,
kind: 'chat-message',
chatId: msg.chatId,
characterId: msg.characterId,
/**
* We will use this soon to retain the original handles.
* This needs further consideration for how it'll be handled in the front-end
* and how ancestors of this chat will retain this information when subsequent exports occur.
*/
// userId: msg.senderId === 'anon' ? userId : msg.senderId,
// handle: msg.handle ? { name: msg.handle, userId: msg.senderId } : undefined,
handle: msg.handle || undefined,
userId: msg.senderId ? userId : undefined,
parent: msg.parent,
ooc: msg.ooc,
json: msg.json,
values: msg.values,
msg: msg.message,
name: msg.name,
adapter: msg.adapter,
createdAt: new Date(start + i).toISOString(),
updatedAt: new Date(start + i).toISOString(),
retries: [],
} as AppSchema.ChatMessage

if (msg.parent) {
parents.set(msg._id!, mapped)
}

return mapped
})

for (const msg of docs) {
if (!msg.parent) continue
const parent = parents.get(msg.parent)
msg.parent = parent?._id
}

await db('chat-message').insertMany(docs)
return docs
Expand Down
40 changes: 22 additions & 18 deletions web/pages/Character/CharacterSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export const CharacterSchema: Component<{
const [store, setStore] = createStore({
response: '',
history: '',
fields: [] as JsonField[],
schema: [] as JsonField[],
})

const vars = createMemo(() => {
return store.fields.map((s) => ({ label: s.name, value: s.name }))
return store.schema.map((s) => ({ label: s.name, value: s.name }))
})

const [resErr, setResErr] = createSignal('')
Expand All @@ -94,7 +94,7 @@ export const CharacterSchema: Component<{
let json: ResponseSchema | undefined

if (props.characterId) {
const char = ctx.activeMap[props.characterId]
const char = ctx.allBots[props.characterId]
json = char ? char.json : chatStore.getState().active?.char.json
} else if (props.presetId || props.inherit) {
json = props.inherit || activePreset()?.json
Expand All @@ -103,15 +103,15 @@ export const CharacterSchema: Component<{
const hasValue = !!json?.schema?.length || !!json?.history || !!json?.response
if (json && hasValue) {
setStore({
fields: json.schema || [],
schema: json.schema || [],
history: json.history || '',
response: json.response || '',
})
} else {
setStore({
history: exampleSchema.history,
response: exampleSchema.response,
fields: exampleSchema.schema.slice(),
schema: exampleSchema.schema.slice(),
})
}
}
Expand All @@ -122,7 +122,7 @@ export const CharacterSchema: Component<{
const resVars = store.response.match(JSON_NAME_RE())
const histVars = store.history.match(JSON_NAME_RE())

const names = new Set(store.fields.map((s) => s.name))
const names = new Set(store.schema.map((s) => s.name))

if (resVars) {
const bad: string[] = []
Expand Down Expand Up @@ -204,7 +204,7 @@ export const CharacterSchema: Component<{
const update = {
history: store.history,
response: store.response,
schema: store.fields,
schema: store.schema,
}
props.update(update)

Expand All @@ -226,7 +226,7 @@ export const CharacterSchema: Component<{

if (save && typeof save !== 'boolean') {
props.update(save)
setStore('fields', save.schema)
setStore('schema', save.schema)

if (props.characterId) {
characterStore.editPartialCharacter(props.characterId, { json: save })
Expand Down Expand Up @@ -258,7 +258,7 @@ export const CharacterSchema: Component<{
<Button size="pill" schema="secondary" onClick={() => setShowImport(true)}>
Import
</Button>
<Show when={store.fields.length}>
<Show when={store.schema.length}>
<Button
size="pill"
schema="secondary"
Expand All @@ -282,11 +282,10 @@ export const CharacterSchema: Component<{
<RootModal
title={
<>
Editing{' '}
Editing Schema:
<Show when={props.characterId} fallback="Preset">
Character
</Show>{' '}
Schema
{ctx.allBots[props.characterId!]?.name || 'Character'}
</Show>
</>
}
show={show()}
Expand Down Expand Up @@ -412,8 +411,8 @@ export const CharacterSchema: Component<{
</Show>

<JsonSchema
inherit={store.fields}
update={(ev) => setStore('fields', ev)}
inherit={store.schema}
update={(ev) => setStore('schema', ev)}
onNameChange={onFieldNameChange}
/>
</div>
Expand All @@ -438,14 +437,19 @@ const ImportModal: Component<{ show: boolean; close: (schema?: ResponseSchema) =
const json = JSON.parse(content)

curr = json
assertValid({ response: 'string', history: 'string', schema: ['any'] }, json)
assertValid(
{ response: 'string', history: 'string', fields: ['any?'], schema: ['any?'] },
json
)

const schema = curr.fields || curr.schema

for (const field of json.schema) {
for (const field of schema) {
curr = field
assertValid({ type: { type: 'string' }, name: 'string' }, field)
}

props.close(json)
props.close({ response: json.response, history: json.history, schema })
} catch (ex: any) {
toastStore.error(`Invalid JSON Schema: ${ex.message}`)
console.error(ex)
Expand Down
2 changes: 1 addition & 1 deletion web/pages/Character/ImportChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const ImportChatModal: Component<{
<FileInput
label="JSON Lines File (.jsonl)"
fieldName="json"
accept="application/json-lines,application/jsonl,text/jsonl,application/json,text/json"
accept=".txt, application/json, application/json-lines, application/jsonl, text/jsonl"
helperText="Supported formats: Agnaistic, TavernAI"
required
onUpdate={onSelectLog}
Expand Down
4 changes: 3 additions & 1 deletion web/pages/Chat/ChatDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ const ChatDetail: Component = () => {
}

const handle = ctx.allBots[msg.characterId] || ctx.tempMap[msg.characterId]
return { ...msg, handle: handle?.name }
if (handle) {
return { ...msg, handle: handle?.name || msg.handle }
}
}

if (msg.userId) {
Expand Down
34 changes: 19 additions & 15 deletions web/pages/Chat/ChatExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ import Modal from '../../shared/Modal'
import { chatStore, msgStore } from '../../store'

const ChatExport: Component<{ show: boolean; close: () => void }> = (props) => {
const chats = chatStore()
const msgs = msgStore()
const chats = chatStore.getState()
const msgs = msgStore.getState()

const json = createMemo(() => {
const chat = chats.active?.chat
const messages = msgs.msgs.slice()
const messages = msgs.messageHistory.concat(msgs.msgs)

const json = {
name: 'Exported',
greeting: chat?.greeting || '',
sampleChat: chat?.sampleChat || '',
scenario: chat?.scenario || '',
treeLeafId: chat?.treeLeafId,
messages: messages.map((msg) => ({
handle: msg.userId ? chats.memberIds[msg.userId]?.handle || 'You' : undefined,
_id: msg._id.slice(0, 8),
createdAt: msg.createdAt,
json: msg.json,
extras: msg.extras,
ooc: msg.ooc,
values: msg.values,
parent: msg.parent ? msg.msg.slice(0, 8) : undefined,
handle: msg.characterId
? chats.allChars.map[msg.characterId!]?.name
: msg.userId
? chats.memberIds[msg.userId]?.handle || 'You'
: undefined,
userId: msg.userId ? msg.userId : undefined,
characterId: msg.characterId ? 'imported' : undefined,
characterId: msg.characterId === chat?.characterId ? 'imported' : msg.characterId,
name: chats.allChars.map[msg.characterId!]?.name || chats.memberIds[msg.userId!]?.handle,
msg: msg.msg,
state: msg.state,
})),
Expand All @@ -46,16 +59,7 @@ const ChatExport: Component<{ show: boolean; close: () => void }> = (props) => {
</>
)

return (
<Modal show={props.show} close={props.close} title="Export Chat" footer={Footer}>
<p class="text-xl font-bold">Note</p>
<p>This will only export the conversation that you currently have loaded.</p>
<p>
If you want to export the entire chat, you will need to scroll up in the chat until the
entire conversation it loaded.
</p>
</Modal>
)
return <Modal show={props.show} close={props.close} title="Export Chat" footer={Footer}></Modal>
}

export default ChatExport
4 changes: 2 additions & 2 deletions web/pages/Chat/components/LogProbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const LogProbs: Component<{ msg: AppSchema.ChatMessage }> = (props) => {
})
return (
<Show when={props.msg.meta?.probs}>
<div class="min-h-48 mb-2 flex h-48 w-full flex-col gap-1">
<div class="mb-2 flex w-full flex-col gap-1">
<b class="text-md">Token Probabilities</b>
<div class="mb-2 text-sm">
<For each={props.msg.meta.probs}>
Expand All @@ -39,7 +39,7 @@ export const LogProbs: Component<{ msg: AppSchema.ChatMessage }> = (props) => {
</For>
</div>

<div class="flex flex-wrap gap-1">
<div class="min-h-16 flex h-16 flex-wrap gap-1">
<Show when={probs().length}>
<For each={probs()}>
{(item) => {
Expand Down
Loading

0 comments on commit 0ecf249

Please sign in to comment.