Spaces:
Build error
Build error
Download pi_vector_utils.py from Dhruv1102/PLAY_WITH_PI: direct link, hf CLI and curl.
- Browser
- Download file 759 Bytes
-
https://huggingface.co/spaces/Dhruv1102/PLAY_WITH_PI/resolve/main/pi_vector_utils.py
- Command line
-
hf download hf://spaces/Dhruv1102/PLAY_WITH_PI/pi_vector_utils.py
-
curl -L -o pi_vector_utils.py https://huggingface.co/spaces/Dhruv1102/PLAY_WITH_PI/resolve/main/pi_vector_utils.py
759 Bytes
| import numpy as np | |
| from numpy.linalg import norm | |
| import openai | |
| def get_embedding(text, openai_key): | |
| openai.api_key = openai_key | |
| try: | |
| result = openai.Embedding.create(model="text-embedding-ada-002", input=text) | |
| return np.array(result['data'][0]['embedding']) | |
| except Exception as e: | |
| return np.zeros(1536) # Return zero vector on error | |
| def pi_rotation(embedding, pi_digit): | |
| theta = (pi_digit / 9) * np.pi | |
| rotated = embedding * np.cos(theta) + np.roll(embedding, 1) * np.sin(theta) | |
| return rotated | |
| def pi_modulated_similarity(query_vec, chunk_vec, pi_digit): | |
| cosine_sim = np.dot(query_vec, chunk_vec) / (norm(query_vec) * norm(chunk_vec)) | |
| weight = 1 + (pi_digit % 5) * 0.1 | |
| return cosine_sim * weight | |