about summary refs log tree commit diff
path: root/gn3
diff options
context:
space:
mode:
authorAlexander_Kabui2025-06-05 15:19:05 +0300
committerBonfaceKilz2025-07-07 07:56:57 +0300
commit5312a03e3de5c62291c3cc19eae144de7c833eff (patch)
tree7a6be31090325d064bc57e1cb10770f9e4ffe093 /gn3
parentf03bb0b3047de47380d3907fb476547e4526e2dd (diff)
downloadgenenetwork3-5312a03e3de5c62291c3cc19eae144de7c833eff.tar.gz
feat: Add function to clean up query before dumping to db.
Diffstat (limited to 'gn3')
-rw-r--r--gn3/api/llm.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/gn3/api/llm.py b/gn3/api/llm.py
index 281cd22..834cd54 100644
--- a/gn3/api/llm.py
+++ b/gn3/api/llm.py
@@ -1,5 +1,6 @@
 """Api endpoints for gnqa"""
 import json
+import string
 from datetime import datetime
 from typing import Optional
 
@@ -49,6 +50,16 @@ def database_setup():
         cursor.execute(RATING_TABLE_CREATE_QUERY)
 
 
+def clean_query(query:str) -> str:
+    """This function cleans up query  removing
+    punctuation  and whitepace and transform to
+    lowercase
+    clean_query("!hello test.") -> "hello test"
+    """
+    strip_chars = string.punctuation + string.whitespace
+    str_query = query.lower().strip(strip_chars)
+    return str_query
+
 @gnqa.route("/search", methods=["GET"])
 @require_token
 def search(auth_token=None):
@@ -68,7 +79,7 @@ def search(auth_token=None):
             WHERE created_at > DATE('now', '-21 day') AND
                 query = ?
             ORDER BY created_at DESC LIMIT 1 """
-        res = cursor.execute(previous_answer_query, (query,))
+        res = cursor.execute(previous_answer_query, (clean_query(query),))
         previous_result = res.fetchone()
         if previous_result:
             _, _, _, response = previous_result
@@ -87,7 +98,7 @@ def search(auth_token=None):
             """INSERT INTO history(user_id, task_id, query, results)
             VALUES(?, ?, ?, ?)
             """, (user_id, str(task_id["task_id"]),
-                  query,
+                  clean_query(query),
                   json.dumps(response))
         )
         return response