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 59fcac7

Browse files
Use try-with-resources
1 parent 6a91185 commit 59fcac7

File tree

2 files changed

+57
-55
lines changed

2 files changed

+57
-55
lines changed

‎RagDemo.java‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,26 @@ private static String getContext(String input) throws Exception {
4242
.body(requestBody)
4343
.asString().getBody();
4444

45-
var connection = new Sql2o(
46-
"jdbc:mariadb://127.0.0.1:3306/demo", "root", "password").open();
47-
48-
var table = connection.createQuery("""
49-
SELECT id, CONCAT(
50-
"Product: ", title, ". Stars: ", stars, ". Price: $", price, ". Category: ", category_name,
51-
". Best seller: ", CASE WHEN is_best_seller THEN "Yes" ELSE "No" END
52-
) AS description
53-
FROM products
54-
WHERE embedding IS NOT NULL
55-
ORDER BY VEC_Distance(embedding, VEC_FromText(JSON_EXTRACT(:response, '$.data[0].embedding')))
56-
LIMIT 10
57-
""")
58-
.addParameter("response", response)
59-
.executeAndFetchTable();
60-
61-
return table.rows().stream()
62-
.map(row -> row.getString("description"))
63-
.collect(Collectors.joining("\n\n"));
45+
try (var connection = new Sql2o(
46+
"jdbc:mariadb://127.0.0.1:3306/demo", "root", "password").open()) {
47+
48+
var table = connection.createQuery("""
49+
SELECT id, CONCAT(
50+
"Product: ", title, ". Stars: ", stars, ". Price: $", price, ". Category: ", category_name,
51+
". Best seller: ", CASE WHEN is_best_seller THEN "Yes" ELSE "No" END
52+
) AS description
53+
FROM products
54+
WHERE embedding IS NOT NULL
55+
ORDER BY VEC_Distance(embedding, VEC_FromText(JSON_EXTRACT(:response, '$.data[0].embedding')))
56+
LIMIT 10
57+
""")
58+
.addParameter("response", response)
59+
.executeAndFetchTable();
60+
61+
return table.rows().stream()
62+
.map(row -> row.getString("description"))
63+
.collect(Collectors.joining("\n\n"));
64+
}
6465
}
6566

6667
private static String buildPrompt(String input, Object context) {

‎UpdateVectors.java‎

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,44 @@
1414
public class UpdateVectors {
1515

1616
public static void main(String[] args) throws Exception {
17-
var connection = new Sql2o(
18-
"jdbc:mariadb://127.0.0.1:3306/demo", "root", "password").open();
19-
20-
var table = connection.createQuery("""
21-
SELECT id, CONCAT(
22-
"Product: ", title, ". Stars: ", stars, ". Price: $", price, ". Category: ", category_name,
23-
". Best seller: ", CASE WHEN is_best_seller THEN "Yes" ELSE "No" END
24-
) AS description
25-
FROM products
26-
WHERE embedding IS NULL
27-
""").executeAndFetchTableLazy();
28-
29-
for (Row row : table.rows()) {
30-
var id = row.getString("id");
31-
var description = row.getString("description");
32-
33-
var requestBody = """
34-
{ "model": "bert-cpp-minilm-v6", "input": %s }
35-
""".formatted(new ObjectMapper().writeValueAsString(description));
36-
37-
var response = Unirest.post("http://localhost:8080/v1/embeddings")
38-
.header("Content-Type", "application/json")
39-
.body(requestBody)
40-
.asString().getBody();
41-
42-
connection.createQuery("""
43-
UPDATE products
44-
SET embedding = VEC_FromText(JSON_EXTRACT(:response, '$.data[0].embedding'))
45-
WHERE id = :id
46-
""")
47-
.addParameter("response", response)
48-
.addParameter("id", id)
49-
.executeUpdate();
50-
51-
System.out.println("Updated embedding for product ID: " + id);
17+
try (var connection = new Sql2o(
18+
"jdbc:mariadb://127.0.0.1:3306/demo", "root", "password").open()) {
19+
20+
var table = connection.createQuery("""
21+
SELECT id, CONCAT(
22+
"Product: ", title, ". Stars: ", stars, ". Price: $", price, ". Category: ", category_name,
23+
". Best seller: ", CASE WHEN is_best_seller THEN "Yes" ELSE "No" END
24+
) AS description
25+
FROM products
26+
WHERE embedding IS NULL
27+
""").executeAndFetchTableLazy();
28+
29+
for (Row row : table.rows()) {
30+
var id = row.getString("id");
31+
var description = row.getString("description");
32+
33+
var requestBody = """
34+
{ "model": "bert-cpp-minilm-v6", "input": %s }
35+
""".formatted(new ObjectMapper().writeValueAsString(description));
36+
37+
var response = Unirest.post("http://localhost:8080/v1/embeddings")
38+
.header("Content-Type", "application/json")
39+
.body(requestBody)
40+
.asString().getBody();
41+
42+
connection.createQuery("""
43+
UPDATE products
44+
SET embedding = VEC_FromText(JSON_EXTRACT(:response, '$.data[0].embedding'))
45+
WHERE id = :id
46+
""")
47+
.addParameter("response", response)
48+
.addParameter("id", id)
49+
.executeUpdate();
50+
51+
System.out.println("Updated embedding for product ID: " + id);
52+
}
5253
}
5354

54-
connection.close();
55+
System.out.println("All embeddings updated!");
5556
}
5657
}

0 commit comments

Comments
(0)

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