|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 | package com.microsoft.semantickernel.samples.syntaxexamples.memory; |
3 | 3 |
|
4 | | -import com.azure.ai.openai.OpenAIAsyncClient; |
5 | | -import com.azure.ai.openai.OpenAIClientBuilder; |
6 | | -import com.azure.core.credential.AzureKeyCredential; |
7 | | -import com.azure.core.credential.KeyCredential; |
8 | | -import com.microsoft.semantickernel.aiservices.openai.textembedding.OpenAITextEmbeddingGenerationService; |
9 | 4 | import com.microsoft.semantickernel.data.jdbc.JDBCVectorStore; |
10 | 5 | import com.microsoft.semantickernel.data.jdbc.JDBCVectorStoreOptions; |
| 6 | +import com.microsoft.semantickernel.data.jdbc.JDBCVectorStoreRecordCollection; |
11 | 7 | import com.microsoft.semantickernel.data.jdbc.JDBCVectorStoreRecordCollectionOptions; |
12 | | -import com.microsoft.semantickernel.data.jdbc.postgres.PostgreSQLVectorStoreQueryProvider; |
13 | | -import com.microsoft.semantickernel.data.vectorsearch.VectorSearchResults; |
| 8 | +import com.microsoft.semantickernel.data.jdbc.oracle.OracleVectorStoreQueryProvider; |
14 | 9 | import com.microsoft.semantickernel.data.vectorstorage.VectorStoreRecordCollection; |
15 | | -import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordData; |
16 | | -import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordKey; |
17 | | -import com.microsoft.semantickernel.data.vectorstorage.annotations.VectorStoreRecordVector; |
18 | | -import com.microsoft.semantickernel.data.vectorstorage.definition.DistanceFunction; |
19 | | - |
20 | | -import java.nio.charset.StandardCharsets; |
| 10 | +import com.microsoft.semantickernel.samples.documentationexamples.data.index.Hotel; |
21 | 11 | import java.sql.SQLException; |
22 | | -import java.util.Arrays; |
23 | | -import java.util.Base64; |
24 | 12 | import java.util.Collections; |
25 | | -import java.util.List; |
26 | | -import java.util.Map; |
27 | | -import java.util.stream.Collectors; |
28 | | - |
29 | | -import org.postgresql.ds.PGSimpleDataSource; |
30 | | -import reactor.core.publisher.Flux; |
31 | | -import reactor.core.publisher.Mono; |
| 13 | +import oracle.jdbc.datasource.impl.OracleDataSource; |
32 | 14 |
|
33 | 15 | public class VectorStoreWithOracle { |
34 | 16 |
|
35 | | - static class GitHubFile { |
36 | | - @VectorStoreRecordKey |
37 | | - private final String id; |
38 | | - @VectorStoreRecordData |
39 | | - private final String description; |
40 | | - @VectorStoreRecordData |
41 | | - private final String link; |
42 | | - @VectorStoreRecordVector(dimensions = EMBEDDING_DIMENSIONS, distanceFunction = DistanceFunction.COSINE_DISTANCE) |
43 | | - private final List<Float> embedding; |
44 | | - |
45 | | - public GitHubFile() { |
46 | | - this(null, null, null, Collections.emptyList()); |
47 | | - } |
48 | | - |
49 | | - public GitHubFile( |
50 | | - String id, |
51 | | - String description, |
52 | | - String link, |
53 | | - List<Float> embedding) { |
54 | | - this.id = id; |
55 | | - this.description = description; |
56 | | - this.link = link; |
57 | | - this.embedding = embedding; |
58 | | - } |
59 | | - |
60 | | - public String getId() { |
61 | | - return id; |
62 | | - } |
63 | | - |
64 | | - public String getDescription() { |
65 | | - return description; |
66 | | - } |
67 | | - |
68 | | - public String getLink() { |
69 | | - return link; |
70 | | - } |
71 | | - |
72 | | - public List<Float> getEmbedding() { |
73 | | - return embedding; |
74 | | - } |
75 | | - |
76 | | - static String encodeId(String realId) { |
77 | | - byte[] bytes = Base64.getUrlEncoder().encode(realId.getBytes(StandardCharsets.UTF_8)); |
78 | | - return new String(bytes, StandardCharsets.UTF_8); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - // Run a PostgreSQL server with: |
83 | | - // docker run -d --name pgvector-container -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=root -e POSTGRES_DB=sk -p 5432:5432 pgvector/pgvector:pg17 |
84 | | - |
85 | 17 | public static void main(String[] args) throws SQLException { |
86 | 18 | System.out.println("=============================================================="); |
87 | 19 | System.out.println("============== Oracle Vector Store Example ==================="); |
88 | 20 | System.out.println("=============================================================="); |
89 | 21 |
|
90 | | - OpenAIAsyncClient client; |
91 | | - |
92 | | - if (AZURE_CLIENT_KEY != null) { |
93 | | - client = new OpenAIClientBuilder() |
94 | | - .credential(new AzureKeyCredential(AZURE_CLIENT_KEY)) |
95 | | - .endpoint(CLIENT_ENDPOINT) |
96 | | - .buildAsyncClient(); |
97 | | - |
98 | | - } else { |
99 | | - client = new OpenAIClientBuilder() |
100 | | - .credential(new KeyCredential(CLIENT_KEY)) |
101 | | - .buildAsyncClient(); |
102 | | - } |
103 | | - |
104 | | - // Create an OpenAI text embedding generation service |
105 | | - var embeddingGeneration = OpenAITextEmbeddingGenerationService.builder() |
106 | | - .withOpenAIAsyncClient(client) |
107 | | - .withModelId(MODEL_ID) |
108 | | - .withDimensions(EMBEDDING_DIMENSIONS) |
109 | | - .build(); |
110 | | - |
111 | | - storeAndSearch(embeddingGeneration); |
112 | | - } |
113 | | - |
114 | | - public static void storeAndSearch(OpenAITextEmbeddingGenerationService embeddingGeneration) { |
115 | 22 | // Configure the data source |
116 | | - PGSimpleDataSource dataSource = new PGSimpleDataSource(); |
117 | | - dataSource.setUrl("jdbc:postgresql://localhost:5432/sk"); |
118 | | - dataSource.setUser("postgres"); |
119 | | - dataSource.setPassword("root"); |
| 23 | + OracleDataSource dataSource = new OracleDataSource(); |
| 24 | + dataSource.setURL("jdbc:oracle:thin:@localhost:1521/FREEPDB1"); |
| 25 | + dataSource.setUser("scott"); |
| 26 | + dataSource.setPassword("tiger"); |
120 | 27 |
|
121 | 28 | // Build a query provider |
122 | | - // Other available query providers are PostgreSQLVectorStoreQueryProvider and SQLiteVectorStoreQueryProvider |
123 | | - var queryProvider = PostgreSQLVectorStoreQueryProvider.builder() |
| 29 | + OracleVectorStoreQueryProvider queryProvider = OracleVectorStoreQueryProvider.builder() |
124 | 30 | .withDataSource(dataSource) |
125 | 31 | .build(); |
126 | 32 |
|
127 | 33 | // Build a vector store |
128 | | - var jdbcVectorStore = JDBCVectorStore.builder() |
| 34 | + JDBCVectorStore vectorStore = JDBCVectorStore.builder() |
129 | 35 | .withDataSource(dataSource) |
130 | 36 | .withOptions(JDBCVectorStoreOptions.builder() |
131 | 37 | .withQueryProvider(queryProvider) |
132 | 38 | .build()) |
133 | 39 | .build(); |
134 | 40 |
|
135 | | - // Set up the record collection to use |
136 | | - String collectionName = "skgithubfiles"; |
137 | | - var collection = jdbcVectorStore.getCollection(collectionName, |
138 | | - JDBCVectorStoreRecordCollectionOptions.<GitHubFile>builder() |
139 | | - .withRecordClass(GitHubFile.class) |
140 | | - .build()); |
141 | | - |
142 | | - // Create collection if it does not exist and store data |
143 | | - collection |
144 | | - .createCollectionIfNotExistsAsync() |
145 | | - .then(storeData(collection, embeddingGeneration, sampleData())) |
| 41 | + // Get a collection from the vector store |
| 42 | + VectorStoreRecordCollection<String, Hotel> collection = |
| 43 | + vectorStore.getCollection("skhotels", |
| 44 | + JDBCVectorStoreRecordCollectionOptions.<Hotel>builder() |
| 45 | + .withRecordClass(Hotel.class) |
| 46 | + .build()); |
| 47 | + |
| 48 | + // Create the collection if it doesn't exist yet. |
| 49 | + collection.createCollectionAsync().block(); |
| 50 | + |
| 51 | + collection.upsertAsync(new Hotel("1", |
| 52 | + "HotelOne", |
| 53 | + "Desc for HotelOne", |
| 54 | + Collections.emptyList(), Collections.emptyList()), |
| 55 | + null) |
146 | 56 | .block(); |
147 | 57 |
|
148 | | - // Search for results |
149 | | - var results = search("How to get started", collection, embeddingGeneration).block(); |
150 | | - |
151 | | - if (results == null || results.getTotalCount() == 0) { |
152 | | - System.out.println("No search results found."); |
153 | | - return; |
154 | | - } |
155 | | - var searchResult = results.getResults().get(0); |
156 | | - System.out.printf("Search result with score: %f.%n Link: %s, Description: %s%n", |
157 | | - searchResult.getScore(), searchResult.getRecord().link, |
158 | | - searchResult.getRecord().description); |
159 | 58 | } |
160 | 59 |
|
161 | | - private static Mono<VectorSearchResults<GitHubFile>> search( |
162 | | - String searchText, |
163 | | - VectorStoreRecordCollection<String, GitHubFile> recordCollection, |
164 | | - OpenAITextEmbeddingGenerationService embeddingGeneration) { |
165 | | - // Generate embeddings for the search text and search for the closest records |
166 | | - return embeddingGeneration.generateEmbeddingAsync(searchText) |
167 | | - .flatMap(r -> recordCollection.searchAsync(r.getVector(), null)); |
168 | | - } |
169 | | - |
170 | | - private static Mono<List<String>> storeData( |
171 | | - VectorStoreRecordCollection<String, GitHubFile> recordStore, |
172 | | - OpenAITextEmbeddingGenerationService embeddingGeneration, |
173 | | - Map<String, String> data) { |
174 | | - |
175 | | - return Flux.fromIterable(data.entrySet()) |
176 | | - .flatMap(entry -> { |
177 | | - System.out.println("Save '" + entry.getKey() + "' to memory."); |
178 | | - |
179 | | - // Generate embeddings for the data and store it |
180 | | - return embeddingGeneration |
181 | | - .generateEmbeddingsAsync(Collections.singletonList(entry.getValue())) |
182 | | - .flatMap(embeddings -> { |
183 | | - GitHubFile gitHubFile = new GitHubFile( |
184 | | - GitHubFile.encodeId(entry.getKey()), |
185 | | - entry.getValue(), |
186 | | - entry.getKey(), |
187 | | - embeddings.get(0).getVector()); |
188 | | - return recordStore.upsertAsync(gitHubFile, null); |
189 | | - }); |
190 | | - }) |
191 | | - .collectList(); |
192 | | - } |
193 | | - |
194 | | - private static Map<String, String> sampleData() { |
195 | | - return Arrays.stream(new String[][] { |
196 | | - { "https://github.com/microsoft/semantic-kernel/blob/main/README.md", |
197 | | - "README: Installation, getting started with Semantic Kernel, and how to contribute" }, |
198 | | - { "https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb", |
199 | | - "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function" }, |
200 | | - { "https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT", |
201 | | - "Sample demonstrating how to create a chat skill interfacing with ChatGPT" }, |
202 | | - { "https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/VolatileMemoryStore.cs", |
203 | | - "C# class that defines a volatile embedding store" }, |
204 | | - { "https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet/KernelHttpServer/README.md", |
205 | | - "README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4" }, |
206 | | - { "https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md", |
207 | | - "README: README associated with a sample chat summary react-based webapp" }, |
208 | | - }).collect(Collectors.toMap(element -> element[0], element -> element[1])); |
209 | | - } |
210 | 60 | } |
0 commit comments