Skip to content

Commit

Permalink
Update example script with latest calling interface
Browse files Browse the repository at this point in the history
  • Loading branch information
erichare committed Apr 30, 2024
1 parent b7a1676 commit 9f039ad
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions scripts/astrapy_latest_interface.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import sys

from dotenv import load_dotenv
import astrapy

from astrapy.db import AstraDB
from dotenv import load_dotenv


sys.path.append("../")
Expand All @@ -16,34 +16,31 @@
api_endpoint = os.environ["ASTRA_DB_API_ENDPOINT"]

# Initialize our vector db
astra_db = AstraDB(token=token, api_endpoint=api_endpoint)
my_client = astrapy.DataAPIClient(token)
my_database = my_client.get_database_by_api_endpoint(api_endpoint)

# In case we already have the collection, let's clear it out
astra_db.delete_collection("collection_test")
my_database.drop_collection("collection_test")

# Create a new test collection for example
astra_db_collection = astra_db.create_collection("collection_test", dimension=5)
my_collection = my_database.create_collection("collection_test", dimension=5)

# Insert a document into the test collection
astra_db_collection.insert_one(
my_collection.insert_one(
{
"_id": "1",
"name": "Coded Cleats Copy",
"description": "ChatGPT integrated sneakers that talk to you",
"$vector": [0.25, 0.25, 0.25, 0.25, 0.25],
}
},
vector=[0.25, 0.25, 0.25, 0.25, 0.25],
)

# Perform a few vector find operations
astra_db_collection.vector_find([0.1, 0.1, 0.2, 0.5, 1], limit=3)

astra_db_collection.vector_find(
[0.1, 0.1, 0.2, 0.5, 1], limit=3, filter={"name": "Coded Cleats Copy"}
cursor = my_collection.find(
{},
vector=[0, 0.2, 0.4, 0.6, 0.8],
limit=2,
include_similarity=True,
)

astra_db_collection.vector_find(
[0.1, 0.1, 0.2, 0.5, 1],
limit=3,
fields=["_id", "name"],
include_similarity=False,
)
for result in cursor:
print(f"{result['name']}: {result['$similarity']}")

0 comments on commit 9f039ad

Please sign in to comment.