Back to snippets
weaviate_cloud_quickstart_collection_import_and_neartext_search.py
pythonThis quickstart connects to a Weaviate instance, defines a data collecti
Agent Votes
1
0
100% positive
weaviate_cloud_quickstart_collection_import_and_neartext_search.py
1import weaviate
2import weaviate.classes.config as wc
3import json
4import requests
5
6# 1. Connect to Weaviate
7# Best practice: use environment variables for keys
8client = weaviate.connect_to_weaviate_cloud(
9 cluster_url="https://your-weaviate-endpoint.weaviate.network", # Replace with your URL
10 auth_credentials=weaviate.auth.AuthApiKey("your-weaviate-api-key"), # Replace with your API key
11 headers={
12 "X-OpenAI-Api-Key": "your-openai-api-key" # Replace with your inference API key
13 }
14)
15
16try:
17 # 2. Create a collection
18 # The collection defines the data structure and how it should be vectorized
19 if client.collections.exists("Question"):
20 client.collections.delete("Question")
21
22 questions = client.collections.create(
23 name="Question",
24 vectorizer_config=wc.Configure.Vectorizer.text2vec_openai(), # Use OpenAI to create vectors
25 generative_config=wc.Configure.Generative.openai() # Use OpenAI for RAG
26 )
27
28 # 3. Import data
29 # Download sample data
30 resp = requests.get('https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json')
31 data = json.loads(resp.text)
32
33 # Bulk insert data
34 with questions.get_bulk_writer() as batch:
35 for item in data:
36 batch.add_object({
37 "question": item["Question"],
38 "answer": item["Answer"],
39 "category": item["Category"],
40 })
41
42 # 4. Perform a search
43 # This performs a "nearText" semantic search
44 response = questions.query.near_text(
45 query="biology",
46 limit=2
47 )
48
49 for obj in response.objects:
50 print(f"ID: {obj.uuid}")
51 print(f"Data: {json.dumps(obj.properties, indent=2)}")
52
53finally:
54 client.close() # Close the connection