Skip to content

Commit 72d6c31

Browse files
author
Milder Hernandez Cagua
committed
Learn exampels
1 parent 607db38 commit 72d6c31

File tree

8 files changed

+286
-0
lines changed

8 files changed

+286
-0
lines changed

samples/semantickernel-learn-resources/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@
6262
<groupId>com.microsoft.semantic-kernel</groupId>
6363
<artifactId>semantickernel-aiservices-openai</artifactId>
6464
</dependency>
65+
<dependency>
66+
<groupId>com.microsoft.semantic-kernel</groupId>
67+
<artifactId>semantickernel-experimental</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.mysql</groupId>
71+
<artifactId>mysql-connector-j</artifactId>
72+
<version>9.0.0</version>
73+
</dependency>
6574
</dependencies>
6675

6776
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.microsoft.semantickernel.samples.documentationexamples.vectorstores.connectors.azureaisearch;
2+
3+
import com.azure.core.credential.AzureKeyCredential;
4+
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
5+
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStore;
6+
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreOptions;
7+
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreRecordCollection;
8+
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreRecordCollectionOptions;
9+
import com.microsoft.semantickernel.samples.documentationexamples.vectorstores.index.Hotel;
10+
11+
public class Main {
12+
public static void main(String[] args) {
13+
// Build the Azure AI Search client
14+
var searchClient = new SearchIndexClientBuilder()
15+
.endpoint("https://<your-search-service-name>.search.windows.net")
16+
.credential(new AzureKeyCredential("<your-search-service-key>"))
17+
.buildAsyncClient();
18+
19+
// Build an Azure AI Search Vector Store
20+
var vectorStore = AzureAISearchVectorStore.builder()
21+
.withSearchIndexAsyncClient(searchClient)
22+
.withOptions(new AzureAISearchVectorStoreOptions())
23+
.build();
24+
25+
var collection = new AzureAISearchVectorStoreRecordCollection<>(searchClient, "skhotels",
26+
AzureAISearchVectorStoreRecordCollectionOptions.<Hotel>builder()
27+
.withRecordClass(Hotel.class)
28+
.build());
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.microsoft.semantickernel.samples.documentationexamples.vectorstores.connectors.inmemory;
2+
3+
import com.microsoft.semantickernel.data.VolatileVectorStore;
4+
import com.microsoft.semantickernel.data.VolatileVectorStoreRecordCollection;
5+
import com.microsoft.semantickernel.data.VolatileVectorStoreRecordCollectionOptions;
6+
import com.microsoft.semantickernel.samples.documentationexamples.vectorstores.index.Hotel;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
// Build an Azure AI Search Vector Store
11+
var vectorStore = new VolatileVectorStore();
12+
13+
var collection = new VolatileVectorStoreRecordCollection<>("skhotels",
14+
VolatileVectorStoreRecordCollectionOptions.<Hotel>builder()
15+
.withRecordClass(Hotel.class)
16+
.build());
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.microsoft.semantickernel.samples.documentationexamples.vectorstores.connectors.jdbc;
2+
3+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStore;
4+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStoreOptions;
5+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStoreRecordCollection;
6+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStoreRecordCollectionOptions;
7+
import com.microsoft.semantickernel.connectors.data.mysql.MySQLVectorStoreQueryProvider;
8+
import com.microsoft.semantickernel.samples.documentationexamples.vectorstores.index.Hotel;
9+
import com.mysql.cj.jdbc.MysqlDataSource;
10+
11+
public class Main {
12+
public static void main(String[] args) {
13+
// Configure the data source
14+
var dataSource = new MysqlDataSource();
15+
dataSource.setUrl("jdbc:mysql://localhost:3306/sk");
16+
dataSource.setPassword("root");
17+
dataSource.setUser("root");
18+
19+
// Build a query provider
20+
// Other available query providers are PostgreSQLVectorStoreQueryProvider, SQLiteVectorStoreQueryProvider
21+
// and HSQDBVectorStoreQueryProvider
22+
var queryProvider = MySQLVectorStoreQueryProvider.builder()
23+
.withDataSource(dataSource)
24+
.build();
25+
26+
// Build a vector store
27+
var vectorStore = JDBCVectorStore.builder()
28+
.withDataSource(dataSource)
29+
.withOptions(JDBCVectorStoreOptions.builder()
30+
.withQueryProvider(queryProvider)
31+
.build())
32+
.build();
33+
34+
35+
var collection = new JDBCVectorStoreRecordCollection<>(
36+
dataSource,
37+
"skhotels",
38+
JDBCVectorStoreRecordCollectionOptions.<Hotel>builder()
39+
.withRecordClass(Hotel.class)
40+
.build()
41+
);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.microsoft.semantickernel.samples.documentationexamples.vectorstores.connectors.redis;
2+
3+
import com.microsoft.semantickernel.connectors.data.redis.RedisJsonVectorStoreRecordCollectionOptions;
4+
import com.microsoft.semantickernel.connectors.data.redis.RedisStorageType;
5+
import com.microsoft.semantickernel.connectors.data.redis.RedisVectorStore;
6+
import com.microsoft.semantickernel.connectors.data.redis.RedisVectorStoreOptions;
7+
import com.microsoft.semantickernel.samples.documentationexamples.vectorstores.index.Hotel;
8+
import redis.clients.jedis.JedisPooled;
9+
10+
public class Main {
11+
public static void main(String[] args) {
12+
JedisPooled jedis = new JedisPooled("<your-redis-url>");
13+
14+
// Build a Redis Vector Store
15+
// Available storage types are JSON and HASHSET. Default is JSON.
16+
var vectorStore = RedisVectorStore.builder()
17+
.withClient(jedis)
18+
.withOptions(
19+
RedisVectorStoreOptions.builder()
20+
.withStorageType(RedisStorageType.JSON).build())
21+
.build();
22+
23+
var collection = vectorStore.getCollection("skhotels",
24+
RedisJsonVectorStoreRecordCollectionOptions.<Hotel>builder()
25+
.withRecordClass(Hotel.class)
26+
.build());
27+
28+
collection = vectorStore.getCollection("skhotels",
29+
RedisJsonVectorStoreRecordCollectionOptions.<Hotel>builder()
30+
.withRecordClass(Hotel.class)
31+
.withPrefixCollectionName(false)
32+
.build());
33+
34+
collection.getAsync("myprefix_h1", null).block();
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.microsoft.semantickernel.samples.documentationexamples.vectorstores.index;
2+
3+
import com.microsoft.semantickernel.data.vectorstorage.attributes.VectorStoreRecordDataAttribute;
4+
import com.microsoft.semantickernel.data.vectorstorage.attributes.VectorStoreRecordKeyAttribute;
5+
import com.microsoft.semantickernel.data.vectorstorage.attributes.VectorStoreRecordVectorAttribute;
6+
import com.microsoft.semantickernel.data.vectorstorage.definition.DistanceFunction;
7+
import com.microsoft.semantickernel.data.vectorstorage.definition.IndexKind;
8+
9+
import java.util.List;
10+
11+
public class Hotel {
12+
@VectorStoreRecordKeyAttribute
13+
private String hotelId;
14+
15+
@VectorStoreRecordDataAttribute(isFilterable = true)
16+
private String name;
17+
18+
@VectorStoreRecordDataAttribute(isFullTextSearchable = true)
19+
private String description;
20+
21+
@VectorStoreRecordVectorAttribute(dimensions = 4, indexKind = IndexKind.HNSW, distanceFunction = DistanceFunction.COSINE_DISTANCE)
22+
private List<Float> descriptionEmbedding;
23+
24+
public Hotel() { }
25+
26+
public Hotel(String hotelId, String name, String description, List<Float> descriptionEmbedding) {
27+
this.hotelId = hotelId;
28+
this.name = name;
29+
this.description = description;
30+
this.descriptionEmbedding = descriptionEmbedding;
31+
}
32+
33+
public String getHotelId() { return hotelId; }
34+
public String getName() { return name; }
35+
public String getDescription() { return description; }
36+
public List<Float> getDescriptionEmbedding() { return descriptionEmbedding; }
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.microsoft.semantickernel.samples.documentationexamples.vectorstores.index;
2+
3+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStore;
4+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStoreOptions;
5+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStoreRecordCollectionOptions;
6+
import com.microsoft.semantickernel.connectors.data.mysql.MySQLVectorStoreQueryProvider;
7+
import com.mysql.cj.jdbc.MysqlDataSource;
8+
9+
import java.util.List;
10+
11+
public class Main {
12+
public static void main(String[] args) {
13+
// Create a MySQL data source
14+
var dataSource = new MysqlDataSource();
15+
dataSource.setUrl("jdbc:mysql://localhost:3306/sk");
16+
dataSource.setPassword("root");
17+
dataSource.setUser("root");
18+
19+
// Create a JDBC vector store
20+
var vectorStore = JDBCVectorStore.builder()
21+
.withDataSource(dataSource)
22+
.withOptions(
23+
JDBCVectorStoreOptions.builder()
24+
.withQueryProvider(MySQLVectorStoreQueryProvider.builder()
25+
.withDataSource(dataSource)
26+
.build())
27+
.build()
28+
)
29+
.build();
30+
31+
// Get a collection from the vector store
32+
var collection = vectorStore.getCollection("skhotels",
33+
JDBCVectorStoreRecordCollectionOptions.<Hotel>builder()
34+
.withRecordClass(Hotel.class)
35+
.build()
36+
);
37+
38+
// Create the collection if it doesn't exist yet.
39+
collection.createCollectionAsync().block();
40+
41+
// Upsert a record.
42+
var description = "A place where everyone can be happy";
43+
var hotelId = "hotel1";
44+
var hotel = new Hotel(hotelId, "Hotel Happy", description, generateEmbeddings(description));
45+
46+
collection.upsertAsync(hotel, null).block();
47+
48+
// Retrieve the upserted record.
49+
var retrievedHotel = collection.getAsync(hotelId, null).block();
50+
}
51+
52+
private static List<Float> generateEmbeddings(String text) {
53+
return List.of(1.0f, 2.0f, 3.0f, 4.0f);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.microsoft.semantickernel.samples.documentationexamples.vectorstores.recorddefinition;
2+
3+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStore;
4+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStoreOptions;
5+
import com.microsoft.semantickernel.connectors.data.jdbc.JDBCVectorStoreRecordCollectionOptions;
6+
import com.microsoft.semantickernel.connectors.data.mysql.MySQLVectorStoreQueryProvider;
7+
import com.microsoft.semantickernel.data.vectorstorage.definition.*;
8+
import com.mysql.cj.jdbc.MysqlDataSource;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
public class Main {
14+
public static void main(String[] args) {
15+
// Create a MySQL data source
16+
var dataSource = new MysqlDataSource();
17+
dataSource.setUrl("jdbc:mysql://localhost:3306/sk");
18+
dataSource.setPassword("root");
19+
dataSource.setUser("root");
20+
21+
// Create a JDBC vector store
22+
var vectorStore = JDBCVectorStore.builder()
23+
.withDataSource(dataSource)
24+
.withOptions(
25+
JDBCVectorStoreOptions.builder()
26+
.withQueryProvider(MySQLVectorStoreQueryProvider.builder()
27+
.withDataSource(dataSource)
28+
.build())
29+
.build()
30+
)
31+
.build();
32+
33+
var hotelDefinition = VectorStoreRecordDefinition.fromFields(
34+
Arrays.asList(
35+
VectorStoreRecordKeyField.builder().withName("hotelId").withFieldType(String.class).build(),
36+
VectorStoreRecordDataField.builder()
37+
.withName("name")
38+
.withFieldType(String.class)
39+
.isFilterable(true).build(),
40+
VectorStoreRecordDataField.builder()
41+
.withName("description")
42+
.withFieldType(String.class)
43+
.isFullTextSearchable(true).build(),
44+
VectorStoreRecordVectorField.builder().withName("descriptionEmbedding")
45+
.withDimensions(4)
46+
.withIndexKind(IndexKind.HNSW)
47+
.withDistanceFunction(DistanceFunction.COSINE_DISTANCE)
48+
.withFieldType(List.class).build()
49+
)
50+
);
51+
52+
var collection = vectorStore.getCollection("skhotels",
53+
JDBCVectorStoreRecordCollectionOptions.builder()
54+
.withRecordDefinition(hotelDefinition)
55+
.build()
56+
);
57+
}
58+
}

0 commit comments

Comments
 (0)