Can anyone help me with the distance calculation? I've tried different ways and I'm lost
`def distances_from_embeddings(q_embeddings, embeddings, distance_metric='cosine'):
try:
q_embeddings = np.array(q_embeddings)
embeddings = np.array(embeddings)
print("Shape of q_embedding:", q_embeddings.shape)
print("Shape of embeddings:", embeddings.shape)
q_embeddings_reshaped = q_embeddings.reshape(1, -1)
embeddings_reshaped = embeddings.reshape(1, -1)
if distance_metric == 'cosine':
distances = cdist(q_embeddings_reshaped, embeddings_reshaped, metric='cosine')[0]
elif distance_metric == 'euclidean':
distances = cdist(q_embeddings_reshaped, embeddings_reshaped, metric='euclidean')[0]
else:
raise ValueError("Invalid distance metric.")
embeddings['distances'] = distances
except openai.APIStatusError as e:
print(e.status_code)
except ValueError as e:
print("invalid metric.", e)
except KeyError as e:
print("no key or dataframe:", e)
except IndexError as e:
print("invalid consult.", e)
except Exception as e:
print("Erro :", e)
return embeddings`
result:
invalid metric. XA and XB must have the same number of columns (i.e. feature dimension.)
'Completion' object is not subscriptable
Shape of q_embedding: (1536,)
Shape of embeddings: (1,)