Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions usaspending_api/llm/tests/helper.py
Comment thread
james-at-kc marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Any

from usaspending_api.common.elasticsearch.search_wrappers import RecipientSearch
from usaspending_api.llm.tools.lookup_recipient import RecipientLookupTool
from usaspending_api.search.v2.es_sanitization import es_sanitize

_tool = RecipientLookupTool()


def build_fuzzy_recipient_query(search_text: str) -> RecipientSearch:
return _tool._build_search(es_sanitize(search_text).strip().upper(), top_k=10)


def fuzzy_search_recipients(
search_text: str,
limit: int = 10,
) -> list | dict[str, Any]:
response = _tool._build_search(es_sanitize(search_text).strip().upper(), top_k=limit).handle_execute()
if not response.hits:
return []
return [
{
"recipient_name": hit.to_dict().get("recipient_name"),
"uei": hit.to_dict().get("uei"),
"duns": hit.to_dict().get("duns"),
"recipient_level": hit.to_dict().get("recipient_level"),
"recipient_hash": hit.to_dict().get("recipient_hash"),
"score": hit.meta.score,
}
for hit in response.hits
]


def retrieve_recipient_names(
search_text: str,
limit: int = 5,
) -> list[str]:
return _tool.lookup_recipient(search_text, top_k=limit)
Loading
Loading