Quickstart: with cloud resources
Expected time: 30 minutes Prerequisites: NoneIn this quickstart guide, you will:
- Set up Weaviate. (10 minutes)
- Populate the database. (10 minutes)
- Perform a semantic search and retrieval augmented generation (RAG). (10 minutes)
This tutorial uses a Sandbox instance on Weaviate Cloud, and the OpenAI API. If you prefer to use locally hosted resources, see QuickStart: locally hosted.
Prerequisites
You will need accounts with Weaviate Cloud and OpenAI.
The Weaviate Sandbox is free, but the OpenAI usage may incur a small cost (e.g. < 10c US). If you have another, preferred model provider, you can use that instead.
We have (a Jupyter notebook) available, or you can try it on Google Colab.
The code examples here are self-contained. You can copy and paste them into your own environment to try them out.
Step 1: Set up Weaviate
1.1 Create a Weaviate database
Go the WCD homepage and create a free Sandbox instance.
- Log onto WCD.
- Click on
Clusters
on the sidebar. - In the following pane, click
Create cluster
.
- Give your cluster a name.
- Set your preferred cloud region.
- Click "Create".
- Cluster provisioning typically takes 1-3 minutes.
- When the cluster is ready, WCD displays a check mark (
✔️
) next to the cluster name. - Note that WCD adds a random suffix to sandbox cluster names to ensure uniqueness.
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
Now you can connect to your Weaviate instance. Get the instance REST Endpoint URL and the Administrator API Key from the WCD console as shown below.
REST Endpoint
URL.Admin
API key.Weaviate supports both REST and gRPC protocols. For Weaviate Cloud deployments, you only need to provide the REST endpoint URL - the client will automatically configure gRPC.
Once you have the REST Endpoint URL and the Admin API key, you can connect to the Sandbox instance, and work with Weaviate.
The example below shows how to connect to Weaviate and perform a basic operation, like checking the cluster status.
- Python
- JS/TS
- Go
- Java
- Curl
import weaviate
from weaviate.classes.init import Auth
import os
# Best practice: store your credentials in environment variables
wcd_url = os.environ["WCD_URL"]
wcd_api_key = os.environ["WCD_API_KEY"]
client = weaviate.connect_to_weaviate_cloud(
cluster_url=wcd_url, # Replace with your Weaviate Cloud URL
auth_credentials=Auth.api_key(wcd_api_key), # Replace with your Weaviate Cloud key
)
print(client.is_ready()) # Should print: `True`
client.close() # Free up resources
import weaviate, { WeaviateClient } from 'weaviate-client';
// Best practice: store your credentials in environment variables
const wcdUrl = process.env.WCD_URL as string;
const wcdApiKey = process.env.WCD_API_KEY as string;
const client: WeaviateClient = await weaviate.connectToWeaviateCloud(
wcdUrl, // Replace with your Weaviate Cloud URL
{
authCredentials: new weaviate.ApiKey(wcdApiKey), // Replace with your Weaviate Cloud API key
}
);
var clientReadiness = await client.isReady();
console.log(clientReadiness); // Should return `true`
client.close(); // Close the client connection
// Set these environment variables
// WCD_HOSTNAME your Weaviate instance hostname
// WCD_API_KEY your Weaviate instance API key
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("WCD_HOSTNAME"),
Scheme: "https",
AuthConfig: auth.ApiKey{Value: os.Getenv("WCD_API_KEY")},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
fmt.Println(err)
}
// Check the connection
ready, err := client.Misc().ReadyChecker().Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%v", ready)
}
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.WeaviateAuthClient;
import io.weaviate.client.base.Result;
// Set these environment variables
// WCD_HOSTNAME Your Weaviate instance hostname
// WCD_API_KEY Your Weaviate instance API key
// OPENAI_API_KEY Your OpenAI API key
public class IsReady {
public static void main(String[] args) throws Exception {
String host = System.getenv("WCD_HOSTNAME");
String apiKey = System.getenv("WCD_API_KEY");
Config config = new Config("https", host);
WeaviateClient client = WeaviateAuthClient.apiKey(config, apiKey);
// check the result
Result<Boolean> result = client.misc().readyChecker().run();
System.out.println(result.getResult());
}
}
# Best practice: store your credentials in environment variables
# export WCD_URL="YOUR_INSTANCE_URL" # Your Weaviate instance URL
# export WCD_API_KEY="YOUR_API_KEY" # Your Weaviate instance API key
curl -w "\nResponse code: %{http_code}\n" \
-H "Authorization: Bearer $WCD_API_KEY" \
$WCD_URL/v1/.well-known/ready
# You should see "Response code: 200" if the instance is ready
If you did not see any errors, you are ready to proceed. We will replace the simple cluster status check with more meaningful operations in the next steps.
Step 2: Populate the database
Now, we can populate our database by first defining a collection then adding data.
2.1 Define a collection
A collection is a set of objects that share the same data structure, like a table in relational databases or a collection in NoSQL databases. A collection also includes additional configurations that define how the data objects are stored and indexed.
The following example creates a collection called Question
with:
- OpenAI embedding model integration to create vectors during ingestion & queries.
- OpenAI generative AI integrations for retrieval augmented generation (RAG).
- Python
- JS/TS
- Go
- Java
- Curl
import weaviate
from weaviate.classes.init import Auth
from weaviate.classes.config import Configure
import os
# Best practice: store your credentials in environment variables
wcd_url = os.environ["WCD_URL"]
wcd_api_key = os.environ["WCD_API_KEY"]
client = weaviate.connect_to_weaviate_cloud(
cluster_url=wcd_url, # Replace with your Weaviate Cloud URL
auth_credentials=Auth.api_key(wcd_api_key), # Replace with your Weaviate Cloud key
)
questions = client.collections.create(
name="Question",
vectorizer_config=Configure.Vectorizer.text2vec_openai(), # Configure the OpenAI embedding integration
generative_config=Configure.Generative.openai() # Configure the OpenAI generative AI integration
)
client.close() # Free up resources
import weaviate, { WeaviateClient, vectorizer, generative } from 'weaviate-client';
// Best practice: store your credentials in environment variables
const wcdUrl = process.env.WCD_URL as string;
const wcdApiKey = process.env.WCD_API_KEY as string;
const client: WeaviateClient = await weaviate.connectToWeaviateCloud(
wcdUrl, // Replace with your Weaviate Cloud URL
{
authCredentials: new weaviate.ApiKey(wcdApiKey), // Replace with your Weaviate Cloud API key
}
);
await client.collections.create({
name: 'Question',
vectorizers: vectorizer.text2VecOpenAI(),
generative: generative.openAI(),
});
client.close(); // Close the client connection
// Set these environment variables
// WCD_HOSTNAME your Weaviate instance hostname
// WCD_API_KEY your Weaviate instance API key
// OPENAI_API_KEY your OpenAI API key
package main
import (
"context"
"fmt"
"os"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/auth"
"github.com/weaviate/weaviate/entities/models"
)
func main() {
cfg := weaviate.Config{
Host: os.Getenv("WCD_HOSTNAME"),
Scheme: "https",
AuthConfig: auth.ApiKey{Value: os.Getenv("WCD_API_KEY")},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
fmt.Println(err)
}
// Define the collection
classObj := &models.Class{
Class: "Question",
Vectorizer: "text2vec-openai",
ModuleConfig: map[string]interface{}{
"text2vec-openai": map[string]interface{}{},
"generative-openai": map[string]interface{}{},
},
}
// add the collection
err = client.Schema().ClassCreator().WithClass(classObj).Do(context.Background())
if err != nil {
panic(err)
}
}
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.base.Result;
import io.weaviate.client.v1.schema.model.WeaviateClass;
import java.util.HashMap;
import java.util.Map;
// Set these environment variables
// WCD_HOSTNAME Your Weaviate instance hostname
// WCD_API_KEY Your Weaviate instance API key
// OPENAI_API_KEY Your OpenAI API key
public class CreateCollection {
public static void main(String[] args) throws Exception {
String host = System.getenv("WCD_HOSTNAME");
String apiKey = System.getenv("WCD_API_KEY");
Config config = new Config("https", host);
WeaviateClient client = WeaviateAuthClient.apiKey(config, apiKey);
Map<String, Object> text2vecOpenAISettings = new HashMap<>();
Map<String, Object> generativeOpenAISettings = new HashMap<>();
Map<String, Object> moduleConfig = new HashMap<>();
moduleConfig.put("text2vec-openai", text2vecOpenAISettings);
moduleConfig.put("generative-openai", generativeOpenAISettings);
// Create the collection "Question"
WeaviateClass clazz = WeaviateClass.builder()
.className("Question")
.vectorizer("text2vec-openai")
.moduleConfig(moduleConfig)
.build();
// Review the response
Result<Boolean> result = client.schema().classCreator().withClass(clazz).run();
System.out.println(result);
Result<WeaviateClass> collectionDefinition = client.schema().classGetter().withClassName("Question").run();
System.out.println(collectionDefinition.getResult());
}
}
# Best practice: store your credentials in environment variables
# export WCD_URL="YOUR_INSTANCE_URL" # Your Weaviate instance URL
# export WCD_API_KEY="YOUR_API_KEY" # Your Weaviate instance API key
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $WCD_API_KEY" \
-d '{
"class": "Question",
"vectorizer": "text2vec-openai",
"moduleConfig": {
"text2vec-openai": {},
"generative-openai": {}
}
}' \
"$WCD_URL/v1/schema"
Run this code to create the collection to which you can add data.
You can optionally specify the model in the collection definition. As we did not specify models in the collection definition above, these integrations will use the Weaviate-defined default models.
See the model providers integration section for more information.
Do you prefer a different setup?
Weaviate is very flexible. If you prefer a different model provider integration, or prefer to import your own vectors, see one of the following guides:
Prefer a different model provider?
Want to specify object vectors?
2.2 Add objects
We can now add data to our collection.
The following example:
- Loads objects, and
- Adds objects to the target collection (
Question
) using a batch process.
(Batch imports) are the most efficient way to add large amounts of data, as it sends multiple objects in a single request. See the How-to: Batch import guide for more information.
- Python
- JS/TS
- Go
- Java
- Curl
import weaviate
from weaviate.classes.init import Auth
import requests, json, os
# Best practice: store your credentials in environment variables
wcd_url = os.environ["WCD_URL"]
wcd_api_key = os.environ["WCD_API_KEY"]
openai_api_key = os.environ["OPENAI_APIKEY"]
client = weaviate.connect_to_weaviate_cloud(
cluster_url=wcd_url, # Replace with your Weaviate Cloud URL
auth_credentials=Auth.api_key(wcd_api_key), # Replace with your Weaviate Cloud key
headers={"X-OpenAI-Api-Key": openai_api_key}, # Replace with your OpenAI API key
)
resp = requests.get(
"https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json"
)
data = json.loads(resp.text)
questions = client.collections.get("Question")
with questions.batch.dynamic() as batch:
for d in data:
batch.add_object({
"answer": d["Answer"],
"question": d["Question"],
"category": d["Category"],
})
client.close() # Free up resources
import weaviate, { WeaviateClient } from 'weaviate-client';
// Best practice: store your credentials in environment variables
const wcdUrl = process.env.WCD_URL as string;
const wcdApiKey = process.env.WCD_API_KEY as string;
const openAIKey = process.env.OPENAI_APIKEY as string;
const client: WeaviateClient = await weaviate.connectToWeaviateCloud(
wcdUrl, // Replace with your Weaviate Cloud URL
{
authCredentials: new weaviate.ApiKey(wcdApiKey), // Replace with your Weaviate Cloud API key
headers: {
'X-OpenAI-Api-Key': openAIKey, // Replace with your OpenAI API key
},
}
);
// Load data
async function getJsonData() {
const file = await fetch(
'https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json'
);
return file.json();
}
// Note: The TS client does not have a `batch` method yet
// We use `insertMany` instead, which sends all of the data in one request
async function importQuestions() {
const questions = client.collections.get('Question');
const data = await getJsonData();
const result = await questions.data.insertMany(data);
console.log('Insertion response: ', result);
}
await importQuestions();
client.close(); // Close the client connection
// Set these environment variables
// WCD_HOSTNAME your Weaviate instance hostname
// WCD_API_KEY your Weaviate instance API key
// OPENAI_API_KEY your OpenAI API key
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/auth"
"github.com/weaviate/weaviate/entities/models"
)
func main() {
cfg := weaviate.Config{
Host: os.Getenv("WCD_HOSTNAME"),
Scheme: "https",
AuthConfig: auth.ApiKey{Value: os.Getenv("WCD_API_KEY")},
Headers: map[string]string{
"X-OpenAI-Api-Key": os.Getenv("OPENAI_API_KEY"),
},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
fmt.Println(err)
}
// Retrieve the data
data, err := http.DefaultClient.Get("https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json")
if err != nil {
panic(err)
}
defer data.Body.Close()
// Decode the data
var items []map[string]string
if err := json.NewDecoder(data.Body).Decode(&items); err != nil {
panic(err)
}
// convert items into a slice of models.Object
objects := make([]*models.Object, len(items))
for i := range items {
objects[i] = &models.Object{
Class: "Question",
Properties: map[string]any{
"category": items[i]["Category"],
"question": items[i]["Question"],
"answer": items[i]["Answer"],
},
}
}
// batch write items
batchRes, err := client.Batch().ObjectsBatcher().WithObjects(objects...).Do(context.Background())
if err != nil {
panic(err)
}
for _, res := range batchRes {
if res.Result.Errors != nil {
panic(res.Result.Errors.Error)
}
}
}
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.v1.batch.api.ObjectsBatcher;
import io.weaviate.client.v1.data.model.WeaviateObject;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
// Set these environment variables
// WCD_HOSTNAME Your Weaviate instance hostname
// WCD_API_KEY Your Weaviate instance API key
// OPENAI_API_KEY Your OpenAI API key
public class Import {
public static void main(String[] args) throws Exception {
String host = System.getenv("WCD_HOSTNAME");
String apiKey = System.getenv("WCD_API_KEY");
String OpenAIKey = System.getenv("OPENAI_API_KEY");
Map<String, String> headers = new HashMap<String, String>() { {
put("X-OpenAI-Api-Key", OpenAIKey);
} };
Config config = new Config("https", host, headers);
WeaviateClient client = WeaviateAuthClient.apiKey(config, apiKey);
// Get JSON data
URL url = new URL("https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json");
String jsonData = new BufferedReader(new InputStreamReader(((HttpURLConnection) url.openConnection()).getInputStream()))
.lines().reduce("", String::concat);
// Create and execute batch
ObjectsBatcher batcher = client.batch().objectsBatcher();
new JSONArray(jsonData).forEach(item -> {
JSONObject json = (JSONObject) item;
HashMap<String, Object> properties = new HashMap<>();
properties.put("category", json.getString("Category"));
properties.put("question", json.getString("Question"));
properties.put("answer", json.getString("Answer"));
batcher.withObject(WeaviateObject.builder()
.className("Question")
.properties(properties)
.build());
});
// Flush
batcher.run();
}
}
- Download the
jeopardy_tiny.json
file from here before running the following script. - This assumes you have
jq
installed.
# Best practice: store your credentials in environment variables
# export WCD_URL="YOUR_INSTANCE_URL" # Your Weaviate instance URL
# export WCD_API_KEY="YOUR_API_KEY" # Your Weaviate instance API key
# export OPENAI_API_KEY="YOUR_API_KEY" # Your OpenAI API key
# Set batch size
BATCH_ENDPOINT="$WCD_URL/v1/batch/objects"
BATCH_SIZE=100
# Read the JSON file and loop through its entries
lines_processed=0
batch_data="{\"objects\": ["
cat jeopardy_tiny.json | jq -c '.[]' | while read line; do
# Concatenate lines
line=$(echo "$line" | jq "{class: \"Question\", properties: {answer: .Answer, question: .Question, category: .Category}}")
if [ $lines_processed -eq 0 ]; then
batch_data+=$line
else
batch_data+=",$line"
fi
lines_processed=$((lines_processed + 1))
# If the batch is full, send it to the API using curl
if [ $lines_processed -eq $BATCH_SIZE ]; then
batch_data+="]}"
curl -X POST "$BATCH_ENDPOINT" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $WCD_API_KEY" \
-H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \
-d "$batch_data"
echo "" # Print a newline for better output formatting
# Reset the batch data and counter
lines_processed=0
batch_data="{\"objects\": ["
fi
done
# Send the remaining data (if any) to the API using curl
if [ $lines_processed -ne 0 ]; then
batch_data+="]}"
curl -X POST "$BATCH_ENDPOINT" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $WCD_API_KEY" \
-H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \
-d "$batch_data"
echo "" # Print a newline for better output formatting
fi
Run this code to add the demo data.
Note that this code includes an additional header for the OpenAI API key. Weaviate uses this key to generate vector embeddings for the data objects as they are being added.
Step 3: Queries
Weaviate provides a wide range of query tools to help you find the right data. We will try a few searches here.
3.1 Semantic search
Semantic search finds results based on meaning. This is called nearText
in Weaviate.
The following example searches for 2 objects whose meaning is most similar to that of biology
.
- Python
- JS/TS
- Go
- Java
- Curl
import weaviate
from weaviate.classes.init import Auth
import os, json
# Best practice: store your credentials in environment variables
wcd_url = os.environ["WCD_URL"]
wcd_api_key = os.environ["WCD_API_KEY"]
openai_api_key = os.environ["OPENAI_APIKEY"]
client = weaviate.connect_to_weaviate_cloud(
cluster_url=wcd_url, # Replace with your Weaviate Cloud URL
auth_credentials=Auth.api_key(wcd_api_key), # Replace with your Weaviate Cloud key
headers={"X-OpenAI-Api-Key": openai_api_key}, # Replace with your OpenAI API key
)
questions = client.collections.get("Question")
response = questions.query.near_text(
query="biology",
limit=2
)
for obj in response.objects:
print(json.dumps(obj.properties, indent=2))
client.close() # Free up resources
import weaviate, { WeaviateClient } from 'weaviate-client';
// Best practice: store your credentials in environment variables
const wcdUrl = process.env.WCD_URL as string;
const wcdApiKey = process.env.WCD_API_KEY as string;
const openAIKey = process.env.OPENAI_APIKEY as string;
const client: WeaviateClient = await weaviate.connectToWeaviateCloud(
wcdUrl, // Replace with your Weaviate Cloud URL
{
authCredentials: new weaviate.ApiKey(wcdApiKey), // Replace with your Weaviate Cloud API key
headers: {
'X-OpenAI-Api-Key': openAIKey, // Replace with your OpenAI API key
},
}
);
const questions = client.collections.get('Question');
const result = await questions.query.nearText('biology', {
limit: 2,
});
result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
});
client.close(); // Close the client connection
// Set these environment variables
// WCD_HOSTNAME your Weaviate instance hostname
// WCD_API_KEY your Weaviate instance API key
// OPENAI_API_KEY your OpenAI API key
package main
import (
"context"
"fmt"
"os"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/auth"
"github.com/weaviate/weaviate-go-client/v4/weaviate/graphql"
)
func main() {
cfg := weaviate.Config{
Host: os.Getenv("WCD_HOSTNAME"),
Scheme: "https",
AuthConfig: auth.ApiKey{Value: os.Getenv("WCD_API_KEY")},
Headers: map[string]string{
"X-OpenAI-Api-Key": os.Getenv("OPENAI_API_KEY"),
},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
fmt.Println(err)
}
ctx := context.Background()
response, err := client.GraphQL().Get().
WithClassName("Question").
WithFields(
graphql.Field{Name: "question"},
graphql.Field{Name: "answer"},
graphql.Field{Name: "category"},
).
WithNearText(client.GraphQL().NearTextArgBuilder().
WithConcepts([]string{"biology"})).
WithLimit(2).
Do(ctx)
if err != nil {
panic(err)
}
fmt.Printf("%v", response)
}
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.base.Result;
import io.weaviate.client.v1.graphql.model.GraphQLResponse;
import io.weaviate.client.v1.graphql.query.argument.NearTextArgument;
import io.weaviate.client.v1.graphql.query.builder.GetBuilder;
import io.weaviate.client.v1.graphql.query.fields.Field;
import io.weaviate.client.v1.graphql.query.fields.Fields;
import java.util.HashMap;
import java.util.Map;
// Set these environment variables
// WCD_HOSTNAME Your Weaviate instance hostname
// WCD_API_KEY Your Weaviate instance API key
// OPENAI_API_KEY Your OpenAI API key
public class NearText {
public static void main(String[] args) throws Exception {
String host = System.getenv("WCD_HOSTNAME");
String apiKey = System.getenv("WCD_API_KEY");
String OpenAIKey = System.getenv("OPENAI_API_KEY");
Map<String, String> headers = new HashMap<String, String>() { {
put("X-OpenAI-Api-Key", OpenAIKey);
} };
Config config = new Config("https", host, headers);
WeaviateClient client = WeaviateAuthClient.apiKey(config, apiKey);
NearTextArgument nearText = NearTextArgument.builder()
.concepts(new String[]{"biology"})
.build();
Fields fields = Fields.builder()
.fields(new Field[]{
Field.builder().name("question").build(),
Field.builder().name("answer").build(),
})
.build();
String query = GetBuilder.builder()
.className("Question")
.fields(fields)
.withNearTextFilter(nearText)
.limit(2)
.build()
.buildQuery();
Result<GraphQLResponse> result = client.graphQL().raw().withQuery(query).run();
System.out.println(result.getResult());
}
}
# Best practice: store your credentials in environment variables
# export WCD_URL="YOUR_INSTANCE_URL" # Your Weaviate instance URL
# export WCD_API_KEY="YOUR_API_KEY" # Your Weaviate instance API key
# export OPENAI_API_KEY="YOUR_API_KEY" # Your OpenAI API key
echo '{
"query": "{
Get {
Question (
limit: 2
nearText: {
concepts: [\"biology\"],
}
) {
question
answer
category
}
}
}"
}' | tr -d "\n" | curl \
-X POST \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $WCD_API_KEY" \
-H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \
-d @- \
$WCD_URL/v1/graphql
Run this code to perform the query. Our query found entries for DNA
and species
.
Example full response in JSON format
{
{
"answer": "DNA",
"question": "In 1953 Watson & Crick built a model of the molecular structure of this, the gene-carrying substance",
"category": "SCIENCE"
},
{
"answer": "species",
"question": "2000 news: the Gunnison sage grouse isn't just another northern sage grouse, but a new one of this classification",
"category": "SCIENCE"
}
}
If you inspect the full response, you will see that the word biology
does not appear anywhere.
Even so, Weaviate was able to return biology-related entries. This is made possible by vector embeddings that capture meaning. Under the hood, semantic search is powered by vectors, or vector embeddings.
Here is a diagram showing the workflow in Weaviate.
Weaviate used the OpenAI API key to generate a vector embedding for each object during import. During the query, Weaviate similarly converted the query (biology
) into a vector.
As we mentioned above, this is optional. See Starter Guide: Bring Your Own Vectors if you would prefer to provide your own vectors.
Weaviate is capable of many types of searches. See, for example, our how-to guides on similarity searches, keyword searches, hybrid searches, and filtered searches.
3.2 Retrieval augmented generation
Retrieval augmented generation (RAG), also called generative search, combines the power of generative AI models such as large language models (LLMs) with the up-to-date truthfulness of a database.
RAG works by prompting a large language model (LLM) with a combination of a user query and data retrieved from a database.
This diagram shows the RAG workflow in Weaviate.
The following example combines the same search (for biology
) with a prompt to generate a tweet.
- Python
- JS/TS
- Go
- Java
- Curl
import weaviate
from weaviate.classes.init import Auth
import os
# Best practice: store your credentials in environment variables
wcd_url = os.environ["WCD_URL"]
wcd_api_key = os.environ["WCD_API_KEY"]
openai_api_key = os.environ["OPENAI_APIKEY"]
client = weaviate.connect_to_weaviate_cloud(
cluster_url=wcd_url, # Replace with your Weaviate Cloud URL
auth_credentials=Auth.api_key(wcd_api_key), # Replace with your Weaviate Cloud key
headers={"X-OpenAI-Api-Key": openai_api_key}, # Replace with your OpenAI API key
)
questions = client.collections.get("Question")
response = questions.generate.near_text(
query="biology",
limit=2,
grouped_task="Write a tweet with emojis about these facts."
)
print(response.generated) # Inspect the generated text
client.close() # Free up resources
import weaviate, { WeaviateClient } from 'weaviate-client';
// Best practice: store your credentials in environment variables
const wcdUrl = process.env.WCD_URL as string;
const wcdApiKey = process.env.WCD_API_KEY as string;
const openAIKey = process.env.OPENAI_APIKEY as string;
const client: WeaviateClient = await weaviate.connectToWeaviateCloud(
wcdUrl, // Replace with your Weaviate Cloud URL
{
authCredentials: new weaviate.ApiKey(wcdApiKey), // Replace with your Weaviate Cloud API key
headers: {
'X-OpenAI-Api-Key': openAIKey, // Replace with your OpenAI API key
},
}
);
const questions = client.collections.get('Question');
const result = await questions.generate.nearText(
'biology',
{
groupedTask: 'Write a tweet with emojis about these facts.',
},
{
limit: 2,
}
);
console.log(result.generated);
client.close(); // Close the client connection
// Set these environment variables
// WCD_HOSTNAME your Weaviate instance hostname
// WCD_API_KEY your Weaviate instance API key
// OPENAI_API_KEY your OpenAI API key
package main
import (
"context"
"fmt"
"os"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/auth"
"github.com/weaviate/weaviate-go-client/v4/weaviate/graphql"
)
func main() {
cfg := weaviate.Config{
Host: os.Getenv("WCD_HOSTNAME"),
Scheme: "https",
AuthConfig: auth.ApiKey{Value: os.Getenv("WCD_API_KEY")},
Headers: map[string]string{
"X-OpenAI-Api-Key": os.Getenv("OPENAI_API_KEY"),
},
}
client, err := weaviate.NewClient(cfg)
if err != nil {
fmt.Println(err)
}
ctx := context.Background()
generatePrompt := "Write a tweet with emojis about these facts."
gs := graphql.NewGenerativeSearch().GroupedResult(generatePrompt)
response, err := client.GraphQL().Get().
WithClassName("Question").
WithFields(
graphql.Field{Name: "question"},
graphql.Field{Name: "answer"},
graphql.Field{Name: "category"},
).
WithGenerativeSearch(gs).
WithNearText(client.GraphQL().NearTextArgBuilder().
WithConcepts([]string{"biology"})).
WithLimit(2).
Do(ctx)
if err != nil {
panic(err)
}
fmt.Printf("%v", response)
}
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.base.Result;
import io.weaviate.client.v1.graphql.model.GraphQLResponse;
import io.weaviate.client.v1.graphql.query.argument.NearTextArgument;
import io.weaviate.client.v1.graphql.query.builder.GetBuilder;
import io.weaviate.client.v1.graphql.query.fields.Field;
import io.weaviate.client.v1.graphql.query.fields.Fields;
import io.weaviate.client.v1.graphql.query.fields.GenerativeSearchBuilder;
import java.util.HashMap;
import java.util.Map;
// Set these environment variables
// WCD_HOSTNAME Your Weaviate instance hostname
// WCD_API_KEY Your Weaviate instance API key
// OPENAI_API_KEY Your OpenAI API key
public class RAG {
public static void main(String[] args) throws Exception {
String host = System.getenv("WCD_HOSTNAME");
String apiKey = System.getenv("WCD_API_KEY");
String OpenAIKey = System.getenv("OPENAI_API_KEY");
Map<String, String> headers = new HashMap<String, String>() { {
put("X-OpenAI-Api-Key", OpenAIKey);
} };
Config config = new Config("https", host, headers);
WeaviateClient client = WeaviateAuthClient.apiKey(config, apiKey);
NearTextArgument nearText = NearTextArgument.builder()
.concepts(new String[]{"biology"})
.build();
GenerativeSearchBuilder ragQuery = GenerativeSearchBuilder.builder()
.groupedResultTask("Write a tweet with emojis about these facts.")
.build();
Fields fields = Fields.builder()
.fields(new Field[]{
Field.builder().name("question").build(),
Field.builder().name("answer").build(),
})
.build();
String query = GetBuilder.builder()
.className("Question")
.fields(fields)
.withNearTextFilter(nearText)
.withGenerativeSearch(ragQuery)
.limit(2)
.build()
.buildQuery();
Result<GraphQLResponse> result = client.graphQL().raw().withQuery(query).run();
System.out.println(result.getResult());
}
}
# Best practice: store your credentials in environment variables
# export WCD_URL="YOUR_INSTANCE_URL" # Your Weaviate instance URL
# export WCD_API_KEY="YOUR_API_KEY" # Your Weaviate instance API key
# export OPENAI_API_KEY="YOUR_API_KEY" # Your OpenAI API key
echo '{
"query": "{
Get {
Question (
limit: 2
nearText: {
concepts: [\"biology\"],
}
) {
question
answer
category
_additional {
generate(
groupedResult: {
task: \"\"\"
Write a tweet with emojis about these facts.
\"\"\"
}
) {
groupedResult
error
}
}
}
}
}"
}' | tr -d "\n" | curl \
-X POST \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $WCD_API_KEY" \
-H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \
-d @- \
$WCD_URL/v1/graphql
Run this code to perform the query. Here is one possible response (your response will likely be different).
🧬 In 1953 Watson & Crick built a model of the molecular structure of DNA, the gene-carrying substance! 🧬🔬
🦢 2000 news: the Gunnison sage grouse isn't just another northern sage grouse, but a new species! 🦢🌿 #ScienceFacts #DNA #SpeciesClassification
The response should be new, yet familiar. This because you have seen the entries above for DNA
and species
in the semantic search section.
The power of RAG comes from the ability to transform your own data. Weaviate helps you in this journey by making it easy to perform a combined search & generation in just a few lines of code.
Recap
In this quickstart guide, you:
- Created a Serverless Weaviate sandbox instance on Weaviate Cloud.
- Defined a collection and added data.
- Performed queries, including:
- Semantic search, and
- Retrieval augmented generation.
Where to go next is up to you. We include some suggested steps and resources below.
Next
Try these additional resources to learn more about Weaviate:
More on search
See how to perform searches, such as keyword, similarity, hybrid, image, filtered and reranked searches.
Manage data
See how to manage data, such as manage collections, create objects, batch import data and use multi-tenancy.
RAG
Check out the Starter guide: retrieval augmented generation, and the Weaviate Academy unit on chunking.
Workshops and office hours
We hold in-person and online workshops, office hours and events for different experience levels. Join us!
FAQs & Troubleshooting
We provide answers to some common questions, or potential issues below.
Questions
Can I use different integrations?
See answer
In this example, we use the OpenAI
inference API. But you can use others.
If you do want to change the embeddings, or the generative AI integrations, you can. You will need to:
- Ensure that the Weaviate module is available in the Weaviate instance you are using,
- Modify your collection definition to use your preferred integration, and
- Make sure to use the right API key(s) (if necessary) for your integration.
See the model providers integration section for more information.
Troubleshooting
If you see Error: Name 'Question' already used as a name for an Object class
See answer
You may see this error if you try to create a collection that already exists in your instance of Weaviate. In this case, you can follow these instructions to delete the collection.
You can delete any unwanted collection(s), along with the data that they contain.
When you delete a collection, you delete all associated objects!
Be very careful with deletes on a production database and anywhere else that you have important data.
This code deletes a collection and its objects.
- Python Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- Go
- Java
- Curl
# collection_name can be a string ("Article") or a list of strings (["Article", "Category"])
client.collections.delete(collection_name) # THIS WILL DELETE THE SPECIFIED COLLECTION(S) AND THEIR OBJECTS
# Note: you can also delete all collections in the Weaviate instance with:
# client.collections.delete_all()
# delete class "Article" - THIS WILL DELETE ALL DATA IN THIS CLASS
client.schema.delete_class("Article") # Replace with your class name
// delete collection "Article" - THIS WILL DELETE THE COLLECTION AND ALL ITS DATA
await client.collections.delete('Article')
// you can also delete all collections of a cluster
// await client.collections.deleteAll()
// delete collection "Article" - THIS WILL DELETE THE COLLECTION AND ALL ITS DATA
await client.schema
.classDeleter()
.withClassName('Article')
.do();
className := "YourClassName"
// delete the class
if err := client.Schema().ClassDeleter().WithClassName(className).Do(context.Background()); err != nil {
// Weaviate will return a 400 if the class does not exist, so this is allowed, only return an error if it's not a 400
if status, ok := err.(*fault.WeaviateClientError); ok && status.StatusCode != http.StatusBadRequest {
panic(err)
}
}
Result<Boolean> result = client.schema().classDeleter()
.withClassName(className)
.run();
curl \
-X DELETE \
https://WEAVIATE_INSTANCE_URL/v1/schema/YourClassName # Replace WEAVIATE_INSTANCE_URL with your instance URL
How to confirm collection creation
See answer
If you are not sure whether the collection has been created, check the schema
endpoint.
Replace WEAVIATE_INSTANCE_URL with your instance's REST Endpoint URL.:
https://WEAVIATE_INSTANCE_URL/v1/schema
You should see:
{
"classes": [
{
"class": "Question",
... // truncated additional information here
"vectorizer": "text2vec-openai"
}
]
}
Where the schema should indicate that the Question
collection has been added.
Weaviate uses a combination of RESTful and GraphQL APIs. In Weaviate, RESTful API endpoints can be used to add data or obtain information about the Weaviate instance, and the GraphQL interface to retrieve data.
How to confirm data import
See answer
To confirm successful data import, check the objects
endpoint to verify that all objects are imported.
Replace WEAVIATE_INSTANCE_URL with your instance REST Endpoint URL:
https://WEAVIATE_INSTANCE_URL/v1/objects
You should see:
{
"deprecations": null,
"objects": [
... // Details of each object
],
"totalResults": 10 // You should see 10 results here
}
Where you should be able to confirm that you have imported all 10
objects.
If the nearText
search is not working
See answer
To perform text-based (nearText
) similarity searches, you need to have a vectorizer enabled, and configured in your collection.
Make sure the vectorizer is configured like this.
If the search still doesn't work, contact us!
Questions and feedback
If you have any questions or feedback, let us know in the user forum.