Spaces:
Paused
Paused
Commit
·
37d88e4
1
Parent(s):
7d7fb11
Delete wiki_funcs.py
Browse files- wiki_funcs.py +0 -70
wiki_funcs.py
DELETED
@@ -1,70 +0,0 @@
|
|
1 |
-
from langchain.agents import tool
|
2 |
-
|
3 |
-
from torch import tensor as torch_tensor
|
4 |
-
from datasets import load_dataset
|
5 |
-
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
6 |
-
|
7 |
-
"""# import models"""
|
8 |
-
|
9 |
-
bi_encoder = SentenceTransformer(
|
10 |
-
'sentence-transformers/multi-qa-MiniLM-L6-cos-v1')
|
11 |
-
bi_encoder.max_seq_length = 256 # Truncate long passages to 256 tokens
|
12 |
-
|
13 |
-
# The bi-encoder will retrieve top_k documents. We use a cross-encoder, to re-rank the results list to improve the quality
|
14 |
-
cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
15 |
-
|
16 |
-
"""# import datasets"""
|
17 |
-
dataset = load_dataset("gfhayworth/wiki_mini", split='train')
|
18 |
-
|
19 |
-
mypassages = list(dataset.to_pandas()['psg'])
|
20 |
-
|
21 |
-
dataset_embed = load_dataset("gfhayworth/wiki_mini_embed", split='train')
|
22 |
-
|
23 |
-
dataset_embed_pd = dataset_embed.to_pandas()
|
24 |
-
mycorpus_embeddings = torch_tensor(dataset_embed_pd.values)
|
25 |
-
|
26 |
-
|
27 |
-
def search(query, top_k=20, top_n=1):
|
28 |
-
question_embedding = bi_encoder.encode(query, convert_to_tensor=True)
|
29 |
-
hits = util.semantic_search(
|
30 |
-
question_embedding, mycorpus_embeddings, top_k=top_k)
|
31 |
-
hits = hits[0] # Get the hits for the first query
|
32 |
-
|
33 |
-
##### Re-Ranking #####
|
34 |
-
cross_inp = [[query, mypassages[hit['corpus_id']]] for hit in hits]
|
35 |
-
cross_scores = cross_encoder.predict(cross_inp)
|
36 |
-
|
37 |
-
# Sort results by the cross-encoder scores
|
38 |
-
for idx in range(len(cross_scores)):
|
39 |
-
hits[idx]['cross-score'] = cross_scores[idx]
|
40 |
-
|
41 |
-
hits = sorted(hits, key=lambda x: x['cross-score'], reverse=True)
|
42 |
-
predictions = hits[:top_n]
|
43 |
-
return predictions
|
44 |
-
# for hit in hits[0:3]:
|
45 |
-
# print("\t{:.3f}\t{}".format(hit['cross-score'], mypassages[hit['corpus_id']].replace("\n", " ")))
|
46 |
-
|
47 |
-
|
48 |
-
def get_text(qry):
|
49 |
-
# predictions = greg_search(qry)
|
50 |
-
predictions = search(qry)
|
51 |
-
prediction_text = []
|
52 |
-
for hit in predictions:
|
53 |
-
prediction_text.append("{}".format(mypassages[hit['corpus_id']]))
|
54 |
-
return prediction_text
|
55 |
-
|
56 |
-
|
57 |
-
@tool
|
58 |
-
def mysearch(query: str) -> str:
|
59 |
-
"""Query our own datasets.
|
60 |
-
"""
|
61 |
-
rslt = get_text(query)
|
62 |
-
return '\n'.join(rslt)
|
63 |
-
|
64 |
-
|
65 |
-
@tool
|
66 |
-
def mygreetings(greeting: str) -> str:
|
67 |
-
"""Let us do our greetings
|
68 |
-
"""
|
69 |
-
|
70 |
-
return "how are you?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|