|
1 | 1 | # Groundlight Python SDK
|
| 2 | + |
| 3 | +This package holds an SDK for accessing the Groundlight public API. |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | +[Make sure you have internal pypi credentials set up](https://github.com/positronix-ai/tools/blob/main/internal-pip-login.sh), and then install with `pip` or `poetry`. |
| 8 | + |
| 9 | +```Bash |
| 10 | +# pip |
| 11 | +$ pip install groundlight |
| 12 | + |
| 13 | +# poetry |
| 14 | +$ poetry add groundlight |
| 15 | +``` |
| 16 | + |
| 17 | +### Basic Usage |
| 18 | + |
| 19 | +To access the API, you need an API token. You can create one at [app.groundlight.ai](https://app.positronix.ai/reef/my-account/api-tokens). Then, add it as an environment variable called `GROUNDLIGHT_API_TOKEN`: |
| 20 | + |
| 21 | +```Bash |
| 22 | +$ export GROUNDLIGHT_API_TOKEN=tok_abc123 |
| 23 | +``` |
| 24 | + |
| 25 | +Now you can use the python SDK! |
| 26 | + |
| 27 | +```Python |
| 28 | +from groundlight import Groundlight |
| 29 | + |
| 30 | +# Load the API client. This defaults to the prod endpoint, |
| 31 | +# but you can specify a different endpoint like so: |
| 32 | +# gl = Groundlight(endpoint="https://device.integ.positronix.ai/device-api") |
| 33 | +gl = Groundlight() |
| 34 | + |
| 35 | +# Call an API method (e.g., retrieve a list of detectors) |
| 36 | +detectors = gl.list_detectors() |
| 37 | +``` |
| 38 | + |
| 39 | +### What API methods are available? |
| 40 | + |
| 41 | +Check out the [User Guide](UserGuide.md)! |
| 42 | + |
| 43 | +For more details, see the [Groundlight](src/groundlight/client.py) class. This SDK closely follows the methods in our [API Docs](https://app.positronix.ai/reef/admin/api-docs). |
| 44 | + |
| 45 | + |
| 46 | +## Development |
| 47 | + |
| 48 | +The auto-generated SDK code is in the `generated/` directory. To re-generate the client code, you'll need to install [openapi-generator](https://openapi-generator.tech/docs/installation#homebrew) (I recommend homebrew if you're on a mac). Then you can run it with: |
| 49 | + |
| 50 | +```Bash |
| 51 | +$ make generate |
| 52 | +``` |
| 53 | + |
| 54 | +## Testing |
| 55 | + |
| 56 | +Most tests need an API endpoint to run. |
| 57 | + |
| 58 | +### Local API endpoint |
| 59 | + |
| 60 | +1. Set up a local [janzu API endpoint](https://github.com/positronix-ai/zuuul/blob/main/deploy/README.md#development-using-local-microk8s) running (e.g., on an AWS GPU instance). |
| 61 | + |
| 62 | +1. Set up an ssh tunnel to your laptop. That way, you can access the endpoint at http://localhost:8000/device-api (and the web UI at http://localhost:8000/reef): |
| 63 | + |
| 64 | + ```Bash |
| 65 | + $ ssh instance-name -L 8000:localhost:80 |
| 66 | + ``` |
| 67 | + |
| 68 | +1. Run the tests (with an API token) |
| 69 | + |
| 70 | + ```Bash |
| 71 | + $ export GROUNDLIGHT_API_TOKEN=tok_abc123 |
| 72 | + $ make test-local |
| 73 | + ``` |
| 74 | + |
| 75 | +(Note: in theory, it's possible to run the janzu API server on your laptop without microk8s - but some API methods don't work because of the dependence on GPUs) |
| 76 | + |
| 77 | +### Integ API endpoint |
| 78 | + |
| 79 | +1. Run the tests (with an API token) |
| 80 | + |
| 81 | + ```Bash |
| 82 | + $ export GROUNDLIGHT_API_TOKEN=tok_abc123 |
| 83 | + $ make test-integ |
| 84 | + ``` |
| 85 | + |
| 86 | +## Releases |
| 87 | + |
| 88 | +To publish a new package version to our [internal pypi repository](https://github.com/positronix-ai/packaging/tree/main/aws), you create a release on github. |
| 89 | + |
| 90 | +```Bash |
| 91 | +# Create a git tag locally. Use semver "vX.Y.Z" format. |
| 92 | +$ git tag -a v0.1.2 -m "Short description" |
| 93 | +
|
| 94 | +# Push the tag to the github repo |
| 95 | +$ git push origin --tags |
| 96 | +``` |
| 97 | + |
| 98 | +Then, go to the [github repo](https://github.com/positronix-ai/groundlight-python-sdk/tags) -> choose your tag -> create a release from this tag -> type in some description -> release. A [github action](https://github.com/positronix-ai/groundlight-python-sdk/actions/workflows/publish.yaml) will trigger a release, and then `groundlight-X.Y.Z` will be available for consumers. |
| 99 | + |
| 100 | +## TODOs |
| 101 | + |
| 102 | +- Figure out how we want to handle tests (since almost everything is an integration test). And, running the stateful (creation) tests can lead to a bunch of objects in the DB. |
| 103 | +- Improve wrappers around API functions (e.g., simplify the responses even further, add auto-pagination managers, etc.) |
| 104 | + - The SDK should allow you to work with the most natural interface, rather than trying to exactly mirror the REST API. |
| 105 | + - E.g. |
| 106 | + - Add an image query long polling helper method (calls POST, then several GETs) |
| 107 | + - It would be nice to have a `get_or_create_detector()` function (even better if it's supported in the API directly). That way, "submit image query" code examples will be simpler. |
| 108 | +- Better auto-generated code docs (e.g. [sphinx](https://www.sphinx-doc.org/en/master/)) |
| 109 | + - Model types (e.g., [autodoc_pydantic](https://github.com/mansenfranzen/autodoc_pydantic)) |
| 110 | + - Cleaner auto-generated model names (e.g., `PaginatedDetectorList` is a little ugly) |
| 111 | +- Better versioning strategy. On the one hand, this package will closely follow the versioning in the HTTP API. On the other hand, we may add features in the client (like image utils, shortcuts, etc.) that are not in the REST API. |
| 112 | +- Better way of managing dependency on `public-api.yaml` OpenAPI spec (right now, we just copy the file over manually) |
| 113 | +- Update the web links (links to website, link to API endpoint, etc.) |
| 114 | +- `with` context manager (auto cleanup the client object) |
| 115 | +- It would be great to add notebooks with interactive examples that can actually run out of the box |
| 116 | +- Have a cleaner distinction between dev docs and user guide docs |
| 117 | +- ... |
0 commit comments