Skip to main content
Weaviate Docs (migrated from docs.weaviate.io) Docs

Search documentation

Type to search this documentation.

On this pageOverview

Quick tour of Weaviate

This tutorial will show you how to use Weaviate Cloud to:

  1. Set up a Weaviate instance.
  2. Import and vectorize your data.
  3. Perform a semantic search and retrieval augmented generation (RAG).

In order to perform Retrieval Augmented Generation (RAG) in the last step, you will need an OpenAI account and an OpenAI API key.

If you have another preferred model provider, you can use that instead of OpenAI.

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.

Install the latest, Python client v4, by adding weaviate-client to your Python environment with pip:

Bash
pip install -U weaviate-client

Install the latest, JS/TS client v3, by adding weaviate-client to your project with npm:

Bash
npm install weaviate-client

Add weaviate-go-client to your project with go get:

Bash
go get github.com/weaviate/weaviate-go-client/v5

Add this dependency to your project:

xml
<dependency>
  <groupId>io.weaviate</groupId>
  <artifactId>client6</artifactId>
  <version>6.2.0</version> <!-- Check latest version: https://github.com/weaviate/java-client -->
</dependency>

Add this package to your project:

xml
<PackageReference Include="Weaviate.Client" Version="1.0.0" />   <!-- Check latest version: https://github.com/weaviate/java-client -->
How to create a Weaviate Cloud free cluster

Go to the Weaviate Cloud console and create a free cluster.

Now you can connect to your Weaviate instance. You will need the:

  • REST Endpoint URL and the
  • Administrator API Key.

You can retrieve them both from the WCD console as shown in the interactive example below.

Once you have the REST Endpoint URL and the admin API key, you can connect to your cluster, and work with Weaviate.

The example below shows how to connect to Weaviate and perform a basic operation, like checking the cluster status.

quickstart_check_readiness.py
import weaviatefrom weaviate.classes.init import Authimport os# Best practice: store your credentials in environment variablesweaviate_url = os.environ["WEAVIATE_URL"]weaviate_api_key = os.environ["WEAVIATE_API_KEY"]client = weaviate.connect_to_weaviate_cloud(    cluster_url=weaviate_url,    auth_credentials=Auth.api_key(weaviate_api_key),)print(client.is_ready())  # Should print: `True`client.close()  # Free up resources
quickstart_check_readiness.ts
import weaviate, { WeaviateClient } from 'weaviate-client';// Best practice: store your credentials in environment variablesconst weaviateUrl = process.env.WEAVIATE_URL as string;const weaviateApiKey = process.env.WEAVIATE_API_KEY as string;const client: WeaviateClient = await weaviate.connectToWeaviateCloud(  weaviateUrl, // Replace with your Weaviate Cloud URL  {    authCredentials: new weaviate.ApiKey(weaviateApiKey), // Replace with your Weaviate Cloud API key  });var clientReadiness = await client.isReady();console.log(clientReadiness); // Should return `true`client.close(); // Close the client connection
quickstart/1_check_readiness/main.go
// Set these environment variables// WEAVIATE_HOSTNAME      your Weaviate instance hostname// WEAVIATE_API_KEY      your Weaviate instance API keypackage mainimport (  "context"  "fmt"  "os"  "github.com/weaviate/weaviate-go-client/v5/weaviate"  "github.com/weaviate/weaviate-go-client/v5/weaviate/auth")func main() {  cfg := weaviate.Config{    Host:       os.Getenv("WEAVIATE_HOSTNAME"),    Scheme:     "https",    AuthConfig: auth.ApiKey{Value: os.Getenv("WEAVIATE_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)}
Java
// Best practice: store your credentials in environment variablesString weaviateUrl = System.getenv("WEAVIATE_URL");String weaviateApiKey = System.getenv("WEAVIATE_API_KEY");WeaviateClient client = WeaviateClient.connectToWeaviateCloud(    weaviateUrl, // Replace with your Weaviate Cloud URL    weaviateApiKey // Replace with your Weaviate Cloud key);System.out.println(client.isReady()); // Should print: `True`client.close(); // Free up resources
C#
// Best practice: store your credentials in environment variablesstring weaviateUrl = Environment.GetEnvironmentVariable("WEAVIATE_URL");string weaviateApiKey = Environment.GetEnvironmentVariable("WEAVIATE_API_KEY");WeaviateClient client = await Connect.Cloud(weaviateUrl, weaviateApiKey);// GetMeta returns server info. A successful call indicates readiness.var meta = await client.IsReady();Console.WriteLine(meta);
Bash
# Best practice: store your credentials in environment variables
# export WEAVIATE_URL="YOUR_INSTANCE_URL"  # Your Weaviate instance URL
# export WEAVIATE_API_KEY="YOUR_API_KEY"   # Your Weaviate instance API key

curl -w "\nResponse code: %{http_code}\n" \
  -H "Authorization: Bearer $WEAVIATE_API_KEY" \
  $WEAVIATE_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.

Now, we can populate our database by first defining a collection and then adding data.

The following example creates a collection called Question with:

quickstart_create_collection.py
import weaviate
from weaviate.classes.init import Auth
from weaviate.classes.config import Configure
import os

# Best practice: store your credentials in environment variables
weaviate_url = os.environ["WEAVIATE_URL"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]

client = weaviate.connect_to_weaviate_cloud(
    cluster_url=weaviate_url,  # Replace with your Weaviate Cloud URL
    auth_credentials=Auth.api_key(weaviate_api_key),  # Replace with your Weaviate Cloud key
)
quickstart_create_collection.ts
import weaviate, { WeaviateClient, vectors } from 'weaviate-client';

// Best practice: store your credentials in environment variables
const weaviateUrl = process.env.WEAVIATE_URL as string;
const weaviateApiKey = process.env.WEAVIATE_API_KEY as string;

const client: WeaviateClient = await weaviate.connectToWeaviateCloud(
  weaviateUrl, // Replace with your Weaviate Cloud URL
  {
    authCredentials: new weaviate.ApiKey(weaviateApiKey), // Replace with your Weaviate Cloud API key
  }
);

The collection also contains a configuration for the generative (RAG) integration:

quickstart/2_1_create_collection/main.go
// Set these environment variables
// WEAVIATE_HOSTNAME      your Weaviate instance hostname
// WEAVIATE_API_KEY      your Weaviate instance API key

package main

import (
  "context"
  "fmt"
  "os"

  "github.com/weaviate/weaviate-go-client/v5/weaviate"
  "github.com/weaviate/weaviate-go-client/v5/weaviate/auth"
  "github.com/weaviate/weaviate/entities/models"
)

func main() {
  cfg := weaviate.Config{
    Host:       os.Getenv("WEAVIATE_HOSTNAME"),
    Scheme:     "https",
    AuthConfig: auth.ApiKey{Value: os.Getenv("WEAVIATE_API_KEY")},
  }

  client, err := weaviate.NewClient(cfg)
  if err != nil {
    fmt.Println(err)
  }
Java
// Best practice: store your credentials in environment variablesString weaviateUrl = System.getenv("WEAVIATE_URL");String weaviateApiKey = System.getenv("WEAVIATE_API_KEY");WeaviateClient client = WeaviateClient.connectToWeaviateCloud(    weaviateUrl, // Replace with your Weaviate Cloud URL    weaviateApiKey // Replace with your Weaviate Cloud key);String collectionName = "Question";try { client.collections.delete(collectionName); } catch (Exception ignored) {} // Clean up from any previous runclient.collections.create(    collectionName,    col -> col        .vectorConfig(VectorConfig.text2vecWeaviate()) // Configure the Weaviate Embeddings integration        .generativeModule(Generative.openai()) // Configure the OpenAI generative AI integration);CollectionHandle<Map<String, Object>> questions = client.collections.use(collectionName);
C#
var questions = await client.Collections.Create(    new CollectionCreateParams    {        Name = collectionName,        Properties =        [            Property.Text("answer"),            Property.Text("question"),            Property.Text("category"),        ],        VectorConfig = Configure.Vector("default", v => v.Text2VecWeaviate()), // Configure the Weaviate Embeddings integration        GenerativeConfig = Configure.Generative.OpenAI(), // Configure the OpenAI generative AI integration    });

The collection also contains a configuration for the generative (RAG) integration:

Bash
# Best practice: store your credentials in environment variables
# export WEAVIATE_URL="YOUR_INSTANCE_URL"  # Your Weaviate instance URL
# export WEAVIATE_API_KEY="YOUR_API_KEY"   # Your Weaviate instance API key

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $WEAVIATE_API_KEY" \
-d '{
  "class": "Question",
  "vectorizer": "text2vec-weaviate",
  "moduleConfig": {
    "text2vec-weaviate": {},
    "generative-openai": {}
  }
}' \
"$WEAVIATE_URL/v1/schema"

Run this code to create the collection to which you can add data.

Do you prefer a different setup?

We can now add data to our collection.

The following example:

  • Loads objects, and
  • Adds objects to the target collection (Question) with a batch import.

Every import reports whether any objects failed. Check for failures in your own code to catch problems such as malformed data or a misconfigured model provider.

quickstart_import.py
import weaviatefrom weaviate.classes.init import Authimport requests, json, os# Best practice: store your credentials in environment variablesweaviate_url = os.environ["WEAVIATE_URL"]weaviate_api_key = os.environ["WEAVIATE_API_KEY"]client = weaviate.connect_to_weaviate_cloud(    cluster_url=weaviate_url,                                    # Replace with your Weaviate Cloud URL    auth_credentials=Auth.api_key(weaviate_api_key),             # Replace with your Weaviate Cloud key)resp = requests.get(    "https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json")data = json.loads(resp.text)questions = client.collections.use("Question")result = questions.data.ingest(    [        {            "answer": d["Answer"],            "question": d["Question"],            "category": d["Category"],        }        for d in data    ])# `errors` holds one entry per failed object, keyed by its position in the inputif result.errors:    print(f"Number of failed imports: {len(result.errors)}")    for index, error in result.errors.items():        print(f"Failed object at index {index}: {error.message}")client.close()  # Free up resources

data.ingest() returns a BatchObjectReturn. Read result.errors to check for failures. It holds one entry per failed object, keyed by its position in the input. For more, see error handling in the Python client reference.

quickstart_import.ts
import weaviate, { WeaviateClient } from 'weaviate-client';// Best practice: store your credentials in environment variablesconst weaviateUrl = process.env.WEAVIATE_URL as string;const weaviateApiKey = process.env.WEAVIATE_API_KEY as string;const client: WeaviateClient = await weaviate.connectToWeaviateCloud(  weaviateUrl, // Replace with your Weaviate Cloud URL  {    authCredentials: new weaviate.ApiKey(weaviateApiKey), // Replace with your Weaviate Cloud API key  });// Load dataasync function getJsonData() {  const file = await fetch(    'https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json'  );  return file.json();}async function importQuestions() {  const questions = client.collections.use('Question');  const data = await getJsonData();  // `ingest` imports the list using server-side batching  const result = await questions.data.ingest(    data.map((properties) => ({ properties }))  );  if (result.hasErrors) {    console.log(`Number of failed imports: ${Object.keys(result.errors).length}`);    // `errors` is keyed by the position of the object in the input    for (const [index, error] of Object.entries(result.errors)) {      console.log(`Failed object at index ${index}: ${error.message}`);    }  }}await importQuestions();client.close(); // Close the client connection

data.ingest() returns a result object. Read result.hasErrors for a quick check, and result.errors for one entry per failed object, keyed by its position in the input.

quickstart/2_2_import/main.go
// Set these environment variables// WEAVIATE_HOSTNAME      your Weaviate instance hostname// WEAVIATE_API_KEY      your Weaviate instance API keypackage mainimport (  "context"  "encoding/json"  "fmt"  "net/http"  "os"  "github.com/weaviate/weaviate-go-client/v5/weaviate"  "github.com/weaviate/weaviate-go-client/v5/weaviate/auth"  "github.com/weaviate/weaviate/entities/models")func main() {  cfg := weaviate.Config{    Host:       os.Getenv("WEAVIATE_HOSTNAME"),    Scheme:     "https",    AuthConfig: auth.ApiKey{Value: os.Getenv("WEAVIATE_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)    }  }}
Java
// Best practice: store your credentials in environment variablesString weaviateUrl = System.getenv("WEAVIATE_URL");String weaviateApiKey = System.getenv("WEAVIATE_API_KEY");WeaviateClient client = WeaviateClient.connectToWeaviateCloud(    weaviateUrl, // Replace with your Weaviate Cloud URL    weaviateApiKey // Replace with your Weaviate Cloud key);// Create the collectionString collectionName = "Question";try { client.collections.delete(collectionName); } catch (Exception ignored) {} // Clean up from any previous runclient.collections.create(collectionName, col -> col    .properties(        Property.text("answer"),        Property.text("question"),        Property.text("category"))    .vectorConfig(VectorConfig.text2vecWeaviate())); // Configure the Weaviate Embeddings integration;// Get JSON data using HttpURLConnectionURL url = URI.create("https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json").toURL();HttpURLConnection connection = (HttpURLConnection) url.openConnection();String jsonData;try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {  jsonData = reader.lines().reduce("", String::concat);}CollectionHandle<Map<String, Object>> questions = client.collections.use(collectionName);List<Map<String, Object>> questionsToInsert = new ArrayList<>();// Parse and prepare objects using org.jsonnew JSONArray(jsonData).forEach(item -> {  JSONObject json = (JSONObject) item;  Map<String, Object> properties = new HashMap<>();  properties.put("answer", json.getString("Answer"));  properties.put("question", json.getString("Question"));  properties.put("category", json.getString("Category"));  questionsToInsert.add(properties);});// `batch.start()` opens a server-side batchBatchContext<Map<String, Object>> batch = questions.batch.start();// Closing the batch sends the remaining objects and waits for the resultstry (batch) {  for (Map<String, Object> properties : questionsToInsert) {    batch.add(WeaviateObject.<Map<String, Object>>of(o -> o.properties(properties)));  }}// Check for errorsif (batch.numberOfErrors() > 0) {  System.err.printf("Number of failed imports: %d\n", batch.numberOfErrors());} else {  System.out.printf("Successfully inserted %d objects.\n", questionsToInsert.size());}

batch.start() opens a server-side batch. Closing the batch sends any remaining objects and waits for the server to report the results. Read batch.numberOfErrors() after the batch closes; before then, the tally is incomplete.

C#
// Get JSON data using HttpClientusing var httpClient = new HttpClient();var jsonData = await httpClient.GetStringAsync(    "https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json");var questionsToInsert = new List<object>();// Parse and prepare objects using System.Text.Jsonvar jsonObjects = JsonSerializer.Deserialize<List<Dictionary<string, JsonElement>>>(    jsonData);foreach (var jsonObj in jsonObjects){    questionsToInsert.Add(        new        {            answer = jsonObj["Answer"].GetString(),            question = jsonObj["Question"].GetString(),            category = jsonObj["Category"].GetString(),        }    );}// `Batch.InsertMany` imports the list using server-side batchingvar insertResponse = await questions.Batch.InsertMany(questionsToInsert);// Check for errorsif (insertResponse.HasErrors){    Console.WriteLine($"Number of failed imports: {insertResponse.Errors.Count()}");    // `Objects` holds one entry per object; `Index` is the position of the object in the input    foreach (var entry in insertResponse.Objects.Where(o => o.Error is not null))    {        Console.WriteLine($"Failed object at index {entry.Index}: {entry.Error.Message}");    }}else{    Console.WriteLine($"Successfully inserted {insertResponse.Count} objects.");}

Batch.InsertMany() returns a BatchInsertResponse. Read HasErrors for a quick check, Errors for the failures alone, and Objects for one entry per object. Each entry's Index is its position in the input, and failed entries carry an Error.

Bash
# Best practice: store your credentials in environment variables
# export WEAVIATE_URL="YOUR_INSTANCE_URL"  # Your Weaviate instance URL
# export WEAVIATE_API_KEY="YOUR_API_KEY"   # Your Weaviate instance API key

# Set batch size
BATCH_ENDPOINT="$WEAVIATE_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 $WEAVIATE_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 $WEAVIATE_API_KEY" \
       -d "$batch_data"
  echo "" # Print a newline for better output formatting
fi

Run this code to add the demo data.

Weaviate provides a wide range of query tools to help you find the right data. We will try a few searches here.

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
import weaviatefrom weaviate.classes.init import Authimport os, json# Best practice: store your credentials in environment variablesweaviate_url = os.environ["WEAVIATE_URL"]weaviate_api_key = os.environ["WEAVIATE_API_KEY"]client = weaviate.connect_to_weaviate_cloud(    cluster_url=weaviate_url,                                    # Replace with your Weaviate Cloud URL    auth_credentials=Auth.api_key(weaviate_api_key),             # Replace with your Weaviate Cloud key)questions = client.collections.use("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
JavaScript/TypeScript
import weaviate, { WeaviateClient } from 'weaviate-client';// Best practice: store your credentials in environment variablesconst weaviateUrl = process.env.WEAVIATE_URL as string;const weaviateApiKey = process.env.WEAVIATE_API_KEY as string;const client: WeaviateClient = await weaviate.connectToWeaviateCloud(  weaviateUrl, // Replace with your Weaviate Cloud URL  {    authCredentials: new weaviate.ApiKey(weaviateApiKey), // Replace with your Weaviate Cloud API key  });const questions = client.collections.use('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
Go
// Set these environment variables// WEAVIATE_HOSTNAME      your Weaviate instance hostname// WEAVIATE_API_KEY      your Weaviate instance API keypackage mainimport (  "context"  "fmt"  "os"  "github.com/weaviate/weaviate-go-client/v5/weaviate"  "github.com/weaviate/weaviate-go-client/v5/weaviate/auth"  "github.com/weaviate/weaviate-go-client/v5/weaviate/graphql")func main() {  cfg := weaviate.Config{    Host:       os.Getenv("WEAVIATE_HOSTNAME"),    Scheme:     "https",    AuthConfig: auth.ApiKey{Value: os.Getenv("WEAVIATE_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)}
Java
// Best practice: store your credentials in environment variablesString weaviateUrl = System.getenv("WEAVIATE_URL");String weaviateApiKey = System.getenv("WEAVIATE_API_KEY");WeaviateClient client = WeaviateClient.connectToWeaviateCloud(    weaviateUrl, // Replace with your Weaviate Cloud URL    weaviateApiKey // Replace with your Weaviate Cloud key);String collectionName = "Question";var questions = client.collections.use(collectionName);var response = questions.query.nearText("biology", q -> q.limit(2));for (var obj : response.objects()) {  System.out.println(obj.properties());}
C#
var response = await questions.Query.NearText("biology", limit: 2);foreach (var obj in response.Objects){    Console.WriteLine(JsonSerializer.Serialize(obj.Properties));}
Curl
# Best practice: store your credentials in environment variables
# export WEAVIATE_URL="YOUR_INSTANCE_URL"    # Your Weaviate instance URL
# export WEAVIATE_API_KEY="YOUR_API_KEY"     # Your Weaviate instance 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 $WEAVIATE_API_KEY" \
    -d @- \
    $WEAVIATE_URL/v1/graphql

Run this code to perform the query. Our query found entries for DNA and species.

Example response
JSON
{
  "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.

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
import osimport weaviatefrom weaviate.classes.init import Authfrom weaviate.classes.generate import GenerativeConfig# Best practice: store your credentials in environment variablesweaviate_url = os.environ["WEAVIATE_URL"]weaviate_api_key = os.environ["WEAVIATE_API_KEY"]openai_api_key = os.environ["OPENAI_API_KEY"]client = weaviate.connect_to_weaviate_cloud(    cluster_url=weaviate_url,  # Replace with your Weaviate Cloud URL    auth_credentials=Auth.api_key(        weaviate_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.use("Question")response = questions.generate.near_text(    query="biology",    limit=2,    grouped_task="Write a tweet with emojis about these facts.",    generative_provider=GenerativeConfig.openai(),  # Configure the OpenAI generative integration for RAG)print(response.generative.text)  # Inspect the generated textclient.close()  # Free up resources
JavaScript/TypeScript
import weaviate, { WeaviateClient, generativeParameters } from 'weaviate-client';// Best practice: store your credentials in environment variablesconst weaviateUrl = process.env.WEAVIATE_URL as string;const weaviateApiKey = process.env.WEAVIATE_API_KEY as string;const openAiKey = process.env.OPENAI_API_KEY as string;const client: WeaviateClient = await weaviate.connectToWeaviateCloud(  weaviateUrl, // Replace with your Weaviate Cloud URL  {    authCredentials: new weaviate.ApiKey(weaviateApiKey), // Replace with your Weaviate Cloud API key    headers: {      'X-OpenAI-Api-Key': openAiKey, // Replace with your OpenAI API key    },  });const questions = client.collections.use('Question');const result = await questions.generate.nearText(  'biology',  {    groupedTask: 'Write a tweet with emojis about these facts.',    config: generativeParameters.openAI(),  },  {    limit: 2,  });console.log(result.generative);client.close(); // Close the client connection
Go
// Set these environment variables// WEAVIATE_HOSTNAME      your Weaviate instance hostname// WEAVIATE_API_KEY      your Weaviate instance API key// OPENAI_API_KEY       your OpenAI API keypackage mainimport (  "context"  "fmt"  "os"  "github.com/weaviate/weaviate-go-client/v5/weaviate"  "github.com/weaviate/weaviate-go-client/v5/weaviate/auth"  "github.com/weaviate/weaviate-go-client/v5/weaviate/graphql")func main() {  cfg := weaviate.Config{    Host:       os.Getenv("WEAVIATE_HOSTNAME"),    Scheme:     "https",    AuthConfig: auth.ApiKey{Value: os.Getenv("WEAVIATE_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)}
Java
// Best practice: store your credentials in environment variablesString weaviateUrl = System.getenv("WEAVIATE_URL");String weaviateApiKey = System.getenv("WEAVIATE_API_KEY");String openaiApiKey = System.getenv("OPENAI_API_KEY");WeaviateClient client = WeaviateClient.connectToWeaviateCloud(    weaviateUrl, // Replace with your Weaviate Cloud URL    weaviateApiKey, // Replace with your Weaviate Cloud key    config -> config.setHeaders(        Map.of("X-OpenAI-Api-Key", openaiApiKey)) // Replace with your OpenAI API key);CollectionHandle<Map<String, Object>> questions = client.collections.use("Question");var response = questions.generate.nearText(    "biology",    // Query configuration (nearText and limit)    q -> q.limit(2),    // Generative configuration (the RAG task)    g -> g.groupedTask(        "Write a tweet with emojis about these facts.",        c -> c.generativeProvider(GenerativeProvider.openai(o -> o))));// Use `.generative()` to access the generated textSystem.out.println(response.generative().text());client.close(); // Free up resources
C#
var ragResponse = await questions.Generate.NearText(    "biology",    limit: 2,    groupedTask: new GroupedTask("Write a tweet with emojis about these facts."),    provider: new Providers.OpenAI() { });// Inspect the resultsConsole.WriteLine(JsonSerializer.Serialize(ragResponse.Generative.Values));
Curl
# Best practice: store your credentials in environment variables
# export WEAVIATE_URL="YOUR_INSTANCE_URL"    # Your Weaviate instance URL
# export WEAVIATE_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 $WEAVIATE_API_KEY" \
    -H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \
    -d @- \
    $WEAVIATE_URL/v1/graphql

Run this code to perform the query. Here is one possible response (your response will likely be different).

text
🧬 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 is 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.

In this quickstart guide, you:

  • Created a free cluster 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.

Have a question or feedback? Here's how to reach us.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu