Update models and add check for assistants model on startup (#998)
Browse files* Remove old models from .env.template
* Make sure we always have a visible model in assistant settings
* Always show correct info in chat window
* Add migration that runs on every server start to update assistant model
* change key
* fix tests
* Update src/lib/migrations/migrations.ts
Co-authored-by: Mishig <mishig.davaadorj@coloradocollege.edu>
* review
* Update src/lib/components/chat/ChatWindow.svelte
Co-authored-by: Mishig <mishig.davaadorj@coloradocollege.edu>
* revert duplicate Model:
* simplify deprecated check
---------
Co-authored-by: Mishig <mishig.davaadorj@coloradocollege.edu>
Files changed (7)
59 unmodified lines606162636465666768697071727374757677787980818283848586878889909192939423 unmodified lines11811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315459 unmodified lines2142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502514 unmodified lines25625725825926026126259 unmodified lines}},{"name": "meta-llama/Llama-2-70b-chat-hf","description": "The latest and biggest model from Meta, fine-tuned for chat.","logoUrl": "https://huggingface.co/datasets/huggingchat/models-logo/resolve/main/meta-logo.png","websiteUrl": "https://ai.meta.com/llama/","preprompt": "","chatPromptTemplate" : "<s>[INST] <<SYS>>\n{{preprompt}}\n<</SYS>>\n\n{{#each messages}}{{#ifUser}}{{content}} [/INST] {{/ifUser}}{{#ifAssistant}}{{content}} </s><s>[INST] {{/ifAssistant}}{{/each}}","promptExamples": [{"title": "Write an email from bullet list","prompt": "As a restaurant owner, write a professional email to the supplier to get these products every week: \n\n- Wine (x10)\n- Eggs (x24)\n- Bread (x12)"}, {"title": "Code a snake game","prompt": "Code a basic snake game in python, give explanations for each step."}, {"title": "Assist in a task","prompt": "How do I make a delicious lemon cheesecake?"}],"parameters": {"temperature": 0.1,"top_p": 0.95,"repetition_penalty": 1.2,"top_k": 50,"truncate": 3072,"max_new_tokens": 1024,"stop" : ["</s>", "</s><s>[INST]"]}},{"name" : "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO","description" : "Nous Hermes 2 Mixtral 8x7B DPO is the new flagship Nous Research model trained over the Mixtral 8x7B MoE LLM.","logoUrl": "https://huggingface.co/datasets/huggingchat/models-logo/resolve/main/nous-logo.png",23 unmodified lines"stop": ["<|im_end|>"]}},{"name": "codellama/CodeLlama-70b-Instruct-hf","displayName": "codellama/CodeLlama-70b-Instruct-hf","description": "Code Llama, a state of the art code model from Meta. Now in 70B!","logoUrl": "https://huggingface.co/datasets/huggingchat/models-logo/resolve/main/meta-logo.png","websiteUrl": "https://ai.meta.com/blog/code-llama-large-language-model-coding/","modelUrl": "https://huggingface.co/codellama/CodeLlama-70b-Instruct-hf","preprompt": "","chatPromptTemplate" : "<s>{{#if @root.preprompt}}Source: system\n\n {{@root.preprompt}} <step> {{/if}}{{#each messages}}{{#ifUser}}Source: user\n\n {{content}} <step> {{/ifUser}}{{#ifAssistant}}Source: assistant\n\n {{content}} <step> {{/ifAssistant}}{{/each}}Source: assistant\nDestination: user\n\n ","promptExamples": [{"title": "Fibonacci in Python","prompt": "Write a python function to calculate the nth fibonacci number."}, {"title": "JavaScript promises","prompt": "How can I wait for multiple JavaScript promises to fulfill before doing something with their values?"}, {"title": "Rust filesystem","prompt": "How can I load a file from disk in Rust?"}],"parameters": {"temperature": 0.1,"top_p": 0.95,"repetition_penalty": 1.2,"top_k": 50,"truncate": 4096,"max_new_tokens": 4096,"stop": ["<step>", " <step>", " <step> "],}},{"name": "mistralai/Mistral-7B-Instruct-v0.1","displayName": "mistralai/Mistral-7B-Instruct-v0.1",59 unmodified lines}]},{"name": "openchat/openchat-3.5-0106","displayName": "openchat/openchat-3.5-0106","description": "OpenChat 3.5 is the #1 model on MT-Bench, with only 7B parameters.","logoUrl": "https://huggingface.co/datasets/huggingchat/models-logo/resolve/main/openchat-logo.png","websiteUrl": "https://huggingface.co/openchat/openchat-3.5-0106","modelUrl": "https://huggingface.co/openchat/openchat-3.5-0106","tokenizer": "openchat/openchat-3.5-0106","preprompt": "","chatPromptTemplate" : "<s>{{#each messages}}{{#ifUser}}GPT4 Correct User: {{#if @first}}{{#if @root.preprompt}}{{@root.preprompt}}\n{{/if}}{{/if}}{{content}}<|end_of_turn|>GPT4 Correct Assistant:{{/ifUser}}{{#ifAssistant}}{{content}}<|end_of_turn|>{{/ifAssistant}}{{/each}}","parameters": {"temperature": 0.6,"top_p": 0.95,"repetition_penalty": 1.2,"top_k": 50,"truncate": 6016,"max_new_tokens": 2048,"stop": ["<|end_of_turn|>"]},"promptExamples": [{"title": "Write an email from bullet list","prompt": "As a restaurant owner, write a professional email to the supplier to get these products every week: \n\n- Wine (x10)\n- Eggs (x24)\n- Bread (x12)"}, {"title": "Code a snake game","prompt": "Code a basic snake game in python, give explanations for each step."}, {"title": "Assist in a task","prompt": "How do I make a delicious lemon cheesecake?"}]}]`OLD_MODELS=`[4 unmodified lines{"name":"openchat/openchat-3.5-1210"},{"name": "tiiuae/falcon-180B-chat"},{"name": "codellama/CodeLlama-34b-Instruct-hf"},{"name": "google/gemma-7b-it"}]`TASK_MODEL='mistralai/Mistral-7B-Instruct-v0.1'59 unmodified lines60616263646523 unmodified lines89909192939459 unmodified lines1541551561571581594 unmodified lines16416516616716816917017117217359 unmodified lines}},{"name" : "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO","description" : "Nous Hermes 2 Mixtral 8x7B DPO is the new flagship Nous Research model trained over the Mixtral 8x7B MoE LLM.","logoUrl": "https://huggingface.co/datasets/huggingchat/models-logo/resolve/main/nous-logo.png",23 unmodified lines"stop": ["<|im_end|>"]}},{"name": "mistralai/Mistral-7B-Instruct-v0.1","displayName": "mistralai/Mistral-7B-Instruct-v0.1",59 unmodified lines}]},]`OLD_MODELS=`[4 unmodified lines{"name":"openchat/openchat-3.5-1210"},{"name": "tiiuae/falcon-180B-chat"},{"name": "codellama/CodeLlama-34b-Instruct-hf"},{"name": "google/gemma-7b-it"},{"name": "meta-llama/Llama-2-70b-chat-hf"},{"name": "codellama/CodeLlama-70b-Instruct-hf"},{"name": "openchat/openchat-3.5-0106"}]`TASK_MODEL='mistralai/Mistral-7B-Instruct-v0.1'
16 unmodified lines17181920212222 unmodified lines454647484950515253545516 unmodified linesimport IconInternet from "./icons/IconInternet.svelte";import TokensCounter from "./TokensCounter.svelte";import HoverTooltip from "./HoverTooltip.svelte";type ActionData = {error: boolean;22 unmodified linesconst module = await import("browser-image-resizer");compress = module.readAndCompressImage;if (assistant) {modelId = assistant.modelId;} else {modelId = models.find((model) => model.id === $settings.activeModel)?.id ?? models[0].id;}});let inputMessage1 = assistant?.exampleInputs[0] ?? "";16 unmodified lines1718192021222322 unmodified lines4647484950515216 unmodified linesimport IconInternet from "./icons/IconInternet.svelte";import TokensCounter from "./TokensCounter.svelte";import HoverTooltip from "./HoverTooltip.svelte";import { findCurrentModel } from "$lib/utils/models";type ActionData = {error: boolean;22 unmodified linesconst module = await import("browser-image-resizer");compress = module.readAndCompressImage;modelId = findCurrentModel(models, assistant ? assistant.modelId : $settings.activeModel).id;});let inputMessage1 = assistant?.exampleInputs[0] ?? "";
308 unmodified lines30931031131231331431537 unmodified lines353354355356357358359360361362363364365366367368308 unmodified lines<ChatInput value="Sorry, something went wrong. Please try again." disabled={true} />{:else}<ChatInputplaceholder="Ask anything"bind:value={message}on:submit={handleSubmit}on:beforeinput={(ev) => {37 unmodified lines<p>Model:{#if !assistant}<a href="{base}/settings/{currentModel.id}" class="hover:underline">{currentModel.displayName}</a>{:else}{@const model = models.find((m) => m.id === assistant?.modelId)}<ahref="{base}/settings/assistants/{assistant._id}"class="inline-flex items-center border-b hover:text-gray-600 dark:border-gray-700 dark:hover:text-gray-300">{model?.displayName}<CarbonCaretDown class="text-xxs" /></a>{/if} <span class="max-sm:hidden">·</span><br class="sm:hidden" /> Generated content maybe inaccurate or false.</p>{#if messages.length}<button308 unmodified lines30931031131231331431531631737 unmodified lines355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387308 unmodified lines<ChatInput value="Sorry, something went wrong. Please try again." disabled={true} />{:else}<ChatInputplaceholder={isReadOnly? "This conversation is read-only. Start a new one to continue!": "Ask anything"}bind:value={message}on:submit={handleSubmit}on:beforeinput={(ev) => {37 unmodified lines<p>Model:{#if !assistant}{#if models.find((m) => m.id === currentModel.id)}<ahref="{base}/settings/{currentModel.id}"class="inline-flex items-center hover:underline">{currentModel.displayName}<CarbonCaretDown class="text-xxs" /></a>{:else}<span class="inline-flex items-center line-through dark:border-gray-700">{currentModel.id}</span>{/if}{:else}{@const model = models.find((m) => m.id === currentModel.id)}{#if model}<ahref="{base}/settings/assistants/{assistant._id}"class="inline-flex items-center border-b hover:text-gray-600 dark:border-gray-700 dark:hover:text-gray-300">{model?.displayName}<CarbonCaretDown class="text-xxs" /></a>{:else}<span class="inline-flex items-center line-through dark:border-gray-700">{currentModel.id}</span>{/if}{/if}<span class="max-sm:hidden">·</span><br class="sm:hidden" /> Generated content may be inaccurateor false.</p>{#if messages.length}<button
2 unmodified lines345678911 unmodified lines212223242526272 unmodified linesimport { acquireLock, isDBLocked, refreshLock, releaseLock } from "./lock";import { collections } from "$lib/server/database";const LOCK_KEY = "migrations";describe("migrations", () => {it("should not have duplicates guid", async () => {11 unmodified linesexpect(locks.length).toBe(1);expect(semaphores).toBeDefined();expect(semaphores.length).toBe(1);expect(semaphores?.[0].key).toBe("migrations");});it("should read the lock correctly", async () => {2 unmodified lines345678911 unmodified lines212223242526272 unmodified linesimport { acquireLock, isDBLocked, refreshLock, releaseLock } from "./lock";import { collections } from "$lib/server/database";const LOCK_KEY = "migrations.test";describe("migrations", () => {it("should not have duplicates guid", async () => {11 unmodified linesexpect(locks.length).toBe(1);expect(semaphores).toBeDefined();expect(semaphores.length).toBe(1);expect(semaphores?.[0].key).toBe(LOCK_KEY);});it("should read the lock correctly", async () => {
13 unmodified lines141516171819202122232425262724 unmodified lines5253545556575859606162637 unmodified lines717273747576777813 unmodified lines// check if all migrations have already been runconst migrationResults = await collections.migrationResults.find().toArray();// if all the migrations._id are in the migrationResults, we can exit earlyif (migrations.every((m) => migrationResults.some((m2) => m2._id.toString() === m._id.toString()))) {console.log("[MIGRATIONS] All migrations already applied.");return;}console.log("[MIGRATIONS] Begin check...");// connect to the database24 unmodified lines// iterate over all migrationsfor (const migration of migrations) {// check if the migration has already been appliedconst existingMigrationResult = migrationResults.find((m) => m._id.toString() === migration._id.toString());// check if the migration has already been appliedif (existingMigrationResult) {console.log(`[MIGRATIONS] "${migration.name}" already applied. Skipping...`);} else {// check the modifiers to see if some cases match7 unmodified linescontinue;}// otherwise all is good and we cna run the migrationconsole.log(`[MIGRATIONS] "${migration.name}" not applied yet. Applying...`);await collections.migrationResults.updateOne({ _id: migration._id },13 unmodified lines14151617181924 unmodified lines4445464748495051525354557 unmodified lines63646566676869707172737413 unmodified lines// check if all migrations have already been runconst migrationResults = await collections.migrationResults.find().toArray();console.log("[MIGRATIONS] Begin check...");// connect to the database24 unmodified lines// iterate over all migrationsfor (const migration of migrations) {// check if the migration has already been appliedconst shouldRun =migration.runEveryTime ||!migrationResults.find((m) => m._id.toString() === migration._id.toString());// check if the migration has already been appliedif (!shouldRun) {console.log(`[MIGRATIONS] "${migration.name}" already applied. Skipping...`);} else {// check the modifiers to see if some cases match7 unmodified linescontinue;}// otherwise all is good and we can run the migrationconsole.log(`[MIGRATIONS] "${migration.name}" ${migration.runEveryTime ? "should run every time" : "not applied yet"}. Applying...`);await collections.migrationResults.updateOne({ _id: migration._id },
12345678910111213141516171819202122232425262728import type { Migration } from ".";import { getCollections } from "$lib/server/database";import { ObjectId } from "mongodb";
const updateAssistantsModels: Migration = { _id: new ObjectId("5f9f3f3f3f3f3f3f3f3f3f3f"), name: "Update deprecated models in assistants with the default model", up: async (client) => { const models = (await import("$lib/server/models")).models;
const { assistants } = getCollections(client);
const modelIds = models.map((el) => el.id); // string[] const defaultModelId = models[0].id;
// Find all assistants whose modelId is not in modelIds, and update it to use defaultModelId await assistants.updateMany( { modelId: { $nin: modelIds } }, { $set: { modelId: defaultModelId } } );
return true; }, runEveryTime: true, runForHuggingChat: "only",};
export default updateAssistantsModels;1234562 unmodified lines91011121314import type { MongoClient, ObjectId } from "mongodb";import updateSearchAssistant from "./01-update-search-assistants";export interface Migration {_id: ObjectId;2 unmodified linesdown?: (client: MongoClient) => Promise<boolean>;runForFreshInstall?: "only" | "never"; // leave unspecified to run for bothrunForHuggingChat?: "only" | "never"; // leave unspecified to run for both}export const migrations: Migration[] = [updateSearchAssistant];12345672 unmodified lines10111213141516import type { MongoClient, ObjectId } from "mongodb";import updateSearchAssistant from "./01-update-search-assistants";import updateAssistantsModels from "./02-update-assistants-models";export interface Migration {_id: ObjectId;2 unmodified linesdown?: (client: MongoClient) => Promise<boolean>;runForFreshInstall?: "only" | "never"; // leave unspecified to run for bothrunForHuggingChat?: "only" | "never"; // leave unspecified to run for bothrunEveryTime?: boolean;}export const migrations: Migration[] = [updateSearchAssistant, updateAssistantsModels];
.env.template+4-93
src/lib/components/AssistantSettings.svelte+2-5
src/lib/components/chat/ChatWindow.svelte+30-11
src/lib/migrations/migrations.spec.ts+2-2
src/lib/migrations/migrations.ts+10-14
src/lib/migrations/routines/02-update-assistants-models.tsAdded+28
src/lib/migrations/routines/index.ts+3-1