banks 2.2.0__tar.gz → 2.3.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 (83) hide show
  1. {banks-2.2.0 → banks-2.3.0}/.github/workflows/test.yml +3 -3
  2. {banks-2.2.0 → banks-2.3.0}/CLAUDE.md +6 -4
  3. {banks-2.2.0 → banks-2.3.0}/PKG-INFO +4 -3
  4. {banks-2.2.0 → banks-2.3.0}/README.md +2 -2
  5. {banks-2.2.0 → banks-2.3.0}/pyproject.toml +3 -2
  6. {banks-2.2.0 → banks-2.3.0}/src/banks/__about__.py +1 -1
  7. {banks-2.2.0 → banks-2.3.0}/src/banks/config.py +4 -1
  8. {banks-2.2.0 → banks-2.3.0}/src/banks/env.py +3 -1
  9. {banks-2.2.0 → banks-2.3.0}/src/banks/errors.py +1 -1
  10. {banks-2.2.0 → banks-2.3.0}/src/banks/extensions/completion.py +16 -2
  11. {banks-2.2.0 → banks-2.3.0}/src/banks/filters/__init__.py +3 -1
  12. banks-2.3.0/src/banks/filters/audio.py +60 -0
  13. banks-2.3.0/src/banks/filters/document.py +96 -0
  14. banks-2.3.0/src/banks/filters/video.py +62 -0
  15. {banks-2.2.0 → banks-2.3.0}/src/banks/prompt.py +11 -2
  16. {banks-2.2.0 → banks-2.3.0}/src/banks/types.py +69 -0
  17. banks-2.3.0/tests/data/1x1.pdf +0 -0
  18. banks-2.3.0/tests/data/empty.mov +0 -0
  19. {banks-2.2.0 → banks-2.3.0}/tests/test_audio.py +29 -1
  20. {banks-2.2.0 → banks-2.3.0}/tests/test_cache_control.py +3 -2
  21. {banks-2.2.0 → banks-2.3.0}/tests/test_directory_registry.py +1 -1
  22. banks-2.3.0/tests/test_document.py +74 -0
  23. {banks-2.2.0 → banks-2.3.0}/tests/test_redis_registry.py +1 -1
  24. banks-2.3.0/tests/test_video.py +73 -0
  25. banks-2.2.0/src/banks/filters/audio.py +0 -23
  26. {banks-2.2.0 → banks-2.3.0}/.github/workflows/docs.yml +0 -0
  27. {banks-2.2.0 → banks-2.3.0}/.github/workflows/release.yml +0 -0
  28. {banks-2.2.0 → banks-2.3.0}/.gitignore +0 -0
  29. {banks-2.2.0 → banks-2.3.0}/CITATION.cff +0 -0
  30. {banks-2.2.0 → banks-2.3.0}/CODE_OF_CONDUCT.md +0 -0
  31. {banks-2.2.0 → banks-2.3.0}/CONTRIBUTING.md +0 -0
  32. {banks-2.2.0 → banks-2.3.0}/LICENSE.txt +0 -0
  33. {banks-2.2.0 → banks-2.3.0}/MANIFEST.in +0 -0
  34. {banks-2.2.0 → banks-2.3.0}/assets/banks.png +0 -0
  35. {banks-2.2.0 → banks-2.3.0}/cookbook/Prompt_Caching_with_Anthropic.ipynb +0 -0
  36. {banks-2.2.0 → banks-2.3.0}/cookbook/Prompt_Versioning.ipynb +0 -0
  37. {banks-2.2.0 → banks-2.3.0}/cookbook/in_prompt_completion.ipynb +0 -0
  38. {banks-2.2.0 → banks-2.3.0}/docs/config.md +0 -0
  39. {banks-2.2.0 → banks-2.3.0}/docs/examples.md +0 -0
  40. {banks-2.2.0 → banks-2.3.0}/docs/index.md +0 -0
  41. {banks-2.2.0 → banks-2.3.0}/docs/prompt.md +0 -0
  42. {banks-2.2.0 → banks-2.3.0}/docs/python.md +0 -0
  43. {banks-2.2.0 → banks-2.3.0}/docs/registry.md +0 -0
  44. {banks-2.2.0 → banks-2.3.0}/mkdocs.yml +0 -0
  45. {banks-2.2.0 → banks-2.3.0}/src/banks/__init__.py +0 -0
  46. {banks-2.2.0 → banks-2.3.0}/src/banks/cache.py +0 -0
  47. {banks-2.2.0 → banks-2.3.0}/src/banks/extensions/__init__.py +0 -0
  48. {banks-2.2.0 → banks-2.3.0}/src/banks/extensions/chat.py +0 -0
  49. {banks-2.2.0 → banks-2.3.0}/src/banks/extensions/docs.py +0 -0
  50. {banks-2.2.0 → banks-2.3.0}/src/banks/filters/cache_control.py +0 -0
  51. {banks-2.2.0 → banks-2.3.0}/src/banks/filters/image.py +0 -0
  52. {banks-2.2.0 → banks-2.3.0}/src/banks/filters/lemmatize.py +0 -0
  53. {banks-2.2.0 → banks-2.3.0}/src/banks/filters/tool.py +0 -0
  54. {banks-2.2.0 → banks-2.3.0}/src/banks/filters/xml.py +0 -0
  55. {banks-2.2.0 → banks-2.3.0}/src/banks/registries/__init__.py +0 -0
  56. {banks-2.2.0 → banks-2.3.0}/src/banks/registries/directory.py +0 -0
  57. {banks-2.2.0 → banks-2.3.0}/src/banks/registries/file.py +0 -0
  58. {banks-2.2.0 → banks-2.3.0}/src/banks/registries/redis.py +0 -0
  59. {banks-2.2.0 → banks-2.3.0}/src/banks/utils.py +0 -0
  60. {banks-2.2.0 → banks-2.3.0}/tests/__init__.py +0 -0
  61. {banks-2.2.0 → banks-2.3.0}/tests/conftest.py +0 -0
  62. {banks-2.2.0 → banks-2.3.0}/tests/data/1x1.png +0 -0
  63. {banks-2.2.0 → banks-2.3.0}/tests/data/empty.wav +0 -0
  64. {banks-2.2.0 → banks-2.3.0}/tests/e2e/__init__.py +0 -0
  65. {banks-2.2.0 → banks-2.3.0}/tests/e2e/conftest.py +0 -0
  66. {banks-2.2.0 → banks-2.3.0}/tests/e2e/test_completion.py +0 -0
  67. {banks-2.2.0 → banks-2.3.0}/tests/e2e/test_function_calling.py +0 -0
  68. {banks-2.2.0 → banks-2.3.0}/tests/templates/blog.jinja +0 -0
  69. {banks-2.2.0 → banks-2.3.0}/tests/templates/cache.jinja +0 -0
  70. {banks-2.2.0 → banks-2.3.0}/tests/templates/chat.jinja +0 -0
  71. {banks-2.2.0 → banks-2.3.0}/tests/templates/summarize.jinja +0 -0
  72. {banks-2.2.0 → banks-2.3.0}/tests/templates/summarize_lemma.jinja +0 -0
  73. {banks-2.2.0 → banks-2.3.0}/tests/test_cache.py +0 -0
  74. {banks-2.2.0 → banks-2.3.0}/tests/test_chat.py +0 -0
  75. {banks-2.2.0 → banks-2.3.0}/tests/test_completion.py +0 -0
  76. {banks-2.2.0 → banks-2.3.0}/tests/test_config.py +0 -0
  77. {banks-2.2.0 → banks-2.3.0}/tests/test_file_registry.py +0 -0
  78. {banks-2.2.0 → banks-2.3.0}/tests/test_image.py +0 -0
  79. {banks-2.2.0 → banks-2.3.0}/tests/test_prompt.py +0 -0
  80. {banks-2.2.0 → banks-2.3.0}/tests/test_tool.py +0 -0
  81. {banks-2.2.0 → banks-2.3.0}/tests/test_types.py +0 -0
  82. {banks-2.2.0 → banks-2.3.0}/tests/test_utils.py +0 -0
  83. {banks-2.2.0 → banks-2.3.0}/tests/test_xml.py +0 -0
@@ -34,7 +34,7 @@ jobs:
34
34
  strategy:
35
35
  fail-fast: false
36
36
  matrix:
37
- python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
37
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
38
38
 
39
39
  steps:
40
40
  - uses: actions/checkout@v4
@@ -72,7 +72,7 @@ jobs:
72
72
  strategy:
73
73
  fail-fast: false
74
74
  matrix:
75
- python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
75
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
76
76
 
77
77
  steps:
78
78
  - uses: actions/checkout@v4
@@ -95,7 +95,7 @@ jobs:
95
95
  strategy:
96
96
  fail-fast: false
97
97
  matrix:
98
- python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
98
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
99
99
 
100
100
  steps:
101
101
  - uses: actions/checkout@v4
@@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
4
4
 
5
5
  ## Project Overview
6
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.
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/video/documents), and integrating with various LLM providers through LiteLLM.
8
8
 
9
9
  ## Development Commands
10
10
 
@@ -28,7 +28,7 @@ Banks is a Python prompt programming language and templating system for LLM appl
28
28
  ### Environment Management
29
29
  - All commands use Hatch environments with automatic dependency management
30
30
  - Use `uv` as the installer for faster dependency resolution
31
- - Python 3.9+ supported across multiple versions (3.9-3.13)
31
+ - Python 3.10+ supported across multiple versions (3.10-3.14)
32
32
 
33
33
  ## Architecture Overview
34
34
 
@@ -42,7 +42,7 @@ Banks is a Python prompt programming language and templating system for LLM appl
42
42
 
43
43
  **Type System** (`src/banks/types.py`):
44
44
  - `ChatMessage`: Core chat message structure with role and content
45
- - `ContentBlock`: Handles different content types (text, image_url, audio) with optional cache control
45
+ - `ContentBlock`: Handles different content types (text, image_url, audio, video, document) with optional cache control
46
46
  - `Tool`: Function calling support with automatic schema generation from Python callables
47
47
  - `CacheControl`: Anthropic-style prompt caching metadata
48
48
 
@@ -67,6 +67,8 @@ Banks is a Python prompt programming language and templating system for LLM appl
67
67
  **Core Filters** (`src/banks/filters/`):
68
68
  - `image`: Convert file paths/URLs to base64-encoded image content blocks
69
69
  - `audio`: Convert audio files to base64-encoded audio content blocks
70
+ - `video`: Convert video files to base64-encoded video content blocks
71
+ - `document`: Convert documents (PDF, TXT, HTML, CSS, XML, CSV, RTF, JS, JSON) to base64-encoded content blocks
70
72
  - `cache_control`: Add Anthropic cache control metadata to content blocks
71
73
  - `tool`: Convert Python callables to LLM function call schemas
72
74
  - `lemmatize`: Text lemmatization using simplemma
@@ -95,7 +97,7 @@ Banks is a Python prompt programming language and templating system for LLM appl
95
97
  4. Caching layer prevents re-rendering identical contexts
96
98
 
97
99
  ### Multimodal Content Handling
98
- - Images/audio converted to base64 during filter application
100
+ - Images/audio/video/documents converted to base64 during filter application
99
101
  - Content blocks maintain type safety and metadata
100
102
  - Cache control integrated at content block level
101
103
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: banks
3
- Version: 2.2.0
3
+ Version: 2.3.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
@@ -15,6 +15,7 @@ Classifier: Programming Language :: Python :: 3.10
15
15
  Classifier: Programming Language :: Python :: 3.11
16
16
  Classifier: Programming Language :: Python :: 3.12
17
17
  Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
18
19
  Classifier: Programming Language :: Python :: Implementation :: CPython
19
20
  Classifier: Programming Language :: Python :: Implementation :: PyPy
20
21
  Requires-Python: >=3.9
@@ -125,11 +126,11 @@ print(p.chat_messages({"persona": "helpful assistant"}))
125
126
  # [
126
127
  # ChatMessage(role='system', content=[
127
128
  # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='You are a helpful assistant.',
128
- # image_url=None, input_audio=None)
129
+ # image_url=None, input_audio=None, input_video=None, input_document=None)
129
130
  # ], tool_call_id=None, name=None),
130
131
  # ChatMessage(role='user', content=[
131
132
  # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='Hello, how are you?',
132
- # image_url=None, input_audio=None)
133
+ # image_url=None, input_audio=None, input_video=None, input_document=None)
133
134
  # ], tool_call_id=None, name=None)
134
135
  # ]
135
136
  ```
@@ -94,11 +94,11 @@ print(p.chat_messages({"persona": "helpful assistant"}))
94
94
  # [
95
95
  # ChatMessage(role='system', content=[
96
96
  # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='You are a helpful assistant.',
97
- # image_url=None, input_audio=None)
97
+ # image_url=None, input_audio=None, input_video=None, input_document=None)
98
98
  # ], tool_call_id=None, name=None),
99
99
  # ChatMessage(role='user', content=[
100
100
  # ContentBlock(type=<ContentBlockType.text: 'text'>, cache_control=None, text='Hello, how are you?',
101
- # image_url=None, input_audio=None)
101
+ # image_url=None, input_audio=None, input_video=None, input_document=None)
102
102
  # ], tool_call_id=None, name=None)
103
103
  # ]
104
104
  ```
@@ -19,6 +19,7 @@ classifiers = [
19
19
  "Programming Language :: Python :: 3.11",
20
20
  "Programming Language :: Python :: 3.12",
21
21
  "Programming Language :: Python :: 3.13",
22
+ "Programming Language :: Python :: 3.14",
22
23
  "Programming Language :: Python :: Implementation :: CPython",
23
24
  "Programming Language :: Python :: Implementation :: PyPy",
24
25
  ]
@@ -65,7 +66,7 @@ cov = ["test-cov", "cov-report"]
65
66
  docs = "mkdocs {args:build}"
66
67
 
67
68
  [[tool.hatch.envs.all.matrix]]
68
- python = ["3.9", "3.10", "3.11", "3.12", "3.13"]
69
+ python = ["3.10", "3.11", "3.12", "3.13", "3.14"]
69
70
 
70
71
  [tool.hatch.envs.lint]
71
72
  detached = false # Normally the linting env can be detached, but mypy doesn't install all the stubs we need
@@ -199,7 +200,7 @@ max-args = 10
199
200
  asyncio_default_fixture_loop_scope = "function"
200
201
  markers = ["e2e"]
201
202
  filterwarnings = [
202
- # Dilence litellm warning coming from their Pydantic config.
203
+ # Silence litellm warning coming from their Pydantic config.
203
204
  # This assumes our use of Pydantic is correct :)
204
205
  "ignore:Support for class-based `config` is deprecated",
205
206
  ]
@@ -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.2.0"
4
+ __version__ = "2.3.0"
@@ -28,9 +28,12 @@ class _BanksConfig:
28
28
  return original_value
29
29
 
30
30
  # Convert string from env var to the actual type
31
- t = super().__getattribute__("__annotations__")[name]
31
+ annotations = getattr(type(self), "__annotations__", {})
32
+ t = annotations.get(name, type(original_value))
32
33
  if t is bool:
33
34
  return strtobool(read_value)
35
+ if t is Any:
36
+ return read_value
34
37
 
35
38
  return t(read_value)
36
39
 
@@ -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, xml
7
+ from .filters import audio, cache_control, document, image, lemmatize, tool, video, xml
8
8
 
9
9
 
10
10
  def _add_extensions(_env):
@@ -38,6 +38,8 @@ 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["video"] = video
42
+ env.filters["document"] = document
41
43
  env.filters["to_xml"] = xml
42
44
 
43
45
  _add_extensions(env)
@@ -14,7 +14,7 @@ class CanaryWordError(Exception):
14
14
 
15
15
 
16
16
  class PromptNotFoundError(Exception):
17
- """The prompt was now found in the registry."""
17
+ """The prompt was not found in the registry."""
18
18
 
19
19
 
20
20
  class InvalidPromptError(Exception):
@@ -3,7 +3,7 @@
3
3
  # SPDX-License-Identifier: MIT
4
4
  import importlib
5
5
  import json
6
- from typing import cast
6
+ from typing import TYPE_CHECKING, Any, Callable, cast
7
7
 
8
8
  from jinja2 import TemplateSyntaxError, nodes
9
9
  from jinja2.ext import Extension
@@ -12,6 +12,8 @@ from pydantic import ValidationError
12
12
  from banks.errors import InvalidPromptError, LLMError
13
13
  from banks.types import ChatMessage, Tool
14
14
 
15
+ if TYPE_CHECKING:
16
+ from litellm.types.utils import ChatCompletionMessageToolCall
15
17
  SUPPORTED_KWARGS = ("model",)
16
18
  LITELLM_INSTALL_MSG = "litellm is not installed. Please install it with `pip install litellm`."
17
19
 
@@ -74,7 +76,19 @@ class CompletionExtension(Extension):
74
76
  return nodes.CallBlock(self.call_method("_do_completion_async", args), [], [], body).set_lineno(lineno)
75
77
  return nodes.CallBlock(self.call_method("_do_completion", args), [], [], body).set_lineno(lineno)
76
78
 
77
- def _get_tool_callable(self, tools, tool_call):
79
+ def _get_tool_callable(self, tools: list[Tool], tool_call: "ChatCompletionMessageToolCall") -> Callable[..., Any]:
80
+ """Get the callable function for a tool call.
81
+
82
+ Args:
83
+ tools: List of available tools
84
+ tool_call: The tool call from the LLM response
85
+
86
+ Returns:
87
+ The callable function
88
+
89
+ Raises:
90
+ ValueError: If the function is not found in available tools
91
+ """
78
92
  for tool in tools:
79
93
  if tool.function.name == tool_call.function.name:
80
94
  module_name, func_name = tool.import_path.rsplit(".", maxsplit=1)
@@ -3,9 +3,11 @@
3
3
  # SPDX-License-Identifier: MIT
4
4
  from .audio import audio
5
5
  from .cache_control import cache_control
6
+ from .document import document
6
7
  from .image import image
7
8
  from .lemmatize import lemmatize
8
9
  from .tool import tool
10
+ from .video import video
9
11
  from .xml import xml
10
12
 
11
- __all__ = ("cache_control", "image", "lemmatize", "tool", "audio", "xml")
13
+ __all__ = ("cache_control", "image", "lemmatize", "tool", "audio", "video", "document", "xml")
@@ -0,0 +1,60 @@
1
+ # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <mpippi@gmail.com>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ import re
5
+ from pathlib import Path
6
+ from typing import cast
7
+ from urllib.parse import urlparse
8
+
9
+ from banks.types import AudioFormat, ContentBlock, InputAudio
10
+
11
+ BASE64_AUDIO_REGEX = re.compile(r"audio\/.*;base64,.*")
12
+
13
+
14
+ def _is_url(string: str) -> bool:
15
+ """Check if a string is a URL."""
16
+ result = urlparse(string)
17
+ if not result.scheme:
18
+ return False
19
+
20
+ if not result.netloc:
21
+ # The only valid format when netloc is empty is base64 data urls
22
+ return all([result.scheme == "data", BASE64_AUDIO_REGEX.match(result.path)])
23
+
24
+ return True
25
+
26
+
27
+ def _get_audio_format_from_url(url: str) -> AudioFormat:
28
+ """Extract audio format from URL.
29
+
30
+ Tries to determine format from URL path or defaults to mp3.
31
+ """
32
+ parsed = urlparse(url)
33
+ path = parsed.path.lower()
34
+ for fmt in ("mp3", "wav", "m4a", "webm", "ogg", "flac"):
35
+ if path.endswith(f".{fmt}"):
36
+ return cast(AudioFormat, fmt)
37
+ # Default to mp3 if format cannot be determined
38
+ return "mp3"
39
+
40
+
41
+ def audio(value: str) -> str:
42
+ """Wrap the filtered value into a ContentBlock of type audio.
43
+
44
+ The resulting ChatMessage will have the field `content` populated with a list of ContentBlock objects.
45
+
46
+ Supports both file paths and URLs (including data URLs).
47
+
48
+ Example:
49
+ ```jinja
50
+ {{ "path/to/audio/file.mp3" | audio }}
51
+ {{ "https://example.com/audio.mp3" | audio }}
52
+ ```
53
+ """
54
+ if _is_url(value):
55
+ audio_format = _get_audio_format_from_url(value)
56
+ input_audio = InputAudio.from_url(value, audio_format)
57
+ else:
58
+ input_audio = InputAudio.from_path(Path(value))
59
+ block = ContentBlock.model_validate({"type": "audio", "input_audio": input_audio})
60
+ return f"<content_block>{block.model_dump_json()}</content_block>"
@@ -0,0 +1,96 @@
1
+ # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <mpippi@gmail.com>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ import re
5
+ from pathlib import Path
6
+ from typing import cast
7
+ from urllib.parse import urlparse
8
+
9
+ from banks.types import ContentBlock, DocumentFormat, InputDocument
10
+
11
+ BASE64_DOCUMENT_REGEX = re.compile(r"(text|application)\/.*;base64,.*")
12
+
13
+
14
+ def _is_url(string: str) -> bool:
15
+ """Check if a string is a URL."""
16
+ result = urlparse(string)
17
+ if not result.scheme:
18
+ return False
19
+
20
+ if not result.netloc:
21
+ # The only valid format when netloc is empty is base64 data urls
22
+ return all([result.scheme == "data", BASE64_DOCUMENT_REGEX.match(result.path)])
23
+
24
+ return True
25
+
26
+
27
+ def _get_document_format_from_url(url: str) -> DocumentFormat:
28
+ """Extract document format from URL.
29
+
30
+ Tries to determine format from URL path or defaults to pdf.
31
+ """
32
+ parsed = urlparse(url)
33
+ path = parsed.path.lower()
34
+ # Gemini supported file types https://ai.google.dev/gemini-api/docs/file-input-methods
35
+ # text/html
36
+ # text/css
37
+ # text/plain
38
+ # text/xml
39
+ # text/scv
40
+ # text/rtf
41
+ # text/javascript
42
+ # application/json
43
+ # application/pdf
44
+
45
+ # Claude supported file types
46
+ # application/pdf
47
+ # text/plain
48
+
49
+ # OpenAI supported file types
50
+ # application/pdf
51
+
52
+ for fmt in (
53
+ "pdf",
54
+ "html",
55
+ "htm",
56
+ "xhtml",
57
+ "css",
58
+ "txt",
59
+ "md",
60
+ "markdown",
61
+ "rst",
62
+ "xml",
63
+ "csv",
64
+ "rtf",
65
+ "js",
66
+ "mjs",
67
+ "cjs",
68
+ "javascript",
69
+ "json",
70
+ ):
71
+ if path.endswith(f".{fmt}"):
72
+ return cast(DocumentFormat, fmt)
73
+ # Default to pdf if format cannot be determined
74
+ return "pdf"
75
+
76
+
77
+ def document(value: str) -> str:
78
+ """Wrap the filtered value into a ContentBlock of type document.
79
+
80
+ The resulting ChatMessage will have the field `content` populated with a list of ContentBlock objects.
81
+
82
+ Supports both file paths and URLs (including data URLs).
83
+
84
+ Example:
85
+ ```jinja
86
+ {{ "path/to/document/file.pdf" | document }}
87
+ {{ "https://example.com/document.pdf" | document }}
88
+ ```
89
+ """
90
+ if _is_url(value):
91
+ document_format = _get_document_format_from_url(value)
92
+ input_document = InputDocument.from_url(value, document_format)
93
+ else:
94
+ input_document = InputDocument.from_path(Path(value))
95
+ block = ContentBlock.model_validate({"type": "document", "input_document": input_document})
96
+ return f"<content_block>{block.model_dump_json()}</content_block>"
@@ -0,0 +1,62 @@
1
+ # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <mpippi@gmail.com>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ import re
5
+ from pathlib import Path
6
+ from typing import cast
7
+ from urllib.parse import urlparse
8
+
9
+ from banks.types import ContentBlock, InputVideo, VideoFormat
10
+
11
+ BASE64_VIDEO_REGEX = re.compile(r"video\/.*;base64,.*")
12
+
13
+
14
+ def _is_url(string: str) -> bool:
15
+ """Check if a string is a URL."""
16
+ result = urlparse(string)
17
+ if not result.scheme:
18
+ return False
19
+
20
+ if not result.netloc:
21
+ # The only valid format when netloc is empty is base64 data urls
22
+ return all([result.scheme == "data", BASE64_VIDEO_REGEX.match(result.path)])
23
+
24
+ return True
25
+
26
+
27
+ def _get_video_format_from_url(url: str) -> VideoFormat:
28
+ """Extract video format from URL.
29
+
30
+ Tries to determine format from URL path or defaults to mp4.
31
+ """
32
+ parsed = urlparse(url)
33
+ path = parsed.path.lower()
34
+
35
+ # Based on formats supported by Gemini https://ai.google.dev/gemini-api/docs/video-understanding
36
+ for fmt in ("mp4", "mpeg", "mov", "avi", "flv", "mpg", "webm", "wmv", "3gpp"):
37
+ if path.endswith(f".{fmt}"):
38
+ return cast(VideoFormat, fmt)
39
+ # Default to mp4 if format cannot be determined
40
+ return "mp4"
41
+
42
+
43
+ def video(value: str) -> str:
44
+ """Wrap the filtered value into a ContentBlock of type video.
45
+
46
+ The resulting ChatMessage will have the field `content` populated with a list of ContentBlock objects.
47
+
48
+ Supports both file paths and URLs (including data URLs).
49
+
50
+ Example:
51
+ ```jinja
52
+ {{ "path/to/video/file.mp4" | video }}
53
+ {{ "https://example.com/video.mp4" | video }}
54
+ ```
55
+ """
56
+ if _is_url(value):
57
+ video_format = _get_video_format_from_url(value)
58
+ input_video = InputVideo.from_url(value, video_format)
59
+ else:
60
+ input_video = InputVideo.from_path(Path(value))
61
+ block = ContentBlock.model_validate({"type": "video", "input_video": input_video})
62
+ return f"<content_block>{block.model_dump_json()}</content_block>"
@@ -81,8 +81,17 @@ class BasePrompt:
81
81
 
82
82
  @property
83
83
  def variables(self) -> set[str]:
84
- ast = env.parse(self.raw)
85
- return meta.find_undeclared_variables(ast)
84
+ try:
85
+ ast = env.parse(self.raw)
86
+ return meta.find_undeclared_variables(ast)
87
+ except Exception as e:
88
+ from jinja2 import TemplateSyntaxError
89
+
90
+ if isinstance(e, TemplateSyntaxError):
91
+ raise
92
+ # Re-raise as TemplateSyntaxError for consistency
93
+ msg = f"Failed to parse template: {e}"
94
+ raise TemplateSyntaxError(msg, 0) from e
86
95
 
87
96
  def canary_leaked(self, text: str) -> bool:
88
97
  """Returns whether the canary word is present in `text`, signalling the prompt might have leaked."""
@@ -23,6 +23,8 @@ class ContentBlockType(str, Enum):
23
23
  text = "text"
24
24
  image_url = "image_url"
25
25
  audio = "audio"
26
+ video = "video"
27
+ document = "document"
26
28
 
27
29
 
28
30
  class CacheControl(BaseModel):
@@ -43,6 +45,8 @@ class ImageUrl(BaseModel):
43
45
 
44
46
 
45
47
  AudioFormat = Literal["mp3", "wav", "m4a", "webm", "ogg", "flac"]
48
+ VideoFormat = Literal["mp4", "mpeg", "mov", "avi", "flv", "mpg", "webm", "wmv", "3gpp"]
49
+ DocumentFormat = Literal["pdf", "html", "css", "plain", "xml", "csv", "rtf", "javascript", "json"]
46
50
 
47
51
 
48
52
  class InputAudio(BaseModel):
@@ -56,6 +60,69 @@ class InputAudio(BaseModel):
56
60
  file_format = cast(AudioFormat, file_path.suffix[1:])
57
61
  return cls(data=encoded_str, format=file_format)
58
62
 
63
+ @classmethod
64
+ def from_url(cls, url: str, audio_format: AudioFormat) -> Self:
65
+ """Create InputAudio from a URL.
66
+
67
+ Args:
68
+ url: The URL to the audio file
69
+ audio_format: The audio format
70
+
71
+ Returns:
72
+ InputAudio instance with the URL as data
73
+ """
74
+ return cls(data=url, format=audio_format)
75
+
76
+
77
+ class InputVideo(BaseModel):
78
+ data: str
79
+ format: VideoFormat
80
+
81
+ @classmethod
82
+ def from_path(cls, file_path: Path) -> Self:
83
+ with open(file_path, "rb") as video_file:
84
+ encoded_str = base64.b64encode(video_file.read()).decode("utf-8")
85
+ file_format = cast(VideoFormat, file_path.suffix[1:])
86
+ return cls(data=encoded_str, format=file_format)
87
+
88
+ @classmethod
89
+ def from_url(cls, url: str, video_format: VideoFormat) -> Self:
90
+ """Create InputVideo from a URL.
91
+
92
+ Args:
93
+ url: The URL to the audio file
94
+ video_format: The audio format
95
+
96
+ Returns:
97
+ InputVideo instance with the URL as data
98
+ """
99
+ return cls(data=url, format=video_format)
100
+
101
+
102
+ class InputDocument(BaseModel):
103
+ data: str
104
+ format: DocumentFormat
105
+
106
+ @classmethod
107
+ def from_path(cls, file_path: Path) -> Self:
108
+ with open(file_path, "rb") as document_file:
109
+ encoded_str = base64.b64encode(document_file.read()).decode("utf-8")
110
+ file_format = cast(DocumentFormat, file_path.suffix[1:])
111
+ return cls(data=encoded_str, format=file_format)
112
+
113
+ @classmethod
114
+ def from_url(cls, url: str, document_format: DocumentFormat) -> Self:
115
+ """Create InputDocument from a URL.
116
+
117
+ Args:
118
+ url: The URL to the document file
119
+ document_format: The document format
120
+
121
+ Returns:
122
+ InputDocument instance with the URL as data
123
+ """
124
+ return cls(data=url, format=document_format)
125
+
59
126
 
60
127
  class ContentBlock(BaseModel):
61
128
  type: ContentBlockType
@@ -63,6 +130,8 @@ class ContentBlock(BaseModel):
63
130
  text: str | None = None
64
131
  image_url: ImageUrl | None = None
65
132
  input_audio: InputAudio | None = None
133
+ input_video: InputVideo | None = None
134
+ input_document: InputDocument | None = None
66
135
 
67
136
 
68
137
  ChatMessageContent = Union[list[ContentBlock], str]
Binary file
Binary file
@@ -4,7 +4,7 @@ from pathlib import Path
4
4
  import pytest
5
5
 
6
6
  from banks import Prompt
7
- from banks.filters.audio import audio
7
+ from banks.filters.audio import _get_audio_format_from_url, _is_url, audio
8
8
 
9
9
 
10
10
  @pytest.fixture
@@ -35,6 +35,34 @@ def test_audio_with_nonexistent_file():
35
35
  audio("nonexistent/audio.wav")
36
36
 
37
37
 
38
+ def test_audio_with_url():
39
+ """Test audio filter with a URL input (no filesystem access)."""
40
+ url = "https://example.com/sound.ogg"
41
+ result = audio(url)
42
+
43
+ assert result.startswith("<content_block>")
44
+ assert result.endswith("</content_block>")
45
+
46
+ json_content = result[15:-16] # Remove wrapper tags
47
+ content_block = json.loads(json_content)
48
+
49
+ assert content_block["type"] == "audio"
50
+ assert content_block["input_audio"]["data"] == url
51
+ assert content_block["input_audio"]["format"] == "ogg"
52
+
53
+
54
+ def test_is_url_variants():
55
+ assert _is_url("relative/path.wav") is False
56
+ assert _is_url("https://example.com/sound.wav") is True
57
+ assert _is_url("data:audio/wav;base64,AAAA") is True
58
+ assert _is_url("data:text/plain;base64,AAAA") is False
59
+
60
+
61
+ def test_get_audio_format_from_url():
62
+ assert _get_audio_format_from_url("https://example.com/sound.WAV") == "wav"
63
+ assert _get_audio_format_from_url("https://example.com/sound") == "mp3"
64
+
65
+
38
66
  def test_audio_no_chat_block(empty_wav):
39
67
  prompt = Prompt("{{ test }} and {{ another | audio }}")
40
68
  messages = prompt.chat_messages({"test": "hello world", "another": str(empty_wav)})
@@ -5,6 +5,7 @@ def test_cache_control():
5
5
  res = cache_control("foo", "ephemeral")
6
6
  res = res.replace("<content_block>", "")
7
7
  res = res.replace("</content_block>", "")
8
- assert (
9
- res == '{"type":"text","cache_control":{"type":"ephemeral"},"text":"foo","image_url":null,"input_audio":null}'
8
+ assert res == (
9
+ '{"type":"text","cache_control":{"type":"ephemeral"},"text":"foo","image_url":null,"input_audio":null,'
10
+ '"input_video":null,"input_document":null}'
10
11
  )
@@ -55,7 +55,7 @@ def test_get_not_found(registry: DirectoryPromptRegistry):
55
55
  def test_set_existing_no_overwrite(registry: DirectoryPromptRegistry):
56
56
  new_prompt = Prompt("a new prompt!", name="blog")
57
57
  with pytest.raises(
58
- InvalidPromptError, match="Prompt with name 'blog' already exists. Use overwrite=True to overwrite"
58
+ InvalidPromptError, match=r"Prompt with name 'blog' already exists. Use overwrite=True to overwrite"
59
59
  ):
60
60
  registry.set(prompt=new_prompt)
61
61
 
@@ -0,0 +1,74 @@
1
+ import json
2
+ from pathlib import Path
3
+
4
+ import pytest
5
+
6
+ from banks import Prompt
7
+ from banks.filters.document import _get_document_format_from_url, _is_url, document
8
+
9
+
10
+ @pytest.fixture
11
+ def tiny_pdf():
12
+ here = Path(__file__).parent
13
+ return here / "data" / "1x1.pdf"
14
+
15
+
16
+ def test_document_with_file_path(tiny_pdf):
17
+ """Test document filter with a file path input"""
18
+ result = document(str(tiny_pdf))
19
+
20
+ # Verify the content block wrapper
21
+ assert result.startswith("<content_block>")
22
+ assert result.endswith("</content_block>")
23
+
24
+ # Parse the JSON content
25
+ json_content = result[15:-16] # Remove wrapper tags
26
+ content_block = json.loads(json_content)
27
+
28
+ assert content_block["type"] == "document"
29
+ assert content_block["input_document"]["format"].startswith("pdf")
30
+
31
+
32
+ def test_document_with_nonexistent_file():
33
+ """Test document filter with a nonexistent file path"""
34
+ with pytest.raises(FileNotFoundError):
35
+ document("nonexistent/document.pdf")
36
+
37
+
38
+ def test_document_with_url():
39
+ """Test document filter with a URL input (no filesystem access)."""
40
+ url = "https://example.com/document.css"
41
+ result = document(url)
42
+
43
+ assert result.startswith("<content_block>")
44
+ assert result.endswith("</content_block>")
45
+
46
+ json_content = result[15:-16] # Remove wrapper tags
47
+ content_block = json.loads(json_content)
48
+
49
+ assert content_block["type"] == "document"
50
+ assert content_block["input_document"]["data"] == url
51
+ assert content_block["input_document"]["format"] == "css"
52
+
53
+
54
+ def test_is_url_variants():
55
+ assert _is_url("relative/path.pdf") is False
56
+ assert _is_url("https://example.com/document.pdf") is True
57
+ assert _is_url("data:application/pdf;base64,AAAA") is True
58
+ assert _is_url("data:text/plain;base64,AAAA") is True
59
+ assert _is_url("data:audio/mp3;base64,AAAA") is False
60
+
61
+
62
+ def test_get_document_format_from_url():
63
+ assert _get_document_format_from_url("https://example.com/document.WAV") == "pdf"
64
+ assert _get_document_format_from_url("https://example.com/document") == "pdf"
65
+
66
+
67
+ def test_document_no_chat_block(tiny_pdf):
68
+ prompt = Prompt("{{ test }} and {{ another | document }}")
69
+ messages = prompt.chat_messages({"test": "hello world", "another": str(tiny_pdf)})
70
+ assert len(messages) == 1
71
+ message = messages[0]
72
+ assert len(message.content) == 2
73
+ assert message.content[0].text == "hello world and" # type: ignore
74
+ assert message.content[1].type == "document" # type:ignore
@@ -45,7 +45,7 @@ def test_set_existing_no_overwrite(registry):
45
45
 
46
46
  new_prompt = Prompt("Hi {{name}}!", name="greeting")
47
47
  with pytest.raises(
48
- InvalidPromptError, match="Prompt with name 'greeting' already exists. Use overwrite=True to overwrite"
48
+ InvalidPromptError, match=r"Prompt with name 'greeting' already exists. Use overwrite=True to overwrite"
49
49
  ):
50
50
  registry.set(prompt=new_prompt)
51
51
 
@@ -0,0 +1,73 @@
1
+ import json
2
+ from pathlib import Path
3
+
4
+ import pytest
5
+
6
+ from banks import Prompt
7
+ from banks.filters.video import _get_video_format_from_url, _is_url, video
8
+
9
+
10
+ @pytest.fixture
11
+ def empty_mov():
12
+ here = Path(__file__).parent
13
+ return here / "data" / "empty.mov"
14
+
15
+
16
+ def test_video_with_file_path(empty_mov):
17
+ """Test video filter with a file path input"""
18
+ result = video(str(empty_mov))
19
+
20
+ # Verify the content block wrapper
21
+ assert result.startswith("<content_block>")
22
+ assert result.endswith("</content_block>")
23
+
24
+ # Parse the JSON content
25
+ json_content = result[15:-16] # Remove wrapper tags
26
+ content_block = json.loads(json_content)
27
+
28
+ assert content_block["type"] == "video"
29
+ assert content_block["input_video"]["format"].startswith("mov")
30
+
31
+
32
+ def test_video_with_nonexistent_file():
33
+ """Test video filter with a nonexistent file path"""
34
+ with pytest.raises(FileNotFoundError):
35
+ video("nonexistent/video.mov")
36
+
37
+
38
+ def test_video_with_url():
39
+ """Test video filter with a URL input (no filesystem access)."""
40
+ url = "https://example.com/video.webm"
41
+ result = video(url)
42
+
43
+ assert result.startswith("<content_block>")
44
+ assert result.endswith("</content_block>")
45
+
46
+ json_content = result[15:-16] # Remove wrapper tags
47
+ content_block = json.loads(json_content)
48
+
49
+ assert content_block["type"] == "video"
50
+ assert content_block["input_video"]["data"] == url
51
+ assert content_block["input_video"]["format"] == "webm"
52
+
53
+
54
+ def test_is_url_variants():
55
+ assert _is_url("relative/path.mov") is False
56
+ assert _is_url("https://example.com/video.mov") is True
57
+ assert _is_url("data:video/mov;base64,AAAA") is True
58
+ assert _is_url("data:text/plain;base64,AAAA") is False
59
+
60
+
61
+ def test_get_video_format_from_url():
62
+ assert _get_video_format_from_url("https://example.com/video.MOV") == "mov"
63
+ assert _get_video_format_from_url("https://example.com/video") == "mp4"
64
+
65
+
66
+ def test_video_no_chat_block(empty_mov):
67
+ prompt = Prompt("{{ test }} and {{ another | video }}")
68
+ messages = prompt.chat_messages({"test": "hello world", "another": str(empty_mov)})
69
+ assert len(messages) == 1
70
+ message = messages[0]
71
+ assert len(message.content) == 2
72
+ assert message.content[0].text == "hello world and" # type: ignore
73
+ assert message.content[1].type == "video" # type:ignore
@@ -1,23 +0,0 @@
1
- # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <mpippi@gmail.com>
2
- #
3
- # SPDX-License-Identifier: MIT
4
- from pathlib import Path
5
-
6
- from banks.types import ContentBlock, InputAudio
7
-
8
-
9
- def audio(value: str) -> str:
10
- """Wrap the filtered value into a ContentBlock of type audio.
11
-
12
- The resulting ChatMessage will have the field `content` populated with a list of ContentBlock objects.
13
-
14
- Example:
15
- ```jinja
16
- Describe what you see
17
-
18
- {{ "path/to/audio/file" | audio }}
19
- ```
20
- """
21
- input_audio = InputAudio.from_path(Path(value))
22
- block = ContentBlock.model_validate({"type": "audio", "input_audio": input_audio})
23
- return f"<content_block>{block.model_dump_json()}</content_block>"
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