diff --git a/poetry.lock b/poetry.lock index 1527a3a..a946347 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1476,7 +1476,7 @@ test = ["hypothesis", "pytest", "readme-renderer"] name = "markdown-it-py" version = "3.0.0" description = "Python port of markdown-it. Markdown parsing, done right!" -optional = true +optional = false python-versions = ">=3.8" files = [ {file = "markdown-it-py-3.0.0.tar.gz", hash = "sha256:e3f60a94fa066dc52ec76661e37c851cb232d92f9886b15cb560aaada2df8feb"}, @@ -1569,7 +1569,7 @@ files = [ name = "mdurl" version = "0.1.2" description = "Markdown URL utilities" -optional = true +optional = false python-versions = ">=3.7" files = [ {file = "mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8"}, @@ -2426,7 +2426,7 @@ typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" name = "pygments" version = "2.18.0" description = "Pygments is a syntax highlighting package written in Python." -optional = true +optional = false python-versions = ">=3.8" files = [ {file = "pygments-2.18.0-py3-none-any.whl", hash = "sha256:b8e6aca0523f3ab76fee51799c488e38782ac06eafcf95e7ba832985c8e7b13a"}, @@ -2840,13 +2840,13 @@ tests = ["coverage (>=6.0.0)", "flake8", "mypy", "pytest (>=7.0.0)", "pytest-asy [[package]] name = "rich" -version = "13.8.0" +version = "13.8.1" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" -optional = true +optional = false python-versions = ">=3.7.0" files = [ - {file = "rich-13.8.0-py3-none-any.whl", hash = "sha256:2e85306a063b9492dffc86278197a60cbece75bcb766022f3436f567cae11bdc"}, - {file = "rich-13.8.0.tar.gz", hash = "sha256:a5ac1f1cd448ade0d59cc3356f7db7a7ccda2c8cbae9c7a90c28ff463d3e91f4"}, + {file = "rich-13.8.1-py3-none-any.whl", hash = "sha256:1760a3c0848469b97b558fc61c85233e3dafb69c7a071b4d60c38099d3cd4c06"}, + {file = "rich-13.8.1.tar.gz", hash = "sha256:8260cda28e3db6bf04d2d1ef4dbc03ba80a824c88b0e7668a0f23126a424844a"}, ] [package.dependencies] @@ -3238,7 +3238,7 @@ files = [ name = "typer" version = "0.12.5" description = "Typer, build great CLIs. Easy to code. Based on Python type hints." -optional = true +optional = false python-versions = ">=3.7" files = [ {file = "typer-0.12.5-py3-none-any.whl", hash = "sha256:62fe4e471711b147e3365034133904df3e235698399bc4de2b36c8579298d52b"}, @@ -3784,9 +3784,9 @@ type = ["pytest-mypy"] [extras] spacy = ["spacy"] -vespa = ["pyvespa", "pyyaml", "sentence-transformers"] +vespa = ["pyvespa", "pyyaml"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "0851e322b4282dc85ed8af27cf06a6107ff5de5eb296c17d09a42333661a9f64" +content-hash = "edfe8501ab3ed88c1da822c9a043db85f94476a21352feb7df9225533a6909de" diff --git a/pyproject.toml b/pyproject.toml index 95829d6..4827e57 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,12 @@ black = "^24.2.0" moto = { extras = ["s3"], version = "^5.0.13" } pytest-dotenv = "^0.5.2" +[tool.poetry.group.cli] +optional = true + +[tool.poetry.group.cli.dependencies] +typer = "^0.12.5" +rich = "^13.8.1" [tool.pytest.ini_options] addopts = "-p no:cacheprovider" diff --git a/src/cpr_sdk/cli/run_search_query.py b/src/cpr_sdk/cli/run_search_query.py new file mode 100644 index 0000000..9ce6fcd --- /dev/null +++ b/src/cpr_sdk/cli/run_search_query.py @@ -0,0 +1,80 @@ +import json +from rich.console import Console +from rich.table import Table +from rich import print_json +from rich import print as rprint +import typer +from src.cpr_sdk.search_adaptors import VespaSearchAdapter +from src.cpr_sdk.models.search import SearchParameters +from tests.conftest import VESPA_TEST_SEARCH_URL + + +def main( + instance_url: str = VESPA_TEST_SEARCH_URL, + exact_match: bool = False, + limit: int = 10, +): + """Run a search query with different rank profiles.""" + console = Console() + search_adapter = VespaSearchAdapter(instance_url) + + while True: + query = input("Enter your search query (or 'q' to quit): ") + if query.lower() == "q": + break + + search_parameters = SearchParameters( + query_string=query, exact_match=exact_match, limit=limit + ) + search_response = search_adapter.search(search_parameters) + + for family in search_response.families: + family_data = family.hits[0].model_dump() + console.rule( + title=f"{family_data['family_name']} ({family_data['family_geography']} ,{family_data['family_import_id']})" + ) + print_json( + json.dumps( + { + k: v + for k, v in family_data.items() + if not k.startswith("text_block") and "metadata" not in k + }, + default=str, + ) + ) + + # There's some typing weirdness going on here: + # hasattr(family.hits[0], 'text_blocks') can be False, but + # family.hits[0].text_block exists + try: + rprint("Text blocks:") + table = Table(title="Hits Table") + + # Add columns to the table + table.add_column( + "Text Block ID", justify="right", style="cyan", no_wrap=True + ) + table.add_column("Text Block", style="magenta") + + # Add rows to the table + for hit in family.hits: + try: + table.add_row(f"{hit.text_block_id}", f"{hit.text_block}") # type: ignore + except Exception: + pass + + # Print the table + console.print(table) + + if family == search_response.families[-1]: + print("No more families to show.") + break + except AttributeError: + print("No text blocks found.") + + input("Press any key to show next family") + + +if __name__ == "__main__": + typer.run(main) diff --git a/src/cpr_sdk/version.py b/src/cpr_sdk/version.py index 3fba0a6..4e1e056 100644 --- a/src/cpr_sdk/version.py +++ b/src/cpr_sdk/version.py @@ -1,6 +1,6 @@ _MAJOR = "1" -_MINOR = "6" -_PATCH = "1" +_MINOR = "7" +_PATCH = "0" _SUFFIX = "" VERSION_SHORT = "{0}.{1}".format(_MAJOR, _MINOR)