From 7f8bfbcecb787230b0678c34e2d7589b91117fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 24 Dec 2024 17:11:58 +0100 Subject: [PATCH] Kotlin examples update (#5) * Update kotlin-function-callback Leverage Spring AI 1.0.0-M5 new Kotlin reflection capabilities. * Fix rag-with-kotlin Leverage Spring AI 1.0.0-M5 new Kotlin reflection capabilities. --- .../KotlinFunctionCallbackApplication.kt | 12 ++++----- .../MockKotlinWeatherService.kt | 12 +++------ kotlin/rag-with-kotlin/pom.xml | 26 ++++++++++++++++--- .../RagWithKotlinApplication.kt | 10 +++---- .../src/main/resources/data.sql | 2 +- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/KotlinFunctionCallbackApplication.kt b/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/KotlinFunctionCallbackApplication.kt index dbfffd9..ec1a1fe 100644 --- a/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/KotlinFunctionCallbackApplication.kt +++ b/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/KotlinFunctionCallbackApplication.kt @@ -12,13 +12,6 @@ import org.springframework.context.annotation.Description @SpringBootApplication class KotlinFunctionCallbackApplication { - companion object { - @JvmStatic - fun main(args: Array) { - runApplication(*args) - } - } - @Bean fun init(chatClientBuilder: ChatClient.Builder) = CommandLineRunner { try { @@ -38,6 +31,7 @@ class KotlinFunctionCallbackApplication { @Configuration class Config { + @Bean fun weatherFunctionInfo(currentWeather: (WeatherRequest) -> WeatherResponse): FunctionCallback { return FunctionCallback.builder() @@ -55,3 +49,7 @@ class Config { MockKotlinWeatherService().invoke(request) } } + +fun main(args: Array) { + runApplication(*args) +} diff --git a/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/MockKotlinWeatherService.kt b/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/MockKotlinWeatherService.kt index ed2e251..32b9686 100644 --- a/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/MockKotlinWeatherService.kt +++ b/kotlin/kotlin-function-callback/src/main/kotlin/com/example/kotlin_function_callback/MockKotlinWeatherService.kt @@ -45,21 +45,17 @@ enum class Unit(val unitName: String) { @JsonInclude(Include.NON_NULL) @JsonClassDescription("Weather API request") data class WeatherRequest( - @get:JsonProperty(required = true, value = "location") @get:JsonPropertyDescription("The city and state e.g. San Francisco, CA") - val location: String = "", + val location: String, - @get:JsonProperty(required = true, value = "lat") @get:JsonPropertyDescription("The city latitude") - val lat: Double = 0.0, + val lat: Double, - @get:JsonProperty(required = true, value = "lon") @get:JsonPropertyDescription("The city longitude") - val lon: Double = 0.0, + val lon: Double, - @get:JsonProperty(required = true, value = "unit") @get:JsonPropertyDescription("Temperature unit") - val unit: Unit = Unit.C + val unit: Unit ) diff --git a/kotlin/rag-with-kotlin/pom.xml b/kotlin/rag-with-kotlin/pom.xml index 3543577..61cbfdc 100644 --- a/kotlin/rag-with-kotlin/pom.xml +++ b/kotlin/rag-with-kotlin/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.4.0-SNAPSHOT + 3.3.5 com.example @@ -133,15 +133,33 @@ - + spring-snapshots Spring Snapshots https://repo.spring.io/snapshot + + false + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone - true + false - + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + diff --git a/kotlin/rag-with-kotlin/src/main/kotlin/com/example/rag_with_kotlin/RagWithKotlinApplication.kt b/kotlin/rag-with-kotlin/src/main/kotlin/com/example/rag_with_kotlin/RagWithKotlinApplication.kt index 0c14a14..e81c517 100644 --- a/kotlin/rag-with-kotlin/src/main/kotlin/com/example/rag_with_kotlin/RagWithKotlinApplication.kt +++ b/kotlin/rag-with-kotlin/src/main/kotlin/com/example/rag_with_kotlin/RagWithKotlinApplication.kt @@ -2,6 +2,7 @@ package com.example.rag_with_kotlin import org.springframework.ai.chat.client.ChatClient import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor +import org.springframework.ai.chat.client.entity import org.springframework.ai.document.Document import org.springframework.ai.vectorstore.VectorStore import org.springframework.boot.autoconfigure.SpringBootApplication @@ -49,13 +50,12 @@ fun main(args: Array) { } bean { val cc = ref() - val dogAdoptionSuggestion = cc + cc .prompt(" do you have any neurotic dogs? ") .call() - .entity() - println(dogAdoptionSuggestion) - - + .entity().also { + print(it) + } } } addInitializers(context) diff --git a/kotlin/rag-with-kotlin/src/main/resources/data.sql b/kotlin/rag-with-kotlin/src/main/resources/data.sql index de71455..eef3735 100644 --- a/kotlin/rag-with-kotlin/src/main/resources/data.sql +++ b/kotlin/rag-with-kotlin/src/main/resources/data.sql @@ -1,5 +1,5 @@ delete from dog; -delete from vector_store ; + INSERT INTO dog (id, name, description, dob, owner, gender, image) VALUES (97, 'Rocky', 'A brown Chihuahua known for being protective.', '2019-01-28', NULL, 'm', 'https://raw.githubusercontent.com/joshlong-attic/dog-images/main/chihuahua-1.png');