banks 2.1.2__tar.gz → 2.2.0__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.
Files changed (76) hide show
  1. {banks-2.1.2 → banks-2.2.0}/.gitignore +1 -19
  2. banks-2.2.0/CLAUDE.md +126 -0
  3. banks-2.2.0/CODE_OF_CONDUCT.md +128 -0
  4. {banks-2.1.2 → banks-2.2.0}/PKG-INFO +1 -1
  5. {banks-2.1.2 → banks-2.2.0}/docs/examples.md +52 -0
  6. {banks-2.1.2 → banks-2.2.0}/pyproject.toml +8 -15
  7. {banks-2.1.2 → banks-2.2.0}/src/banks/__about__.py +1 -1
  8. {banks-2.1.2 → banks-2.2.0}/src/banks/env.py +2 -1
  9. {banks-2.1.2 → banks-2.2.0}/src/banks/filters/__init__.py +2 -1
  10. {banks-2.1.2 → banks-2.2.0}/src/banks/filters/image.py +11 -1
  11. banks-2.2.0/src/banks/filters/xml.py +62 -0
  12. {banks-2.1.2 → banks-2.2.0}/src/banks/registries/directory.py +6 -2
  13. {banks-2.1.2 → banks-2.2.0}/tests/test_image.py +17 -0
  14. banks-2.2.0/tests/test_xml.py +70 -0
  15. {banks-2.1.2 → banks-2.2.0}/.github/workflows/docs.yml +0 -0
  16. {banks-2.1.2 → banks-2.2.0}/.github/workflows/release.yml +0 -0
  17. {banks-2.1.2 → banks-2.2.0}/.github/workflows/test.yml +0 -0
  18. {banks-2.1.2 → banks-2.2.0}/CITATION.cff +0 -0
  19. {banks-2.1.2 → banks-2.2.0}/CONTRIBUTING.md +0 -0
  20. {banks-2.1.2 → banks-2.2.0}/LICENSE.txt +0 -0
  21. {banks-2.1.2 → banks-2.2.0}/MANIFEST.in +0 -0
  22. {banks-2.1.2 → banks-2.2.0}/README.md +0 -0
  23. {banks-2.1.2 → banks-2.2.0}/assets/banks.png +0 -0
  24. {banks-2.1.2 → banks-2.2.0}/cookbook/Prompt_Caching_with_Anthropic.ipynb +0 -0
  25. {banks-2.1.2 → banks-2.2.0}/cookbook/Prompt_Versioning.ipynb +0 -0
  26. {banks-2.1.2 → banks-2.2.0}/cookbook/in_prompt_completion.ipynb +0 -0
  27. {banks-2.1.2 → banks-2.2.0}/docs/config.md +0 -0
  28. {banks-2.1.2 → banks-2.2.0}/docs/index.md +0 -0
  29. {banks-2.1.2 → banks-2.2.0}/docs/prompt.md +0 -0
  30. {banks-2.1.2 → banks-2.2.0}/docs/python.md +0 -0
  31. {banks-2.1.2 → banks-2.2.0}/docs/registry.md +0 -0
  32. {banks-2.1.2 → banks-2.2.0}/mkdocs.yml +0 -0
  33. {banks-2.1.2 → banks-2.2.0}/src/banks/__init__.py +0 -0
  34. {banks-2.1.2 → banks-2.2.0}/src/banks/cache.py +0 -0
  35. {banks-2.1.2 → banks-2.2.0}/src/banks/config.py +0 -0
  36. {banks-2.1.2 → banks-2.2.0}/src/banks/errors.py +0 -0
  37. {banks-2.1.2 → banks-2.2.0}/src/banks/extensions/__init__.py +0 -0
  38. {banks-2.1.2 → banks-2.2.0}/src/banks/extensions/chat.py +0 -0
  39. {banks-2.1.2 → banks-2.2.0}/src/banks/extensions/completion.py +0 -0
  40. {banks-2.1.2 → banks-2.2.0}/src/banks/extensions/docs.py +0 -0
  41. {banks-2.1.2 → banks-2.2.0}/src/banks/filters/audio.py +0 -0
  42. {banks-2.1.2 → banks-2.2.0}/src/banks/filters/cache_control.py +0 -0
  43. {banks-2.1.2 → banks-2.2.0}/src/banks/filters/lemmatize.py +0 -0
  44. {banks-2.1.2 → banks-2.2.0}/src/banks/filters/tool.py +0 -0
  45. {banks-2.1.2 → banks-2.2.0}/src/banks/prompt.py +0 -0
  46. {banks-2.1.2 → banks-2.2.0}/src/banks/registries/__init__.py +0 -0
  47. {banks-2.1.2 → banks-2.2.0}/src/banks/registries/file.py +0 -0
  48. {banks-2.1.2 → banks-2.2.0}/src/banks/registries/redis.py +0 -0
  49. {banks-2.1.2 → banks-2.2.0}/src/banks/types.py +0 -0
  50. {banks-2.1.2 → banks-2.2.0}/src/banks/utils.py +0 -0
  51. {banks-2.1.2 → banks-2.2.0}/tests/__init__.py +0 -0
  52. {banks-2.1.2 → banks-2.2.0}/tests/conftest.py +0 -0
  53. {banks-2.1.2 → banks-2.2.0}/tests/data/1x1.png +0 -0
  54. {banks-2.1.2 → banks-2.2.0}/tests/data/empty.wav +0 -0
  55. {banks-2.1.2 → banks-2.2.0}/tests/e2e/__init__.py +0 -0
  56. {banks-2.1.2 → banks-2.2.0}/tests/e2e/conftest.py +0 -0
  57. {banks-2.1.2 → banks-2.2.0}/tests/e2e/test_completion.py +0 -0
  58. {banks-2.1.2 → banks-2.2.0}/tests/e2e/test_function_calling.py +0 -0
  59. {banks-2.1.2 → banks-2.2.0}/tests/templates/blog.jinja +0 -0
  60. {banks-2.1.2 → banks-2.2.0}/tests/templates/cache.jinja +0 -0
  61. {banks-2.1.2 → banks-2.2.0}/tests/templates/chat.jinja +0 -0
  62. {banks-2.1.2 → banks-2.2.0}/tests/templates/summarize.jinja +0 -0
  63. {banks-2.1.2 → banks-2.2.0}/tests/templates/summarize_lemma.jinja +0 -0
  64. {banks-2.1.2 → banks-2.2.0}/tests/test_audio.py +0 -0
  65. {banks-2.1.2 → banks-2.2.0}/tests/test_cache.py +0 -0
  66. {banks-2.1.2 → banks-2.2.0}/tests/test_cache_control.py +0 -0
  67. {banks-2.1.2 → banks-2.2.0}/tests/test_chat.py +0 -0
  68. {banks-2.1.2 → banks-2.2.0}/tests/test_completion.py +0 -0
  69. {banks-2.1.2 → banks-2.2.0}/tests/test_config.py +0 -0
  70. {banks-2.1.2 → banks-2.2.0}/tests/test_directory_registry.py +0 -0
  71. {banks-2.1.2 → banks-2.2.0}/tests/test_file_registry.py +0 -0
  72. {banks-2.1.2 → banks-2.2.0}/tests/test_prompt.py +0 -0
  73. {banks-2.1.2 → banks-2.2.0}/tests/test_redis_registry.py +0 -0
  74. {banks-2.1.2 → banks-2.2.0}/tests/test_tool.py +0 -0
  75. {banks-2.1.2 → banks-2.2.0}/tests/test_types.py +0 -0
  76. {banks-2.1.2 → banks-2.2.0}/tests/test_utils.py +0 -0
@@ -82,25 +82,6 @@ target/
82
82
  profile_default/
83
83
  ipython_config.py
84
84
 
85
- # pyenv
86
- # For a library or package, you might want to ignore these files since the code is
87
- # intended to run in multiple environments; otherwise, check them in:
88
- # .python-version
89
-
90
- # pipenv
91
- # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
- # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
- # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
- # install all needed dependencies.
95
- #Pipfile.lock
96
-
97
- # poetry
98
- # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
- # This is especially recommended for binary packages to ensure reproducibility, and is more
100
- # commonly ignored for libraries.
101
- # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
- #poetry.lock
103
-
104
85
  # pdm
105
86
  # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
87
  #pdm.lock
@@ -156,3 +137,4 @@ cython_debug/
156
137
  .idea/
157
138
  .vscode/
158
139
  .zed/
140
+ .claude/
banks-2.2.0/CLAUDE.md ADDED
@@ -0,0 +1,126 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ Banks is a Python prompt programming language and templating system for LLM applications. It provides a Jinja2-based template engine with specialized extensions and filters for creating dynamic prompts, managing chat messages, handling multimodal content (images/audio), and integrating with various LLM providers through LiteLLM.
8
+
9
+ ## Development Commands
10
+
11
+ ### Testing
12
+ - Run tests: `hatch run test`
13
+ - Run tests with coverage: `hatch run test-cov`
14
+ - Generate coverage report: `hatch run cov`
15
+ - Run e2e tests specifically: `hatch run test tests/e2e/`
16
+
17
+ ### Linting and Type Checking
18
+ - Format code: `hatch run lint:fmt`
19
+ - Check formatting: `hatch run lint:check`
20
+ - Run type checking: `hatch run lint:typing`
21
+ - Run pylint: `hatch run lint:lint`
22
+ - Run all lint checks: `hatch run lint:all`
23
+
24
+ ### Documentation
25
+ - Build docs: `hatch run docs build`
26
+ - Serve docs locally: `hatch run docs serve` (available at http://127.0.0.1:8000/)
27
+
28
+ ### Environment Management
29
+ - All commands use Hatch environments with automatic dependency management
30
+ - Use `uv` as the installer for faster dependency resolution
31
+ - Python 3.9+ supported across multiple versions (3.9-3.13)
32
+
33
+ ## Architecture Overview
34
+
35
+ ### Core Components
36
+
37
+ **Prompt Classes** (`src/banks/prompt.py`):
38
+ - `BasePrompt`: Base class with common functionality for template rendering, metadata, versioning, and caching
39
+ - `Prompt`: Synchronous prompt rendering with `text()` and `chat_messages()` methods
40
+ - `AsyncPrompt`: Asynchronous version for use within asyncio loops (requires `BANKS_ASYNC_ENABLED=true`)
41
+ - `PromptRegistry`: Protocol interface for prompt storage backends
42
+
43
+ **Type System** (`src/banks/types.py`):
44
+ - `ChatMessage`: Core chat message structure with role and content
45
+ - `ContentBlock`: Handles different content types (text, image_url, audio) with optional cache control
46
+ - `Tool`: Function calling support with automatic schema generation from Python callables
47
+ - `CacheControl`: Anthropic-style prompt caching metadata
48
+
49
+ **Template Environment** (`src/banks/env.py`):
50
+ - Global Jinja2 environment with Banks-specific extensions and filters
51
+ - Async support detection and configuration
52
+ - Custom template loader integration
53
+
54
+ ### Extensions System
55
+
56
+ **Chat Extension** (`src/banks/extensions/chat.py`):
57
+ - `{% chat role="..." %}...{% endchat %}` blocks for structured message creation
58
+ - Automatic conversion to `ChatMessage` objects during rendering
59
+
60
+ **Completion Extension** (`src/banks/extensions/completion.py`):
61
+ - `{% completion model="..." %}...{% endcompletion %}` for in-prompt LLM calls
62
+ - Integrated with LiteLLM for multi-provider support
63
+ - Function calling support within completion blocks
64
+
65
+ ### Filters System
66
+
67
+ **Core Filters** (`src/banks/filters/`):
68
+ - `image`: Convert file paths/URLs to base64-encoded image content blocks
69
+ - `audio`: Convert audio files to base64-encoded audio content blocks
70
+ - `cache_control`: Add Anthropic cache control metadata to content blocks
71
+ - `tool`: Convert Python callables to LLM function call schemas
72
+ - `lemmatize`: Text lemmatization using simplemma
73
+
74
+ ### Registry System
75
+
76
+ **Storage Backends** (`src/banks/registries/`):
77
+ - `DirectoryTemplateRegistry`: File system-based prompt storage
78
+ - `FileTemplateRegistry`: Single file-based storage
79
+ - `RedisTemplateRegistry`: Redis-backed storage for distributed scenarios
80
+ - All registries implement the `PromptRegistry` protocol
81
+
82
+ ### Configuration
83
+
84
+ **Config System** (`src/banks/config.py`):
85
+ - Environment variable-based configuration with `BANKS_` prefix
86
+ - `BANKS_ASYNC_ENABLED`: Enable async template rendering
87
+ - `BANKS_USER_DATA_PATH`: Custom user data directory
88
+
89
+ ## Key Development Patterns
90
+
91
+ ### Template Rendering Flow
92
+ 1. Templates parsed by Jinja2 environment with Banks extensions
93
+ 2. Chat blocks converted to JSON during rendering
94
+ 3. `chat_messages()` parses JSON back to `ChatMessage` objects
95
+ 4. Caching layer prevents re-rendering identical contexts
96
+
97
+ ### Multimodal Content Handling
98
+ - Images/audio converted to base64 during filter application
99
+ - Content blocks maintain type safety and metadata
100
+ - Cache control integrated at content block level
101
+
102
+ ### Function Calling Integration
103
+ - Python functions automatically converted to LLM schemas via introspection
104
+ - Docstring parsing for parameter descriptions
105
+ - Type annotations converted to JSON Schema
106
+
107
+ ### Async Support Architecture
108
+ - Global environment state requires async decision at import time
109
+ - `BANKS_ASYNC_ENABLED` must be set before importing banks modules
110
+ - `AsyncPrompt` provides `await`-able rendering methods
111
+
112
+ ## Testing Strategy
113
+
114
+ - Unit tests for individual components in `tests/`
115
+ - E2e tests requiring API keys in `tests/e2e/` (marked with `@pytest.mark.e2e`)
116
+ - Template examples in `tests/templates/` for integration testing
117
+ - Coverage excludes async-specific code paths and deprecated modules
118
+
119
+ ## Key Dependencies
120
+
121
+ - `jinja2`: Core templating engine
122
+ - `pydantic`: Type validation and serialization
123
+ - `litellm`: Multi-provider LLM integration (optional)
124
+ - `redis`: Redis registry backend (optional)
125
+ - `griffe`: Code introspection utilities
126
+ - `platformdirs`: Cross-platform data directory handling
@@ -0,0 +1,128 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, religion, or sexual identity
10
+ and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the
26
+ overall community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or
31
+ advances of any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email
35
+ address, without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ mpippi at gmail dot com.
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series
86
+ of actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or
93
+ permanent ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within
113
+ the community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.0, available at
119
+ https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120
+
121
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
+ enforcement ladder](https://github.com/mozilla/diversity).
123
+
124
+ [homepage]: https://www.contributor-covenant.org
125
+
126
+ For answers to common questions about this code of conduct, see the FAQ at
127
+ https://www.contributor-covenant.org/faq. Translations are available at
128
+ https://www.contributor-covenant.org/translations.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: banks
3
- Version: 2.1.2
3
+ Version: 2.2.0
4
4
  Summary: A prompt programming language
5
5
  Project-URL: Documentation, https://github.com/masci/banks#readme
6
6
  Project-URL: Issues, https://github.com/masci/banks/issues
@@ -3,6 +3,7 @@
3
3
  - [Create a blog writing prompt](#create-a-blog-writing-prompt)
4
4
  - [Create a summarizer prompt](#create-a-summarizer-prompt)
5
5
  - [Lemmatize text while processing a template](#lemmatize-text-while-processing-a-template)
6
+ - [Convert a JSON-like object into XML while processing the template](#convert-a-json-like-object-into-xml-while-processing-the-template)
6
7
  - [Use a LLM to generate a text while rendering a prompt](#use-a-llm-to-generate-a-text-while-rendering-a-prompt)
7
8
  - [Render a prompt template as chat messages](#render-a-prompt-template-as-chat-messages)
8
9
  - [Use prompt caching from Anthropic](#use-prompt-caching-from-anthropic)
@@ -135,6 +136,57 @@ the cat be run
135
136
  Summary:
136
137
  ```
137
138
 
139
+ ## Convert a JSON-like object into XML while processing the template
140
+
141
+ Banks has built-in support for filtering JSON-like objects (Pydantic `BaseModel` subclasses, dictionaries, deserializable strings) and returning an XML string.
142
+
143
+ Here is an example of how you can use it:
144
+
145
+ ```python
146
+ from banks import Prompt
147
+ from pydantic import BaseModel
148
+ from typing import Dict
149
+
150
+ prompt_template = """
151
+ Please extract the contact details from this user:
152
+
153
+ {{ data | to_xml }}
154
+
155
+ Contact details:
156
+ """
157
+
158
+ class User(BaseModel):
159
+ username: str
160
+ account_id: str
161
+ registered_at: str
162
+ email: str
163
+ phone_number: str
164
+ social_media_accounts: Dict[str, str]
165
+
166
+ user = User(username="example", account_id="0000", registered_at="10-25-2024", email="example@email.com", phone_number="0123456789", social_media_accounts={"BlueSky": "@example.com"})
167
+
168
+ p = Prompt(prompt_template)
169
+ print(p.text({"data": user}))
170
+ ```
171
+
172
+ This will output:
173
+
174
+ ```text
175
+ Please extract the contact details from this user:
176
+
177
+ <user>
178
+ <username>example</username>
179
+ <account_id>0000</account_id>
180
+ <registered_at>10-25-2024</registered_at>
181
+ <email>example@email.com</email>
182
+ <phone_number>0123456789</phone_number>
183
+ <social_media_accounts>{'BlueSky': '@example.com'}</social_media_accounts>
184
+ </user>
185
+
186
+
187
+ Contact details:
188
+ ```
189
+
138
190
  ## Use a LLM to generate a text while rendering a prompt
139
191
 
140
192
  Sometimes it might be useful to ask another LLM to generate examples for you in a
@@ -32,10 +32,7 @@ dependencies = [
32
32
  ]
33
33
 
34
34
  [project.optional-dependencies]
35
- all = [
36
- "litellm",
37
- "redis",
38
- ]
35
+ all = ["litellm", "redis"]
39
36
 
40
37
  [project.urls]
41
38
  Documentation = "https://github.com/masci/banks#readme"
@@ -71,7 +68,7 @@ docs = "mkdocs {args:build}"
71
68
  python = ["3.9", "3.10", "3.11", "3.12", "3.13"]
72
69
 
73
70
  [tool.hatch.envs.lint]
74
- detached = false # Normally the linting env can be detached, but mypy doesn't install all the stubs we need
71
+ detached = false # Normally the linting env can be detached, but mypy doesn't install all the stubs we need
75
72
  dependencies = ["mypy>=1.0.0", "ruff>=0.0.243", "pylint", "redis", "litellm"]
76
73
 
77
74
  [tool.hatch.envs.lint.scripts]
@@ -147,6 +144,8 @@ ignore = [
147
144
  "PLR2004",
148
145
  # __all__ sorted
149
146
  "RUF022",
147
+ # Conditional imports
148
+ "PLC0415",
150
149
  ]
151
150
 
152
151
  [tool.ruff.lint.isort]
@@ -180,11 +179,7 @@ exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]
180
179
 
181
180
 
182
181
  [[tool.mypy.overrides]]
183
- module = [
184
- "litellm.*",
185
- "simplemma.*",
186
- "deprecated.*"
187
- ]
182
+ module = ["litellm.*", "simplemma.*", "deprecated.*"]
188
183
  ignore_missing_imports = true
189
184
 
190
185
  [tool.pylint]
@@ -202,11 +197,9 @@ max-args = 10
202
197
 
203
198
  [tool.pytest.ini_options]
204
199
  asyncio_default_fixture_loop_scope = "function"
205
- markers = [
206
- "e2e",
207
- ]
208
- filterwarnings =[
200
+ markers = ["e2e"]
201
+ filterwarnings = [
209
202
  # Dilence litellm warning coming from their Pydantic config.
210
203
  # This assumes our use of Pydantic is correct :)
211
- "ignore:Support for class-based `config` is deprecated"
204
+ "ignore:Support for class-based `config` is deprecated",
212
205
  ]
@@ -1,4 +1,4 @@
1
1
  # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <mpippi@gmail.com>
2
2
  #
3
3
  # SPDX-License-Identifier: MIT
4
- __version__ = "2.1.2"
4
+ __version__ = "2.2.0"
@@ -4,7 +4,7 @@
4
4
  from jinja2 import Environment, select_autoescape
5
5
 
6
6
  from .config import config
7
- from .filters import audio, cache_control, image, lemmatize, tool
7
+ from .filters import audio, cache_control, image, lemmatize, tool, xml
8
8
 
9
9
 
10
10
  def _add_extensions(_env):
@@ -38,5 +38,6 @@ env.filters["image"] = image
38
38
  env.filters["lemmatize"] = lemmatize
39
39
  env.filters["tool"] = tool
40
40
  env.filters["audio"] = audio
41
+ env.filters["to_xml"] = xml
41
42
 
42
43
  _add_extensions(env)
@@ -6,5 +6,6 @@ from .cache_control import cache_control
6
6
  from .image import image
7
7
  from .lemmatize import lemmatize
8
8
  from .tool import tool
9
+ from .xml import xml
9
10
 
10
- __all__ = ("cache_control", "image", "lemmatize", "tool", "audio")
11
+ __all__ = ("cache_control", "image", "lemmatize", "tool", "audio", "xml")
@@ -1,15 +1,25 @@
1
1
  # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <mpippi@gmail.com>
2
2
  #
3
3
  # SPDX-License-Identifier: MIT
4
+ import re
4
5
  from pathlib import Path
5
6
  from urllib.parse import urlparse
6
7
 
7
8
  from banks.types import ContentBlock, ImageUrl
8
9
 
10
+ BASE64_PATH_REGEX = re.compile(r"image\/.*;base64,.*")
11
+
9
12
 
10
13
  def _is_url(string: str) -> bool:
11
14
  result = urlparse(string)
12
- return all([result.scheme, result.netloc])
15
+ if not result.scheme:
16
+ return False
17
+
18
+ if not result.netloc:
19
+ # The only valid format when netloc is empty is base64 data urls
20
+ return all([result.scheme == "data", BASE64_PATH_REGEX.match(result.path)])
21
+
22
+ return True
13
23
 
14
24
 
15
25
  def image(value: str) -> str:
@@ -0,0 +1,62 @@
1
+ import json
2
+ import xml.etree.ElementTree as ET
3
+ from typing import Any, Optional, Union
4
+ from xml.dom.minidom import parseString
5
+
6
+ from pydantic import BaseModel
7
+
8
+
9
+ def _deserialize(string: str) -> Optional[dict]:
10
+ try:
11
+ return json.loads(string)
12
+ except json.JSONDecodeError:
13
+ return None
14
+
15
+
16
+ def _prepare_dictionary(value: Union[str, BaseModel, dict[str, Any]]):
17
+ root_tag = "input"
18
+ if isinstance(value, str):
19
+ model: Optional[dict[str, Any]] = _deserialize(value)
20
+ if model is None:
21
+ msg = f"{value} is not deserializable"
22
+ raise ValueError(msg)
23
+ elif isinstance(value, BaseModel):
24
+ model = value.model_dump()
25
+ root_tag = value.__class__.__name__.lower()
26
+ elif isinstance(value, dict):
27
+ model = value.copy()
28
+ for k in value.keys():
29
+ if not isinstance(k, str):
30
+ key = str(k)
31
+ if isinstance(k, (int, float)):
32
+ key = "_" + key
33
+ v = model.pop(k)
34
+ model[key.lower()] = v
35
+ else:
36
+ msg = f"Input can only be of type BaseModel, dictionary or deserializable string. Got {type(value)}"
37
+ raise ValueError(msg)
38
+ return model, root_tag
39
+
40
+
41
+ def xml(value: Union[str, BaseModel, dict[str, Any]]) -> str:
42
+ """
43
+ Convert a Pydantic model, a deserializable string or a dictionary into an XML string.
44
+
45
+ Example:
46
+ ```jinja
47
+ {{'{"username": "user", "email": "example@email.com"}' | to_xml}}
48
+ "
49
+ <input>
50
+ <username>user</username>
51
+ <email>example@email.com</email>
52
+ </input>
53
+ "
54
+ ```
55
+ """
56
+ model, root_tag = _prepare_dictionary(value)
57
+ xml_model = ET.Element(root_tag)
58
+ for k, v in model.items():
59
+ sub = ET.SubElement(xml_model, k)
60
+ sub.text = str(v)
61
+ xml_str = ET.tostring(xml_model, encoding="unicode")
62
+ return parseString(xml_str).toprettyxml().replace('<?xml version="1.0" ?>\n', "") # noqa: S318
@@ -27,7 +27,7 @@ DEFAULT_INDEX_NAME = "index.json"
27
27
  class PromptFile(PromptModel):
28
28
  """Model representing a prompt file stored on disk."""
29
29
 
30
- path: Path = Field(exclude=True)
30
+ path: Path | None = Field(default=None, exclude=True)
31
31
 
32
32
  @classmethod
33
33
  def from_prompt_path(cls: type[Self], prompt: Prompt, path: Path) -> Self:
@@ -101,7 +101,7 @@ class DirectoryPromptRegistry:
101
101
  """
102
102
  version = version or DEFAULT_VERSION
103
103
  for pf in self._index.files:
104
- if pf.name == name and pf.version == version and pf.path.exists():
104
+ if pf.name == name and pf.version == version and pf.path and pf.path.exists():
105
105
  return Prompt(**pf.model_dump())
106
106
  raise PromptNotFoundError
107
107
 
@@ -135,6 +135,10 @@ class DirectoryPromptRegistry:
135
135
  def _load(self):
136
136
  """Load the prompt index from disk."""
137
137
  self._index = PromptFileIndex.model_validate_json(self._index_path.read_text())
138
+ # Reconstruct the file paths since they're excluded from serialization
139
+ for pf in self._index.files:
140
+ if pf.path is None:
141
+ pf.path = self._path / f"{pf.name}.{pf.version}.jinja"
138
142
 
139
143
  def _save(self):
140
144
  """Save the prompt index to disk."""
@@ -57,6 +57,23 @@ def test_image_with_file_path(tmp_path):
57
57
  assert content_block["image_url"]["url"].startswith("data:image/jpeg;base64,")
58
58
 
59
59
 
60
+ def test_image_base64(tmp_path):
61
+ """Test image filter with a binary input"""
62
+ test_image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABMgA"
63
+ result = image(test_image)
64
+
65
+ # Verify the content block wrapper
66
+ assert result.startswith("<content_block>")
67
+ assert result.endswith("</content_block>")
68
+
69
+ # Parse the JSON content
70
+ json_content = result[15:-16] # Remove wrapper tags
71
+ content_block = json.loads(json_content)
72
+
73
+ assert content_block["type"] == "image_url"
74
+ assert content_block["image_url"]["url"].startswith("data:image/png;base64,")
75
+
76
+
60
77
  def test_image_with_nonexistent_file():
61
78
  """Test image filter with a nonexistent file path"""
62
79
  with pytest.raises(FileNotFoundError):
@@ -0,0 +1,70 @@
1
+ import xml.etree.ElementTree as ET
2
+ from typing import Any
3
+ from xml.dom.minidom import parseString
4
+
5
+ import pytest
6
+ from pydantic import BaseModel
7
+
8
+ from banks.filters.xml import xml
9
+
10
+
11
+ @pytest.fixture
12
+ def xml_string_from_basemodel() -> str:
13
+ model = ET.Element("person")
14
+ age = ET.SubElement(model, "age")
15
+ age.text = "30"
16
+ name = ET.SubElement(model, "name")
17
+ name.text = "John Doe"
18
+ xml_str = ET.tostring(model, encoding="unicode")
19
+ return parseString(xml_str).toprettyxml().replace('<?xml version="1.0" ?>\n', "") # noqa: S318
20
+
21
+
22
+ @pytest.fixture
23
+ def xml_string_from_other() -> str:
24
+ model = ET.Element("input")
25
+ age = ET.SubElement(model, "age")
26
+ age.text = "30"
27
+ name = ET.SubElement(model, "name")
28
+ name.text = "John Doe"
29
+ xml_str = ET.tostring(model, encoding="unicode")
30
+ return parseString(xml_str).toprettyxml().replace('<?xml version="1.0" ?>\n', "") # noqa: S318
31
+
32
+
33
+ @pytest.fixture
34
+ def xml_string_from_edge_case() -> str:
35
+ model = ET.Element("input")
36
+ hello = ET.SubElement(model, "_1")
37
+ hello.text = "hello"
38
+ world = ET.SubElement(model, "_2.4")
39
+ world.text = "world"
40
+ xml_str = ET.tostring(model, encoding="unicode")
41
+ return parseString(xml_str).toprettyxml().replace('<?xml version="1.0" ?>\n', "") # noqa: S318
42
+
43
+
44
+ @pytest.fixture
45
+ def starting_value() -> tuple[BaseModel, dict[str, Any], str]:
46
+ class Person(BaseModel):
47
+ age: int
48
+ name: str
49
+
50
+ p = Person(age=30, name="John Doe")
51
+ return p, p.model_dump(), p.model_dump_json()
52
+
53
+
54
+ @pytest.fixture
55
+ def dict_edge_case() -> tuple[dict, str]:
56
+ return {1: "hello", 2.4: "world"}
57
+
58
+
59
+ def test_xml_filter(
60
+ xml_string_from_basemodel: str,
61
+ xml_string_from_other: str,
62
+ starting_value: tuple[BaseModel, dict[str, Any], str],
63
+ dict_edge_case: dict,
64
+ xml_string_from_edge_case: str,
65
+ ) -> None:
66
+ model, dictionary, string = starting_value
67
+ assert xml(model) == xml_string_from_basemodel
68
+ assert xml(dictionary) == xml_string_from_other
69
+ assert xml(string) == xml_string_from_other
70
+ assert xml(dict_edge_case) == xml_string_from_edge_case
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes