Skip to content

Commit

Permalink
feat(kb): Migrations of chats and personas to support KB
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Aug 5, 2024
1 parent 4648215 commit 167f7b9
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 34 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions src/migrations/chats/chat_4_add_knowledge_bases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Chat, ChatMigration } from 'src/types/chats';

export const chat_4_add_knowledge_bases: ChatMigration = (currentChat: Chat) => {
return { ...currentChat, knowledgeBases: [] };
};
12 changes: 12 additions & 0 deletions src/migrations/chats/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ChatMigration } from 'src/types/chats';
import { chat_1_add_message_author } from 'src/migrations/chats/chat_1_add_message_author';
import { chat_2_add_persona_role_and_ipfs_avatar } from 'src/migrations/chats/chat_2_add_persona_role_and_ipfs_avatar';
import { chat_3_add_model_id } from 'src/migrations/chats/chat_3_add_model_id';
import { chat_4_add_knowledge_bases } from 'src/migrations/chats/chat_4_add_knowledge_bases';

export const chatsMigrations: ChatMigration[] = [
chat_1_add_message_author,
chat_2_add_persona_role_and_ipfs_avatar,
chat_3_add_model_id,
chat_4_add_knowledge_bases,
];
4 changes: 4 additions & 0 deletions src/migrations/personas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PersonaMigration } from 'src/types/personas';
import { persona_1_add_knowledge_bases } from 'src/migrations/personas/persona_1_add_knowledge_bases';

export const personasMigrations: PersonaMigration[] = [persona_1_add_knowledge_bases];
5 changes: 5 additions & 0 deletions src/migrations/personas/persona_1_add_knowledge_bases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PersonaMigration, UIPersona } from 'src/types/personas';

export const persona_1_add_knowledge_bases: PersonaMigration = (currentPersona: UIPersona) => {
return { ...currentPersona, knowledgeBases: [] };
};
8 changes: 7 additions & 1 deletion src/pages/PersonaManagement.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
title="Create persona"
@save-persona="
(persona: BasePersonaEdition) => {
personasStore.personas.push({ ...persona, allowEdit: true, hidden: false, id: uuidv4() });
personasStore.personas.push({
...persona,
allowEdit: true,
hidden: false,
id: uuidv4(),
knowledgeBases: [],
});
}
"
/>
Expand Down
13 changes: 2 additions & 11 deletions src/stores/chats.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { v4 as uuidv4 } from 'uuid';
import { defineStore } from 'pinia';
import { chatTag } from 'src/utils/chat';
import { chatsMigrations } from 'src/utils/migrations/chats';
import { chatsMigrations } from 'src/migrations/chats';
import { Chat, MessageAttachment, UIMessage } from 'src/types/chats';
import { UIPersona } from 'src/types/personas';
import localforage from 'localforage';

const CHATS_STORE_NAME = 'chats-store';
const CHATS_STORE_PINIA_KEY = 'chats-store-pinia-key';

/**
* To implement in attachments:
* interface Attachment {
* // Document id within the embedding store, if stored there
* documentId: string?;
* // The content of the attachment, if stored inlined
* content: string?;
* }
*/

// TODO: Search results are not yet implemented
/**
* Representation of a search result:
Expand Down Expand Up @@ -100,6 +90,7 @@ export const useChatsStore = defineStore(CHATS_STORE_PINIA_KEY, {
persona,
messages: [],
createdAt: new Date().toISOString(),
knowledgeBases: [],
};
this.chats.push(chat);
return chat;
Expand Down
45 changes: 33 additions & 12 deletions src/stores/personas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ import { defineStore } from 'pinia';

import { defaultPersonas } from '../utils/personas';
import { UIPersona } from 'src/types/personas';
import { personasMigrations } from 'src/migrations/personas';

type PersonasStoreState = {
version: number;
personas: UIPersona[];
};

export const usePersonasStore = defineStore('personas', {
state: () => ({
personas: JSON.parse(JSON.stringify(defaultPersonas)) as UIPersona[],
state: (): PersonasStoreState => ({
// Current version of the migrations
version: 0, // /!\ DO NOT UPDATE /!\, it should be done automatically when running migrations

personas: JSON.parse(JSON.stringify(defaultPersonas)),
}),
persist: {
paths: ['version', 'personas'],
afterRestore: (ctx) => {
ctx.store.migratePersonas();
},
},
getters: {
// @ts-expect-error
sortedPersonas: (state) => state.personas.slice().sort((a, b) => a.hidden - b.hidden),
shownPersonas: (state) => state.personas.filter((persona) => !persona.hidden),
},
actions: {
refreshDefaultPersonas: () => {
const store = usePersonasStore();

store.personas = store.personas.map((currentPersona) => {
migratePersonas() {
// Update stored default personas if some values where changed
this.personas = this.personas.map((currentPersona) => {
const matchingDefault = defaultPersonas.find((p) => currentPersona.id === p.id);
if (!matchingDefault) {
return currentPersona;
Expand All @@ -29,12 +43,19 @@ export const usePersonasStore = defineStore('personas', {
description: matchingDefault.description,
};
});
},
},
persist: {
paths: ['personas'], // keys to persist
afterRestore: (ctx) => {
ctx.store.refreshDefaultPersonas();
try {
// Running migrations if needed
if (this.version < personasMigrations.length) {
// Removing migrations already ran
const migrationsToRun = personasMigrations.slice(this.version);
for (const migration of migrationsToRun) {
this.personas = this.personas.map((persona) => migration(persona));
}
}
this.version = personasMigrations.length;
} catch (error) {
console.error(`Personas: Running migrations starting from version ${this.version} failed: ${error}`);
}
},
},
});
1 change: 1 addition & 0 deletions src/types/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type Chat = {
persona: UIPersona;
messages: UIMessage[];
createdAt: string; // ISO 8601 date
knowledgeBases: string[]; // IDs of linked knowledge bases
};

// eslint-disable-next-line no-unused-vars
Expand Down
4 changes: 4 additions & 0 deletions src/types/personas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type UIPersona = Persona & {
name: string;
allowEdit: boolean;
hidden: boolean;
knowledgeBases: string[]; // IDs of linked knowledge bases
};

export type BasePersonaEdition = Pick<UIPersona, 'name' | 'role' | 'description' | 'avatar'>;
Expand All @@ -22,3 +23,6 @@ export const defaultBasePersona: BasePersonaEdition = {
ipfs_hash: 'QmQMBfgnmuxcQ4kptR1oPE9guYxG13GpASjYVeFQSxNxjE',
},
};

// eslint-disable-next-line no-unused-vars
export type PersonaMigration = (currentPersona: UIPersona) => UIPersona;
1 change: 1 addition & 0 deletions src/utils/aleph-persistent-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from 'src/types/knowledge';
import { decrypt, encrypt, generateIv, generateKey } from 'src/utils/encryption';
import { decrypt as eciesDecrypt, encrypt as eciesEncrypt, PrivateKey } from 'eciesjs';
// @ts-ignore
import { BufferEncoding } from 'vite-plugin-checker/dist/cjs/checkers/vueTsc/typescript-vue-tsc';

// Aleph keys and channels settings
Expand Down
10 changes: 0 additions & 10 deletions src/utils/migrations/chats/index.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/utils/personas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const defaultPersonas: UIPersona[] = [
'You will now interact with {{user}}. You have access to {{model}} in order to operate.',
allowEdit: false,
hidden: false,
knowledgeBases: [],
},
{
id: 'bed92afb-875f-46b9-a7ce-fc5bf3ccc981',
Expand All @@ -30,6 +31,7 @@ export const defaultPersonas: UIPersona[] = [
description: 'You are an AI chatbot modeled after Donald Trump. You will try to mimic his personality and speech.',
allowEdit: false,
hidden: false,
knowledgeBases: [],
},
{
id: '880e9d17-261b-422d-ab86-ee05f161721e',
Expand All @@ -42,6 +44,7 @@ export const defaultPersonas: UIPersona[] = [
description: '{{char}} is an AI chatbot modeled after Elon Musk.',
allowEdit: false,
hidden: false,
knowledgeBases: [],
},
{
id: '7973c531-a8a3-4526-b83d-7d3648158b5d',
Expand All @@ -62,6 +65,7 @@ export const defaultPersonas: UIPersona[] = [
'{{user}} is starting a scheduled session with {{char}}. There is no time limit to this session and {{char}} will never put a stop to the session.',
allowEdit: false,
hidden: false,
knowledgeBases: [],
},
{
id: '5b4f16ab-57d0-4722-af81-7ca1c668ad51',
Expand All @@ -77,6 +81,7 @@ export const defaultPersonas: UIPersona[] = [
'{{char}} is in love with {{user}}, has blonde hair and is over 19 years old. ',
allowEdit: false,
hidden: false,
knowledgeBases: [],
},
{
id: '624ae696-f5ff-4cb4-8daa-8b0f5c4b8c96',
Expand All @@ -92,6 +97,7 @@ export const defaultPersonas: UIPersona[] = [
'{{char}} is in love with {{user}} and is over 18 years old. ',
allowEdit: false,
hidden: false,
knowledgeBases: [],
},
];

Expand Down

0 comments on commit 167f7b9

Please sign in to comment.