-
Notifications
You must be signed in to change notification settings - Fork 404
test: add regression test for Ollama configured model binding (#1735) #1828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
igordayen
merged 3 commits into
embabel:main
from
sumin220:fix/1735-ollama-configured-model
Jul 27, 2026
+139
−0
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...rc/test/kotlin/com/embabel/agent/config/models/ollama/OllamaConfiguredModelBindingTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * Copyright 2024-2026 Embabel Pty Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.embabel.agent.config.models.ollama | ||
|
|
||
| import com.embabel.agent.spi.support.springai.SpringAiLlmService | ||
| import com.embabel.common.ai.model.ConfigurableModelProviderProperties | ||
| import com.embabel.common.ai.model.LlmOptions | ||
| import io.micrometer.observation.ObservationRegistry | ||
| import io.mockk.* | ||
| import org.junit.jupiter.api.AfterEach | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import org.springframework.ai.ollama.api.OllamaChatOptions | ||
| import org.springframework.beans.factory.ObjectProvider | ||
| import org.springframework.beans.factory.config.ConfigurableBeanFactory | ||
| import org.springframework.core.ParameterizedTypeReference | ||
| import org.springframework.http.MediaType | ||
| import org.springframework.web.client.RestClient | ||
| import kotlin.test.assertEquals | ||
|
|
||
| /** | ||
| * Regression test for https://github.com/embabel/embabel-agent/issues/1735. | ||
| * | ||
| * Spring AI 2.0 no longer merges the [org.springframework.ai.ollama.OllamaChatModel]'s | ||
| * configured default options into a prompt that already carries per-request options, and | ||
| * `OllamaChatOptions` coerces a null model to `OllamaModel.MISTRAL.id()`. Because Embabel | ||
| * always supplies per-request options built by [OllamaOptionsConverter] — which sets no | ||
| * model — every request to a registered Ollama LLM would silently target `mistral` | ||
| * instead of the model the service was registered for. | ||
| */ | ||
| class OllamaConfiguredModelBindingTest { | ||
|
|
||
| private val registeredBeans = mutableMapOf<String, Any>() | ||
| private val mockBeanFactory = mockk<ConfigurableBeanFactory>(relaxed = true) | ||
| private val mockProperties = mockk<ConfigurableModelProviderProperties>() | ||
| private val mockObservationRegistry = mockk<ObjectProvider<ObservationRegistry>>() | ||
| private val mockRestClient = mockk<RestClient>() | ||
| private val mockRestClientBuilder = mockk<RestClient.Builder>() | ||
| private val mockClonedBuilder = mockk<RestClient.Builder>(relaxed = true) | ||
| private val mockRestClientBuilderProvider = mockk<ObjectProvider<RestClient.Builder>>() | ||
| private val mockRequestHeadersUriSpec = mockk<RestClient.RequestHeadersUriSpec<*>>() | ||
| private val mockRequestHeadersSpec = mockk<RestClient.RequestHeadersSpec<*>>() | ||
| private val mockResponseSpec = mockk<RestClient.ResponseSpec>() | ||
|
|
||
| // Use reflection to access the actual internal ModelResponse class from OllamaModelsConfig | ||
| private val modelResponseClass = | ||
| Class.forName("com.embabel.agent.config.models.ollama.OllamaModelsConfig\$ModelResponse") | ||
| private val modelDetailsClass = | ||
| Class.forName("com.embabel.agent.config.models.ollama.OllamaModelsConfig\$ModelDetails") | ||
|
|
||
| private val testModels by lazy { | ||
| val modelDetailsConstructor = | ||
| modelDetailsClass.getDeclaredConstructor(String::class.java, Long::class.java, String::class.java) | ||
| val modelResponseConstructor = modelResponseClass.getDeclaredConstructor(List::class.java) | ||
|
|
||
| modelResponseConstructor.newInstance( | ||
| listOf(modelDetailsConstructor.newInstance("gemma3:latest", 12345L, "2024-01-01T00:00:00Z")) | ||
| ) | ||
| } | ||
|
|
||
| @BeforeEach | ||
| fun setup() { | ||
| clearAllMocks() | ||
| registeredBeans.clear() | ||
|
|
||
| every { mockBeanFactory.registerSingleton(any(), any()) } answers { | ||
| registeredBeans[firstArg()] = secondArg() | ||
| } | ||
| every { mockProperties.allWellKnownEmbeddingServiceNames() } returns emptySet() | ||
| every { mockObservationRegistry.getIfUnique(any()) } returns ObservationRegistry.NOOP | ||
|
|
||
| every { mockRestClientBuilder.observationRegistry(any()) } returns mockRestClientBuilder | ||
| every { mockRestClientBuilder.clone() } returns mockClonedBuilder | ||
| every { mockRestClientBuilder.build() } returns mockRestClient | ||
| every { mockRestClientBuilderProvider.getIfAvailable(any<java.util.function.Supplier<RestClient.Builder>>()) } returns mockRestClientBuilder | ||
|
|
||
| every { mockRestClient.get() } returns mockRequestHeadersUriSpec | ||
| every { mockRequestHeadersUriSpec.uri(any<String>()) } returns mockRequestHeadersSpec | ||
| every { mockRequestHeadersSpec.accept(MediaType.APPLICATION_JSON) } returns mockRequestHeadersSpec | ||
| every { mockRequestHeadersSpec.retrieve() } returns mockResponseSpec | ||
| every { mockResponseSpec.hint(any(), any()) } returns mockResponseSpec | ||
| every { mockResponseSpec.body(any<ParameterizedTypeReference<Any>>()) } returns testModels | ||
| } | ||
|
|
||
| @AfterEach | ||
| fun tearDown() { | ||
| unmockkAll() | ||
| } | ||
|
|
||
| @Test | ||
| fun `registered Ollama LLM uses the configured model instead of the mistral default`() { | ||
| // Given - a single discovered model "gemma3:latest" registered via default mode | ||
| val config = createConfig("http://localhost:11434") | ||
| config.ollamaModelsInitializer() | ||
| val llm = registeredBeans["ollamaModel-gemma3-latest"] as SpringAiLlmService | ||
|
sumin220 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // When - per-request options are built exactly as ChatClientLlmOperations does | ||
| // (post-#1818: SpringAiLlmService.convertOptions stamps the registered model | ||
| // name onto the converter output) | ||
| val chatOptions = llm.convertOptions(LlmOptions()) as OllamaChatOptions | ||
|
sumin220 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Then - the request options must target the model the service was registered for, | ||
| // not the OllamaChatOptions "mistral" fallback | ||
| assertEquals("gemma3:latest", chatOptions.model) | ||
| } | ||
|
|
||
| // Helper methods | ||
| private fun createConfig(baseUrl: String) = | ||
| OllamaModelsConfig( | ||
| baseUrl = baseUrl, | ||
| nodeProperties = null, | ||
| configurableBeanFactory = mockBeanFactory, | ||
| properties = mockProperties, | ||
| observationRegistry = mockObservationRegistry, | ||
| restClientBuilder = mockRestClientBuilderProvider, | ||
| ) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.