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.
This commit is contained in:
Sébastien Deleuze
2024-12-24 17:11:58 +01:00
committed by GitHub
parent 4ddd9135f0
commit 7f8bfbcecb
5 changed files with 37 additions and 25 deletions

View File

@@ -12,13 +12,6 @@ import org.springframework.context.annotation.Description
@SpringBootApplication @SpringBootApplication
class KotlinFunctionCallbackApplication { class KotlinFunctionCallbackApplication {
companion object {
@JvmStatic
fun main(args: Array<String>) {
runApplication<KotlinFunctionCallbackApplication>(*args)
}
}
@Bean @Bean
fun init(chatClientBuilder: ChatClient.Builder) = CommandLineRunner { fun init(chatClientBuilder: ChatClient.Builder) = CommandLineRunner {
try { try {
@@ -38,6 +31,7 @@ class KotlinFunctionCallbackApplication {
@Configuration @Configuration
class Config { class Config {
@Bean @Bean
fun weatherFunctionInfo(currentWeather: (WeatherRequest) -> WeatherResponse): FunctionCallback { fun weatherFunctionInfo(currentWeather: (WeatherRequest) -> WeatherResponse): FunctionCallback {
return FunctionCallback.builder() return FunctionCallback.builder()
@@ -55,3 +49,7 @@ class Config {
MockKotlinWeatherService().invoke(request) MockKotlinWeatherService().invoke(request)
} }
} }
fun main(args: Array<String>) {
runApplication<KotlinFunctionCallbackApplication>(*args)
}

View File

@@ -45,21 +45,17 @@ enum class Unit(val unitName: String) {
@JsonInclude(Include.NON_NULL) @JsonInclude(Include.NON_NULL)
@JsonClassDescription("Weather API request") @JsonClassDescription("Weather API request")
data class WeatherRequest( data class WeatherRequest(
@get:JsonProperty(required = true, value = "location")
@get:JsonPropertyDescription("The city and state e.g. San Francisco, CA") @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") @get:JsonPropertyDescription("The city latitude")
val lat: Double = 0.0, val lat: Double,
@get:JsonProperty(required = true, value = "lon")
@get:JsonPropertyDescription("The city longitude") @get:JsonPropertyDescription("The city longitude")
val lon: Double = 0.0, val lon: Double,
@get:JsonProperty(required = true, value = "unit")
@get:JsonPropertyDescription("Temperature unit") @get:JsonPropertyDescription("Temperature unit")
val unit: Unit = Unit.C val unit: Unit
) )

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0-SNAPSHOT</version> <version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.example</groupId> <groupId>com.example</groupId>
@@ -133,15 +133,33 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<repositories> <repositories>
<repository> <repository>
<id>spring-snapshots</id> <id>spring-snapshots</id>
<name>Spring Snapshots</name> <name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url> <url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots> <snapshots>
<enabled>true</enabled> <enabled>false</enabled>
</snapshots> </snapshots>
</repository> </repository>
</repositories> </repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project> </project>

View File

@@ -2,6 +2,7 @@ package com.example.rag_with_kotlin
import org.springframework.ai.chat.client.ChatClient import org.springframework.ai.chat.client.ChatClient
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor 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.document.Document
import org.springframework.ai.vectorstore.VectorStore import org.springframework.ai.vectorstore.VectorStore
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
@@ -49,13 +50,12 @@ fun main(args: Array<String>) {
} }
bean { bean {
val cc = ref<ChatClient>() val cc = ref<ChatClient>()
val dogAdoptionSuggestion = cc cc
.prompt(" do you have any neurotic dogs? ") .prompt(" do you have any neurotic dogs? ")
.call() .call()
.entity<DogAdoptionSuggestion>() .entity<DogAdoptionSuggestion>().also {
println(dogAdoptionSuggestion) print(it)
}
} }
} }
addInitializers(context) addInitializers(context)

View File

@@ -1,5 +1,5 @@
delete from dog; delete from dog;
delete from vector_store ;
INSERT INTO dog (id, name, description, dob, owner, gender, image) VALUES 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'); (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');