如何提高openAI语义搜索速度

问题描述 投票:0回答:1

我有一个关键字数据库(当前是一个 json 文件)及其使用 openAI 嵌入创建的嵌入数据。 我想做的是使用输入关键字进行相似性搜索。 因此,在我当前的流程中,当我首先输入关键字时,它会被嵌入,并将其与数据库中数据的嵌入进行比较。 我观察到,当我添加一万个数据时,相似性查找过程需要花费太多时间。 即使数据库中有数十万嵌入数据,我也可以实施什么好的解决方案来获得更快的结果。

我已经看到关于通过meta实现faiss。 但不确定这是否是正确的解决方案 以下是我目前使用的相似度函数

def cosine_similarity(a, b):
            return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
similarities = [cosine_similarity(embedding, embedding_query) for embedding in embeddings]

当前嵌入数据是从如下所示的 json 文件中读取的

        with open("data_a_b_j.json", "r") as f:
            embedding_data = json.load(f)

        # Extract embeddings and corresponding descriptions from the loaded data
        embeddings = [item["embedding"] for item in embedding_data]
openai-api embedding openaiembeddings semantic-search similarity-search
1个回答
0
投票

您应该使用矢量数据库,例如:

更容易,您会更快获得结果。

我以前用过Milvus,成功做了相似度搜索,如下:

# Connect to the Milvus collection named "Movies"
collection = Collection("Movies")

# Perform similarity search using Milvus
similarity_search_result = collection.search(
    data=[user_vector],
    anns_field="vector",
    param=search_params,
    limit=3,
    output_fields=['description']
)

# Display search results to the user
for idx, hit in enumerate(similarity_search_result[0]):
    score = hit.distance
    description = hit.entity.description
    print(f"{idx + 1}. {description} (distance: {score})")

查看完整代码这里和官方Milvus文档以了解代码。

我建议你观看我的Milvus YouTube 教程。在前三个教程中,我展示了如何设置 Milvus、创建集合以及在 Python 中进行相似性搜索。

© www.soinside.com 2019 - 2024. All rights reserved.