Quickstart
This quickstart shows you how to combine Weaviate Cloud and the Weaviate Embeddings service to:
- Set up a Weaviate Cloud instance. (10 minutes)
- Add and vectorize your data using Weaviate Embeddings. (10 minutes)
- Perform a semantic (vector) search and hybrid search. (10 minutes)
Notes:
- The code examples here are self-contained. You can copy and paste them into your own environment to try them out.
Requirements
To use Weaviate Embeddings, you will need:
- A Weaviate Cloud Sandbox running at least Weaviate
1.28.5
- A Weaviate client library that supports Weaviate Embeddings:
- Python client version
4.9.5
or higher - JavaScript/TypeScript client version
3.2.5
or higher - Go/Java clients are not yet officially supported; you must pass the
X-Weaviate-Api-Key
andX-Weaviate-Cluster-Url
headers manually upon instantiation as shown below.
- Python client version
Step 1: Set up Weaviate
1.1 Create a new cluster
To create a free Sandbox cluster in Weaviate Cloud, follow these instructions.
When possible, try to use the latest Weaviate version. New releases include cutting-edge features, performance enhancements, and critical security updates to keep your application safe and up-to-date.
1.2 Install a client library
We recommend using a client library to work with Weaviate. Follow the instructions below to install one of the official client libraries, available in Python, JavaScript/TypeScript, Go, and Java.
- Python
- JS/TS
- Go
- Java
Install the latest, Python client v4
, by adding weaviate-client
to your Python environment with pip
:
pip install -U weaviate-client
Install the latest, JS/TS client v3
, by adding weaviate-client
to your project with npm
:
npm install weaviate-client
Add weaviate-go-client
to your project with go get
:
go get github.com/weaviate/weaviate-go-client/v4
Add this dependency to your project:
<dependency>
<groupId>io.weaviate</groupId>
<artifactId>client</artifactId>
<version>4.8.3</version> <!-- Check latest version: https://github.com/weaviate/java-client -->
</dependency>
1.3 Connect to Weaviate Cloud
Weaviate Embeddings is integrated with Weaviate Cloud. Your Weaviate Cloud credentials will be used to authorize your Weaviate Cloud instance's access for Weaviate Embeddings.
- Python API v4
- JS/TS API v3
- Go
- Java
import weaviate
from weaviate.classes.init import Auth
import os
# Best practice: store your credentials in environment variables
wcd_url = os.getenv("WEAVIATE_URL")
wcd_key = os.getenv("WEAVIATE_API_KEY")
client = weaviate.connect_to_weaviate_cloud(
cluster_url=wcd_url, # Weaviate URL: "REST Endpoint" in Weaviate Cloud console
auth_credentials=Auth.api_key(wcd_key), # Weaviate API key: "ADMIN" API key in Weaviate Cloud console
)
print(client.is_ready()) # Should print: `True`
# Work with Weaviate
client.close()
import weaviate from 'weaviate-client'
// Best practice: store your credentials in environment variables
const wcdUrl = process.env.WEAVIATE_URL as string; // Weaviate URL: "REST Endpoint" in Weaviate Cloud console
const wcdApiKey = process.env.WEAVIATE_API_KEY as string; // Weaviate API key: "ADMIN" API key in Weaviate Cloud console
const client = await weaviate.connectToWeaviateCloud(
wcdUrl,
{
authCredentials: new weaviate.ApiKey(wcdApiKey),
}
)
// Work with Weaviate
client.close()
// Best practice: store your credentials in environment variables
// WEAVIATE_URL Weaviate URL: "REST Endpoint" in Weaviate Cloud console
// WEAVIATE_API_KEY Weaviate API key: "ADMIN" API key in Weaviate Cloud console
package main
import (
"context"
"fmt"
"os"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/auth"
)
func main() {
cfg := weaviate.Config{
Host: os.Getenv("WEAVIATE_URL"),
Scheme: "https",
AuthConfig: auth.ApiKey{Value: os.Getenv("WEAVIATE_API_KEY")},
Headers: map[string]string{
"X-Weaviate-Api-Key": os.Getenv("WEAVIATE_API_KEY"),
"X-Weaviate-Cluster-Url": fmt.Sprintf("https://%s", os.Getenv("WEAVIATE_URL")),
},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
fmt.Println(err)
}
// Work with Weaviate
}
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.base.Result;
public class ConnectWeaviateEmbeddings {
public static void main(String[] args) throws Exception {
// Best practice: store your credentials in environment variables
String wcdUrl = System.getenv("WEAVIATE_URL"); // Weaviate URL: "REST Endpoint" in Weaviate Cloud console
String wcdKey = System.getenv("WEAVIATE_API_KEY"); // Weaviate API key: "ADMIN" API key in Weaviate Cloud console
Map<String, String> headers = new HashMap<String, String>() { {
put("X-Weaviate-Api-Key", wcdKey);
put("X-Weaviate-Cluster-Url", "https://" + wcdUrl);
} };
Config config = new Config("https", wcdUrl, headers);
WeaviateClient client = WeaviateAuthClient.apiKey(config, wcdKey);
// check the result
Result<Boolean> result = client.misc().readyChecker().run();
System.out.println(result.getResult());
}
}
Step 2: Populate the database
2.1 Define a collection
Now we can define a collection that will store our data. When creating a collection, you need to specify one of the available models for the vectorizer to use. This model will be used to create vector embeddings from your data.
- Python API v4
- JS/TS API v3
- Go
- Java
from weaviate.classes.config import Configure
client.collections.create(
"DemoCollection",
vectorizer_config=[
Configure.NamedVectors.text2vec_weaviate(
name="title_vector",
source_properties=["title"],
model="Snowflake/snowflake-arctic-embed-l-v2.0",
# Further options
# dimensions=256
# base_url="<custom_weaviate_embeddings_url>",
)
],
# Additional parameters not shown
)
await client.collections.create({
name: 'DemoCollection',
properties: [
{
name: 'title',
dataType: 'text' as const,
},
],
vectorizers: [
weaviate.configure.vectorizer.text2VecWeaviate({
name: 'title_vector',
sourceProperties: ['title'],
model: 'Snowflake/snowflake-arctic-embed-l-v2.0',
// // Further options
// dimensions: 256,
// baseUrl: '<custom_weaviate_embeddings_url>',
},
),
],
// Additional parameters not shown
});
// package, imports not shown
func main() {
// Instantiation not shown
ctx := context.Background()
// Define the collection
weaviateVectorizerArcticEmbedLV20 := &models.Class{
Class: "DemoCollection",
VectorConfig: map[string]models.VectorConfig{
"title_vector": {
VectorIndexType: `hnsw`,
Vectorizer: map[string]interface{}{
"text2vec-weaviate": map[string]interface{}{
"model": "Snowflake/snowflake-arctic-embed-l-v2.0",
"dimensions": 1024, // Or 256
// "base_url": "<custom_weaviate_url>",
},
},
},
},
}
// add the collection
err = client.Schema().ClassCreator().WithClass(weaviateVectorizerArcticEmbedLV20).Do(ctx)
if err != nil {
panic(err)
}
}
Map<String, Object> text2vecWeaviate = new HashMap<>();
Map<String, Object> text2vecWeaviateSettings = new HashMap<>();
text2vecWeaviateSettings.put("properties", new String[]{"title"});
text2vecWeaviateSettings.put("model", new String[]{"Snowflake/snowflake-arctic-embed-l-v2.0"});
text2vecWeaviateSettings.put("dimensions", new Integer[]{1024}); // 1024, 256
text2vecWeaviateSettings.put("base_url", new String[]{"<custom_weaviate_url>"});
text2vecWeaviate.put("text2vec-weaviate", text2vecWeaviateSettings);
// Define the vector configurations
Map<String, WeaviateClass.VectorConfig> vectorConfig = new HashMap<>();
vectorConfig.put("title_vector", WeaviateClass.VectorConfig.builder()
.vectorIndexType("hnsw")
.vectorizer(text2vecWeaviate)
.build());
// Create the collection "DemoCollection"
WeaviateClass clazz = WeaviateClass.builder()
.className("DemoCollection")
.vectorConfig(vectorConfig)
.build();
Result<Boolean> result = client.schema().classCreator().withClass(clazz).run();
For more information about the available model options visit the Choose a model page.
2.2 Import objects
After configuring the vectorizer, import data into Weaviate. Weaviate generates embeddings for text objects using the specified model.
- Python API v4
- JS/TS API v3
- Go
- Java
source_objects = [
{"title": "The Shawshank Redemption", "description": "A wrongfully imprisoned man forms an inspiring friendship while finding hope and redemption in the darkest of places."},
{"title": "The Godfather", "description": "A powerful mafia family struggles to balance loyalty, power, and betrayal in this iconic crime saga."},
{"title": "The Dark Knight", "description": "Batman faces his greatest challenge as he battles the chaos unleashed by the Joker in Gotham City."},
{"title": "Jingle All the Way", "description": "A desperate father goes to hilarious lengths to secure the season's hottest toy for his son on Christmas Eve."},
{"title": "A Christmas Carol", "description": "A miserly old man is transformed after being visited by three ghosts on Christmas Eve in this timeless tale of redemption."}
]
collection = client.collections.get("DemoCollection")
with collection.batch.dynamic() as batch:
for src_obj in source_objects:
# The model provider integration will automatically vectorize the object
batch.add_object(
properties={
"title": src_obj["title"],
"description": src_obj["description"],
},
# vector=vector # Optionally provide a pre-obtained vector
)
if batch.number_errors > 10:
print("Batch import stopped due to excessive errors.")
break
failed_objects = collection.batch.failed_objects
if failed_objects:
print(f"Number of failed imports: {len(failed_objects)}")
print(f"First failed object: {failed_objects[0]}")
let srcObjects = [
{ title: "The Shawshank Redemption", description: "A wrongfully imprisoned man forms an inspiring friendship while finding hope and redemption in the darkest of places." },
{ title: "The Godfather", description: "A powerful mafia family struggles to balance loyalty, power, and betrayal in this iconic crime saga." },
{ title: "The Dark Knight", description: "Batman faces his greatest challenge as he battles the chaos unleashed by the Joker in Gotham City." },
{ title: "Jingle All the Way", description: "A desperate father goes to hilarious lengths to secure the season's hottest toy for his son on Christmas Eve." },
{ title: "A Christmas Carol", description: "A miserly old man is transformed after being visited by three ghosts on Christmas Eve in this timeless tale of redemption." }
];
const collectionName = 'DemoCollection'
const myCollection = client.collections.get(collectionName)
let dataObjects = []
for (let srcObject of srcObjects) {
dataObjects.push({
title: srcObject.title,
description: srcObject.description,
});
}
const response = await myCollection.data.insertMany(dataObjects);
console.log(response);
// package, imports not shown
func main() {
// Instantiation not shown
ctx := context.Background()
var sourceObjects = []map[string]string{
{"title": "The Shawshank Redemption", "description": "A wrongfully imprisoned man forms an inspiring friendship while finding hope and redemption in the darkest of places."},
{"title": "The Godfather", "description": "A powerful mafia family struggles to balance loyalty, power, and betrayal in this iconic crime saga."},
{"title": "The Dark Knight", "description": "Batman faces his greatest challenge as he battles the chaos unleashed by the Joker in Gotham City."},
{"title": "Jingle All the Way", "description": "A desperate father goes to hilarious lengths to secure the season's hottest toy for his son on Christmas Eve."},
{"title": "A Christmas Carol", "description": "A miserly old man is transformed after being visited by three ghosts on Christmas Eve in this timeless tale of redemption."},
}
// Convert items into a slice of models.Object
objects := []models.PropertySchema{}
for i := range sourceObjects {
objects = append(objects, map[string]interface{}{
// Populate the object with the data
"title": sourceObjects[i]["title"],
"description": sourceObjects[i]["description"],
})
}
// Batch write items
batcher := client.Batch().ObjectsBatcher()
for _, dataObj := range objects {
batcher.WithObjects(&models.Object{
Class: "DemoCollection",
Properties: dataObj,
})
}
// Flush
batchRes, err := batcher.Do(ctx)
// Error handling
if err != nil {
panic(err)
}
for _, res := range batchRes {
if res.Result.Errors != nil {
for _, err := range res.Result.Errors.Error {
if err != nil {
fmt.Printf("Error details: %v\n", *err)
panic(err.Message)
}
}
}
}
}
List<HashMap<String, Object>> objects = new ArrayList<>();
for (Map<String, String> sourceObject : sourceObjects) {
HashMap<String, Object> schema = new HashMap<>();
schema.put("title", sourceObject.get("title"));
schema.put("description", sourceObject.get("description"));
objects.add(schema);
}
// Batch write items
ObjectsBatcher batcher = client.batch().objectsBatcher();
for (Map<String, Object> properties : objects) {
batcher.withObject(WeaviateObject.builder()
.className("DemoCollection")
.properties(properties)
// .tenant("tenantA") // If multi-tenancy is enabled, specify the tenant to which the object will be added.
.build()
);
}
// Flush
batcher.run();
Step 3: Query your data
Once the vectorizer is configured, Weaviate will perform vector search operations using the specified model.
Vector (near text) search
When you perform a vector search, Weaviate converts the text query into an embedding using the specified model and returns the most similar objects from the database.
The query below returns the n
most similar objects from the database, set by limit
.
- Python API v4
- JS/TS API v3
- Go
- Java
collection = client.collections.get("DemoCollection")
response = collection.query.near_text(
query="A holiday film", # The model provider integration will automatically vectorize the query
limit=2
)
for obj in response.objects:
print(obj.properties["title"])
const collectionName = 'DemoCollection'
const myCollection = client.collections.get(collectionName)
let result;
result = await myCollection.query.nearText(
'A holiday film', // The model provider integration will automatically vectorize the query
{
limit: 2,
}
)
console.log(JSON.stringify(result.objects, null, 2));
// package, imports not shown
func main() {
// Instantiation not shown
ctx := context.Background()
nearTextResponse, err := client.GraphQL().Get().
WithClassName("DemoCollection").
WithFields(
graphql.Field{Name: "title"},
).
WithNearText(client.GraphQL().NearTextArgBuilder().
WithConcepts([]string{"A holiday film"})).
WithLimit(2).
Do(ctx)
if err != nil {
panic(err)
}
fmt.Printf("%v", nearTextResponse)
}
Fields returnFields = Fields.builder()
.fields(new Field[]{
Field.builder().name("title").build(),
})
.build();
NearTextArgument nearText = NearTextArgument.builder()
.concepts(new String[]{"A holiday film"})
.build();
String nearTextQuery = GetBuilder.builder()
.className("DemoCollection")
.fields(returnFields)
.withNearTextFilter(nearText)
.limit(2)
.build()
.buildQuery();
Result<GraphQLResponse> nearTextResult = client.graphQL().raw().withQuery(nearTextQuery).run();
if (nearTextResult.hasErrors()) {
System.err.println(nearTextResult.getError());
} else {
System.out.println("Near Text Results: " + nearTextResult.getResult().getData());
}
Next steps
Support
For help with Serverless Cloud, Enterprise Cloud, and Bring Your Own Cloud accounts, contact Weaviate support directly to open a support ticket.
For questions and support from the Weaviate community, try these resources:
To add a support plan, contact Weaviate sales.