Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add script entrypoint #110

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ adaptor.get_by_id(document_id="id:YOUR_NAMESPACE:YOUR_SCHEMA_NAME::SOME_DOCUMENT

All of the above search functionality assumes that a valid set of vespa credentials is available in `~/.vespa`, or in a directory supplied to the `VespaSearchAdapter` constructor directly. See [the docs](docs/vespa-auth.md) for more information on how vespa expects credentials.

# CLI

There is a simple CLI provided.

```bash
poetry poetry install --extras "vespa"
poetry run cpr
```

# Test setup

Some tests rely on a local running instance of vespa.
Expand Down Expand Up @@ -247,4 +256,4 @@ make vespa_dev_down
- Merge.
- Tag a release manually in github with a version that matches the latest on main that you just merged.
- In CI/CD we will check that the latest release matches the versions defined in code.
- Check in `pypi`.
- Check in `pypi`.
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ classifiers = [

license = "LICENSE"

[tool.poetry.scripts]
cpr = "src.cpr_sdk.cli.run_search_query:app"

[project.urls]
Homepage = "https://github.com/climatepolicyradar/cpr-sdk"
Repository = "https://github.com/climatepolicyradar/cpr-sdk"
Expand All @@ -44,7 +47,7 @@ poetry = "^1.8.3"
flatten-dict = "^0.4.2"

[tool.poetry.extras]
vespa = ["pyvespa", "pyyaml", "sentence-transformers", "torch"]
vespa = ["pyvespa", "pyyaml"]
spacy = ["spacy"]

[tool.poetry.group.dev]
Expand All @@ -58,6 +61,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"
Expand Down Expand Up @@ -109,3 +118,7 @@ line-length = 88
[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]
"tests/*" = ["E501"]

[tool.pyright]
stubPath = ""
reportMissingImports = false
83 changes: 83 additions & 0 deletions src/cpr_sdk/cli/run_search_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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

app = typer.Typer()


@app.callback()
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__":
app()
2 changes: 1 addition & 1 deletion src/cpr_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_MAJOR = "1"
_MINOR = "6"
_PATCH = "1"
_PATCH = "2"
_SUFFIX = ""

VERSION_SHORT = "{0}.{1}".format(_MAJOR, _MINOR)
Expand Down
Loading