33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
|
|
import httpx
|
||
|
|
|
||
|
|
def get_embedding(text: str) -> list[float]:
|
||
|
|
"""Get embedding vector for text using the configured embedding service"""
|
||
|
|
|
||
|
|
api_key = "h7ARU7tP7cblbpIQFpFXnhxVdFwH9rLXP654UfSJd8xKCJzeg4VOJQQJ99AKACi0881XJ3w3AAABACOGTlOf"
|
||
|
|
model = "text-embedding-3-small"
|
||
|
|
base_url = "https://aoai-lab-jpe-fl.openai.azure.com/openai/deployments/text-embedding-3-small/embeddings?api-version=2024-12-01-preview"
|
||
|
|
headers = {
|
||
|
|
"Content-Type": "application/json",
|
||
|
|
"Authorization": f"Bearer {api_key}"
|
||
|
|
}
|
||
|
|
|
||
|
|
payload = {
|
||
|
|
"input": text,
|
||
|
|
"model": model
|
||
|
|
}
|
||
|
|
|
||
|
|
try:
|
||
|
|
response = httpx.post(f"{base_url}", json=payload, headers=headers )
|
||
|
|
response.raise_for_status()
|
||
|
|
result = response.json()
|
||
|
|
print(result)
|
||
|
|
return result["data"][0]["embedding"]
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Failed to get embedding: {e}")
|
||
|
|
raise Exception(f"Embedding generation failed: {str(e)}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
print("Begin")
|
||
|
|
text = "Sample text for embedding"
|
||
|
|
result = get_embedding(text)
|
||
|
|
print(result)
|