karni-ai
Structured extraction for Karni: rows for any resource read from text, a web page or a PDF by a language model, always through the import preview.
Experimental. A language model reads a source (text, a web page or a PDF) into rows of any of your resources. The rows always go through Karni's import preview: nothing is saved until a user confirms the preview through the import's own commit. Every call is logged with its tokens and cost, and monthly budgets cap the spending.
The full guide is AI extraction.
pip install "karni-ai[anthropic]" # or karni-ai[openai], or neither for the test provider
karni add ai
karni migrate
karni add ai adds the app. The API rides the auto-router, so there is no
URL include. Karni core never imports this module.
Main features
- Rows for any resource, validated against the resource's own schema
- Text, a web page or a PDF as the source, with URL fetching that never reaches the server's own network
- Always through the import preview: a user commits, never the model
- Every call logged with its tokens and cost, monthly token budgets and a per-user rate limit
- A test provider for CI, and checks
AI001toAI005 - React components in
@karni/ai-ui
The endpoint
POST /api/ai/extract/ with resource (a router prefix, products) and one
of text, url or file (a PDF, multipart) answers 201 with the proposed
rows, a problems list (row, field, message, source: schema or
preview), the preview's counts, the import job's id and its commit path.
POST /api/<resource>/import/<id>/commit/ saves the rows. Only a signed-in
user the resource's access lets create may call it, at most 30 times an
hour (throttle scope ai.extract). GET /api/ai/ lists the caller's own
calls.
Settings
| Setting | Env var | Default | Meaning |
|---|---|---|---|
KARNI_AI_PROVIDER |
same | "" |
anthropic, openai, test or a dotted path to a Provider subclass. No default: karni check fails without it (AI001) |
ANTHROPIC_API_KEY, ANTHROPIC_MODEL |
same | "" |
the anthropic provider's key and model. Environment only, no default model (AI004, AI005) |
OPENAI_API_KEY, OPENAI_MODEL |
same | "" |
the openai provider's key and model. Environment only, no default model (AI004, AI005) |
KARNI_AI_TIMEOUT |
same | 60 |
seconds one provider call may take |
KARNI_AI_MAX_RETRIES |
same | 1 |
retries after a rate limit, an overload or a network failure |
KARNI_AI_MAX_OUTPUT_TOKENS |
same | 16000 |
the longest answer |
KARNI_AI_MAX_ROWS |
same | 50 |
rows proposed at once |
KARNI_AI_MAX_SOURCE_CHARS |
same | 100000 |
the longest source text. Longer is refused, never cut |
KARNI_AI_MAX_UPLOAD_BYTES, KARNI_AI_MAX_PDF_PAGES |
same | 10 MB, 50 |
PDF limits |
KARNI_AI_FETCH_TIMEOUT, KARNI_AI_FETCH_MAX_BYTES, KARNI_AI_FETCH_MAX_REDIRECTS, KARNI_AI_FETCH_PORTS |
same | 10, 5 MB, 3, [80, 443] |
URL fetch limits |
KARNI_AI_PRICES |
{} |
{"<model>": {"input": "1.00", "output": "4.00"}}, per million tokens |
|
KARNI_AI_MONTHLY_TOKENS_PER_USER, KARNI_AI_MONTHLY_TOKENS |
same | None |
monthly token budgets (AI013, AI014) |
KARNI_AI_FETCHER, KARNI_AI_EXTRACTOR |
same | Karni's classes | escape hatches: dotted paths to subclasses |
In karni.yaml, modules: ai: {provider: anthropic} sets the provider. The
key and the model name are never options there: they stay in the
environment.
Escape hatches
- A provider Karni does not ship: subclass
karni_ai.providers.base.Providerand pointKARNI_AI_PROVIDERat it. SubclassAnthropicProviderorOpenAIProviderand overriderequest_params()to add request options. - The prompt, the schema, the access check or the budget rule: subclass
karni_ai.extraction.Extractorand pointKARNI_AI_EXTRACTORat it. - URL fetching (through a proxy, other addresses): subclass
karni_ai.services.fetch.Fetcherand pointKARNI_AI_FETCHERat it. - The rate:
REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["ai.extract"]. - The admin: unregister
AICalland register your ownKarniModelAdmin.
Tests
The test provider needs no network and no key. Without a queued answer it
reads field: value lines, one record per paragraph.
TestProvider.answer_with(...) and TestProvider.fail_with(...) script its
next answers, and TestProvider.reset() clears them.
cd modules/karni-ai && python -m pytest
Links
- AI extraction: https://cbsshekhawat18-lab.github.io/karni/ai-extraction/
- React components: https://www.npmjs.com/package/@karni/ai-ui
- Karni, the framework this package is part of: https://github.com/cbsshekhawat18-lab/karni
- Changelog: https://github.com/cbsshekhawat18-lab/karni/blob/main/CHANGELOG.md
- Issues: https://github.com/cbsshekhawat18-lab/karni/issues
- License: Apache-2.0. karni-ai is released with every other Karni package, at one version.
Metadata
Release files for karni-ai 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| karni_ai-0.5.1.tar.gz | 39.3 kB | Without provenance | Details for karni_ai-0.5.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| karni_ai-0.5.1-py3-none-any.whl | Python 3 | none | any | Details for karni_ai-0.5.1-py3-none-any.whl |
Total release size: 90.7 kB
Release files / karni_ai-0.5.1.tar.gz
| Download URL | karni_ai-0.5.1.tar.gz |
|---|---|
| Size | 39.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f48200b500fb507bbff9ff0ab31fb2a48ed5dae509679b3f9885c3adf86fbca5
|
|
BLAKE2b-256 checksum How to use checksums |
a8db0256e0bb5d7ea0823511b59ad92de5d825667cb5a699e2d0484c66a9fce2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / karni_ai-0.5.1-py3-none-any.whl
| Download URL | karni_ai-0.5.1-py3-none-any.whl |
|---|---|
| Size | 51.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
094ef526ac6f9faf4f44f25781299dab1cd93ebf15703543b2ea6af8da37e0cb
|
|
BLAKE2b-256 checksum How to use checksums |
7bc07bb1038b60e56ee9bc951a8df14c10e2bbfb4ab0fb96067323ce8226daaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for karni-ai 0.5.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.