Last active
April 18, 2024 08:42
-
-
Save mahiya/28563fd8c137f4ed2b6df5393cd9bf40 to your computer and use it in GitHub Desktop.
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 json | |
import requests | |
class ImageEmbeddingsClient: | |
def __init__(self, computer_vision_endpoint: str = None, computer_vision_key: str = None,): | |
self.endpoint = computer_vision_endpoint if computer_vision_endpoint else os.getenv("AZURE_COMPUTER_VISION_ENDPOINT") | |
self.key = computer_vision_key if computer_vision_key else os.getenv("AZURE_COMPUTER_VISION_KEY") | |
def get_embeds(self, image_file_path: str): | |
with open(image_file_path, "rb") as f: | |
url = f"{self.endpoint}computervision/retrieval:vectorizeImage?api-version=2023-02-01-preview&modelVersion=latest" | |
headers = {"Content-Type": "application/octet-stream", "Ocp-Apim-Subscription-Key": self.key} | |
resp = requests.post(url, data=f.read(), headers=headers) | |
resp.raise_for_status() | |
resp = json.loads(resp.text) | |
if "vector" not in resp: | |
return [] | |
return resp["vector"] |
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
{ | |
"name": "", | |
"fields": [ | |
{ | |
"name": "id", | |
"type": "Edm.String", | |
"searchable": false, | |
"filterable": false, | |
"sortable": false, | |
"facetable": false, | |
"key": true | |
}, | |
{ | |
"name": "fileName", | |
"type": "Edm.String", | |
"searchable": true, | |
"filterable": false, | |
"sortable": false, | |
"facetable": false | |
}, | |
{ | |
"name": "contentVector", | |
"type": "Collection(Edm.Single)", | |
"searchable": true, | |
"dimensions": 1024, | |
"vectorSearchProfile": "vectorProfile" | |
} | |
], | |
"vectorSearch": { | |
"algorithms": [ | |
{ | |
"name": "hnsw", | |
"kind": "hnsw", | |
"hnswParameters": { | |
"m": 10, | |
"efConstruction": 1000, | |
"efSearch": 1000, | |
"metric": "cosine" | |
} | |
} | |
], | |
"vectorizers": [], | |
"profiles": [ | |
{ | |
"name": "vectorProfile", | |
"algorithm": "hnsw" | |
} | |
] | |
}, | |
"corsOptions": { | |
"allowedOrigins": [ | |
"*" | |
], | |
"maxAgeInSeconds": 300 | |
}, | |
"similarity": { | |
"@odata.type": "#Microsoft.Azure.Search.BM25Similarity", | |
"k1": 1.2, | |
"b": 0.75 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment