Skip to content

Instantly share code, notes, and snippets.

@lazamar
Last active August 21, 2025 16:01
Show Gist options
  • Save lazamar/6749fc7fadfb95ae72ff9ca5f6c88107 to your computer and use it in GitHub Desktop.
Save lazamar/6749fc7fadfb95ae72ff9ca5f6c88107 to your computer and use it in GitHub Desktop.
Weaviate test
import weaviate, { WeaviateClient, Collection, vectors, generative } 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;
async function importData(collection: Collection) {
const data : { sentence: string }[] = [
// Nationalities
"British",
"German",
"American",
"Austrian",
"Brazilian",
"Honduran",
// Games
"Roulette",
"Blackjack",
"Poker",
"Slot machine",
"Capture the flag",
"Tag",
"Rock paper scissors",
"Musical chairs",
// Miscellaneous
"Death",
"Life",
"Chance",
"Luck",
// Risk sentences
"It’s not worth the danger.",
"Gloss paint can burn strongly and create a fire hazard.",
"What parent wouldn’t put their own life on the line for their child’s?",
"The project could fail if we don’t speed up.",
"I can’t stay longer or I’ll end up missing my train.",
"What are the chances of him catching the illness?",
"Lower the chances of heart disease.",
"That customer is not a reliable prospect.",
"We can’t put everything on the line for just a chance that we’re being spied on.",
"Let’s go for it anyway.",
// Safe sentences
"It’s better to play it safe.",
"Let’s take the cautious approach.",
"I’d rather be safe than sorry.",
"We should err on the side of caution.",
"Let’s stick with the sure thing.",
"It’s wiser to avoid unnecessary danger.",
"Better to double-check than regret later.",
"We should proceed carefully.",
"Taking precautions now will save trouble later.",
"Let’s choose the safest option.",
].map(sentence => ({ sentence }));
const result = await collection.data.insertMany(data);
console.log("Insertion response: ", result);
}
async function createCollection(name : string, client: WeaviateClient) {
// Create collection
await client.collections.create({
name,
vectorizers: vectors.text2VecWeaviate(),
generative: generative.cohere(),
});
}
async function main() {
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 name = "Sentences"
await createCollection(name, client)
const sentences = client.collections.use(name);
// Add data
await importData(sentences)
// Wait for collection ready
var clientReadiness = await client.isReady();
// Make query
const result = await sentences.query.nearText('Russian roulette', { limit: 20, });
console.log(JSON.stringify(result, null, 4))
client.close(); // Close the client connection
}
main();
@lazamar
Copy link
Author

lazamar commented Aug 21, 2025

Code from following the Cloud Quickstart
Results

{
    "objects": [
        {
            "metadata": {},
            "properties": {
                "sentence": "Roulette"
            },
            "uuid": "1c9ac83b-d5c6-46a6-a2ff-b69d78a25680",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Slot machine"
            },
            "uuid": "e8e4f257-c358-4841-b3d4-89b76372f21e",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Blackjack"
            },
            "uuid": "0b8cf1f2-24bb-4958-8093-fad9b20c8129",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Poker"
            },
            "uuid": "b5e2bf6b-67a7-48db-81f1-592f8cba12e5",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "That customer is not a reliable prospect."
            },
            "uuid": "e53682d2-e312-433b-aca5-314d16214f4f",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Luck"
            },
            "uuid": "f9e4528b-08e7-4a03-bfa2-d9f18e361709",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "It’s better to play it safe."
            },
            "uuid": "ca7577f7-5e63-4e22-aefa-8f121495253e",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "British"
            },
            "uuid": "2bf886c1-5eef-467c-b1ac-c0b3198f81bd",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Rock paper scissors"
            },
            "uuid": "81b244db-ec7e-45dd-9866-fa1783d909fe",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "We can’t put everything on the line for just a chance that we’re being spied on."
            },
            "uuid": "82a9aa45-30f1-4186-96dc-759b9ed2a4f0",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "American"
            },
            "uuid": "9c1ed604-cd08-4581-834c-5ac9649bd530",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "We should err on the side of caution."
            },
            "uuid": "62447733-b6b3-4ea2-b579-eed2d23132d7",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "It’s wiser to avoid unnecessary danger."
            },
            "uuid": "88fb08d6-ea40-4604-86e1-ff6b6da37354",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Chance"
            },
            "uuid": "3f065d7f-e4be-4e07-b8a3-ac0a9f34854d",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "I’d rather be safe than sorry."
            },
            "uuid": "aa02424e-9270-457b-9fa0-5766e345e480",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Austrian"
            },
            "uuid": "319c7a8a-5d33-411f-8814-fd6b8425e5dc",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "It’s not worth the danger."
            },
            "uuid": "daf92e6a-3ed1-425e-abc2-49a2183744c8",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "I can’t stay longer or I’ll end up missing my train."
            },
            "uuid": "c24e44e7-a806-42ef-9e71-ffe812fc29a1",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Let’s take the cautious approach."
            },
            "uuid": "902a50d9-7abf-47b3-9dd8-674c68b4caa9",
            "vectors": {}
        },
        {
            "metadata": {},
            "properties": {
                "sentence": "Better to double-check than regret later."
            },
            "uuid": "9240afa6-3262-4fce-973e-b1c4f986291c",
            "vectors": {}
        }
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment