-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiris.py
More file actions
108 lines (90 loc) · 3.17 KB
/
Copy pathiris.py
File metadata and controls
108 lines (90 loc) · 3.17 KB
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
106
107
108
# Iris
# A messenger goddess
# Takes in question, returns most relevant segments
import os
from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer
import json
model = SentenceTransformer("all-MiniLM-L6-v2")
os.environ["TOKENIZERS_PARALLELISM"] = "true"
def pretty_print_response(response):
if len(response["hits"]["hits"]) == 0:
print("Your search returned no results.")
else:
for hit in response["hits"]["hits"]:
id = hit["_id"]
#publication_date = hit["_source"]["publish_date"]
#rank = hit["_rank"]
title = hit["_source"]["title"]
author = hit["_source"]["author"]
text = hit["_source"]["text"]
description = hit["_source"]["description"]
tags = hit["_source"].get("tags", [])
pretty_output = f"""\nID: {id}
Title: {title}
Author: {author}
Summary: {description}
Tags: {tags}
Text: {text}"""
print(pretty_output)
with open('config.json', 'r') as file:
configs = json.load(file)
client = Elasticsearch(
# For local development
hosts=[configs["elasticsearch"]["host"]],
#cloud_id=configs["elasticsearch"]["name"],
api_key=configs["elasticsearch"]["key"],
)
print(client.info())
def weighted_encode(strs, weights, model):
for i, x in enumerate(strs):
if x is None or len(x) == 0:
strs[i] = "dummy"
weights[i] = 0
vectors = [model.encode(x).tolist() for x in strs]
vector_len = len(vectors[0])
weight_sum = sum(weights)
if weight_sum == 0:
raise ValueError("Sum of weights cannot be zero")
result = [0] * vector_len
for i in range(vector_len):
for vec, weight in zip(vectors, weights):
result[i] += vec[i] * weight
# Divide by sum of weights to get weighted average
return [x / weight_sum for x in result]
def iris_convo(queries, fade, num_of_results=5, relevance = 0.5):
weights = []
for i, x in enumerate(queries):
if i == 0:
weights.append(1)
else:
weights.append(weights[-1] / fade)
vector = weighted_encode(strs=queries, weights=weights, model=model)
return iris_search(vector,num_of_results, relevance)
def iris_search(query, num_of_results=5, relevance = 0.5):
#x = model.encode(query.lower()).tolist()
#print(str(type(x)))
response = client.search(
index="book_index",
size=num_of_results,
query={"match": {"summary": str(query)}},
knn={
"field": "text_vector",
"query_vector": query if isinstance(query, list) else model.encode(query.lower()).tolist(),
"k": num_of_results+2,
"num_candidates": min((num_of_results+2) * 100, 9999),
},
min_score = relevance
)
return response
def iris_neighbor(query):
response = client.search(
index="book_index",
size=3,
body={"query": {"match": {"title": query}}},
)
return response
if __name__ == "__main__":
while (True):
response = iris_search(input("\nEnter text query\n\n> "), 10, 0.656565656565)
pretty_print_response(response)