very basic followups check in backend

This commit is contained in:
Fraser
2023-06-01 23:31:11 -04:00
parent 844ce744e6
commit 820d70a3ab
2 changed files with 48 additions and 0 deletions
+3
View File
@@ -1,4 +1,5 @@
# ------------------------------- env, constants -------------------------------
from followups import search_authored
from get_blocks import get_top_k_blocks, Block
@@ -170,6 +171,8 @@ def talk_to_robot(index, query: str, history: List[Dict[str, str]], k: int = STA
log(query)
log(response)
search_authored(response, DEBUG_PRINT)
yield json.dumps({"state": "done"})
except Exception as e:
+45
View File
@@ -0,0 +1,45 @@
from dataclasses import dataclass
from urllib.parse import quote
import requests
SIMILARITY_THRESHOLD = 0.5 # total shot in the dark - play with this later
MAX_FOLLOWUPS = 3
@dataclass
class Followup:
text: str
pageid: str
score: float
# do a search like this:
# https://nlp.stampy.ai/api/search?query=what%20is%20agi
def search_authored(query: str, DEBUG_PRINT: bool = False):
url = 'https://nlp.stampy.ai/api/search?query=' + quote(query)
response = requests.get(url).json()
followups = [ Followup(entry['title'], entry['pageid'], entry['score']) for entry in response ]
# (note: api presently returns followups pre-sorted, but idk if that's
# guaranteed to stay the case. Re-sorting should be cheap anyway).
followups.sort(key=lambda f: f.score, reverse=True)
followups = followups[:MAX_FOLLOWUPS]
if DEBUG_PRINT:
print(" ------------------------------ suggested followups: -----------------------------")
for followup in followups:
if followup.score > SIMILARITY_THRESHOLD:
print(f'{followup.score:.2f} - suggested to user')
else:
print(f'{followup.score:.2f} - not suggested')
print(followup.text)
print(followup.pageid)
print()
followups = [ f for f in followups if f.score > SIMILARITY_THRESHOLD ]
return followups