-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
135 lines (114 loc) Β· 4.62 KB
/
Copy pathbasic_usage.py
File metadata and controls
135 lines (114 loc) Β· 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Basic Usage Example for AINative Python SDK
This example demonstrates the fundamental operations of the AINative SDK.
"""
import os
from ainative import AINativeClient
from ainative.zerodb.memory import MemoryPriority
def main():
# Initialize client using environment variable or direct API key
api_key = os.getenv("AINATIVE_API_KEY", "your-api-key-here")
# Create client instance
client = AINativeClient(api_key=api_key)
print("π AINative SDK Basic Usage Example\n")
try:
# 1. Health Check
print("1. Checking API health...")
health = client.health_check()
print(f" β
API Status: {health.get('status', 'unknown')}\n")
# 2. Create a Project
print("2. Creating a new project...")
project = client.zerodb.projects.create(
name="SDK Demo Project",
description="Created via Python SDK example",
metadata={
"environment": "development",
"version": "1.0.0"
}
)
project_id = project["id"]
print(f" β
Project created: {project_id}\n")
# 3. Store Some Vectors
print("3. Storing vectors...")
vectors = [
[0.1, 0.2, 0.3, 0.4, 0.5],
[0.2, 0.3, 0.4, 0.5, 0.6],
[0.3, 0.4, 0.5, 0.6, 0.7],
]
metadata = [
{"text": "Hello world", "category": "greeting"},
{"text": "Python SDK", "category": "technology"},
{"text": "Vector database", "category": "technology"},
]
upsert_result = client.zerodb.vectors.upsert(
project_id=project_id,
vectors=vectors,
metadata=metadata,
namespace="examples"
)
print(f" β
Stored {len(vectors)} vectors\n")
# 4. Search Vectors
print("4. Searching for similar vectors...")
query_vector = [0.15, 0.25, 0.35, 0.45, 0.55]
search_results = client.zerodb.vectors.search(
project_id=project_id,
vector=query_vector,
top_k=2,
namespace="examples"
)
print(f" β
Found {len(search_results)} similar vectors")
for i, result in enumerate(search_results, 1):
print(f" {i}. Score: {result.get('score', 0):.3f}, "
f"Text: {result.get('metadata', {}).get('text', 'N/A')}")
print()
# 5. Create Memory Entries
print("5. Creating memory entries...")
memory1 = client.zerodb.memory.create(
content="The SDK was initialized successfully with project " + project_id,
title="SDK Initialization",
tags=["sdk", "initialization", "example"],
priority=MemoryPriority.HIGH,
project_id=project_id
)
memory2 = client.zerodb.memory.create(
content="Vectors can be stored and searched efficiently using the ZeroDB module",
title="Vector Operations",
tags=["vectors", "search", "example"],
priority=MemoryPriority.MEDIUM,
project_id=project_id
)
print(f" β
Created 2 memory entries\n")
# 6. Search Memories
print("6. Searching memories...")
memory_results = client.zerodb.memory.search(
query="SDK initialization",
project_id=project_id,
semantic=True,
limit=5
)
print(f" β
Found {len(memory_results)} relevant memories\n")
# 7. Get Project Statistics
print("7. Getting project statistics...")
stats = client.zerodb.projects.get_statistics(project_id)
print(f" β
Project Statistics:")
print(f" - Vectors: {stats.get('vector_count', 0)}")
print(f" - Memories: {stats.get('memory_count', 0)}")
print(f" - Storage: {stats.get('storage_bytes', 0)} bytes\n")
# 8. Get Usage Analytics
print("8. Getting usage analytics...")
usage = client.zerodb.analytics.get_usage(project_id=project_id)
print(f" β
Usage data retrieved\n")
# 9. Clean Up (Optional)
print("9. Cleaning up...")
# Uncomment to delete the project
# client.zerodb.projects.delete(project_id)
# print(f" β
Project {project_id} deleted\n")
print("β¨ Basic usage example completed successfully!")
except Exception as e:
print(f"β Error: {e}")
finally:
# Close client connection
client.close()
if __name__ == "__main__":
main()