-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_ada2.py
50 lines (40 loc) · 1.79 KB
/
02_ada2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import harmony
import numpy as np
import openai
from harmony.schemas.requests.text import Instrument, Question
import evaluation_helper
for input_file, data in evaluation_helper.get_datasets():
all_questions = list(sorted(set(data.text_1).union(set(data.text_2))))
question_text_to_idx = dict([b, a] for a, b in enumerate(all_questions))
questions = []
for idx, question_text in enumerate(all_questions):
questions.append(Question(question_text=question_text, question_no=f"{idx}"))
instrument = Instrument(questions=questions)
model_name = "text-embedding-ada-002"
def convert_texts_to_vector(texts):
batch_size = 100
embeddings_as_list = []
texts = list(texts)
for j in range(len(texts)):
if texts[j] == "":
texts[j] = "empty"
for batch_start in range(0, len(texts), batch_size):
batch_end = batch_start + batch_size
if batch_end > len(texts):
batch_end = len(texts)
batch = texts[batch_start:batch_end]
vectors = openai.Embedding.create(input=batch, model=model_name)['data']
embeddings_as_list.extend([vectors[i]["embedding"] for i in range(len(vectors))])
return np.asarray(embeddings_as_list)
all_questions, similarity, query_similarity, new_vectors_dict = harmony.match_instruments_with_function(
[instrument], None,
convert_texts_to_vector)
preds = [0] * len(data)
for idx in range(len(data)):
text_1 = data.text_1.iloc[idx]
text_2 = data.text_2.iloc[idx]
idx_1 = question_text_to_idx[text_1]
idx_2 = question_text_to_idx[text_2]
preds[idx] = np.abs(similarity[idx_1, idx_2])
data["y_pred"] = preds
evaluation_helper.save_results(input_file, data)