使用余弦算法计算向量相似性

import pandas as pd
import numpy as np
import openai

from openai.embeddings_utils import get_embedding, cosine_similarity

openai.api_key = 'sk-????'
embedding_model = "text-embedding-ada-002"
embedding_encoding = "cl100k_base"  # this the encoding for text-embedding-ada-002
max_tokens = 8000

# 也许不需要对模型返回的向量进行reshape
data_source = pd.read_csv("")
query_vector =np.array(get_embedding(data_source["query_variable"])).reshape(1, -1)
searched_vector = np.array(data_source["search_variable"]).reshape(1, -1)

similarity = cosine_similarity(query_vector, searched_vector.T)[0][0]

你可能感兴趣的:(算法)