Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/db-schema/llm-utils.test.ts
Views: 687
import {1DEFAULT_LLM_PRIORITY,2DEFAULT_MODEL,3getValidLanguageModelName,4isCoreLanguageModel,5isFreeModel,6LANGUAGE_MODEL_SERVICES,7LANGUAGE_MODELS,8LanguageService,9LLM_COST,10LLMServicesAvailable,11model2vendor,12OLLAMA_PREFIX,13SERVICES,14USER_SELECTABLE_LANGUAGE_MODELS,15USER_SELECTABLE_LLMS_BY_VENDOR,16} from "./llm-utils";1718describe("llm", () => {19const is_cocalc_com = true; // otherwise, the test makes no sense2021test("isFreeModel", () => {22expect(isFreeModel("gpt-3", is_cocalc_com)).toBe(true);23expect(isFreeModel("gpt-4", is_cocalc_com)).toBe(false);24// WARNING: if the following breaks, and ollama becomes non-free, then a couple of assumptions are broken as well.25// search for model2service(...) as LanguageService in the codebase!26expect(isFreeModel(`${OLLAMA_PREFIX}-1`, is_cocalc_com)).toBe(true);27});2829test.each(Object.keys(LLM_COST))(30"is valid model names in LLM_COST: '%s'",31(model) => {32expect(LANGUAGE_MODELS.includes(model as any)).toBe(true);33},34);3536test("all user selectable ones are valid", () => {37for (const model of USER_SELECTABLE_LANGUAGE_MODELS) {38expect(LANGUAGE_MODELS.includes(model)).toBe(true);39}40});4142// none of the user selectable models start with any of the vendor prefixes43test.each(USER_SELECTABLE_LANGUAGE_MODELS)(44"model '%s' does not start with any vendor prefix",45(model) => {46for (const prefix of LANGUAGE_MODEL_SERVICES) {47expect(model.startsWith(prefix)).toBe(false);48}49},50);5152test.each(LANGUAGE_MODELS)(53`check that model2vendor('%s') knows the model`,54(model) => {55const vendor = model2vendor(model);56expect(LANGUAGE_MODEL_SERVICES.includes(vendor.name)).toBe(true);57},58);5960test(`check model by vendor`, () => {61for (const vendor in USER_SELECTABLE_LLMS_BY_VENDOR) {62const models = USER_SELECTABLE_LLMS_BY_VENDOR[vendor];63for (const model of models) {64const v = model2vendor(model);65expect(v.name).toBe(vendor);66expect(v.url).toContain("https://");67}68}69});7071test("just checking the price", () => {72expect(1_000_000 * LLM_COST["gpt-4"].prompt_tokens).toBeCloseTo(30);73expect(1_000_000 * LLM_COST["gpt-4"].completion_tokens).toBeCloseTo(60);74expect(1_000_000 * LLM_COST["claude-3-opus"].prompt_tokens).toBeCloseTo(15);75expect(1_000_000 * LLM_COST["claude-3-opus"].completion_tokens).toBeCloseTo(7675,77);78});7980test("priority list is a shuffle of all llm vendors", () => {81// except for "user"82const prio = DEFAULT_LLM_PRIORITY;83const vend = SERVICES;84// test, that those lists have the same elements85expect(prio.length).toBe(vend.length);86for (const v of vend) {87expect(prio.includes(v)).toBe(true);88}89});9091test("getting valid language model", () => {92const selectable_llms = [...USER_SELECTABLE_LANGUAGE_MODELS];93const notAvailable = selectable_llms.pop();9495function getModel(model: LanguageService, disabled?: LanguageService) {96const allEnabled = LANGUAGE_MODEL_SERVICES.reduce((acc, svc) => {97acc[svc] = disabled !== svc;98return acc;99}, {}) as LLMServicesAvailable;100return getValidLanguageModelName({101model,102filter: allEnabled,103ollama: ["phi3"],104custom_openai: ["bar"],105selectable_llms,106});107}108109// meaningless name110expect(getModel("foobar")).toEqual(DEFAULT_MODEL);111expect(getModel("baz-delta99")).toEqual(DEFAULT_MODEL);112// gpt 3.5 is disabled113expect(getModel("gpt-3.5-turbo")).toEqual(DEFAULT_MODEL);114// not available115expect(116typeof notAvailable === "string" && isCoreLanguageModel(notAvailable),117).toBe(true);118if (typeof notAvailable === "string") {119expect(getModel(notAvailable)).toEqual(DEFAULT_MODEL);120}121// not disabled122expect(getModel("mistral-large-latest")).toEqual("mistral-large-latest");123expect(getModel("gpt-4")).toEqual("gpt-4");124expect(getModel(DEFAULT_MODEL)).toEqual(DEFAULT_MODEL);125expect(getModel("mistral-medium-latest")).toEqual(DEFAULT_MODEL);126expect(getModel("mistral-large-latest")).toEqual("mistral-large-latest");127expect(getModel("claude-3-haiku-8k")).toEqual("claude-3-haiku-8k");128// anthropic service disabled129expect(getModel("claude-3-haiku-8k", "anthropic")).toEqual(DEFAULT_MODEL);130// ollama131expect(getModel("ollama-foo")).toEqual(DEFAULT_MODEL);132expect(getModel("ollama-phi3")).toEqual("ollama-phi3");133// openai api134expect(getModel("custom_openai-foo")).toEqual(DEFAULT_MODEL);135expect(getModel("custom_openai-bar")).toEqual("custom_openai-bar");136// user models: there are no further checks137expect(getModel("user-custom_openai-foo")).toEqual(138"user-custom_openai-foo",139);140expect(getModel("user-openai-gpt-3.5-turbo")).toEqual(141"user-openai-gpt-3.5-turbo",142);143// it's ok to use a model if disabled by the admin, since it's their key144expect(getModel("user-anthropic-claude-3-haiku-8k", "anthropic")).toEqual(145"user-anthropic-claude-3-haiku-8k",146);147// meaningless user service148expect(getModel("user-baz-delta99")).toEqual(DEFAULT_MODEL);149});150});151152153