You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Exercise - Develop a RAG application with Spring AI and Azure OpenAI
4
4
metadata:
5
5
title: Exercise - Develop a RAG Application with Spring AI and Azure OpenAI
6
-
description: Learn how to develop a Spring AI application integrated with Azure OpenAI for retrieval augmented generation (RAG) and implement an EvaluatorOptimizer Agent to iteratively refine AI-generated content.
6
+
description: Learn how to develop a Spring AI application integrated with Azure OpenAI for retrieval augmented generation (RAG) and implement an Evaluator-Optimizer agent to iteratively refine AI-generated content.
Copy file name to clipboardExpand all lines: learn-pr/azure/build-enterprise-ai-agents-with-java-spring/includes/1-introduction.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ A RAG application typically has the following capabilities:
21
21
22
22
## Agents and workflows
23
23
24
-
Spring AI also empowers you to build dynamic AI agents and orchestrated workflows. While various patterns exist - like chain, parallelization and routing - this module focuses exclusively on the *Evaluator-Optimizer* workflow.
24
+
Spring AI also empowers you to build dynamic AI agents and orchestrated workflows. Although various patterns exist - like chain, parallelization and routing - this module focuses exclusively on the *Evaluator-Optimizer* workflow.
25
25
26
26
This pattern iteratively refines outputs through a generator-evaluator loop, ensuring high-quality and adaptive AI responses.
Copy file name to clipboardExpand all lines: learn-pr/azure/build-enterprise-ai-agents-with-java-spring/includes/2-pgvector-azure-postgresql-setup.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
## What is Vector Search Similarity?
1
+
## What is vector similarity search?
2
2
3
-
Vector Search Similarity is a technique used to find similar items in a dataset by comparing their vector representations. Vector similarity is commonly calculated using distance metrics, such as Euclidean distance or `cosine` similarity. In the context of AI and machine learning, vector representations or embeddings are numerical representations of data points, such as words, images, or user queries. These vectors capture the semantic meaning of the data points, enabling efficient similarity searches.
3
+
Vector similarity search is a technique used to find similar items in a dataset by comparing their vector representations. Vector similarity is commonly calculated using distance metrics, such as Euclidean distance or `cosine` similarity. In the context of AI and machine learning, vector representations or embeddings are numerical representations of data points, such as words, images, or user queries. These vectors capture the semantic meaning of the data points, enabling efficient similarity searches.
4
4
5
5
For example, in a question-answering system, user queries can be converted into vector embeddings. These embeddings can then be compared to a database of precomputed embeddings of previous prompts and answers to find the most similar ones. This process is known as vector similarity search.
6
6
@@ -48,6 +48,6 @@ On startup, Spring Boot installs the required extensions and creates the require
48
48
49
49
## Unit summary
50
50
51
-
Vector Search Similarity and the `pgvector` extension provide powerful capabilities for building AI-powered applications that require semantic search functionality. By using these tools in Azure PostgreSQL, you can efficiently store, index, and query vector embeddings, enabling advanced search and retrieval features in your applications.
51
+
Vector similarity search and the `pgvector` extension provide powerful capabilities for building AI-powered applications that require semantic search functionality. By using these tools in Azure PostgreSQL, you can efficiently store, index, and query vector embeddings, enabling advanced search and retrieval features in your applications.
52
52
53
53
In the next unit, you perform the steps to create and configure a PostgreSQL database, enable the `pgvector` extension, and create required tables for use with Spring AI.
Copy file name to clipboardExpand all lines: learn-pr/azure/build-enterprise-ai-agents-with-java-spring/includes/4-spring-ai-development.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
In this unit, you learn how to implement retrieval-augmented generation (RAG) in a Spring Boot application using vector search similarity provided by the `pgvector` extension in a PostgreSQL database. We introduce relevant RAG concepts such as embeddings, embedding models, and vector stores. By the end of this unit, you should understand how to apply these tools to enhance your applications with advanced search and retrieval capabilities.
1
+
In this unit, you learn how to implement retrieval-augmented generation (RAG) in a Spring Boot application using vector similarity search provided by the `pgvector` extension in a PostgreSQL database. We introduce relevant RAG concepts such as embeddings, embedding models, and vector stores. By the end of this unit, you should understand how to apply these tools to enhance your applications with advanced search and retrieval capabilities.
The generated Spring Boot starter project includes the following configurations and dependencies:
95
95
96
-
- Spring Boot Version: 3.4.3
97
-
- Java Version: 17
96
+
- Spring Boot version: 3.4.3
97
+
- Java version: 17
98
98
- Dependencies:
99
99
-`web`: adds support for building web applications, including RESTful services using Spring Model View Controller (MVC).
100
100
-`jdbc`: provides Java Database Connectivity (JDBC) support for database access.
101
101
-`azure-support`: adds support for integrating with Azure services.
102
102
-`spring-ai-azure-openai`: adds support for integrating with Azure OpenAI services.
103
103
-`spring-ai-vectordb-pgvector`: adds support for using `pgvector`, a PostgreSQL extension for vector embeddings.
104
104
105
-
Unzip the downloaded file using the command:
105
+
Use the following commands to unzip the downloaded file and navigate to the created directory:
106
106
107
107
```bash
108
108
unzip -u spring-ai-app.zip -d spring-ai-app
109
-
```
110
-
111
-
Switch directory to this path:
112
-
113
-
```bash
114
109
cd spring-ai-app
115
110
```
116
111
117
-
You need to change the **pom.xml** file to include a dependency for passwordless authentication for PostgreSQL.
118
-
119
-
Open the **pom.xml** file, locate the `<dependencies>` section and add the following dependency:
112
+
Next, you need to change the **pom.xml** file to include a dependency for passwordless authentication for PostgreSQL. Open the **pom.xml** file, locate the `<dependencies>` section, and then add the following dependency:
@@ -210,9 +203,9 @@ az postgres flexible-server show \
210
203
--output tsv
211
204
```
212
205
213
-
#### Review the application.properties file
206
+
#### Update the application.properties file
214
207
215
-
Locate and open the **application.properties** file in the **src/main/resources** directory. There are three properties that are initialized using values from the following environment variables: `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_ENDPOINT`, and `PGHOST`:
208
+
Locate and open the **application.properties** file in the **src/main/resources** directory. There are three properties that are initialized using values from the following environment variables: `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_ENDPOINT`, and `PGHOST`. Replace the file's content with the following content:
Within the **service** directory, create a new file named **RagService.java**with the following content:
228
+
Within the **service** directory, create a new file named **RagService.java**and add the following code:
236
229
237
230
```java
238
231
packagecom.example.springaiapp.service;
@@ -298,7 +291,7 @@ This code implements RAG by generating an answer to a given query, augmenting th
298
291
299
292
#### Create the RAG controller
300
293
301
-
Next, you need to expose a REST endpoint for your RAG application. Create a new file named **RagController.java** within the **controller** directory:
294
+
Next, you need to expose a REST endpoint for your RAG application. Create a new file named **RagController.java** within the **controller** directory, and then add the following code:
302
295
303
296
```java
304
297
packagecom.example.springaiapp.controller;
@@ -323,7 +316,7 @@ public class RagController {
323
316
324
317
#### Test the RAG application
325
318
326
-
With these changes in place, test the implementation by using the following command:
319
+
With these changes in place, use the following command to compile and run the code:
327
320
328
321
```bash
329
322
mvn spring-boot:run
@@ -332,7 +325,8 @@ mvn spring-boot:run
332
325
Test the new REST endpoint either from a browser or by using the following command:
333
326
334
327
```bash
335
-
curl -G "http://localhost:8080/api/rag" --data-urlencode "query=What is pgvector?"
328
+
curl -G "http://localhost:8080/api/rag" \
329
+
--data-urlencode "query=What is pgvector?"
336
330
```
337
331
338
332
You should see a valid response similar to the following example:
@@ -345,14 +339,15 @@ and querying of vector embeddings within a PostgreSQL database.
345
339
Next, try the following command:
346
340
347
341
```bash
348
-
curl -G "http://localhost:8080/api/rag" --data-urlencode "query=How does QuestionAnswerAdvisor work in Spring AI?"
342
+
curl -G "http://localhost:8080/api/rag" \
343
+
--data-urlencode "query=How does QuestionAnswerAdvisor work in Spring AI?"
349
344
```
350
345
351
-
While the answer might appear valid, it might include phrasing that indicates it's a reasoned guess.
346
+
Although the answer might appear valid, it might include phrasing that indicates it's a reasoned guess.
352
347
353
348
#### Test the application with extra knowledge
354
349
355
-
Next, provide extra knowledge by adding the following documents to the vector store. Create a new file named **DocumentService.java** within the **service** directory, and then add the following contents:
350
+
Next, provide extra knowledge by adding documents to the vector store. Create a new file named **DocumentService.java** within the **service** directory, and then add the following code:
356
351
357
352
```java
358
353
packagecom.example.springaiapp.service;
@@ -410,7 +405,7 @@ public class DocumentService {
410
405
}
411
406
```
412
407
413
-
Test these changes by using the following commands:
408
+
To test these changes, use the following command to compile and run the code:
414
409
415
410
```bash
416
411
mvn spring-boot:run
@@ -419,7 +414,8 @@ mvn spring-boot:run
419
414
Then, ask a question by using the following command:
420
415
421
416
```bash
422
-
curl -G "http://localhost:8080/api/rag" --data-urlencode "query=How does QuestionAnswerAdvisor work in Spring AI?"
417
+
curl -G "http://localhost:8080/api/rag" \
418
+
--data-urlencode "query=How does QuestionAnswerAdvisor work in Spring AI?"
423
419
```
424
420
425
421
You should now see an answer that clearly explains the role of `QuestionAnswerAdvisor` within Spring AI.
0 commit comments