from openai import OpenAI
# Replace "YOUR_API_KEY" with your actual Neosantara AI API Key
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.neosantara.xyz/v1"
)
def get_embeddings(texts: list[str], model: str = "nusa-embedding-0001"):
"""
Generates embeddings for a list of texts using the Neosantara AI API.
"""
print(f"Generating embeddings for model: {model}")
try:
response = client.embeddings.create(
model=model,
input=texts,
encoding_format="float"
)
# The response.data contains a list of embedding objects
# Each object has 'embedding' (the vector) and 'index'
embeddings = [item.embedding for item in response.data]
print(f"Generated {len(embeddings)} embeddings. Dimension: {len(embeddings[0]) if embeddings else 0}")
print(f"Total tokens used: {response.usage.total_tokens}")
return embeddings
except Exception as e:
print(f"An error occurred during embedding generation: {e}")
return None
if __name__ == "__main__":
example_texts = [
"Artificial intelligence is transforming industries.",
"Machine learning is a subset of AI.",
"Deep learning enables neural networks."
]
generated_vectors = get_embeddings(example_texts)
if generated_vectors:
print("\nFirst embedding vector (truncated):")
print(generated_vectors[0][:10], "...") # Print first 10 elements