Elffuss BraidHub

kikocis/continue public

b0a27770390e · 5.7 KiBRawPermalink
1import { ChatMessage, LLMOptions } from "..";
2
3import { allModelProviders } from "@continuedev/llm-info";
4import { LlmInfo } from "@continuedev/llm-info/dist/types";
5import { BaseLLM } from ".";
6import { DEFAULT_CONTEXT_LENGTH } from "./constants";
7import { LLMClasses } from "./llms";
8import { LLMLogger } from "./logger";
9
10class 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}
22describe("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
100 test("should return false when modelSupportsImages returns false", () => {
101 expect(baseLLM.supportsImages()).toBe(false);
102 });
103 });
104
105 describe("supportsCompletions", () => {
106 test("should return correctly under specific conditions", () => {
107 // Mocking properties and scenarios to match the conditions in supportsCompletions
108 baseLLM.apiBase = "api.groq.com";
109 expect(baseLLM.supportsCompletions()).toBe(false);
110
111 baseLLM.apiBase = "integrate.api.nvidia.com";
112 expect(baseLLM.supportsCompletions()).toBe(false);
113
114 baseLLM.apiBase = "api.mistral.ai";
115 expect(baseLLM.supportsCompletions()).toBe(false);
116
117 baseLLM.apiBase = ":1337";
118 expect(baseLLM.supportsCompletions()).toBe(false);
119
120 baseLLM.apiBase = "something:3000";
121 expect(baseLLM.supportsCompletions()).toBe(true);
122 });
123 });
124 describe("supportsPrefill", () => {
125 test("should return correctly under specific conditions", () => {
126 expect(baseLLM.supportsPrefill()).toBe(false);
127
128 class PrefillLLM extends BaseLLM {
129 static providerName = "ollama";
130 }
131 const prefillLLM = new PrefillLLM({ model: "some-model" });
132 expect(prefillLLM.supportsPrefill()).toBe(true);
133 });
134 });
135 describe("fetch", () => {
136 // TODO: Implement tests for fetch method
137 });
138 describe("*_streamFim", () => {
139 // TODO: Implement tests for *_streamFim method
140 });
141 describe("complete", () => {
142 // TODO: Implement tests for complete method
143 });
144 describe("*streamChat", () => {
145 // TODO: Implement tests for *streamChat method
146 });
147
148 describe("default context length", () => {
149 allModelProviders.map((modelProvider) => {
150 const LLMClass = LLMClasses.find(
151 (llm) => llm.providerName === modelProvider.id,
152 );
153 if (!LLMClass) {
154 throw new Error(`did not find LLM provider for ${modelProvider.id}`);
155 }
156 const testContextLength = (llmInfo: LlmInfo) => () => {
157 const llm = new LLMClass({ model: llmInfo.model });
158 if (llmInfo.contextLength) {
159 expect(llm.contextLength).toEqual(llmInfo.contextLength);
160 } else {
161 expect(llm.contextLength).toEqual(DEFAULT_CONTEXT_LENGTH);
162 }
163 };
164 describe(`${modelProvider.id}`, () => {
165 modelProvider.models.forEach((llmInfo) => {
166 test(
167 `should have correct context length for ${llmInfo.model}`,
168 testContextLength(llmInfo),
169 );
170 });
171 });
172 });
173 });
174});