Create a collection
Weaviate stores data in "collections". A collection is a set of objects that share the same data structure. In our movie database, we might have a collection of movies, a collection of actors, and a collection of reviews.
Here we will create a collection of movies.
Code
This example creates a collection for the movie data:
import weaviate from "weaviate-client";
import { WeaviateClient, configure, vectorizer, toBase64FromMedia } from "weaviate-client";
// Instantiate your client (not shown). e.g.:
// const requestHeaders = {'X-VoyageAI-Api-Key': process.env.VOYAGEAI_API_KEY as string,}
// client = weaviate.connectToWeaviateCloud(..., headers: requestHeaders) or
// client = weaviate.connectToLocal(..., headers: requestHeaders)
await client.collections.create({
name: "Movie",
properties: [
{ name: "title", dataType: configure.dataType.TEXT },
{ name: "overview", dataType: configure.dataType.TEXT },
{ name: "vote_average", dataType: configure.dataType.NUMBER },
{ name: "genre_ids", dataType: configure.dataType.INT_ARRAY },
{ name: "release_date", dataType: configure.dataType.DATE },
{ name: "tmdb_id", dataType: configure.dataType.INT },
{ name: "poster", dataType: configure.dataType.BLOB }
],
// Define the vectorizer module
vectorizers: vectorizer.multi2VecVoyageAI({
imageFields: [{ name: "poster", weight: 0.9 }],
textFields: [{ name: "title", weight: 0.1 }],
model: "voyage-multimodal-3"
}),
// Define the generative module
generative: configure.generative.cohere(),
})
client.close()
Each collection definition must have a name. Then, you can define additional parameters like we've done in this example.
Explain the code
Properties
Properties are the object attributes that you want to store in the collection. Each property has a name and a data type.
In our movie database, we have properties like title
, release_date
and genre_ids
, with data types like TEXT
(string), DATE
(date), or INT
(integer). It's also possible to have arrays of integers, like we have with genre_ids
.
As a multimodal object, we also have the poster
property which is the image data, which is saved as a BLOB
(binary large object) data type.
Auto-schema
Weaviate can automatically infer the schema from the data. However, it's a good practice to define the properties explicitly, for better control and to avoid surprises.
Vectorizer configuration
If you do not specify the vector yourself, Weaviate will use a specified vectorizer to generate vector embeddings from your data.
In this code example, we specify the multi2vec-voyageai
module. This module uses the voyage-multimodal-3 model to generate vector embeddings from the text and image data.
You can specify any number of text and image properties to be used for vectorization, and weight them differently. The weights are used to determine the relative importance of each property in the vector embedding generation process. In this example, we vectorize the poster
property (an image) with a 90% weight and the title
property (a string) with a 10% weight.
vectorizers: vectorizer.multi2VecVoyageAI({
imageFields: [{ name: "poster", weight: 0.9 }],
textFields: [{ name: "title", weight: 0.1 }],
model: "voyage-multimodal-3"
}),
Generative configuration
If you wish to use your collection with a generative model (e.g. a large language model), you must specify the generative module.
In this code example, we specify the cohere
module (generative-cohere
is the full name) with default options.
generative: configure.generative.cohere(),
A collection's generative
model integration configuration is mutable from v1.25.23
, v1.26.8
and v1.27.1
. See this section for details on how to update the collection configuration.
TypeScript named imports
The code example makes use of named imports such as vectorizer
and configure
. They are defined in the weaviate
module and are used during the collection definition.
import weaviate from "weaviate-client";
import { WeaviateClient, configure, vectorizer, toBase64FromMedia } from "weaviate-client";
Questions and feedback
If you have any questions or feedback, let us know in the user forum.