Created
May 14, 2024 23:54
-
-
Save skeptrunedev/7d35574b29bc354aa7c04620165280d0 to your computer and use it in GitHub Desktop.
keos trieve example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import dotenv | |
import requests | |
dotenv.load_dotenv() | |
TRIEVE_API_KEY = os.getenv("TRIEVE_API_KEY") | |
TRIEVE_DATASET_ID = os.getenv("TRIEVE_DATASET_ID") | |
# Start by creating a chunk to represent your item | |
# https://api.trieve.ai/redoc#tag/chunk/operation/create_chunk | |
item = { | |
"id": "item-id", | |
"name": "Item Name", | |
"description": "Item Description", | |
"image": "https://cdn.trieve.ai/trieve-logo.png", | |
"price": 100, | |
"color": "red", | |
} | |
try: | |
requests.post( | |
"https://api.trieve.ai/api/chunk", | |
headers={"Authorization": TRIEVE_API_KEY, "TR-Dataset": TRIEVE_DATASET_ID}, | |
json={ | |
"tracking_id": item["id"], | |
"chunk_html": f"{item['name']}\n\n{item['description']}", | |
"tag_set": [item["color"]], | |
"metadata": { | |
"image": item["image"], | |
"price": item["price"], | |
"color": item["color"], | |
}, | |
"upsert_by_tracking_id": True, | |
}, | |
) | |
print("Chunk created successfully") | |
except requests.exceptions.RequestException as e: | |
print(f"Error creating chunk: {e}") | |
# Now you can search. In this example, we'll search for items with a price greater than 100 and less than 150 | |
# https://api.trieve.ai/redoc#tag/chunk/operation/search_chunks | |
try: | |
response = requests.post( | |
"https://api.trieve.ai/api/chunk/search", | |
headers={"Authorization": TRIEVE_API_KEY, "TR-Dataset": TRIEVE_DATASET_ID}, | |
json={ | |
"query": "item description", | |
"filters": [ | |
{ | |
"field": "metadata.price", | |
"range": { | |
"gt": 100, | |
"lt": 150, | |
}, | |
} | |
], | |
}, | |
) | |
print(f"{len(response.json())} items found from search") | |
except requests.exceptions.RequestException as e: | |
print(f"Error searching: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment