banks 2.1.1__tar.gz → 2.1.3__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 (75) hide show
  1. {banks-2.1.1 → banks-2.1.3}/.gitignore +1 -19
  2. banks-2.1.3/CLAUDE.md +126 -0
  3. banks-2.1.3/CODE_OF_CONDUCT.md +128 -0
  4. {banks-2.1.1 → banks-2.1.3}/PKG-INFO +9 -3
  5. {banks-2.1.1 → banks-2.1.3}/README.md +8 -2
  6. {banks-2.1.1 → banks-2.1.3}/pyproject.toml +8 -15
  7. {banks-2.1.1 → banks-2.1.3}/src/banks/__about__.py +1 -1
  8. {banks-2.1.1 → banks-2.1.3}/src/banks/extensions/chat.py +2 -4
  9. {banks-2.1.1 → banks-2.1.3}/src/banks/registries/directory.py +6 -2
  10. {banks-2.1.1 → banks-2.1.3}/src/banks/types.py +1 -1
  11. {banks-2.1.1 → banks-2.1.3}/tests/conftest.py +8 -0
  12. banks-2.1.3/tests/test_chat.py +82 -0
  13. banks-2.1.1/tests/test_chat.py +0 -29
  14. {banks-2.1.1 → banks-2.1.3}/.github/workflows/docs.yml +0 -0
  15. {banks-2.1.1 → banks-2.1.3}/.github/workflows/release.yml +0 -0
  16. {banks-2.1.1 → banks-2.1.3}/.github/workflows/test.yml +0 -0
  17. {banks-2.1.1 → banks-2.1.3}/CITATION.cff +0 -0
  18. {banks-2.1.1 → banks-2.1.3}/CONTRIBUTING.md +0 -0
  19. {banks-2.1.1 → banks-2.1.3}/LICENSE.txt +0 -0
  20. {banks-2.1.1 → banks-2.1.3}/MANIFEST.in +0 -0
  21. {banks-2.1.1 → banks-2.1.3}/assets/banks.png +0 -0
  22. {banks-2.1.1 → banks-2.1.3}/cookbook/Prompt_Caching_with_Anthropic.ipynb +0 -0
  23. {banks-2.1.1 → banks-2.1.3}/cookbook/Prompt_Versioning.ipynb +0 -0
  24. {banks-2.1.1 → banks-2.1.3}/cookbook/in_prompt_completion.ipynb +0 -0
  25. {banks-2.1.1 → banks-2.1.3}/docs/config.md +0 -0
  26. {banks-2.1.1 → banks-2.1.3}/docs/examples.md +0 -0
  27. {banks-2.1.1 → banks-2.1.3}/docs/index.md +0 -0
  28. {banks-2.1.1 → banks-2.1.3}/docs/prompt.md +0 -0
  29. {banks-2.1.1 → banks-2.1.3}/docs/python.md +0 -0
  30. {banks-2.1.1 → banks-2.1.3}/docs/registry.md +0 -0
  31. {banks-2.1.1 → banks-2.1.3}/mkdocs.yml +0 -0
  32. {banks-2.1.1 → banks-2.1.3}/src/banks/__init__.py +0 -0
  33. {banks-2.1.1 → banks-2.1.3}/src/banks/cache.py +0 -0
  34. {banks-2.1.1 → banks-2.1.3}/src/banks/config.py +0 -0
  35. {banks-2.1.1 → banks-2.1.3}/src/banks/env.py +0 -0
  36. {banks-2.1.1 → banks-2.1.3}/src/banks/errors.py +0 -0
  37. {banks-2.1.1 → banks-2.1.3}/src/banks/extensions/__init__.py +0 -0
  38. {banks-2.1.1 → banks-2.1.3}/src/banks/extensions/completion.py +0 -0
  39. {banks-2.1.1 → banks-2.1.3}/src/banks/extensions/docs.py +0 -0
  40. {banks-2.1.1 → banks-2.1.3}/src/banks/filters/__init__.py +0 -0
  41. {banks-2.1.1 → banks-2.1.3}/src/banks/filters/audio.py +0 -0
  42. {banks-2.1.1 → banks-2.1.3}/src/banks/filters/cache_control.py +0 -0
  43. {banks-2.1.1 → banks-2.1.3}/src/banks/filters/image.py +0 -0
  44. {banks-2.1.1 → banks-2.1.3}/src/banks/filters/lemmatize.py +0 -0
  45. {banks-2.1.1 → banks-2.1.3}/src/banks/filters/tool.py +0 -0
  46. {banks-2.1.1 → banks-2.1.3}/src/banks/prompt.py +0 -0
  47. {banks-2.1.1 → banks-2.1.3}/src/banks/registries/__init__.py +0 -0
  48. {banks-2.1.1 → banks-2.1.3}/src/banks/registries/file.py +0 -0
  49. {banks-2.1.1 → banks-2.1.3}/src/banks/registries/redis.py +0 -0
  50. {banks-2.1.1 → banks-2.1.3}/src/banks/utils.py +0 -0
  51. {banks-2.1.1 → banks-2.1.3}/tests/__init__.py +0 -0
  52. {banks-2.1.1 → banks-2.1.3}/tests/data/1x1.png +0 -0
  53. {banks-2.1.1 → banks-2.1.3}/tests/data/empty.wav +0 -0
  54. {banks-2.1.1 → banks-2.1.3}/tests/e2e/__init__.py +0 -0
  55. {banks-2.1.1 → banks-2.1.3}/tests/e2e/conftest.py +0 -0
  56. {banks-2.1.1 → banks-2.1.3}/tests/e2e/test_completion.py +0 -0
  57. {banks-2.1.1 → banks-2.1.3}/tests/e2e/test_function_calling.py +0 -0
  58. {banks-2.1.1 → banks-2.1.3}/tests/templates/blog.jinja +0 -0
  59. {banks-2.1.1 → banks-2.1.3}/tests/templates/cache.jinja +0 -0
  60. {banks-2.1.1 → banks-2.1.3}/tests/templates/chat.jinja +0 -0
  61. {banks-2.1.1 → banks-2.1.3}/tests/templates/summarize.jinja +0 -0
  62. {banks-2.1.1 → banks-2.1.3}/tests/templates/summarize_lemma.jinja +0 -0
  63. {banks-2.1.1 → banks-2.1.3}/tests/test_audio.py +0 -0
  64. {banks-2.1.1 → banks-2.1.3}/tests/test_cache.py +0 -0
  65. {banks-2.1.1 → banks-2.1.3}/tests/test_cache_control.py +0 -0
  66. {banks-2.1.1 → banks-2.1.3}/tests/test_completion.py +0 -0
  67. {banks-2.1.1 → banks-2.1.3}/tests/test_config.py +0 -0
  68. {banks-2.1.1 → banks-2.1.3}/tests/test_directory_registry.py +0 -0
  69. {banks-2.1.1 → banks-2.1.3}/tests/test_file_registry.py +0 -0
  70. {banks-2.1.1 → banks-2.1.3}/tests/test_image.py +0 -0
  71. {banks-2.1.1 → banks-2.1.3}/tests/test_prompt.py +0 -0
  72. {banks-2.1.1 → banks-2.1.3}/tests/test_redis_registry.py +0 -0
  73. {banks-2.1.1 → banks-2.1.3}/tests/test_tool.py +0 -0
  74. {banks-2.1.1 → banks-2.1.3}/tests/test_types.py +0 -0
  75. {banks-2.1.1 → banks-2.1.3}/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.1.3/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.1
3
+ Version: 2.1.3
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
@@ -123,8 +123,14 @@ print(p.chat_messages({"persona": "helpful assistant"}))
123
123
 
124
124
  # Output:
125
125
  # [
126
- # ChatMessage(role='system', content='You are a helpful assistant.\n'),
127
- # ChatMessage(role='user', content='Hello, how are you?\n')
126
+ # ChatMessage(role='system', content=[
127
+ # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='You are a helpful assistant.',
128
+ # image_url=None, input_audio=None)
129
+ # ], tool_call_id=None, name=None),
130
+ # ChatMessage(role='user', content=[
131
+ # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='Hello, how are you?',
132
+ # image_url=None, input_audio=None)
133
+ # ], tool_call_id=None, name=None)
128
134
  # ]
129
135
  ```
130
136
 
@@ -92,8 +92,14 @@ print(p.chat_messages({"persona": "helpful assistant"}))
92
92
 
93
93
  # Output:
94
94
  # [
95
- # ChatMessage(role='system', content='You are a helpful assistant.\n'),
96
- # ChatMessage(role='user', content='Hello, how are you?\n')
95
+ # ChatMessage(role='system', content=[
96
+ # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='You are a helpful assistant.',
97
+ # image_url=None, input_audio=None)
98
+ # ], tool_call_id=None, name=None),
99
+ # ChatMessage(role='user', content=[
100
+ # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='Hello, how are you?',
101
+ # image_url=None, input_audio=None)
102
+ # ], tool_call_id=None, name=None)
97
103
  # ]
98
104
  ```
99
105
 
@@ -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.1"
4
+ __version__ = "2.1.3"
@@ -1,7 +1,6 @@
1
1
  # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <mpippi@gmail.com>
2
2
  #
3
3
  # SPDX-License-Identifier: MIT
4
- import re
5
4
 
6
5
  from jinja2 import TemplateSyntaxError, nodes
7
6
  from jinja2.ext import Extension
@@ -9,7 +8,6 @@ from jinja2.ext import Extension
9
8
  from banks.types import chat_message_from_text
10
9
 
11
10
  SUPPORTED_TYPES = ("system", "user", "assistant")
12
- CONTENT_BLOCK_REGEX = re.compile(r"<content_block>((?s:.)*)<\/content_block>")
13
11
 
14
12
 
15
13
  class ChatExtension(Extension):
@@ -52,8 +50,8 @@ class ChatExtension(Extension):
52
50
  raise TemplateSyntaxError(error_msg, lineno)
53
51
 
54
52
  if attr_value.value not in SUPPORTED_TYPES:
55
- types = ",".join(SUPPORTED_TYPES)
56
- msg = f"Unknown role type '{attr_value}', use one of ({types})"
53
+ types = ", ".join(SUPPORTED_TYPES)
54
+ msg = f"Unknown role type '{attr_value.value}', use one of ({types})"
57
55
  raise TemplateSyntaxError(msg, lineno)
58
56
 
59
57
  # Pass the role name to the CallBlock node
@@ -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."""
@@ -16,7 +16,7 @@ from typing_extensions import Self
16
16
  from .utils import parse_params_from_docstring, python_type_to_jsonschema
17
17
 
18
18
  # pylint: disable=invalid-name
19
- CONTENT_BLOCK_REGEX = re.compile(r"(<content_block>\{.*?\}<\/content_block>)|([^<](?:(?!<content_block>).)*)")
19
+ CONTENT_BLOCK_REGEX = re.compile(r"(<content_block>\{.*?\}<\/content_block>)|([^<](?:(?!<content_block>)[\s\S])*)")
20
20
 
21
21
 
22
22
  class ContentBlockType(str, Enum):
@@ -1,3 +1,5 @@
1
+ from pathlib import Path
2
+
1
3
  import pytest
2
4
  import redis
3
5
 
@@ -19,3 +21,9 @@ def skip_by_redis(request):
19
21
  if request.node.get_closest_marker("redis"):
20
22
  if not is_redis_available():
21
23
  pytest.skip("Redis is not available")
24
+
25
+
26
+ @pytest.fixture
27
+ def data_path() -> Path:
28
+ here = Path(__file__).parent
29
+ return here / "data"
@@ -0,0 +1,82 @@
1
+ from typing import cast
2
+
3
+ import pytest
4
+ from jinja2 import TemplateSyntaxError
5
+
6
+ from banks import Prompt
7
+ from banks.types import ContentBlock
8
+
9
+
10
+ def test_wrong_tag():
11
+ with pytest.raises(TemplateSyntaxError):
12
+ Prompt("{% chat %}{% endchat %}")
13
+
14
+
15
+ def test_wrong_tag_params():
16
+ with pytest.raises(TemplateSyntaxError):
17
+ Prompt('{% chat foo="bar" %}{% endchat %}')
18
+
19
+
20
+ def test_wrong_role_type():
21
+ with pytest.raises(TemplateSyntaxError):
22
+ Prompt('{% chat role="does not exist" %}{% endchat %}')
23
+
24
+
25
+ def test_blocks():
26
+ p = Prompt('{% chat role="user" %}Tell me what you see: {{ picture | image }}{% endchat %}')
27
+ messages = p.chat_messages({"picture": "http://foo.bar"})
28
+ assert len(messages) == 1
29
+ content = messages[0].content
30
+ assert len(content) == 2
31
+ assert content[0].type == "text" # type: ignore
32
+ assert content[1].type == "image_url" # type: ignore
33
+
34
+
35
+ def test_blocks_newlines():
36
+ p = Prompt("""{% chat role="system" %}
37
+ Some instructions:
38
+
39
+ 1. Do this.
40
+ 2. Then this.
41
+ 3. Finally, that.
42
+ {% endchat %}""")
43
+ messages = p.chat_messages()
44
+ assert len(messages) == 1
45
+ assert len(messages[0].content) == 1
46
+
47
+
48
+ def test_blocks_complex(data_path):
49
+ p = Prompt("""
50
+ {% chat role="system" %}
51
+ Given a list of images and text from each image, please answer the question to the best of your ability.
52
+ {% endchat %}
53
+
54
+ {% chat role="user" %}
55
+ {% for image_path, text in images_and_texts %}
56
+ Here is some text: {{ text }}
57
+ And here is an image:
58
+ {{ image_path | image }}
59
+ {% endfor %}
60
+ {% endchat %}
61
+ """)
62
+
63
+ messages = p.chat_messages(
64
+ {
65
+ "images_and_texts": [
66
+ (str(data_path / "1x1.png"), "This is the first page of the document"),
67
+ (str(data_path / "1x1.png"), "This is the second page of the document"),
68
+ ]
69
+ }
70
+ )
71
+ assert len(messages) == 2
72
+
73
+ blocks = cast(list[ContentBlock], messages[0].content)
74
+ assert len(blocks) == 1
75
+ assert blocks[0].type == "text"
76
+
77
+ blocks = cast(list[ContentBlock], messages[1].content)
78
+ assert len(blocks) == 4
79
+ assert blocks[0].type == "text"
80
+ assert blocks[1].type == "image_url"
81
+ assert blocks[2].type == "text"
82
+ assert blocks[3].type == "image_url"
@@ -1,29 +0,0 @@
1
- import pytest
2
- from jinja2 import TemplateSyntaxError
3
-
4
- from banks import Prompt
5
-
6
-
7
- def test_wrong_tag():
8
- with pytest.raises(TemplateSyntaxError):
9
- Prompt("{% chat %}{% endchat %}")
10
-
11
-
12
- def test_wrong_tag_params():
13
- with pytest.raises(TemplateSyntaxError):
14
- Prompt('{% chat foo="bar" %}{% endchat %}')
15
-
16
-
17
- def test_wrong_role_type():
18
- with pytest.raises(TemplateSyntaxError):
19
- Prompt('{% chat role="does not exist" %}{% endchat %}')
20
-
21
-
22
- def test_blocks():
23
- p = Prompt('{% chat role="user" %}Tell me what you see: {{ picture | image }}{% endchat %}')
24
- messages = p.chat_messages({"picture": "http://foo.bar"})
25
- assert len(messages) == 1
26
- content = messages[0].content
27
- assert len(content) == 2
28
- assert content[0].type == "text" # type: ignore
29
- assert content[1].type == "image_url" # type: ignore
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