Skip to content

Commit 57eed07

Browse files
committed
Add embeddings example script
Add examples/embeddings.py demonstrating AIMLAPI embeddings usage with model 'text-embedding-3-small'. The script creates embeddings for two sample texts, prints the first 5 dimensions of each embedding, and computes their cosine similarity using numpy. Serves as a simple usage example; requires aimlapi and numpy.
1 parent 1a7b4b1 commit 57eed07

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

examples/embeddings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from aimlapi import AIMLAPI
2+
import numpy as np
3+
4+
aimlapi = AIMLAPI()
5+
6+
7+
response = aimlapi.embeddings.create(
8+
model="text-embedding-3-small",
9+
input=[
10+
"The quick brown fox jumped over the lazy dogs",
11+
],
12+
)
13+
14+
similar_response = aimlapi.embeddings.create(
15+
model="text-embedding-3-small",
16+
input="The dogs were not happy that the fox jumped over them",
17+
)
18+
19+
print(response.data[0].embedding[:5]) # Print first 5 dimensions of embedding for first input
20+
print(similar_response.data[0].embedding[:5]) # Print first 5 dimensions of embedding for similar input
21+
22+
# Calculate cosine similarity between the two embeddings (expected: 0.6747... or similar, since the inputs are semantically similar)
23+
print("Cosine similarity:",
24+
np.dot(response.data[0].embedding, similar_response.data[0].embedding) /
25+
(np.linalg.norm(response.data[0].embedding) * np.linalg.norm(similar_response.data[0].embedding))
26+
)

0 commit comments

Comments
 (0)