Skip to content

Commit 5e1f681

Browse files
sobychackomarkpollack
authored andcommitted
Fix Ollama Kotlin function callback integration tests
- Add default values to KotlinRequest data class to fix Jackson deserialization - Replace hardcoded model name with OllamaModel.LLAMA3_2 for consistency - Extract common test setup into companion objects for Ollama tests - Replace var with val using when expression in weather service - Clean up imports and remove unused annotations - Standardize property access using Kotlin conventions
1 parent 634ac20 commit 5e1f681

File tree

3 files changed

+61
-38
lines changed

3 files changed

+61
-38
lines changed

spring-ai-spring-boot-autoconfigure/src/test/kotlin/org/springframework/ai/autoconfigure/ollama/tool/FunctionCallbackContextKotlinIT.kt

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,51 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
package org.springframework.ai.autoconfigure.ollama.tool
1918

2019
import org.assertj.core.api.Assertions.assertThat
20+
import org.junit.jupiter.api.BeforeAll
2121
import org.junit.jupiter.api.Test
22-
import org.junit.jupiter.api.condition.DisabledIf
2322
import org.slf4j.LoggerFactory
23+
2424
import org.springframework.ai.autoconfigure.ollama.BaseOllamaIT
2525
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration
2626
import org.springframework.ai.chat.messages.UserMessage
2727
import org.springframework.ai.chat.prompt.Prompt
2828
import org.springframework.ai.model.function.FunctionCallingOptions
2929
import org.springframework.ai.ollama.OllamaChatModel
30+
import org.springframework.ai.ollama.api.OllamaModel
3031
import org.springframework.ai.ollama.api.OllamaOptions
3132
import org.springframework.boot.autoconfigure.AutoConfigurations
3233
import org.springframework.boot.test.context.runner.ApplicationContextRunner
3334
import org.springframework.context.annotation.Bean
3435
import org.springframework.context.annotation.Configuration
3536
import org.springframework.context.annotation.Description
36-
import org.testcontainers.junit.jupiter.Testcontainers
3737

3838
class FunctionCallbackContextKotlinIT : BaseOllamaIT() {
3939

40-
private val logger = LoggerFactory.getLogger(FunctionCallbackContextKotlinIT::class.java)
40+
companion object {
41+
42+
private val MODEL_NAME = OllamaModel.LLAMA3_2.getName();
43+
44+
@JvmStatic
45+
@BeforeAll
46+
fun beforeAll() {
47+
initializeOllama(MODEL_NAME)
48+
}
49+
}
4150

42-
private val MODEL_NAME = "qwen2.5:3b"
51+
private val logger = LoggerFactory.getLogger(FunctionCallbackContextKotlinIT::class.java)
4352

44-
val contextRunner = buildOllamaApiWithModel(MODEL_NAME).let { baseUrl ->
45-
ApplicationContextRunner().withPropertyValues(
46-
"spring.ai.ollama.baseUrl=$baseUrl",
53+
private val contextRunner = ApplicationContextRunner()
54+
.withPropertyValues(
55+
"spring.ai.ollama.baseUrl=${getBaseUrl()}",
4756
"spring.ai.ollama.chat.options.model=$MODEL_NAME",
4857
"spring.ai.ollama.chat.options.temperature=0.5",
4958
"spring.ai.ollama.chat.options.topK=10"
5059
)
51-
.withConfiguration(AutoConfigurations.of(OllamaAutoConfiguration::class.java))
52-
.withUserConfiguration(Config::class.java)
53-
}
60+
.withConfiguration(AutoConfigurations.of(OllamaAutoConfiguration::class.java))
61+
.withUserConfiguration(Config::class.java)
5462

5563
@Test
5664
fun functionCallTest() {
@@ -98,18 +106,13 @@ class FunctionCallbackContextKotlinIT : BaseOllamaIT() {
98106
@Bean
99107
@Description("Find the weather conditions, forecasts, and temperatures for a location, like a city or state.")
100108
open fun weatherInfo(): (KotlinRequest) -> KotlinResponse = { request ->
101-
var temperature = 10.0
102-
if (request.location.contains("Paris")) {
103-
temperature = 15.0
109+
val temperature = when {
110+
request.location.contains("Paris") -> 15.0
111+
request.location.contains("Tokyo") -> 10.0
112+
request.location.contains("San Francisco") -> 30.0
113+
else -> 10.0
104114
}
105-
else if (request.location.contains("Tokyo")) {
106-
temperature = 10.0
107-
}
108-
else if (request.location.contains("San Francisco")) {
109-
temperature = 30.0
110-
}
111-
KotlinResponse(temperature, 15.0, 20.0, 2.0, 53, 45, Unit.C);
115+
KotlinResponse(temperature, 15.0, 20.0, 2.0, 53, 45, Unit.C)
112116
}
113-
114117
}
115118
}

spring-ai-spring-boot-autoconfigure/src/test/kotlin/org/springframework/ai/autoconfigure/ollama/tool/FunctionCallbackWrapperKotlinIT.kt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
package org.springframework.ai.autoconfigure.ollama.tool
1918

2019
import org.assertj.core.api.Assertions.assertThat
20+
import org.junit.jupiter.api.BeforeAll
2121
import org.junit.jupiter.api.Test
22-
import org.junit.jupiter.api.condition.DisabledIf
2322
import org.slf4j.LoggerFactory
23+
2424
import org.springframework.ai.autoconfigure.ollama.BaseOllamaIT
2525
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration
2626
import org.springframework.ai.chat.messages.UserMessage
@@ -29,29 +29,37 @@ import org.springframework.ai.model.function.FunctionCallback
2929
import org.springframework.ai.model.function.FunctionCallbackWrapper
3030
import org.springframework.ai.model.function.FunctionCallingOptions
3131
import org.springframework.ai.ollama.OllamaChatModel
32+
import org.springframework.ai.ollama.api.OllamaModel
3233
import org.springframework.ai.ollama.api.OllamaOptions
3334
import org.springframework.boot.autoconfigure.AutoConfigurations
3435
import org.springframework.boot.test.context.runner.ApplicationContextRunner
3536
import org.springframework.context.annotation.Bean
3637
import org.springframework.context.annotation.Configuration
37-
import org.testcontainers.junit.jupiter.Testcontainers
3838

3939
class FunctionCallbackWrapperKotlinIT : BaseOllamaIT() {
4040

41-
private val logger = LoggerFactory.getLogger(FunctionCallbackWrapperKotlinIT::class.java)
41+
companion object {
4242

43-
private val MODEL_NAME = "qwen2.5:3b"
43+
private val MODEL_NAME = OllamaModel.LLAMA3_2.getName();
4444

45-
val contextRunner = buildOllamaApiWithModel(MODEL_NAME).let { baseUrl ->
46-
ApplicationContextRunner().withPropertyValues(
47-
"spring.ai.ollama.baseUrl=$baseUrl",
45+
@JvmStatic
46+
@BeforeAll
47+
fun beforeAll() {
48+
initializeOllama(MODEL_NAME)
49+
}
50+
}
51+
52+
private val logger = LoggerFactory.getLogger(FunctionCallbackWrapperKotlinIT::class.java)
53+
54+
private val contextRunner = ApplicationContextRunner()
55+
.withPropertyValues(
56+
"spring.ai.ollama.baseUrl=${getBaseUrl()}",
4857
"spring.ai.ollama.chat.options.model=$MODEL_NAME",
4958
"spring.ai.ollama.chat.options.temperature=0.5",
5059
"spring.ai.ollama.chat.options.topK=10"
5160
)
52-
.withConfiguration(AutoConfigurations.of(OllamaAutoConfiguration::class.java))
53-
.withUserConfiguration(Config::class.java)
54-
}
61+
.withConfiguration(AutoConfigurations.of(OllamaAutoConfiguration::class.java))
62+
.withUserConfiguration(Config::class.java)
5563

5664
@Test
5765
fun functionCallTest() {

spring-ai-spring-boot-autoconfigure/src/test/kotlin/org/springframework/ai/autoconfigure/ollama/tool/MockKotlinWeatherService.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,24 @@ enum class Unit(val unitName: String) {
6060
*/
6161
@JsonInclude(Include.NON_NULL)
6262
@JsonClassDescription("Weather API request")
63-
data class KotlinRequest(@get:JsonProperty(required = true, value = "location") @get:JsonPropertyDescription("The city and state e.g. San Francisco, CA") val location: String,
64-
@get:JsonProperty(required = true, value = "lat") @get:JsonPropertyDescription("The city latitude") val lat: Double,
65-
@get:JsonProperty(required = true, value = "lon") @get:JsonPropertyDescription("The city longitude") val lon: Double,
66-
@get:JsonProperty(required = true, value = "unit") @get:JsonPropertyDescription("Temperature unit") val unit: Unit) {
63+
data class KotlinRequest(
64+
@get:JsonProperty(required = true, value = "location")
65+
@get:JsonPropertyDescription("The city and state e.g. San Francisco, CA")
66+
val location: String = "",
67+
68+
@get:JsonProperty(required = true, value = "lat")
69+
@get:JsonPropertyDescription("The city latitude")
70+
val lat: Double = 0.0,
71+
72+
@get:JsonProperty(required = true, value = "lon")
73+
@get:JsonPropertyDescription("The city longitude")
74+
val lon: Double = 0.0,
75+
76+
@get:JsonProperty(required = true, value = "unit")
77+
@get:JsonPropertyDescription("Temperature unit")
78+
val unit: Unit = Unit.C
79+
)
6780

68-
}
6981

7082
/**
7183
* Weather Function response.

0 commit comments

Comments
 (0)