diff options
| author | Johannes Medagbe <johanmedagbe@gmail.com> | 2026-09-21 10:03:20 +0000 |
|---|---|---|
| committer | Johannes Medagbe <johanmedagbe@gmail.com> | 2026-09-22 12:18:32 +0000 |
| commit | dce53892085c02c8571d8a6495e39c449294aa5f (patch) | |
| tree | ae64d32cc845d30ec2a4cc3514a23dc5683a09a1 /aisearch/src/gnais/search/classification.py | |
| download | genenetwork_ai_search-master.tar.gz | |
New: commit aisearch repo master
Diffstat (limited to 'aisearch/src/gnais/search/classification.py')
| -rw-r--r-- | aisearch/src/gnais/search/classification.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/aisearch/src/gnais/search/classification.py b/aisearch/src/gnais/search/classification.py new file mode 100644 index 0000000..833a16e --- /dev/null +++ b/aisearch/src/gnais/search/classification.py @@ -0,0 +1,56 @@ +from functools import lru_cache + +import dspy +from gnais.search.tools import route_model + + +class Classification(dspy.Signature): + input_text: str = dspy.InputField() + decision: str = dspy.OutputField(desc='"keyword" or "semantic"') + + +class Extraction(dspy.Signature): + input_text: str = dspy.InputField() + keywords: str = dspy.OutputField() + + +@lru_cache(maxsize=2048) +def extract_keywords(query: str) -> str: + """Extract list of keywords from query + + Args: + query: user query + + Returns: + list of keywords + """ + return route_model()(dspy.Predict(Extraction))( + input_text=f""" +You are extremely good at extracting keywords from a search query related to specific entities (traits, markers, etc) in GeneNetwork. +Produce a list of space separated keywords featured in the query below. Only return that list. + +{query}""" + ) + + +@lru_cache(maxsize=2048) +def classify_search(query: str) -> str: + """Classify user query as keyword search or semantic search + + Args: + query: user query + + Returns: + type of search for query processing + """ + return route_model()(dspy.Predict(Classification))( + input_text=f""" +You are an experienced search classifier. +You can accurately tell from a query if a keyword search or semantic search is more appropriate to provide satisfactory answers to the user. +A keyword search is appropriate when specific entities feature in the query (i.e trait id, marker code, etc.). +A semantic search is better when the system needs to understand the meaning of the query and make implicit connections. +Infer the type of search that should be performed given the query below: + +{query} +""" + ) |
