Skip to content

Commit 63a9fe5

Browse files
authored
update: updating Spring AI tutorial for 1.0.0 release (#4889)
* update: updating docs for 1.0.0 spring ai version * update: update spring ai tutorial with 1.0.0 changes * adding updates * fixing indendation * adding java 17 tip to spring ai tutorial
1 parent 2ce8daf commit 63a9fe5

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

docs/topics/spring-ai-guide.md

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Create a new Spring Boot project in IntelliJ IDEA Ultimate Edition:
5959
6060
* **Java**: 17
6161
62+
> If you don't have Java 17 installed, you can download it from the JDK dropdown list.
63+
>
64+
{style="tip"}
65+
6266
![Create Spring Boot project](create-spring-ai-project.png){width=800}
6367

6468
4. Make sure that you have specified all the fields and click **Next**.
@@ -100,10 +104,10 @@ The generated Gradle project corresponds to the Maven's standard directory layou
100104
}
101105
```
102106
103-
2. Update your `springAiVersion` to `1.0.0-M6`:
107+
2. Set `springAiVersion` to `1.0.0`:
104108
105109
```kotlin
106-
extra["springAiVersion"] = "1.0.0-M6"
110+
extra["springAiVersion"] = "1.0.0"
107111
```
108112
109113
3. Click the **Sync Gradle Changes** button to synchronize the Gradle files.
@@ -122,7 +126,7 @@ The generated Gradle project corresponds to the Maven's standard directory layou
122126
```
123127
124128
> Set your OpenAI API key to the `spring.ai.openai.api-key` property.
125-
>
129+
>
126130
{style="note"}
127131
128132
5. Run the `SpringAiDemoApplication.kt` file to start the Spring Boot application.
@@ -153,9 +157,6 @@ Create a Spring `@RestController` to search documents and store them in the Qdra
153157
import kotlin.uuid.ExperimentalUuidApi
154158
import kotlin.uuid.Uuid
155159
156-
// Data class representing the chat request payload
157-
data class ChatRequest(val query: String, val topK: Int = 3)
158-
159160
@RestController
160161
@RequestMapping("/kotlin")
161162
class KotlinSTDController(
@@ -249,7 +250,7 @@ Create a Spring `@RestController` to search documents and store them in the Qdra
249250
![GET request results](spring-ai-get-results.png){width="700"}
250251

251252
> You can also view the results on the [Qdrant collections](http://localhost:6333/dashboard#/collections) page.
252-
>
253+
>
253254
{style="tip"}
254255

255256
## Implement an AI chat endpoint
@@ -260,16 +261,21 @@ Once the documents are loaded, the final step is to add an endpoint that answers
260261
261262
```kotlin
262263
import org.springframework.ai.chat.client.ChatClient
263-
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor
264264
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor
265+
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor
265266
import org.springframework.ai.chat.prompt.Prompt
266267
import org.springframework.ai.chat.prompt.PromptTemplate
267-
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer
268-
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever
269268
import org.springframework.web.bind.annotation.RequestBody
270269
```
271270
272-
2. Add `ChatClient.Builder` to the controller's constructor parameters:
271+
2. Define a `ChatRequest` data class:
272+
273+
```kotlin
274+
// Represents the request payload for chat queries
275+
data class ChatRequest(val query: String, val topK: Int = 3)
276+
```
277+
278+
3. Add `ChatClient.Builder` to the controller's constructor parameters:
273279

274280
```kotlin
275281
class KotlinSTDController(
@@ -279,56 +285,53 @@ Once the documents are loaded, the final step is to add an endpoint that answers
279285
)
280286
```
281287

282-
3. Inside the controller class, create a `ChatClient` instance and a query transformer:
288+
4. Inside the controller class, create a `ChatClient` instance:
283289

284290
```kotlin
285291
// Builds the chat client with a simple logging advisor
286292
private val chatClient = chatClientBuilder.defaultAdvisors(SimpleLoggerAdvisor()).build()
287-
// Builds the query transformer used to rewrite the input query
288-
private val rqtBuilder = RewriteQueryTransformer.builder().chatClientBuilder(chatClientBuilder)
289293
```
290294

291-
4. At the bottom of your `KotlinSTDController.kt` file, add a new `chatAsk()` endpoint, with the following logic:
295+
5. At the bottom of your `KotlinSTDController.kt` file, add a new `chatAsk()` endpoint, with the following logic:
292296
293297
```kotlin
294-
@PostMapping("/chat/ask")
295-
fun chatAsk(@RequestBody request: ChatRequest): String? {
296-
// Defines the prompt template with placeholders
297-
val promptTemplate = PromptTemplate(
298-
"""
299-
{query}.
300-
Please provide a concise answer based on the {target} documentation.
301-
""".trimIndent()
298+
@PostMapping("/chat/ask")
299+
fun chatAsk(@RequestBody request: ChatRequest): String? {
300+
// Defines the prompt template with placeholders
301+
val promptTemplate = PromptTemplate(
302+
"""
303+
{query}.
304+
Please provide a concise answer based on the "Kotlin standard library" documentation.
305+
""".trimIndent()
306+
)
307+
308+
// Creates the prompt by substituting placeholders with actual values
309+
val prompt: Prompt =
310+
promptTemplate.create(mapOf("query" to request.query))
311+
312+
// Configures the retrieval advisor to augment the query with relevant documents
313+
val retrievalAdvisor = QuestionAnswerAdvisor.builder(vectorStore)
314+
.searchRequest(
315+
SearchRequest.builder()
316+
.similarityThreshold(0.7)
317+
.topK(request.topK)
318+
.build()
302319
)
303-
304-
// Creates the prompt by substituting placeholders with actual values
305-
val prompt: Prompt =
306-
promptTemplate.create(mapOf("query" to request.query, "target" to "Kotlin standard library"))
307-
308-
// Configures the retrieval advisor to augment the query with relevant documents
309-
val retrievalAdvisor = RetrievalAugmentationAdvisor.builder()
310-
.documentRetriever(
311-
VectorStoreDocumentRetriever.builder()
312-
.similarityThreshold(0.7)
313-
.topK(request.topK)
314-
.vectorStore(vectorStore)
315-
.build()
316-
)
317-
.queryTransformers(rqtBuilder.promptTemplate(promptTemplate).build())
318-
.build()
319-
320-
// Sends the prompt to the LLM with the retrieval advisor and get the response
321-
val response = chatClient.prompt(prompt)
322-
.advisors(retrievalAdvisor)
323-
.call()
324-
.content()
325-
logger.info("Chat response generated for query: '${request.query}'")
326-
return response
327-
}
320+
.promptTemplate(promptTemplate)
321+
.build()
322+
323+
// Sends the prompt to the LLM with the retrieval advisor and retrieves the generated content
324+
val response = chatClient.prompt(prompt)
325+
.advisors(retrievalAdvisor)
326+
.call()
327+
.content()
328+
logger.info("Chat response generated for query: '${request.query}'")
329+
return response
330+
}
328331
```
329332
330-
5. Run the application.
331-
6. In the terminal, send a POST request to the new endpoint to see the results:
333+
6. Run the application.
334+
7. In the terminal, send a POST request to the new endpoint to see the results:
332335
333336
```bash
334337
curl -X POST "http://localhost:8080/kotlin/chat/ask" \
@@ -342,5 +345,5 @@ Congratulations! You now have a Kotlin app that connects to OpenAI and answers q
342345
documentation stored in Qdrant.
343346
Try experimenting with different queries or importing other documents to explore more possibilities.
344347
345-
You can view the completed project in the [Spring AI demo GitHub repository](https://github.com/Kotlin/Kotlin-AI-Examples/tree/master/projects/spring-ai/springAI-demo),
348+
You can view the completed project in the [Spring AI demo GitHub repository](https://github.com/Kotlin/Kotlin-AI-Examples/tree/master/projects/spring-ai/springAI-demo),
346349
or explore other Spring AI examples in [Kotlin AI Examples](https://github.com/Kotlin/Kotlin-AI-Examples/tree/master).

0 commit comments

Comments
 (0)