Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,40 @@ import ballerinax/ai.weaviate;

```ballerina
ai:VectorStore vectorStore = check new weaviate:VectorStore(
serviceUrl = "add-weaviate-service-url",
config = {
className: "add-collection-name"
},
auth = {
token: "add-access-token"
}
serviceUrl = "add-weaviate-service-url",
config = {
collectionName: "add-collection-name"
},
auth = {
token: "add-access-token"
}
);
```

### Step 3: Add vectors

```ballerina
ai:Error? result = vectorStore.add(
[
{
id: uuid:createRandomUuid(),
embedding: [1.0, 2.0, 3.0],
chunk: {
'type: "text",
content: "This is a chunk"
}
}
]
[
{
id: uuid:createRandomUuid(),
embedding: [1.0, 2.0, 3.0],
chunk: {
'type: "text",
content: "This is a chunk"
}
}
]
);
```

## Examples

The Ballerina Weaviate vector store module provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-ai.weaviate/tree/main/examples).

1. [Book Recommendation System](https://github.com/ballerina-platform/module-ballerinax-ai.weaviate/tree/main/examples/book-recommendation-system)
This example shows how to use Weaviate vector store APIs to implement a book recommendation system that stores book embeddings and queries them to find similar books based on vector similarity and metadata filtering.

## Issues and projects

Issues and Projects tabs are disabled for this repository as this is part of the Ballerina Library. To report bugs, request new features, start new discussions, view project boards, etc., go to the [Ballerina Library parent repository](https://github.com/ballerina-platform/ballerina-standard-library).
Expand Down
34 changes: 32 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.12.0"
[[package]]
org = "ballerina"
name = "ai"
version = "1.1.4"
version = "1.5.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "data.jsondata"},
Expand Down Expand Up @@ -87,7 +87,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "data.xmldata"
version = "1.4.2"
version = "1.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
Expand Down Expand Up @@ -241,6 +241,14 @@ dependencies = [
{org = "ballerina", name = "lang.regexp"}
]

[[package]]
org = "ballerina"
name = "lang.transaction"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.value"
Expand Down Expand Up @@ -391,6 +399,27 @@ dependencies = [
{org = "ballerina", name = "observe"}
]

[[package]]
org = "ballerinai"
name = "transaction"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "lang.transaction"},
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "task"},
{org = "ballerina", name = "time"},
{org = "ballerina", name = "uuid"}
]
modules = [
{org = "ballerinai", packageName = "transaction", moduleName = "transaction"}
]

[[package]]
org = "ballerinax"
name = "ai.weaviate"
Expand All @@ -400,6 +429,7 @@ dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "uuid"},
{org = "ballerinai", name = "transaction"},
{org = "ballerinax", name = "weaviate"}
]
modules = [
Expand Down
9 changes: 8 additions & 1 deletion ballerina/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import ballerinax/ai.weaviate;
ai:VectorStore vectorStore = check new weaviate:VectorStore(
serviceUrl = "add-weaviate-service-url",
config = {
className: "add-collection-name"
collectionName: "add-collection-name"
},
auth = {
token: "add-access-token"
Expand All @@ -87,3 +87,10 @@ ai:Error? result = vectorStore.add(
]
);
```

## Examples

The Ballerina Weaviate vector store module provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-ai.weaviate/tree/main/examples).

1. [Book Recommendation System](https://github.com/ballerina-platform/module-ballerinax-ai.weaviate/tree/main/examples/book-recommendation-system)
This example shows how to use Weaviate vector store APIs to implement a book recommendation system that stores book embeddings and queries them to find similar books based on vector similarity and metadata filtering.
76 changes: 0 additions & 76 deletions ballerina/tests/mock_weaviate_service.bal

This file was deleted.

72 changes: 68 additions & 4 deletions ballerina/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ final VectorStore mockVectorStore = check new (
}
);

string id = uuid:createRandomUuid();

@test:Config {}
function testAddingValuesToVectorStore() returns error? {
ai:VectorEntry[] entries = [
{
id: uuid:createRandomUuid(),
id,
embedding: [1.0, 2.0, 3.0],
chunk: {
'type: "text",
Expand All @@ -45,15 +47,32 @@ function testAddingValuesToVectorStore() returns error? {
}

@test:Config {}
function testDeleteValuesFromVectorStore() returns error? {
ai:Error? result = mockVectorStore.delete("mock-id");
function testDeleteValueFromVectorStore() returns error? {
ai:Error? result = mockVectorStore.delete(id);
test:assertTrue(result !is error);
}

@test:Config {}
function testDeleteMultipleValuesFromVectorStore() returns error? {
string index = uuid:createRandomUuid();
ai:VectorEntry[] entries = [
{
id: index,
embedding: [1.0, 2.0, 3.0],
chunk: {
'type: "text",
content: "This is a test chunk"
}
}
];
_ = check mockVectorStore.add(entries);
ai:Error? result = mockVectorStore.delete([id, index]);
test:assertTrue(result !is error);
}

@test:Config {}
function testQueryValuesFromVectorStore() returns error? {
ai:VectorStoreQuery query = {
embedding: [1.0, 2.0, 3.0],
filters: {
filters: [
{
Expand All @@ -67,3 +86,48 @@ function testQueryValuesFromVectorStore() returns error? {
ai:VectorMatch[]|ai:Error result = mockVectorStore.query(query);
test:assertTrue(result !is error);
}

@test:Config {}
function testVectorStoreInitializationWithInvalidURL() returns error? {
VectorStore store = check new (
serviceUrl = "invalid-url",
config = {
collectionName: "TestChunk"
},
auth = {
token: "test-token"
}
);
ai:VectorMatch[]|ai:Error result = store.query({
topK: 0,
embedding: [1.0, 2.0, 3.0]
});
test:assertTrue(result is ai:Error);
}

@test:Config {}
function testAddEmptyVectorEntriesArray() returns error? {
ai:VectorEntry[] emptyEntries = [];
ai:Error? result = mockVectorStore.add(emptyEntries);
test:assertTrue(result is error, "");
}

@test:Config {}
function testQueryWithTopKZero() returns error? {
ai:VectorStoreQuery query = {
topK: 0,
embedding: [1.0, 2.0, 3.0]
};
ai:VectorMatch[]|ai:Error result = mockVectorStore.query(query);
test:assertTrue(result is error);
}

@test:Config {}
function testQueryWithNegativeTopK() returns error? {
ai:VectorStoreQuery query = {
topK: -5,
embedding: [1.0, 2.0, 3.0]
};
ai:VectorMatch[]|ai:Error result = mockVectorStore.query(query);
test:assertTrue(result is error);
}
4 changes: 1 addition & 3 deletions ballerina/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
#
# + collectionName - The name of the collection to use
# + chunkFieldName - The name of the field to contain the chunk details
# + topK - The number of top similar vectors to return in queries
public type Configuration record {|
string collectionName;
string chunkFieldName?;
int topK = 10;
|};

type QueryResult record {
record {
string? id;
float certainty;
float? certainty;
float[] vector;
} _additional;
string content?;
Expand Down
Loading
Loading