gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
for query, correctdocument in querydocumentpairs: queryembedding =…
- User
for query, correct_document in query_document_pairs: query_embedding = model.encode([query])[0] correct_document_embedding = model.encode([correct_document])[0]
encode all this at once then loop through
- Assistant
to calculate the similarity scores. Here is the modified code:
# Encode all queries and documents at once query_embeddings = model.encode([pair[0] for pair in query_document_pairs]) document_embeddings = model.encode([pair[1] for pair in query_document_pairs]) # Loop through the encoded pairs for i, pair in enumerate(query_document_pairs): query_embedding = query_embeddings[i] document_embedding = document_embeddings[i] # Calculate the similarity score similarity_score = cosine_similarity(query_embedding.reshape(1, -1), document_embedding.reshape(1, -1))[0][0] # Do something with the similarity score # (e.g., store it, print it, etc.)In this code, we first encode all the queries and documents at once using the
model.encode()method. Then, we loop through the list of encoded pairs and compute the similarity score using thecosine_similarity()function. Finally, you can perform further actions with the similarity score as desired.