forked from AntoniZap/IBM-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-matching.py
More file actions
67 lines (49 loc) · 1.83 KB
/
query-matching.py
File metadata and controls
67 lines (49 loc) · 1.83 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
# import fuzzy logic library
from thefuzz import process, fuzz
import pymongo
from pymongo import MongoClient
import random
uri = "mongodb+srv://admin:wzE6nBcB4bnpyDUY@ibm.zbskp8h.mongodb.net/?retryWrites=true&w=majority&appName=IBM"
# Connect to the server
client = MongoClient(uri)
db = client["IBM"]
collection = db["feature_ranking"]
#
# Gets all the past queries from the database
#
def get_old_queries():
entries = collection.find({}, {'Query': 1})
old_queries = []
for entry in entries:
old_queries.append(entry['Query'])
return old_queries
#
# Returns the LLMs best-ranked for the paramater query
#
def get_best_LLM(query):
old_queries = get_old_queries()
matches = process.extract(query, old_queries, score_cutoff = 65, scorer=fuzz.token_set_ratio)
for match in matches:
entries_matched = collection.find({"Query" : match})
positive_ratings = []
negative_ratings = 0
no_rating = []
for entry in entries_matched:
rating = entry['Rating']
if rating == 3:
no_rating.append({'LLM' : entry['LLM']})
else:
if rating >= 4:
positive_ratings.append({'LLM': entry['LLM'], 'Rating': rating})
else:
negative_ratings +=1
if positive_ratings:
max_rating = max(positive_ratings, key=lambda x: x['Rating'])
best_LLMs = []
for llm in positive_ratings:
if llm['Rating'] == max_rating['Rating']:
best_LLMs.append(llm['LLM'])
return best_LLMs
if negative_ratings > 0 and no_rating:
return no_rating
return None