Commit 4f1fbd7
committed
refactor(InferenceParameters): immutable + wither/append pattern
Convert InferenceParameters from a mutable fluent builder into a fully
immutable value class with a functional wither API:
InferenceParameters params = InferenceParameters.of("two plus two?")
.withNPredict(8)
.withSeed(1)
.withTemperature(0.2f);
The parent JsonParameters base is reshaped to match: parameters map is
final and Collections.unmodifiableMap-wrapped, and the helpers
putScalar / putEnum / putOptionalJson are replaced by withScalar /
withEnum / withOptionalJson / withRaw, all of which allocate a new
subclass instance through the abstract withParameters(Map) factory
hook. ModelParameters extends a different parent (CliParameters) and
is unchanged — it is constructed once and consumed once, so the
immutability refactor brings no correctness payoff there but ~250 test
line changes; deliberately skipped to keep this commit focused.
Hidden mutation bug fixed: LlamaModel.complete /
completeWithStats / chatComplete and the cancellable complete-with-token
variant silently called parameters.setStream(true|false) on the
caller's instance. They now bind a local derivation via withStream so
the caller's parameters object is never touched. Same pattern applied
to LlamaIterator.
API breaks (intentional, alongside the parameter rename):
- Consumer<InferenceParameters> -> UnaryOperator<InferenceParameters>
in Session and ChatRequest. The customiser must return its
transformed result because the input is immutable; lambdas like
`p -> p.withSeed(1).withNPredict(8)` keep working with the
expression-form return.
- ChatRequest.applyCustomizer now returns InferenceParameters instead
of being a void mutator; callers (only LlamaModel.chat) updated.
Tests:
- JsonParametersTest rewritten to cover the new wither helpers
(withScalar / withEnum / withRaw / withOptionalJson) plus the
unmodifiable-map invariant. The legacy CliParameters putScalar /
putEnum tests are preserved unchanged because ModelParameters still
uses them.
- Bulk-renamed setX -> withX across the entire test surface
(InferenceParametersTest 71+, ChatAdvancedTest 84, LlamaModelTest 90,
ChatScenarioTest 64, MemoryManagementTest 40, plus smaller files and
all examples), preserving ModelParameters' fluent setX chains where
the overlap methods (setSeed, setGrammar, setJsonSchema, setSamplers,
setChatTemplate, setChatTemplateKwargs, setReasoningFormat) appear.
- A handful of tests that did `params.setX(...)` without capturing the
return value were rewritten to `params = params.withX(...)`.
SpotBugs Max+Low: net unchanged at 6 findings. The one new
OCP_OVERLY_CONCRETE_PARAMETER on InferenceParameters.withReasoningFormat
is suppressed with the same design-intent rationale as the existing
ModelParameters OCP block (the narrow enum type is the API contract;
widening to CliArg would silently accept any enum and emit a nonsense
JSON value).1 parent c42a2fc commit 4f1fbd7
35 files changed
Lines changed: 1074 additions & 950 deletions
File tree
- src
- main/java/net/ladenthin/llama
- args
- json
- test/java
- examples
- net/ladenthin/llama
- benchmark
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
91 | 105 | | |
92 | 106 | | |
93 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
| |||
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
50 | | - | |
| 54 | + | |
51 | 55 | | |
52 | 56 | | |
53 | 57 | | |
| |||
58 | 62 | | |
59 | 63 | | |
60 | 64 | | |
61 | | - | |
| 65 | + | |
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| |||
93 | 97 | | |
94 | 98 | | |
95 | 99 | | |
96 | | - | |
| 100 | + | |
97 | 101 | | |
98 | 102 | | |
99 | 103 | | |
| |||
105 | 109 | | |
106 | 110 | | |
107 | 111 | | |
108 | | - | |
| 112 | + | |
109 | 113 | | |
110 | 114 | | |
111 | 115 | | |
| |||
212 | 216 | | |
213 | 217 | | |
214 | 218 | | |
215 | | - | |
| 219 | + | |
216 | 220 | | |
217 | 221 | | |
218 | 222 | | |
| |||
319 | 323 | | |
320 | 324 | | |
321 | 325 | | |
322 | | - | |
323 | | - | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
324 | 329 | | |
325 | | - | |
| 330 | + | |
| 331 | + | |
326 | 332 | | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
| 333 | + | |
| 334 | + | |
331 | 335 | | |
332 | 336 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
0 commit comments