-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathapp.py
More file actions
39 lines (28 loc) · 990 Bytes
/
app.py
File metadata and controls
39 lines (28 loc) · 990 Bytes
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
import chromadb
from chromadb.utils.embedding_functions import DefaultEmbeddingFunction
chroma_client = chromadb.Client()
default_ef = DefaultEmbeddingFunction()
colection_name = "test_collection"
collection = chroma_client.get_or_create_collection(
colection_name, embedding_function=default_ef
)
# Define text documents
documents = [
{"id": "doc1", "text": "Hello, world!"},
{"id": "doc2", "text": "How are you today?"},
{"id": "doc3", "text": "Goodbye, see you later!"},
]
for doc in documents:
collection.upsert(ids=[doc["id"]], documents=[doc["text"]])
# define a query text
query_text = "Age of the Earth"
results = collection.query(
query_texts=[query_text],
n_results=2,
)
for idx, document in enumerate(results["documents"][0]):
doc_id = results["ids"][0][idx]
distance = results["distances"][0][idx]
print(
f" For the query: {query_text}, \n Found similar document: {document} (ID: {doc_id}, Distance: {distance})"
)