Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f2949a

Browse files
Update readme and format code
1 parent f72c21d commit 7f2949a

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

‎README.md‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
1-
# Demo: RAG with MariaDB
1+
# Demo: RAG with MariaDB (Java)
22

33
This demo demonstrates how to build a Retrieval-Augmented Generation (RAG) application
4-
using MariaDBand LocalAI.
4+
using [MariaDB](https://mariadb.com/), [LocalAI](https://localai.io/), and [Java](https://en.wikipedia.org/wiki/Java_(programming_language)).
55

66
**Note:** This demo uses a _preview version_ of MariaDB which includes SQL syntax that
77
will likely change in the next GA (stable) version.
88

99
## Prerequisites
10-
- Docker
10+
11+
You only need [Docker](https://www.docker.com/) installed and running on your computer.
1112

1213
## Setup
1314

1415
Start the LocalAI and MariaDB services:
16+
1517
```shell
1618
docker compose up -d
1719
```
1820

1921
Check the model downloads progress:
22+
2023
```
2124
docker logs -f local-ai
2225
```
2326

2427
Download the dataset:
2528
https://www.kaggle.com/datasets/asaniczka/amazon-canada-products-2023年2月1日m-products
2629

27-
Create a slice of the dataset. For example 50k products:
30+
Create a _slice_ of the dataset. For example 50k products:
31+
2832
```shell
2933
head -n 50001 ~/Datasets/amz_ca_total_products_data_processed.csv > ~/Datasets/slice.csv
3034
```
3135

36+
Copy the file to the Docker container that is running MariaDB:
37+
38+
```shell
39+
docker cp ~/Datasets/slice.csv mariadb:/slice.csv
40+
```
41+
3242
Connect to the MariaDB server:
43+
3344
```shell
3445
docker exec -it mariadb mariadb -u root -p'password' demo
3546
```
3647

3748
Load the data from the CSV file into the MariaDB database:
49+
3850
```sql
39-
LOAD DATA LOCAL INFILE '/Users/alejandro/Datasets/slice.csv'
51+
LOAD DATA LOCAL INFILE '/slice.csv'
4052
INTO TABLE products
4153
FIELDS TERMINATED BY ','
4254
ENCLOSED BY '"'
@@ -57,9 +69,15 @@ IGNORE 1 LINES
5769
);
5870
```
5971

72+
Exit the MariaDB client:
73+
74+
```shell
75+
exit
76+
```
77+
6078
Calculate the vector embeddings:
79+
6180
```shell
62-
cd rag-demo
6381
./UpdateVectors.java
6482
```
6583

‎RagDemo.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//DEPS org.sql2o:sql2o:1.8.0
77
//DEPS org.slf4j:slf4j-simple:2.0.16
88

9+
import com.fasterxml.jackson.core.JsonProcessingException;
910
import com.fasterxml.jackson.databind.ObjectMapper;
1011
import kong.unirest.Unirest;
1112
import org.sql2o.*;
@@ -17,18 +18,19 @@ public class RagDemo {
1718
Unirest.config().socketTimeout(5 * 60 * 1000);
1819
}
1920

20-
public static void main(String[] args) throws Exception {
21+
public static void main(String[] args) throws JsonProcessingException {
2122
var input = System.console().readLine("I'm looking for: ");
2223

2324
System.out.println("Finding closest products...");
2425
var context = getContext(input);
26+
2527
System.out.println("Generating response...");
2628
var prompt = buildPrompt(input, context);
2729
var response = getResponse(prompt);
2830
System.out.println(response);
2931
}
3032

31-
private static String getContext(String input) throws Exception {
33+
private static String getContext(String input) throws JsonProcessingException {
3234
var requestBody = """
3335
{
3436
"model": "bert-cpp-minilm-v6",
@@ -71,7 +73,7 @@ private static String buildPrompt(String input, Object context) {
7173
""".formatted(input, context);
7274
}
7375

74-
private static String getResponse(String prompt) throws Exception {
76+
private static String getResponse(String prompt) throws JsonProcessingException {
7577
var requestBody = """
7678
{
7779
"model": "phi-2-chat",

‎UpdateVectors.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ SELECT id, CONCAT(
3131
var description = row.getString("description");
3232

3333
var requestBody = """
34-
{ "model": "bert-cpp-minilm-v6", "input": %s }
34+
{
35+
"model": "bert-cpp-minilm-v6",
36+
"input": %s
37+
}
3538
""".formatted(new ObjectMapper().writeValueAsString(description));
3639

3740
var response = Unirest.post("http://localhost:8080/v1/embeddings")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /