-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfullDemo.py
More file actions
96 lines (75 loc) · 2.6 KB
/
fullDemo.py
File metadata and controls
96 lines (75 loc) · 2.6 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
# example_1_embed_store.py
# Embed healthcare texts and store in MongoDB
import voyageai
import numpy as np
from pymongo import MongoClient
# --- Setup ---
VOYAGE_API_KEY = "your-voyageai-api-key"
ATLAS_URI = "your-mongodb-uri"
client = voyageai.Client(api_key=VOYAGE_API_KEY)
mongo_client = MongoClient(ATLAS_URI)
collection = mongo_client["healthcare"]["documents"]
# --- Normalize helper ---
def normalize(v):
norm = np.linalg.norm(v)
return v / norm if norm > 0 else v
# --- Texts to embed ---
texts = [
"Patient exhibits symptoms of type 2 diabetes with elevated A1C.",
"Eligibility criteria for cardiac rehabilitation include a recent MI or CABG.",
"Flu vaccine recommended for all patients over age 65."
]
# --- Embed and store ---
response = client.embed(texts=texts, model="voyage-2", input_type="document")
for text, emb in zip(texts, response.embeddings):
doc = {"text": text, "embedding": normalize(emb)}
collection.insert_one(doc)
print("Documents embedded and stored.")
# example_2_search.py
# Search healthcare texts using VoyageAI + MongoDB Atlas Vector Search
import voyageai
import numpy as np
from pymongo import MongoClient
# --- Setup ---
VOYAGE_API_KEY = "your-voyageai-api-key"
ATLAS_URI = "your-mongodb-uri"
client = voyageai.Client(api_key=VOYAGE_API_KEY)
mongo_client = MongoClient(ATLAS_URI)
collection = mongo_client["healthcare"]["documents"]
# --- Normalize helper ---
def normalize(v):
norm = np.linalg.norm(v)
return v / norm if norm > 0 else v
# --- Query embedding ---
query = "What are the guidelines for flu shots for seniors?"
query_emb = client.embed(texts=[query], model="voyage-2", input_type="query").embeddings[0]
query_emb = normalize(query_emb)
# --- Vector search ---
results = collection.aggregate([
{
"$vectorSearch": {
"queryVector": query_emb,
"path": "embedding",
"numCandidates": 100,
"limit": 5,
"index": "healthcare_vector_index"
}
},
{"$project": {"text": 1, "_id": 0}}
])
print("Search results:")
for doc in results:
print("-", doc["text"])
# example_3_rerank.py
# Rerank candidate texts with VoyageAI
import voyageai
client = voyageai.Client(api_key="your-voyageai-api-key")
query = "What are the guidelines for flu shots for seniors?"
documents = [
"Flu vaccine recommended for all patients over age 65.",
"Cardiac rehab includes exercise and counseling.",
"Diabetes treatment involves diet and exercise."
]
response = client.rerank(query=query, documents=documents, model="voyage-2")
print("Top result:")
print(response.results[0].document)