forked from conceptofmind/toolformer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
105 lines (90 loc) · 3.45 KB
/
tools.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import copy
import requests
import calendar
import json
import torch
import wolframalpha
import openai
import datetime
import time
from transformers import (
AutoModelForSeq2SeqLM,
AutoTokenizer,
AutoModel,
T5ForConditionalGeneration,
)
from typing import List
from operator import truediv, mul, add, sub
# Optional imports
from googleapiclient.discovery import build
"""
retrieval
Uses Carptriever to retrieve sentences before the current context.
input_sentences - List[String], sentences to retrieve from
input_text - String, the input text (e.g. The dog's name is)
k - The number of sentences to retrieve
output - A list of strings, each string is the retrieved sentence, and the sentence after.
"""
class Retriever:
def __init__(self, input_sentences: List[str]):
self.model = AutoModel.from_pretrained(
"CarperAI/carptriever-1", add_pooling_layer=False
).cuda()
self.tokenizer = AutoTokenizer.from_pretrained("CarperAI/carptriever-1")
self.index = None
self.index_sentences = input_sentences
self.build_index(input_sentences)
print("index built")
def build_index(self, input_sentences: List[str]):
output_list = []
for sentence in input_sentences:
print(sentence)
inputs = self.tokenizer(
sentence, padding=True, truncation=True, return_tensors="pt"
)
# print(inputs)
inputs["input_ids"] = inputs["input_ids"].cuda()
inputs["token_type_ids"] = inputs["token_type_ids"].cuda()
inputs["attention_mask"] = inputs["attention_mask"].cuda()
with torch.no_grad():
outputs = self.model(**inputs)
embeddings = mean_pooling(outputs[0], inputs["attention_mask"])
output_list.append(embeddings)
self.index = torch.concat(
output_list, 0
)
return
def retrieval(
self, input_text: str, k: int
) -> List[str]:
inputs = self.tokenizer(
input_text, padding=True, truncation=True, return_tensors="pt"
)
# print(inputs)
inputs["input_ids"] = inputs["input_ids"].cuda()
inputs["token_type_ids"] = inputs["token_type_ids"].cuda()
inputs["attention_mask"] = inputs["attention_mask"].cuda()
with torch.no_grad():
outputs = self.model(**inputs)
embeddings = mean_pooling(outputs[0], inputs["attention_mask"])
query_embedding = embeddings
print(query_embedding.shape, self.index.shape)
scores = (query_embedding @ self.index.transpose(0, 1)).cpu().tolist()
print(scores, self.index_sentences)
sentence_score_pairs = sorted(
zip(self.index_sentence, scores[0]), reverse=True, key=lambda x: x[1]
)
continued_sentence_score_pairs = sorted(
zip(self.index_sentences[1:], scores[0]), reverse=True, key=lambda x: x[1]
)
# print(sentence_score_pairs)
return [
sentence_pair[0] + " " + continue_pair[0]
for sentence_pair, continue_pair in zip(
sentence_score_pairs[:k], continued_sentence_score_pairs[:k]
)
]
def mean_pooling(token_embeddings: torch.Tensor, mask: torch.Tensor):
token_embeddings = token_embeddings.masked_fill(~mask[..., None].bool(), 0.0)
sentence_embeddings = token_embeddings.sum(dim=1) / mask.sum(dim=1)[..., None]
return sentence_embeddings