|
| 1 | +/* |
| 2 | +Copyright 2025 The llm-d-inference-sim Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package llmdinferencesim |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "errors" |
| 23 | + |
| 24 | + vllmapi "github.com/llm-d/llm-d-inference-sim/pkg/vllm-api" |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + "github.com/openai/openai-go" |
| 28 | + "github.com/openai/openai-go/option" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("LoRAs", func() { |
| 32 | + Context("LoRAs config and load", func() { |
| 33 | + It("Should config, load and load LoRAs correctly", func() { |
| 34 | + ctx := context.TODO() |
| 35 | + client, err := startServerWithArgs(ctx, "", |
| 36 | + []string{"cmd", "--model", model, "--mode", modeEcho, |
| 37 | + "--lora-modules", "{\"name\":\"lora3\",\"path\":\"/path/to/lora3\"}", |
| 38 | + "{\"name\":\"lora4\",\"path\":\"/path/to/lora4\"}"}) |
| 39 | + Expect(err).NotTo(HaveOccurred()) |
| 40 | + |
| 41 | + openaiclient := openai.NewClient( |
| 42 | + option.WithBaseURL(baseURL), |
| 43 | + option.WithHTTPClient(client)) |
| 44 | + |
| 45 | + // Request to lora3 |
| 46 | + params := openai.ChatCompletionNewParams{ |
| 47 | + Messages: []openai.ChatCompletionMessageParamUnion{ |
| 48 | + openai.UserMessage(userMessage), |
| 49 | + }, |
| 50 | + Model: "lora3", |
| 51 | + } |
| 52 | + resp, err := openaiclient.Chat.Completions.New(ctx, params) |
| 53 | + Expect(err).ToNot(HaveOccurred()) |
| 54 | + |
| 55 | + Expect(resp.Choices).ShouldNot(BeEmpty()) |
| 56 | + Expect(string(resp.Object)).To(Equal(chatCompletionObject)) |
| 57 | + |
| 58 | + msg := resp.Choices[0].Message.Content |
| 59 | + Expect(msg).Should(Equal(userMessage)) |
| 60 | + |
| 61 | + // Unknown model, should return 404 |
| 62 | + params.Model = "lora1" |
| 63 | + _, err = openaiclient.Chat.Completions.New(ctx, params) |
| 64 | + Expect(err).To(HaveOccurred()) |
| 65 | + var openaiError *openai.Error |
| 66 | + ok := errors.As(err, &openaiError) |
| 67 | + Expect(ok).To(BeTrue()) |
| 68 | + Expect(openaiError.StatusCode).To(Equal(404)) |
| 69 | + |
| 70 | + // Add lora1 |
| 71 | + payload := map[string]string{ |
| 72 | + "lora_name": "lora1", // Name to register the adapter as |
| 73 | + "lora_path": "/path/to/lora1", // Local or remote path |
| 74 | + } |
| 75 | + |
| 76 | + loraParams, err := json.Marshal(payload) |
| 77 | + Expect(err).ToNot(HaveOccurred()) |
| 78 | + |
| 79 | + options := option.WithHeader("Content-Type", "application/json") |
| 80 | + err = openaiclient.Post(ctx, "/load_lora_adapter", loraParams, nil, options) |
| 81 | + Expect(err).ToNot(HaveOccurred()) |
| 82 | + |
| 83 | + // Should be four models: base model and three LoRAs |
| 84 | + var modelsResp vllmapi.ModelsResponse |
| 85 | + err = openaiclient.Get(ctx, "/models", nil, &modelsResp) |
| 86 | + Expect(err).ToNot(HaveOccurred()) |
| 87 | + Expect(modelsResp).NotTo(BeNil()) |
| 88 | + Expect(modelsResp.Data).To(HaveLen(4)) |
| 89 | + |
| 90 | + // Request to lora1, should work now |
| 91 | + resp, err = openaiclient.Chat.Completions.New(ctx, params) |
| 92 | + Expect(err).ToNot(HaveOccurred()) |
| 93 | + |
| 94 | + Expect(resp.Choices).ShouldNot(BeEmpty()) |
| 95 | + Expect(string(resp.Object)).To(Equal(chatCompletionObject)) |
| 96 | + |
| 97 | + msg = resp.Choices[0].Message.Content |
| 98 | + Expect(msg).Should(Equal(userMessage)) |
| 99 | + |
| 100 | + // Unload lora3 |
| 101 | + payload = map[string]string{ |
| 102 | + "lora_name": "lora3", // Name to register the adapter as |
| 103 | + "lora_path": "/path/to/lora3", // Local or remote path |
| 104 | + } |
| 105 | + |
| 106 | + loraParams, err = json.Marshal(payload) |
| 107 | + Expect(err).ToNot(HaveOccurred()) |
| 108 | + options = option.WithHeader("Content-Type", "application/json") |
| 109 | + err = openaiclient.Post(ctx, "/unload_lora_adapter", loraParams, nil, options) |
| 110 | + Expect(err).ToNot(HaveOccurred()) |
| 111 | + |
| 112 | + // We should now get an error now |
| 113 | + params.Model = "lora3" |
| 114 | + _, err = openaiclient.Chat.Completions.New(ctx, params) |
| 115 | + Expect(err).To(HaveOccurred()) |
| 116 | + ok = errors.As(err, &openaiError) |
| 117 | + Expect(ok).To(BeTrue()) |
| 118 | + Expect(openaiError.StatusCode).To(Equal(404)) |
| 119 | + |
| 120 | + // Should be three models: base model and two LoRAs |
| 121 | + err = openaiclient.Get(ctx, "/models", nil, &modelsResp) |
| 122 | + Expect(err).ToNot(HaveOccurred()) |
| 123 | + Expect(modelsResp).NotTo(BeNil()) |
| 124 | + Expect(modelsResp.Data).To(HaveLen(3)) |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments