AI Client
habarico_enterprise.ai.client.ai_client is the shared client the AI features call, and can be
used directly.
Methods
| Method | Returns | Description |
|---|---|---|
get_generated_file_content(description) | ContentFile | Generates an image from a text description and downloads it under a timeout and a size cap. |
get_predicted_text(prefix, length, data=None) | str | Completes prefix, using data (a mapping of field name to value) as the source. length is the completion budget in characters. |
get_assistant_response(messages) | str | Runs one turn of the form assistant conversation. The system prompt goes in as the first message; the raw completion text comes back unparsed. |
get_str_query_filter(model, value) | str | Turns a natural-language query into a Django Q(...) expression for model. |
from habarico_enterprise.ai.client import ai_client
# Generate an image from a text description
image_content = ai_client.get_generated_file_content("A professional photo of a modern office building")
# Complete a product description from the fields already filled in
completion = ai_client.get_predicted_text(
"The Acme kettle is ",
length=200,
data={"name": "Acme kettle", "category": "Kitchen"},
)
Calling the client directly bypasses the permission gate and the payload limits that guard the AI endpoints — see Usage. Whatever you pass in is billed, so bound the input yourself.
length counts characters, not tokens, and it is a budget the model is asked to respect — it
is stated in the prompt and demonstrated by the examples, not enforced on the returned string.
Nothing truncates the answer. What does bound the cost is max_completion_tokens, which
get_predicted_text derives from length and sends with every request; it is set with headroom,
so it acts as a ceiling on the bill rather than as the thing that decides how long the answer is.
get_str_query_filter returns the model's own answer. Validate every lookup path in it against a
whitelist before applying it to a queryset — the endpoint that uses it does, and an unvalidated
expression lets the caller filter on any column or relation of the model. See
Which fields the AI search may filter on.
get_generated_file_content fetches the provider's URL under a connect timeout of 3.05 s and a read
timeout of 30 s, and refuses anything past MAX_GENERATED_FILE_SIZE (10 MiB) with
habarico_enterprise.ai.exceptions.GeneratedFileTooLarge. The cap is counted while the body streams
in, so an oversized file is abandoned mid-transfer rather than buffered and then rejected. A
non-success status — the URL expires roughly an hour after the provider issues it — raises
requests.HTTPError instead of storing the error body as the image. The method can therefore raise
where it previously always returned a ContentFile.
The read timeout is the gap tolerated between two received bytes, not a ceiling on how long the
download may take; what bounds the transfer is the size cap. All four values are module-level
constants in habarico_enterprise.ai.client — override them there if your images are larger.
See also
- AI integration — configuring the provider key
- Usage — who may call the AI actions, and the payload limits