Weaviate Dart Wrapper
A Dart wrapper for the Weaviate REST API, allowing you to easily integrate Weaviate into your Dart projects.
Table of Contents
Installation
Add weaviate
as a dependency in your pubspec.yaml
file:
dependencies:
...
weaviate: ^1.0.0
Then run flutter pub get
to fetch the package.
Usage
Import the package in your Dart file:
import 'package:weaviate/weaviate.dart';
Create a new instance of the Weaviate client:
final weaviate = Weaviate(
weaviateUrl: '[your cloud instance or other host]',
));
Now you can use the client to interact with the Weaviate API.
Examples
Here are a few examples demonstrating the usage of the Weaviate Dart wrapper:
Creating an object
import 'package:weaviate/weaviate.dart';
void main() async {
final weaviate = WeaviateClient('[your cloud instance or other host]');
// delete schema if it exists
await weaviate.deleteSchema('Question');
// define the schema for for your objects
final schema = SchemaClass(
className: 'Question',
vectorizer: 'text2vec-huggingface',
moduleConfig: Text2vecHuggingFace(
model: 'sentence-transformers/all-MiniLM-L6-v2',
).toJson(),
);
// add the schema to your weaviate instance
await weaviate.addSchema(schema);
try {
// use a json file as input documents
final inputData = json.decode(File('jeopardy_tiny.json').readAsStringSync())
as List<dynamic>;
// create the objects that will be uploaded
final objects = inputData
.map((element) => WeaviateObject(
className: 'Question',
properties: {
'category': element['Category'],
'question': element['Question'],
'answer': element['Answer'],
},
))
.toList();
// upload the docs into your instance as a batch
await weaviate.batchObjects(BatchObjectRequest(objects: objects));
print('Object created successfully!');
} catch (e) {
print('Error creating object: $e');
}
}
Querying objects
import 'package:graphql/client.dart';
import 'package:weaviate/weaviate.dart';
void main() async {
final weaviate = WeaviateClient('[your cloud instance or other host]');
try {
final QueryOptions options = QueryOptions(document: gql(r'''{
Get{
Question (
limit: 2
where: {
path: ["category"],
operator: Equal,
valueText: "ANIMALS"
}
nearText: {
concepts: ["biology"],
}
){
question
answer
category
}
}
}'''));
print('querying...');
final result = await weaviate.getGraphQLClient().query(options);
print(result.data?['Get']['Question']);
} catch (e) {
print('Error querying objects: $e');
}
}
Contributing
Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please create an issue on the GitHub repository.
To contribute code, please fork the repository and create a pull request with your changes.
License
This project is licensed under the MIT License.