| 1 | import { ChatMessage, LLMOptions } from ".."; |
| 2 | |
| 3 | import { allModelProviders } from "@continuedev/llm-info"; |
| 4 | import { LlmInfo } from "@continuedev/llm-info/dist/types"; |
| 5 | import { BaseLLM } from "."; |
| 6 | import { DEFAULT_CONTEXT_LENGTH } from "./constants"; |
| 7 | import { LLMClasses } from "./llms"; |
| 8 | import { LLMLogger } from "./logger"; |
| 9 | |
| 10 | class DummyLLM extends BaseLLM { |
| 11 | static providerName = "openai"; |
| 12 | static defaultOptions: Partial<LLMOptions> = { |
| 13 | model: "dummy-model", |
| 14 | contextLength: 200_000, |
| 15 | completionOptions: { |
| 16 | model: "some-model", |
| 17 | maxTokens: 4096, |
| 18 | }, |
| 19 | apiBase: "https://api.test-api-dummy.com/v1/", |
| 20 | }; |
| 21 | } |
| 22 | describe("BaseLLM", () => { |
| 23 | let baseLLM: BaseLLM; |
| 24 | |
| 25 | beforeEach(() => { |
| 26 | const options: LLMOptions = { |
| 27 | model: "dummy-model", |
| 28 | }; |
| 29 | // Instantiate a DummyLLM instance |
| 30 | baseLLM = new DummyLLM(options); |
| 31 | }); |
| 32 | |
| 33 | describe("BaseLLM constructor", () => { |
| 34 | it("should correctly initialize with given options", () => { |
| 35 | const templatMessagesFunction = (messages: ChatMessage[]) => { |
| 36 | return messages[0]?.content.toString() ?? ""; |
| 37 | }; |
| 38 | const llmLogger = new LLMLogger(); |
| 39 | const options: LLMOptions = { |
| 40 | model: "gpt-3.5-turbo", |
| 41 | uniqueId: "testId", |
| 42 | contextLength: 1024, |
| 43 | completionOptions: { |
| 44 | model: "some-model", |
| 45 | maxTokens: 150, |
| 46 | }, |
| 47 | requestOptions: {}, |
| 48 | promptTemplates: {}, |
| 49 | templateMessages: templatMessagesFunction, |
| 50 | logger: llmLogger, |
| 51 | llmRequestHook: () => {}, |
| 52 | apiKey: "testApiKey", |
| 53 | aiGatewaySlug: "testSlug", |
| 54 | apiBase: "https://api.example.com", |
| 55 | accountId: "testAccountId", |
| 56 | deployment: "davinci", |
| 57 | apiVersion: "v1", |
| 58 | apiType: "public", |
| 59 | region: "us", |
| 60 | projectId: "testProjectId", |
| 61 | }; |
| 62 | |
| 63 | const instance = new DummyLLM(options); |
| 64 | |
| 65 | expect(instance.title).toBeDefined(); |
| 66 | expect(instance.uniqueId).toBe("testId"); |
| 67 | expect(instance.model).toBe("gpt-3.5-turbo"); |
| 68 | expect(instance.contextLength).toBe(1024); |
| 69 | expect(instance.completionOptions.maxTokens).toBe(150); |
| 70 | expect(instance.requestOptions).toEqual({}); |
| 71 | expect(instance.promptTemplates).toEqual({}); |
| 72 | expect(instance.templateMessages).toEqual(templatMessagesFunction); |
| 73 | expect(instance.logger).toBe(llmLogger); |
| 74 | expect(instance.apiKey).toBe("testApiKey"); |
| 75 | expect(instance.aiGatewaySlug).toBe("testSlug"); |
| 76 | expect(instance.apiBase).toBe("https://api.example.com/"); |
| 77 | expect(instance.accountId).toBe("testAccountId"); |
| 78 | expect(instance.deployment).toBe("davinci"); |
| 79 | expect(instance.apiVersion).toBe("v1"); |
| 80 | expect(instance.apiType).toBe("public"); |
| 81 | expect(instance.region).toBe("us"); |
| 82 | expect(instance.projectId).toBe("testProjectId"); |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | test("model should return correct provider model", () => { |
| 87 | expect(baseLLM.model).toBe("dummy-model"); |
| 88 | }); |
| 89 | |
| 90 | test("supportsFim should always return false", () => { |
| 91 | expect(baseLLM.supportsFim()).toBe(false); |
| 92 | }); |
| 93 | |
| 94 | describe("supportsImages", () => { |
| 95 | test("should return true when modelSupportsImages returns true", () => { |
| 96 | baseLLM.model = "gpt-4-vision"; |
| 97 | expect(baseLLM.supportsImages()).toBe(true); |
| 98 | |
| 99 | baseLLM.model = "fancy-vision-model"; |
| 100 | expect(baseLLM.supportsImages()).toBe(true); |
| 101 | |
| 102 | baseLLM.model = "gemma3:4b"; |
| 103 | expect(baseLLM.supportsImages()).toBe(true); |
| 104 | |
| 105 | baseLLM.model = "google/gemma-3-270m"; |
| 106 | expect(baseLLM.supportsImages()).toBe(true); |
| 107 | |
| 108 | baseLLM.model = "foo/paligemma-custom-100"; |
| 109 | expect(baseLLM.supportsImages()).toBe(true); |
| 110 | |
| 111 | baseLLM.model = "foo/medgemma_4b_it_16Q"; |
| 112 | expect(baseLLM.supportsImages()).toBe(true); |
| 113 | |
| 114 | baseLLM.model = "qwen2.5vl"; |
| 115 | expect(baseLLM.supportsImages()).toBe(true); |
| 116 | }); |
| 117 | |
| 118 | test("should return false when modelSupportsImages returns false", () => { |
| 119 | expect(baseLLM.supportsImages()).toBe(false); |
| 120 | |
| 121 | baseLLM.model = "gemma3n"; |
| 122 | expect(baseLLM.supportsImages()).toBe(false); |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | describe("supportsCompletions", () => { |
| 127 | test("should return correctly under specific conditions", () => { |
| 128 | // Mocking properties and scenarios to match the conditions in supportsCompletions |
| 129 | baseLLM.apiBase = "api.groq.com"; |
| 130 | expect(baseLLM.supportsCompletions()).toBe(false); |
| 131 | |
| 132 | baseLLM.apiBase = "integrate.api.nvidia.com"; |
| 133 | expect(baseLLM.supportsCompletions()).toBe(false); |
| 134 | |
| 135 | baseLLM.apiBase = "api.mistral.ai"; |
| 136 | expect(baseLLM.supportsCompletions()).toBe(false); |
| 137 | |
| 138 | baseLLM.apiBase = ":1337"; |
| 139 | expect(baseLLM.supportsCompletions()).toBe(false); |
| 140 | |
| 141 | baseLLM.apiBase = "something:3000"; |
| 142 | expect(baseLLM.supportsCompletions()).toBe(true); |
| 143 | }); |
| 144 | }); |
| 145 | describe("supportsPrefill", () => { |
| 146 | test("should return correctly under specific conditions", () => { |
| 147 | expect(baseLLM.supportsPrefill()).toBe(false); |
| 148 | |
| 149 | class PrefillLLM extends BaseLLM { |
| 150 | static providerName = "ollama"; |
| 151 | } |
| 152 | const prefillLLM = new PrefillLLM({ model: "some-model" }); |
| 153 | expect(prefillLLM.supportsPrefill()).toBe(true); |
| 154 | }); |
| 155 | }); |
| 156 | describe("fetch", () => { |
| 157 | // TODO: Implement tests for fetch method |
| 158 | }); |
| 159 | describe("*_streamFim", () => { |
| 160 | // TODO: Implement tests for *_streamFim method |
| 161 | }); |
| 162 | describe("complete", () => { |
| 163 | // TODO: Implement tests for complete method |
| 164 | }); |
| 165 | describe("*streamChat", () => { |
| 166 | // TODO: Implement tests for *streamChat method |
| 167 | }); |
| 168 | |
| 169 | describe("default context length", () => { |
| 170 | allModelProviders.map((modelProvider) => { |
| 171 | const LLMClass = LLMClasses.find( |
| 172 | (llm) => llm.providerName === modelProvider.id, |
| 173 | ); |
| 174 | if (!LLMClass) { |
| 175 | throw new Error(`did not find LLM provider for ${modelProvider.id}`); |
| 176 | } |
| 177 | const testContextLength = (llmInfo: LlmInfo) => () => { |
| 178 | const llm = new LLMClass({ model: llmInfo.model }); |
| 179 | if (llmInfo.contextLength) { |
| 180 | expect(llm.contextLength).toEqual(llmInfo.contextLength); |
| 181 | } else { |
| 182 | expect(llm.contextLength).toEqual(DEFAULT_CONTEXT_LENGTH); |
| 183 | } |
| 184 | }; |
| 185 | describe(`${modelProvider.id}`, () => { |
| 186 | modelProvider.models.forEach((llmInfo) => { |
| 187 | test( |
| 188 | `should have correct context length for ${llmInfo.model}`, |
| 189 | testContextLength(llmInfo), |
| 190 | ); |
| 191 | }); |
| 192 | }); |
| 193 | }); |
| 194 | }); |
| 195 | }); |