Metadata-Version: 2.5
Name: karni-ai
Version: 0.7.0
Summary: 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. Karni is an open-source Django + React framework for building web apps and web games with AI coding agents. Your agent writes only your domain or your game. The API, admin, permissions, React screens and game engine come from packages you upgrade. One rule set covers every coding agent. The build fails when an agent hand-writes what Karni ships.
Project-URL: Homepage, https://github.com/cbsshekhawat18-lab/karni
Project-URL: Documentation, https://cbsshekhawat18-lab.github.io/karni/ai-extraction/
Project-URL: Repository, https://github.com/cbsshekhawat18-lab/karni
Project-URL: Issues, https://github.com/cbsshekhawat18-lab/karni/issues
Project-URL: Changelog, https://github.com/cbsshekhawat18-lab/karni/tree/main/docs/migrations
Author-email: Chandrabhan Shekhawat <cbsshekhawat18@gmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: ai,django,extraction,import,karni,llm,react,structured-output
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: jsonschema>=4.18
Requires-Dist: karni<0.8,>=0.7
Requires-Dist: pypdf>=5
Provides-Extra: anthropic
Requires-Dist: anthropic<3,>=2; extra == 'anthropic'
Provides-Extra: openai
Requires-Dist: openai<4,>=3; extra == 'openai'
Description-Content-Type: text/markdown

# 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.

[![PyPI](https://img.shields.io/pypi/v/karni-ai)](https://pypi.org/project/karni-ai/) [![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/cbsshekhawat18-lab/karni/blob/main/LICENSE) [![Docs](https://img.shields.io/badge/docs-online-FB5D08)](https://cbsshekhawat18-lab.github.io/karni/ai-extraction/)

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](https://cbsshekhawat18-lab.github.io/karni/ai-extraction/).

```bash
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 `AI001` to `AI005`
- React components in [`@karni/ai-ui`](https://www.npmjs.com/package/@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.Provider`
  and point `KARNI_AI_PROVIDER` at it. Subclass `AnthropicProvider` or
  `OpenAIProvider` and override `request_params()` to add request options.
- The prompt, the schema, the access check or the budget rule: subclass
  `karni_ai.extraction.Extractor` and point `KARNI_AI_EXTRACTOR` at it.
- URL fetching (through a proxy, other addresses): subclass
  `karni_ai.services.fetch.Fetcher` and point `KARNI_AI_FETCHER` at it.
- The rate: `REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["ai.extract"]`.
- The admin: unregister `AICall` and register your own `KarniModelAdmin`.

## 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.

```bash
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.
