Metadata-Version: 2.4
Name: fabricatio-capabilities
Version: 0.4.1.dev1
Summary: A foundational Python library providing core capabilities for building LLM-driven applications using an event-based agent structure.
Project-URL: Homepage, https://github.com/Whth/fabricatio
Project-URL: Repository, https://github.com/Whth/fabricatio
Project-URL: Issues, https://github.com/Whth/fabricatio/issues
Author-email: Whth <zettainspector@foxmail.com>
License: MIT License
        
        Copyright (c) 2025 Whth Yotta
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Framework :: AsyncIO
Classifier: Framework :: Pydantic :: 2
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Typing :: Typed
Requires-Python: <3.15,>=3.12
Requires-Dist: fabricatio-core
Requires-Dist: orjson>=3.10.18
Description-Content-Type: text/markdown

# `fabricatio-capabilities`

[MIT](https://img.shields.io/badge/license-MIT-blue.svg)
![Python Versions](https://img.shields.io/pypi/pyversions/fabricatio-capabilities)
[![PyPI Version](https://img.shields.io/pypi/v/fabricatio-capabilities)](https://pypi.org/project/fabricatio-capabilities/)
[![PyPI Downloads](https://static.pepy.tech/badge/fabricatio-capabilities/week)](https://pepy.tech/projects/fabricatio-capabilities)
[![PyPI Downloads](https://static.pepy.tech/badge/fabricatio-capabilities)](https://pepy.tech/projects/fabricatio-capabilities)
[![Build Tool: uv](https://img.shields.io/badge/built%20with-uv-orange)](https://github.com/astral-sh/uv)

High-level LLM agent capabilities for structured extraction, content rating, sequence ordering, and task dispatch. Built on `fabricatio-core`.

## Installation

```bash
pip install fabricatio[capabilities]
# or
uv pip install fabricatio[capabilities]
```

For the full Fabricatio suite:

```bash
pip install fabricatio[full]
```

## Overview

`fabricatio-capabilities` provides opinionated, composable mixins that give agents higher-level reasoning abilities:

- **Extract** structured data from unstructured text into Pydantic models.
- **Rate** content against multi-criteria rubrics, including automated criteria drafting, weighted composite scoring, and top-*k* selection.
- **Order** sequences of items (strings or `WithBriefing` objects) by a requirement or by computed scores.
- **Propose & dispatch** tasks to candidate roles based on semantic matching.
- **Patch** and **persist** Pydantic models with type-safe update mechanisms.

Every capability is an ABC mixin — subclass alongside your agent's base to compose exactly the abilities you need.

## Package Structure

```
fabricatio_capabilities/
 ├── capabilities/         # Mixin classes
 │   ├── extract.py        # Extract — structured extraction from text
 │   ├── rating.py         # Rating — multi-criteria rating, criteria drafting, composite scoring, best-k selection
 │   ├── order.py          # Ordering — LLM-based and score-based sequence ordering
 │   └── task.py           # ProposeTask, DispatchTask — task proposal and delegation
 ├── models/               # Reusable Pydantic base models
 │   ├── generic.py        # Patch, SequencePatch, PersistentAble, FinalizedDumpAble, ModelHash, UpdateFrom, etc.
 │   └── kwargs_types.py   # TypedDict kwargs: CompositeScoreKwargs, OrderStringKwargs, ReferencedKwargs
 └── config.py             # Template name configuration (CapabilitiesConfig)
```

## Key Classes

### Capabilities

| Class | Base | Purpose |
|-------|------|---------|
| `Extract` | `Propose` | Extracts one or more Pydantic model instances from a string or list of strings. Uses configurable prompt templates. |
| `Rating` | `Propose` | Fine-grained rating against a manual and score range. Can draft rating manuals, criteria, and weights (Klee method AHP). Computes composite scores and picks best-*k* candidates. |
| `Ordering` | `Rating` | Orders a sequence of strings or `WithBriefing` items by a natural-language requirement or by computed composite scores. |
| `ProposeTask` | `Propose` | Proposes a `Task` object from a natural-language prompt. |
| `DispatchTask` | `UseLLM` | Dispatches a `Task` to the best-matching candidate `Role` based on briefing text and event subscriptions. |

### Models

| Class | Purpose |
|-------|---------|
| `Patch[T]` | Type-safe field-level updates to a target Pydantic model. Fields present on the patch are copied onto the target. Supports JSON schema generation with reference-class documentation. |
| `SequencePatch[T]` | Patch for sequences of objects carrying a `tweaked` list. |
| `ProposedUpdateAble` | Combines `SketchedAble` + `UpdateFrom` — allows an object to be updated in-place from a proposed replacement. |
| `FinalizedDumpAble` | JSON serialization with alias support and direct file writing. |
| `PersistentAble` | Save to / load from a file path with BLAKE3 content hashing and JSON serialization. |
| `ModelHash` | Consistent `__hash__` based on `model_dump_json()`. |
| `UpdateFrom` | Abstract base for in-place updates with type-checked pre-validation. |
| `AsPrompt` | Converts a model instance into an LLM prompt string. |
| `WordCount` | Mixin providing word count tracking for models. |

### Configuration

`CapabilitiesConfig` (accessible as `capabilities_config`) holds template name defaults for all capability operations: extraction, dispatch, rating, criteria drafting, and ordering.

### Kwargs Types

`CompositeScoreKwargs`, `BestKwargs`, `OrderStringKwargs`, `ReferencedKwargs[T]` — TypedDicts that extend `ValidateKwargs` with capability-specific parameters (topic, criteria, weights, manual, reference).

## Usage

### Structured Extraction

```python
from pydantic import BaseModel
from fabricatio_capabilities.capabilities.extract import Extract

class Person(BaseModel):
    name: str
    age: int

class MyAgent(Extract, YourBaseAgent):
    ...

agent = MyAgent()
person = await agent.extract(Person, "Alice is 30 years old.")
assert person.name == "Alice"
```

### Multi-Criteria Rating

```python
from fabricatio_capabilities.capabilities.rating import Rating

class MyAgent(Rating, YourBaseAgent):
    ...

agent = MyAgent()
manual = await agent.draft_rating_manual("essay quality", {"clarity", "argument"})
scores = await agent.rate("The essay is well-structured.", manual, (0.0, 10.0))
```

### Sequence Ordering

```python
from fabricatio_capabilities.capabilities.order import Ordering

class MyAgent(Ordering, YourBaseAgent):
    ...

agent = MyAgent()
ordered = await agent.order(
    ["clean kitchen", "buy groceries", "pay bills"],
    "by urgency",
)
```

### Task Dispatch

```python
from fabricatio_capabilities.capabilities.task import ProposeTask, DispatchTask

class MyAgent(ProposeTask, DispatchTask, YourBaseAgent):
    ...

agent = MyAgent()
task = await agent.propose_task("Summarize this document.")
result = await agent.dispatch_task(task, candidates={role_a, role_b})
```

### Patching Models

```python
from pydantic import BaseModel
from fabricatio_capabilities.models.generic import Patch

class User(BaseModel):
    name: str
    age: int
    email: str = ""

class UserPatch(Patch[User], BaseModel):
    name: str | None = None
    email: str | None = None

user = User(name="Alice", age=30)
patch = UserPatch(name="Bob")
updated = patch.apply(user)
assert updated.name == "Bob" and updated.age == 30
```

## Dependencies

- `fabricatio-core` — core interfaces (`Propose`, `UseLLM`, `Task`, `Role`, `TEMPLATE_MANAGER`)
- `orjson` — fast JSON serialization
- `pydantic` — model validation and schema generation
- `more-itertools` — utility iterators

## License

MIT — see [LICENSE](../../LICENSE)
