Last active
August 21, 2025 16:01
-
-
Save lazamar/6749fc7fadfb95ae72ff9ca5f6c88107 to your computer and use it in GitHub Desktop.
Weaviate test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code from following the Cloud Quickstart
Results