Skip to content

Commit 2e31333

Browse files
committed
Books demo with vector similarity search
1 parent 6bd3a8e commit 2e31333

2 files changed

Lines changed: 56 additions & 19 deletions

File tree

  • samples/semantickernel-learn-resources/src/main/java/com/microsoft/semantickernel/samples/documentationexamples/data/vectorstores/oracle

samples/semantickernel-learn-resources/src/main/java/com/microsoft/semantickernel/samples/documentationexamples/data/vectorstores/oracle/Book.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordData;
55
import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordKey;
66
import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordVector;
7-
import com.microsoft.semantickernel.data.vectorstorage.definition.DistanceFunction;
8-
import com.microsoft.semantickernel.data.vectorstorage.definition.IndexKind;
97
import java.util.List;
108

119
public class Book {
1210

11+
public Book() {}
12+
1313
public Book(String isbn, String title, String author, int pages,
1414
List<String> tags, String summary, List<Float> summaryEmbedding) {
1515
this.isbn = isbn;
@@ -21,27 +21,26 @@ public Book(String isbn, String title, String author, int pages,
2121
this.summaryEmbedding = summaryEmbedding;
2222
}
2323

24-
2524
@VectorStoreRecordKey
26-
private final String isbn;
25+
private String isbn;
2726

2827
@VectorStoreRecordData(isFilterable = true)
29-
private final String title;
28+
private String title;
3029

3130
@VectorStoreRecordData(isFilterable = true)
32-
private final String author;
31+
private String author;
3332

3433
@VectorStoreRecordData
35-
private final int pages;
34+
private int pages;
3635

3736
@VectorStoreRecordData(isFilterable = true)
38-
private final List<String> tags;
37+
private List<String> tags;
3938

4039
@VectorStoreRecordData( isFilterable = true, isFullTextSearchable = true )
41-
private final String summary;
40+
private String summary;
4241

43-
@VectorStoreRecordVector(dimensions = 4, distanceFunction = DistanceFunction.COSINE_DISTANCE, indexKind = IndexKind.HNSW)
44-
private final List<Float> summaryEmbedding;
42+
@VectorStoreRecordVector(dimensions = 2)
43+
private List<Float> summaryEmbedding;
4544

4645
public String getIsbn() {
4746
return isbn;
@@ -70,4 +69,32 @@ public String getSummary() {
7069
public List<Float> getSummaryEmbedding() {
7170
return summaryEmbedding;
7271
}
72+
73+
public void setIsbn(String isbn) {
74+
this.isbn = isbn;
75+
}
76+
77+
public void setTitle(String title) {
78+
this.title = title;
79+
}
80+
81+
public void setAuthor(String author) {
82+
this.author = author;
83+
}
84+
85+
public void setPages(int pages) {
86+
this.pages = pages;
87+
}
88+
89+
public void setTags(List<String> tags) {
90+
this.tags = tags;
91+
}
92+
93+
public void setSummaryEmbedding(List<Float> summaryEmbedding) {
94+
this.summaryEmbedding = summaryEmbedding;
95+
}
96+
97+
public void setSummary(String summary) {
98+
this.summary = summary;
99+
}
73100
}

samples/semantickernel-learn-resources/src/main/java/com/microsoft/semantickernel/samples/documentationexamples/data/vectorstores/oracle/Main.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import com.microsoft.semantickernel.data.jdbc.JDBCVectorStoreOptions;
66
import com.microsoft.semantickernel.data.jdbc.JDBCVectorStoreRecordCollectionOptions;
77
import com.microsoft.semantickernel.data.jdbc.oracle.OracleVectorStoreQueryProvider;
8+
import com.microsoft.semantickernel.data.vectorsearch.VectorSearchResults;
89
import com.microsoft.semantickernel.data.vectorstorage.VectorStoreRecordCollection;
910
import java.sql.SQLException;
1011
import java.util.Arrays;
1112
import java.util.List;
13+
import com.microsoft.semantickernel.data.vectorstorage.options.VectorSearchOptions;
1214
import oracle.jdbc.datasource.impl.OracleDataSource;
15+
import reactor.core.publisher.Mono;
1316

1417
public class Main {
1518
public static void main(String[] args) throws SQLException {
@@ -45,23 +48,30 @@ public static void main(String[] args) throws SQLException {
4548
collection.upsertBatchAsync(books, null).block();
4649

4750
// Retrieve the upserted record.
48-
//var retrievedBook = collection.getAsync("1", null).block();
51+
Book retrievedBook = collection.getAsync("2", null).block();
52+
53+
System.out.println(retrievedBook.getAuthor());
4954

5055
// Generate a vector for your search text, using your chosen embedding generation implementation.
5156
// Just showing a placeholder method here for brevity.
52-
// var searchVector = generateEmbeddingsAsync(
53-
// "I'm looking for a Book where customer happiness is the priority.").block();
57+
List<Float> searchVector = generateEmbeddingsAsync(
58+
"I'm looking for a horror book.").block();
5459

5560
// Do the search.
56-
// var searchResult = collection.searchAsync(searchVector, VectorSearchOptions.builder()
57-
// .withTop(1).build()).block();
61+
VectorSearchResults<Book> searchResult = collection.searchAsync(
62+
searchVector, VectorSearchOptions.builder().withTop(1).build()).block();
5863

59-
// Book record = searchResult.getResults().get(0).getRecord();
60-
// System.out.printf("Found Book description: %s\n", record.getDescription());
64+
retrievedBook = searchResult.getResults().get(0).getRecord();
65+
System.out.println("Found Book: " + retrievedBook.getIsbn());
6166

6267
}
6368

6469
static List<Book> books = Arrays.asList(
65-
new Book("1", "one", "sking", 0, null, "sum", null));
70+
new Book("1", "one", "sking", 0, null, "horror", List.of(1f, 1f)),
71+
new Book("2", "two", "squeen", 0, null, "non-fiction", List.of(-11f, -11f)));
72+
73+
private static Mono<List<Float>> generateEmbeddingsAsync(String text) {
74+
return Mono.just(List.of(-0.9f, -0.9f));
75+
}
6676

6777
}

0 commit comments

Comments
 (0)