Skip to content

Commit 3901f08

Browse files
committed
feat: allow model definition inheritance
Signed-off-by: blob42 <contact@blob42.xyz>
1 parent 71a5909 commit 3901f08

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This fork does the following:
1515
- **UI Query and select** local or remote model
1616
- **Strips thinking tokens** from replies if the model forgets to use codeblocks
1717
- **New callback types**: `insert_lines` and `prepend_lines`
18+
- **Model definition inheritance**: Define models that inherit other model parameters
1819
- **Refactored for idiomatic Lua** and neovim plugin style
1920
- **Simplified command system** with explicit configuration
2021
- **Tests with plenary library**

lua/codegpt/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ M.persistent_override = nil
150150
---@field callback_type? codegpt.CallbackType Controls what the plugin does with the response
151151
---@field extra_params? table Custom parameters to include with this model query
152152
---@field append_string? string String to append to prompt -- ex: /no_think
153+
---@field from? string (optional) Name of parent model to inherit params from
153154

154155
---@alias ModelDef { [string] : codegpt.Model | string }
155156

lua/codegpt/models.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ function M.get_model_by_name(name)
3131
end
3232
end
3333

34+
-- (optional) inherit a parent model
35+
if result ~= nil and result.from ~= nil and #result.from > 0 then
36+
local parent_name, parent = M.get_model_by_name(result.from)
37+
if parent ~= nil then
38+
result = vim.tbl_deep_extend("force", parent, result)
39+
selected = parent_name
40+
end
41+
end
42+
3443
return selected, result
3544
end
3645
--- default model selection order from highest to lowest priority
@@ -74,6 +83,15 @@ function M.get_model()
7483
end
7584
end
7685

86+
-- (optional) inherit a parent model
87+
if result ~= nil and result.from ~= nil and #result.from > 0 then
88+
local parent_name, parent = M.get_model_by_name(result.from)
89+
if parent ~= nil then
90+
result = vim.tbl_deep_extend("force", parent, result)
91+
selected = parent_name
92+
end
93+
end
94+
7795
return selected, result
7896
end
7997

tests/models_spec.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,58 @@ describe("models selection", function()
164164
local name, model = models.get_model()
165165
assert(name == "gpt4-o")
166166
end)
167+
168+
it("should inherit a model definition", function()
169+
codegpt.setup({
170+
connection = {
171+
api_provider = "openai",
172+
},
173+
models = {
174+
openai = {
175+
default = "fbar",
176+
foo = {
177+
temperature = 0.5,
178+
max_tokens = 420,
179+
},
180+
foobar = {
181+
alias = "fbar",
182+
from = "foo",
183+
temperature = 1,
184+
append_string = "/no_think",
185+
},
186+
bar = {
187+
from = "fbar",
188+
temperature = 0,
189+
},
190+
},
191+
},
192+
})
193+
local _, model = models.get_model()
194+
assert(vim.deep_equal(model, {
195+
temperature = 1,
196+
alias = "fbar",
197+
from = "foo",
198+
max_tokens = 420,
199+
append_string = "/no_think",
200+
}))
201+
202+
local name, model = models.get_model_by_name("fbar")
203+
assert(vim.deep_equal(model, {
204+
temperature = 1,
205+
alias = "fbar",
206+
from = "foo",
207+
max_tokens = 420,
208+
append_string = "/no_think",
209+
}))
210+
assert(name == "foo")
211+
local name, model = models.get_model_by_name("bar")
212+
assert(vim.deep_equal(model, {
213+
temperature = 0,
214+
alias = "fbar",
215+
from = "fbar",
216+
max_tokens = 420,
217+
append_string = "/no_think",
218+
}))
219+
assert(name == "foo")
220+
end)
167221
end)

0 commit comments

Comments
 (0)