File size: 759 Bytes
6e272be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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