django-model-agent 2026.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_model_agent-2026.1.1/.gitignore +48 -0
- django_model_agent-2026.1.1/LICENSE.md +131 -0
- django_model_agent-2026.1.1/PKG-INFO +229 -0
- django_model_agent-2026.1.1/README.md +203 -0
- django_model_agent-2026.1.1/pyproject.toml +68 -0
- django_model_agent-2026.1.1/src/django_model_agent/README.md +267 -0
- django_model_agent-2026.1.1/src/django_model_agent/__init__.py +28 -0
- django_model_agent-2026.1.1/src/django_model_agent/base.py +753 -0
- django_model_agent-2026.1.1/src/django_model_agent/capabilities.py +862 -0
- django_model_agent-2026.1.1/src/django_model_agent/examples.py +462 -0
- django_model_agent-2026.1.1/src/django_model_agent/memory.py +263 -0
- django_model_agent-2026.1.1/src/django_model_agent/migrations/0001_initial.py +33 -0
- django_model_agent-2026.1.1/src/django_model_agent/migrations/__init__.py +0 -0
- django_model_agent-2026.1.1/src/django_model_agent/models.py +15 -0
- django_model_agent-2026.1.1/src/django_model_agent/tools.py +404 -0
- django_model_agent-2026.1.1/tests/__init__.py +1 -0
- django_model_agent-2026.1.1/tests/conftest.py +68 -0
- django_model_agent-2026.1.1/tests/models.py +121 -0
- django_model_agent-2026.1.1/tests/settings.py +36 -0
- django_model_agent-2026.1.1/tests/test_approval.py +194 -0
- django_model_agent-2026.1.1/tests/test_audit_capability.py +248 -0
- django_model_agent-2026.1.1/tests/test_capabilities.py +259 -0
- django_model_agent-2026.1.1/tests/test_fsm_capability.py +211 -0
- django_model_agent-2026.1.1/tests/test_memory_capability.py +247 -0
- django_model_agent-2026.1.1/tests/test_model_agent.py +472 -0
- django_model_agent-2026.1.1/tests/test_model_agent_capabilities.py +518 -0
- django_model_agent-2026.1.1/tests/test_original.py +951 -0
- django_model_agent-2026.1.1/tests/test_pydantic_ai.py +229 -0
- django_model_agent-2026.1.1/tests/test_streaming.py +95 -0
- django_model_agent-2026.1.1/tests/test_tool_arguments.py +202 -0
- django_model_agent-2026.1.1/tests/test_tool_connections.py +117 -0
- django_model_agent-2026.1.1/tests/test_tool_wrapping.py +121 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
*.egg-info/
|
|
7
|
+
*.egg
|
|
8
|
+
dist/
|
|
9
|
+
build/
|
|
10
|
+
.eggs/
|
|
11
|
+
|
|
12
|
+
# Virtual environments
|
|
13
|
+
.venv/
|
|
14
|
+
venv/
|
|
15
|
+
ENV/
|
|
16
|
+
|
|
17
|
+
# IDE
|
|
18
|
+
.idea/
|
|
19
|
+
.vscode/
|
|
20
|
+
*.swp
|
|
21
|
+
*.swo
|
|
22
|
+
*~
|
|
23
|
+
|
|
24
|
+
# Testing
|
|
25
|
+
.pytest_cache/
|
|
26
|
+
.coverage
|
|
27
|
+
htmlcov/
|
|
28
|
+
.tox/
|
|
29
|
+
.nox/
|
|
30
|
+
|
|
31
|
+
# Django
|
|
32
|
+
*.sqlite3
|
|
33
|
+
db.sqlite3
|
|
34
|
+
|
|
35
|
+
# Docs build output
|
|
36
|
+
site/
|
|
37
|
+
|
|
38
|
+
# Distribution
|
|
39
|
+
*.tar.gz
|
|
40
|
+
*.whl
|
|
41
|
+
|
|
42
|
+
# OS
|
|
43
|
+
.DS_Store
|
|
44
|
+
Thumbs.db
|
|
45
|
+
|
|
46
|
+
# Environment
|
|
47
|
+
.env
|
|
48
|
+
.env.*
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# PolyForm Noncommercial License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree
|
|
8
|
+
to them as both strict obligations and conditions to all
|
|
9
|
+
your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the
|
|
14
|
+
software to do everything you might do with the software
|
|
15
|
+
that would otherwise infringe the licensor's copyright
|
|
16
|
+
in it for any permitted purpose. However, you may
|
|
17
|
+
only distribute the software according to [Distribution
|
|
18
|
+
License](#distribution-license) and make changes or new works
|
|
19
|
+
based on the software according to [Changes and New Works
|
|
20
|
+
License](#changes-and-new-works-license).
|
|
21
|
+
|
|
22
|
+
## Distribution License
|
|
23
|
+
|
|
24
|
+
The licensor grants you an additional copyright license
|
|
25
|
+
to distribute copies of the software. Your license
|
|
26
|
+
to distribute covers distributing the software with
|
|
27
|
+
changes and new works permitted by [Changes and New Works
|
|
28
|
+
License](#changes-and-new-works-license).
|
|
29
|
+
|
|
30
|
+
## Notices
|
|
31
|
+
|
|
32
|
+
You must ensure that anyone who gets a copy of any part of
|
|
33
|
+
the software from you also gets a copy of these terms or the
|
|
34
|
+
URL for them above, as well as copies of any plain-text lines
|
|
35
|
+
beginning with `Required Notice:` that the licensor provided
|
|
36
|
+
with the software. For example:
|
|
37
|
+
|
|
38
|
+
> Required Notice: Copyright Jeff Triplett. (https://github.com/jefftriplett)
|
|
39
|
+
|
|
40
|
+
## Changes and New Works License
|
|
41
|
+
|
|
42
|
+
The licensor grants you an additional copyright license to
|
|
43
|
+
make changes and new works based on the software for any
|
|
44
|
+
permitted purpose.
|
|
45
|
+
|
|
46
|
+
## Patent License
|
|
47
|
+
|
|
48
|
+
The licensor grants you a patent license for the software that
|
|
49
|
+
covers patent claims the licensor can license, or becomes able
|
|
50
|
+
to license, that you would infringe by using the software.
|
|
51
|
+
|
|
52
|
+
## Noncommercial Purposes
|
|
53
|
+
|
|
54
|
+
Any noncommercial purpose is a permitted purpose.
|
|
55
|
+
|
|
56
|
+
## Personal Uses
|
|
57
|
+
|
|
58
|
+
Personal use for research, experiment, and testing for
|
|
59
|
+
the benefit of public knowledge, personal study, private
|
|
60
|
+
entertainment, hobby projects, amateur pursuits, or religious
|
|
61
|
+
observance, without any anticipated commercial application,
|
|
62
|
+
is use for a permitted purpose.
|
|
63
|
+
|
|
64
|
+
## Noncommercial Organizations
|
|
65
|
+
|
|
66
|
+
Use by any charitable organization, educational institution,
|
|
67
|
+
public research organization, public safety or health
|
|
68
|
+
organization, environmental protection organization,
|
|
69
|
+
or government institution is use for a permitted purpose
|
|
70
|
+
regardless of the source of funding or obligations resulting
|
|
71
|
+
from the funding.
|
|
72
|
+
|
|
73
|
+
## Fair Use
|
|
74
|
+
|
|
75
|
+
You may have "fair use" rights for the software under the
|
|
76
|
+
law. These terms do not limit them.
|
|
77
|
+
|
|
78
|
+
## No Other Rights
|
|
79
|
+
|
|
80
|
+
These terms do not allow you to sublicense or transfer any of
|
|
81
|
+
your licenses to anyone else, or prevent the licensor from
|
|
82
|
+
granting licenses to anyone else. These terms do not imply
|
|
83
|
+
any other licenses.
|
|
84
|
+
|
|
85
|
+
## Patent Defense
|
|
86
|
+
|
|
87
|
+
If you make any written claim that the software infringes or
|
|
88
|
+
contributes to infringement of any patent, your patent license
|
|
89
|
+
for the software granted under these terms ends immediately. If
|
|
90
|
+
your company makes such a claim, your patent license ends
|
|
91
|
+
immediately for work on behalf of your company.
|
|
92
|
+
|
|
93
|
+
## Violations
|
|
94
|
+
|
|
95
|
+
The first time you are notified in writing that you have
|
|
96
|
+
violated any of these terms, or done anything with the software
|
|
97
|
+
not covered by your licenses, your licenses can nonetheless
|
|
98
|
+
continue if you come into full compliance with these terms,
|
|
99
|
+
and take practical steps to correct past violations, within
|
|
100
|
+
32 days of receiving notice. Otherwise, all your licenses
|
|
101
|
+
end immediately.
|
|
102
|
+
|
|
103
|
+
## No Liability
|
|
104
|
+
|
|
105
|
+
***As far as the law allows, the software comes as is, without
|
|
106
|
+
any warranty or condition, and the licensor will not be liable
|
|
107
|
+
to you for any damages arising out of these terms or the use
|
|
108
|
+
or nature of the software, under any kind of legal claim.***
|
|
109
|
+
|
|
110
|
+
## Definitions
|
|
111
|
+
|
|
112
|
+
The **licensor** is the individual or entity offering these
|
|
113
|
+
terms, and the **software** is the software the licensor makes
|
|
114
|
+
available under these terms.
|
|
115
|
+
|
|
116
|
+
**You** refers to the individual or entity agreeing to these
|
|
117
|
+
terms.
|
|
118
|
+
|
|
119
|
+
**Your company** is any legal entity, sole proprietorship,
|
|
120
|
+
or other kind of organization that you work for, plus all
|
|
121
|
+
organizations that have control over, are under the control of,
|
|
122
|
+
or are under common control with that organization. **Control**
|
|
123
|
+
means ownership of substantially all the assets of an entity,
|
|
124
|
+
or the power to direct its management and policies by vote,
|
|
125
|
+
contract, or otherwise. Control can be direct or indirect.
|
|
126
|
+
|
|
127
|
+
**Your licenses** are all the licenses granted to you for the
|
|
128
|
+
software under these terms.
|
|
129
|
+
|
|
130
|
+
**Use** means anything you do with the software requiring one
|
|
131
|
+
of your licenses.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: django-model-agent
|
|
3
|
+
Version: 2026.1.1
|
|
4
|
+
Summary: A Django Ninja-style abstraction for binding Django models to Pydantic AI Agents
|
|
5
|
+
Project-URL: Changelog, https://github.com/jefftriplett/django-model-agent/releases
|
|
6
|
+
Project-URL: Documentation, https://django-model-agent.readthedocs.io/en/latest/
|
|
7
|
+
Project-URL: Homepage, https://github.com/jefftriplett/django-model-agent
|
|
8
|
+
Project-URL: Issues, https://github.com/jefftriplett/django-model-agent/issues
|
|
9
|
+
Project-URL: Repository, https://github.com/jefftriplett/django-model-agent
|
|
10
|
+
Author: Jeff Triplett
|
|
11
|
+
License-Expression: PolyForm-Noncommercial-1.0.0
|
|
12
|
+
License-File: LICENSE.md
|
|
13
|
+
Classifier: Framework :: Django
|
|
14
|
+
Classifier: Framework :: Django :: 5.2
|
|
15
|
+
Classifier: Framework :: Django :: 6.0
|
|
16
|
+
Classifier: Framework :: Django :: 6.1
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
|
|
21
|
+
Requires-Python: >=3.13
|
|
22
|
+
Requires-Dist: django>=5.2
|
|
23
|
+
Requires-Dist: pydantic-ai>=2.22
|
|
24
|
+
Requires-Dist: pydantic>=2.12
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Django Model Agent
|
|
28
|
+
|
|
29
|
+
> **Note:** This project is experimental and the API will change.
|
|
30
|
+
|
|
31
|
+
A Django Ninja-style abstraction for binding Django models to Pydantic AI Agents.
|
|
32
|
+
|
|
33
|
+
**[Documentation](https://django-model-agent.readthedocs.io/en/latest/)** · [Getting started](https://django-model-agent.readthedocs.io/en/latest/getting-started/) · [Cookbook](https://django-model-agent.readthedocs.io/en/latest/cookbook/) · [Examples](https://django-model-agent.readthedocs.io/en/latest/examples/)
|
|
34
|
+
|
|
35
|
+
This library provides a declarative way to create AI agents that understand and operate on Django model instances, similar to how Django Ninja's ModelSchema provides a declarative way to serialize models.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
This package is not yet published on PyPI. Install directly from GitHub:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
uv add git+https://github.com/jefftriplett/django-model-agent
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Or with pip:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install git+https://github.com/jefftriplett/django-model-agent
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Once published on PyPI:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
uv add django-model-agent
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or with pip:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install django-model-agent
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Quick Start
|
|
64
|
+
|
|
65
|
+
Today, the stable part of the package is the Django model/schema/tool abstraction layer. To execute prompts, subclass `ModelAgent` and implement `build_agent()` for your own `pydantic-ai` integration.
|
|
66
|
+
|
|
67
|
+
### Class Attribute Style
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from django_model_agent import ModelAgent, ModelTool
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class RestaurantAgent(ModelAgent):
|
|
74
|
+
model = Restaurant
|
|
75
|
+
fields = ["name", "address", "hours", "neighborhood"]
|
|
76
|
+
|
|
77
|
+
_system_prompts = """
|
|
78
|
+
You are an assistant that helps reason about restaurant information.
|
|
79
|
+
Use the provided model fields as your source of truth.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
tools = [UpdateHoursTool, FlagForReviewTool]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Decorator Style (Pydantic-AI inspired)
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
class RestaurantAgent(ModelAgent):
|
|
89
|
+
model = Restaurant
|
|
90
|
+
fields = ["name", "address", "hours"]
|
|
91
|
+
|
|
92
|
+
@ModelAgent.system_prompt
|
|
93
|
+
def context_prompt(self) -> str:
|
|
94
|
+
return "You help with restaurant information."
|
|
95
|
+
|
|
96
|
+
@ModelAgent.instructions
|
|
97
|
+
def dynamic_instructions(self) -> str:
|
|
98
|
+
return f"Current restaurant: {self.instance.name}"
|
|
99
|
+
|
|
100
|
+
@ModelAgent.tool
|
|
101
|
+
def get_hours(self) -> str:
|
|
102
|
+
"""Get the restaurant hours."""
|
|
103
|
+
return str(self.instance.hours)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Usage
|
|
107
|
+
|
|
108
|
+
Inspect the schema, prompts, and tools:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
restaurant = Restaurant.objects.get(pk=123)
|
|
112
|
+
agent = RestaurantAgent(restaurant)
|
|
113
|
+
|
|
114
|
+
schema = agent.schema
|
|
115
|
+
system_prompt = agent.get_system_prompts()
|
|
116
|
+
instructions = agent.get_instructions()
|
|
117
|
+
tools = agent.get_tools()
|
|
118
|
+
|
|
119
|
+
# Override prompts at init time
|
|
120
|
+
agent = RestaurantAgent(
|
|
121
|
+
restaurant,
|
|
122
|
+
system_prompt="You are a concise assistant.",
|
|
123
|
+
instructions="Focus only on hours of operation.",
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Wire in your own runtime:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from pydantic_ai import Agent
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class RunnableRestaurantAgent(RestaurantAgent):
|
|
134
|
+
def build_agent(self) -> Agent:
|
|
135
|
+
return Agent(
|
|
136
|
+
model=self.schema,
|
|
137
|
+
system_prompt=self.get_system_prompts(),
|
|
138
|
+
instructions=self.get_instructions(),
|
|
139
|
+
tools=self.get_tools(),
|
|
140
|
+
)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Features
|
|
144
|
+
|
|
145
|
+
- **Declarative model binding**: Define which Django model fields your agent can access
|
|
146
|
+
- **Field sets**: Create named groups of fields for role-based exposure
|
|
147
|
+
- **Multiple prompt sources**: Combine class-level prompts with decorated methods
|
|
148
|
+
- **Tool registration**: Register tools via class attributes or decorators
|
|
149
|
+
- **Template support**: Use Django templates for dynamic instructions
|
|
150
|
+
- **Automatic schema generation**: Pydantic schemas are generated from Django models
|
|
151
|
+
|
|
152
|
+
## Init Parameters
|
|
153
|
+
|
|
154
|
+
| Parameter | Description |
|
|
155
|
+
|-----------|-------------|
|
|
156
|
+
| `instance` | The Django model instance to operate on |
|
|
157
|
+
| `system_prompt` | Override or extend the class-level system prompts |
|
|
158
|
+
| `instructions` | Override or extend the class-level instructions |
|
|
159
|
+
| `field_set` | Name of a field set to use for schema generation |
|
|
160
|
+
|
|
161
|
+
## Class Attributes
|
|
162
|
+
|
|
163
|
+
| Attribute | Description |
|
|
164
|
+
|-----------|-------------|
|
|
165
|
+
| `model` | The Django model class this agent operates on |
|
|
166
|
+
| `fields` | List of field names to expose to the agent (None = all fields) |
|
|
167
|
+
| `exclude` | List of field names to exclude from the schema |
|
|
168
|
+
| `_system_prompts` | System prompt string or list of strings for the agent |
|
|
169
|
+
| `_instructions` | Instructions string or list of strings for the agent |
|
|
170
|
+
| `_instructions_template` | Path to a Django template for instructions |
|
|
171
|
+
| `tools` | List of tool classes available to the agent |
|
|
172
|
+
| `_field_sets` | Named groups of fields for role-based exposure |
|
|
173
|
+
|
|
174
|
+
## Decorators
|
|
175
|
+
|
|
176
|
+
| Decorator | Description |
|
|
177
|
+
|-----------|-------------|
|
|
178
|
+
| `@ModelAgent.system_prompt` | Register a method as a system prompt provider |
|
|
179
|
+
| `@ModelAgent.instructions` | Register a method as an instructions provider |
|
|
180
|
+
| `@ModelAgent.tool` | Register a method as a tool available to the agent |
|
|
181
|
+
|
|
182
|
+
## Creating Tools
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
from django_model_agent.tools import UpdateTool, ToolResult
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class UpdateHoursTool(UpdateTool):
|
|
189
|
+
name = "update_hours"
|
|
190
|
+
description = "Update the operating hours"
|
|
191
|
+
|
|
192
|
+
def update(self, *, new_hours: str) -> None:
|
|
193
|
+
self.instance.hours = new_hours
|
|
194
|
+
# save() is called automatically by UpdateTool
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Or for read-only tools:
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from django_model_agent.tools import ReadOnlyTool
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class GetHoursTool(ReadOnlyTool):
|
|
204
|
+
name = "get_hours"
|
|
205
|
+
description = "Get the current operating hours"
|
|
206
|
+
|
|
207
|
+
def read(self, **kwargs) -> dict:
|
|
208
|
+
return {"hours": self.instance.hours}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Local Development
|
|
212
|
+
|
|
213
|
+
Python `3.13+` is currently required, including free-threaded (`t`) builds.
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
uv sync --group dev
|
|
217
|
+
just test
|
|
218
|
+
just lint
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Useful commands:
|
|
222
|
+
|
|
223
|
+
- `just test tests/test_model_agent.py`
|
|
224
|
+
- `just lint`
|
|
225
|
+
- `uv build`
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
PolyForm Noncommercial License 1.0.0
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# Django Model Agent
|
|
2
|
+
|
|
3
|
+
> **Note:** This project is experimental and the API will change.
|
|
4
|
+
|
|
5
|
+
A Django Ninja-style abstraction for binding Django models to Pydantic AI Agents.
|
|
6
|
+
|
|
7
|
+
**[Documentation](https://django-model-agent.readthedocs.io/en/latest/)** · [Getting started](https://django-model-agent.readthedocs.io/en/latest/getting-started/) · [Cookbook](https://django-model-agent.readthedocs.io/en/latest/cookbook/) · [Examples](https://django-model-agent.readthedocs.io/en/latest/examples/)
|
|
8
|
+
|
|
9
|
+
This library provides a declarative way to create AI agents that understand and operate on Django model instances, similar to how Django Ninja's ModelSchema provides a declarative way to serialize models.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
This package is not yet published on PyPI. Install directly from GitHub:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
uv add git+https://github.com/jefftriplett/django-model-agent
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or with pip:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install git+https://github.com/jefftriplett/django-model-agent
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Once published on PyPI:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
uv add django-model-agent
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or with pip:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install django-model-agent
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
Today, the stable part of the package is the Django model/schema/tool abstraction layer. To execute prompts, subclass `ModelAgent` and implement `build_agent()` for your own `pydantic-ai` integration.
|
|
40
|
+
|
|
41
|
+
### Class Attribute Style
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from django_model_agent import ModelAgent, ModelTool
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class RestaurantAgent(ModelAgent):
|
|
48
|
+
model = Restaurant
|
|
49
|
+
fields = ["name", "address", "hours", "neighborhood"]
|
|
50
|
+
|
|
51
|
+
_system_prompts = """
|
|
52
|
+
You are an assistant that helps reason about restaurant information.
|
|
53
|
+
Use the provided model fields as your source of truth.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
tools = [UpdateHoursTool, FlagForReviewTool]
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Decorator Style (Pydantic-AI inspired)
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
class RestaurantAgent(ModelAgent):
|
|
63
|
+
model = Restaurant
|
|
64
|
+
fields = ["name", "address", "hours"]
|
|
65
|
+
|
|
66
|
+
@ModelAgent.system_prompt
|
|
67
|
+
def context_prompt(self) -> str:
|
|
68
|
+
return "You help with restaurant information."
|
|
69
|
+
|
|
70
|
+
@ModelAgent.instructions
|
|
71
|
+
def dynamic_instructions(self) -> str:
|
|
72
|
+
return f"Current restaurant: {self.instance.name}"
|
|
73
|
+
|
|
74
|
+
@ModelAgent.tool
|
|
75
|
+
def get_hours(self) -> str:
|
|
76
|
+
"""Get the restaurant hours."""
|
|
77
|
+
return str(self.instance.hours)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Usage
|
|
81
|
+
|
|
82
|
+
Inspect the schema, prompts, and tools:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
restaurant = Restaurant.objects.get(pk=123)
|
|
86
|
+
agent = RestaurantAgent(restaurant)
|
|
87
|
+
|
|
88
|
+
schema = agent.schema
|
|
89
|
+
system_prompt = agent.get_system_prompts()
|
|
90
|
+
instructions = agent.get_instructions()
|
|
91
|
+
tools = agent.get_tools()
|
|
92
|
+
|
|
93
|
+
# Override prompts at init time
|
|
94
|
+
agent = RestaurantAgent(
|
|
95
|
+
restaurant,
|
|
96
|
+
system_prompt="You are a concise assistant.",
|
|
97
|
+
instructions="Focus only on hours of operation.",
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Wire in your own runtime:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from pydantic_ai import Agent
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class RunnableRestaurantAgent(RestaurantAgent):
|
|
108
|
+
def build_agent(self) -> Agent:
|
|
109
|
+
return Agent(
|
|
110
|
+
model=self.schema,
|
|
111
|
+
system_prompt=self.get_system_prompts(),
|
|
112
|
+
instructions=self.get_instructions(),
|
|
113
|
+
tools=self.get_tools(),
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Features
|
|
118
|
+
|
|
119
|
+
- **Declarative model binding**: Define which Django model fields your agent can access
|
|
120
|
+
- **Field sets**: Create named groups of fields for role-based exposure
|
|
121
|
+
- **Multiple prompt sources**: Combine class-level prompts with decorated methods
|
|
122
|
+
- **Tool registration**: Register tools via class attributes or decorators
|
|
123
|
+
- **Template support**: Use Django templates for dynamic instructions
|
|
124
|
+
- **Automatic schema generation**: Pydantic schemas are generated from Django models
|
|
125
|
+
|
|
126
|
+
## Init Parameters
|
|
127
|
+
|
|
128
|
+
| Parameter | Description |
|
|
129
|
+
|-----------|-------------|
|
|
130
|
+
| `instance` | The Django model instance to operate on |
|
|
131
|
+
| `system_prompt` | Override or extend the class-level system prompts |
|
|
132
|
+
| `instructions` | Override or extend the class-level instructions |
|
|
133
|
+
| `field_set` | Name of a field set to use for schema generation |
|
|
134
|
+
|
|
135
|
+
## Class Attributes
|
|
136
|
+
|
|
137
|
+
| Attribute | Description |
|
|
138
|
+
|-----------|-------------|
|
|
139
|
+
| `model` | The Django model class this agent operates on |
|
|
140
|
+
| `fields` | List of field names to expose to the agent (None = all fields) |
|
|
141
|
+
| `exclude` | List of field names to exclude from the schema |
|
|
142
|
+
| `_system_prompts` | System prompt string or list of strings for the agent |
|
|
143
|
+
| `_instructions` | Instructions string or list of strings for the agent |
|
|
144
|
+
| `_instructions_template` | Path to a Django template for instructions |
|
|
145
|
+
| `tools` | List of tool classes available to the agent |
|
|
146
|
+
| `_field_sets` | Named groups of fields for role-based exposure |
|
|
147
|
+
|
|
148
|
+
## Decorators
|
|
149
|
+
|
|
150
|
+
| Decorator | Description |
|
|
151
|
+
|-----------|-------------|
|
|
152
|
+
| `@ModelAgent.system_prompt` | Register a method as a system prompt provider |
|
|
153
|
+
| `@ModelAgent.instructions` | Register a method as an instructions provider |
|
|
154
|
+
| `@ModelAgent.tool` | Register a method as a tool available to the agent |
|
|
155
|
+
|
|
156
|
+
## Creating Tools
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from django_model_agent.tools import UpdateTool, ToolResult
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class UpdateHoursTool(UpdateTool):
|
|
163
|
+
name = "update_hours"
|
|
164
|
+
description = "Update the operating hours"
|
|
165
|
+
|
|
166
|
+
def update(self, *, new_hours: str) -> None:
|
|
167
|
+
self.instance.hours = new_hours
|
|
168
|
+
# save() is called automatically by UpdateTool
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Or for read-only tools:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from django_model_agent.tools import ReadOnlyTool
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class GetHoursTool(ReadOnlyTool):
|
|
178
|
+
name = "get_hours"
|
|
179
|
+
description = "Get the current operating hours"
|
|
180
|
+
|
|
181
|
+
def read(self, **kwargs) -> dict:
|
|
182
|
+
return {"hours": self.instance.hours}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Local Development
|
|
186
|
+
|
|
187
|
+
Python `3.13+` is currently required, including free-threaded (`t`) builds.
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
uv sync --group dev
|
|
191
|
+
just test
|
|
192
|
+
just lint
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Useful commands:
|
|
196
|
+
|
|
197
|
+
- `just test tests/test_model_agent.py`
|
|
198
|
+
- `just lint`
|
|
199
|
+
- `uv build`
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
PolyForm Noncommercial License 1.0.0
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "hatchling.build"
|
|
3
|
+
requires = ["hatchling"]
|
|
4
|
+
|
|
5
|
+
[dependency-groups]
|
|
6
|
+
dev = [
|
|
7
|
+
"pytest>=8.0",
|
|
8
|
+
"pytest-django>=4.5",
|
|
9
|
+
"zensical>=0.0.52"
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
[project]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Jeff Triplett" }
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Framework :: Django",
|
|
18
|
+
"Framework :: Django :: 5.2",
|
|
19
|
+
"Framework :: Django :: 6.0",
|
|
20
|
+
"Framework :: Django :: 6.1",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Programming Language :: Python :: Free Threading :: 3 - Stable"
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"django>=5.2",
|
|
28
|
+
"pydantic>=2.12",
|
|
29
|
+
"pydantic-ai>=2.22"
|
|
30
|
+
]
|
|
31
|
+
description = "A Django Ninja-style abstraction for binding Django models to Pydantic AI Agents"
|
|
32
|
+
license = "PolyForm-Noncommercial-1.0.0"
|
|
33
|
+
name = "django-model-agent"
|
|
34
|
+
readme = "README.md"
|
|
35
|
+
requires-python = ">=3.13"
|
|
36
|
+
version = "2026.1.1"
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Changelog = "https://github.com/jefftriplett/django-model-agent/releases"
|
|
40
|
+
Documentation = "https://django-model-agent.readthedocs.io/en/latest/"
|
|
41
|
+
Homepage = "https://github.com/jefftriplett/django-model-agent"
|
|
42
|
+
Issues = "https://github.com/jefftriplett/django-model-agent/issues"
|
|
43
|
+
Repository = "https://github.com/jefftriplett/django-model-agent"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.sdist]
|
|
46
|
+
include = [
|
|
47
|
+
"/README.md",
|
|
48
|
+
"/src",
|
|
49
|
+
"/tests"
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.hatch.build.targets.wheel]
|
|
53
|
+
packages = ["src/django_model_agent"]
|
|
54
|
+
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
57
|
+
addopts = "-v --tb=short"
|
|
58
|
+
python_files = ["test_*.py"]
|
|
59
|
+
pythonpath = [".", "src"]
|
|
60
|
+
testpaths = ["tests"]
|
|
61
|
+
|
|
62
|
+
[tool.ruff]
|
|
63
|
+
extend-exclude = ["src/django_model_agent/migrations"]
|
|
64
|
+
|
|
65
|
+
[tool.ruff.lint]
|
|
66
|
+
# Declarative class attributes (`fields = [...]`, `tools = [...]`) are this
|
|
67
|
+
# library's API, so mutable class defaults are intended.
|
|
68
|
+
ignore = ["RUF012"]
|