如何使用OpenAI进行embedding句子后,输入新句子比较

import openai
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity


# Set up OpenAI API credentials
openai.api_key = 'YOUR_API_KEY'
response=openai.Embedding.create(
  model="text-embedding-ada-002",
  input="The food was delicious and the waiter..."
)
print(response)


# Define the sentences to be embedded and sorted
sentences = [
    "How to use ChatGPT for sentence embedding?",
    "What are the applications of sentence embedding?",
    "Can you provide examples of sentence embedding?",
    "What is the best approach for sentence embedding?"
]

# Define a new sentence for similarity comparison
new_sentence = "How does sentence embedding work?"

# Preprocess and generate the embeddings
embeddings = []
for sentence in sentences:


    response = openai.Embedding.create(
        model="text-embedding-ada-002",
        input=sentence
    )
    embedding = response.data[0].embedding
    embeddings.append(embedding)

# Convert embeddings to numpy array
embeddings = np.array(embeddings)

# Calculate pairwise cosine similarity
similarity_matrix = cosine_similarity(embeddings)

# Sort sentences based on similarity to the first sentence
sorted_indices = np.argsort(similarity_matrix[:, 0])[::-1]
sorted_sentences = [sentences[i] for i in sorted_indices]


response = openai.Embedding.create(
    model="text-embedding-ada-002",
    input=new_sentence
)
new_embedding = response.data[0].embedding

# Calculate cosine similarity of the new embedding with existing embeddings
similarity_scores = cosine_similarity([new_embedding], embeddings)[0]

# Sort the indices based on similarity scores
sorted_indices = np.argsort(similarity_scores)[::-1]

# Retrieve the most similar sentence
most_similar_sentence = sentences[sorted_indices[0]]

# Print the sorted sentences and the most similar sentence
print("Sorted Sentences:")
for sentence in sorted_sentences:
    print(sentence)

print("\nMost Similar Sentence:")
print(most_similar_sentence)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨航 AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值