Python API
banks.prompt.Prompt
The Prompt
class is the only thing you need to know about Banks on the Python side. The class can be
initialized with a string variable containing the prompt template text, then you can invoke the method text
on your instance to pass the data needed to render the template and get back the final prompt.
A quick example:
from banks import Prompt
p = Prompt("Write a 500-word blog post on {{ topic }}.")
my_topic = "retrogame computing"
print(p.text({"topic": my_topic}))
__init__
__init__(text: str, *, name: str | None = None, version: str | None = None, metadata: dict[str, Any] | None = None, canary_word: str | None = None, render_cache: RenderCache | None = None) -> None
Prompt constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The template text. |
required |
name
|
str | None
|
The name to identify this prompt. |
None
|
version
|
str | None
|
The version string attached to this prompt. |
None
|
metadata
|
dict[str, Any] | None
|
A key-value set of metadata pairs attached to this prompt. |
None
|
canary_word
|
str | None
|
The string to use for the |
None
|
render_cache
|
RenderCache | None
|
The caching backend to store rendered prompts. If |
None
|
canary_leaked
Returns whether the canary word is present in text
, signalling the prompt might have leaked.
chat_messages
Render the prompt using variables present in data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict[str, Any] | None
|
A dictionary containing the context variables. |
None
|
banks.prompt.AsyncPrompt
Banks provides async support through the machinery provided by Jinja
Since the Jinja environment is a global state in banks, the library can work either with or without async support, and this must be known before importing anything.
If the application using banks runs within an asyncio
loop, you can do two things
to optimize banks' execution:
- Set the environment variable
BANKS_ASYNC_ENABLED=true
. - Use the
AsyncPrompt
class that has an awaitablerun
method.
For example, let's render a prompt that contains some calls to the generate
extension. Those calls
will be heavily I/O bound, so other tasks can take advantage and advance while the prompt is being
rendered.
Example:
# Enable async support before importing from banks
import os
os.environ["BANKS_ASYNC_ENABLED"] = "true"
# Show logs to see what's happening at runtime
import logging
logging.basicConfig(level=logging.INFO)
import asyncio
from banks import AsyncPrompt
prompt_template = """
Generate a tweet about the topic '{{ topic }}' with a positive sentiment.
"""
async def task(task_id: int, sleep_time: int):
logging.info(f"Task {task_id} is running.")
await asyncio.sleep(sleep_time)
logging.info(f"Task {task_id} done.")
async def main():
p = AsyncPrompt(prompt_template)
# Schedule the prompt rendering along with two executions of 'task', one sleeping for 10 seconds
# and one sleeping for 1 second
results = await asyncio.gather(p.text({"topic": "AI frameworks"}), task(1, 10), task(2, 1))
print("All tasks done, rendered prompt:")
print(results[0])
asyncio.run(main())
banks.registries.directory.DirectoryPromptRegistry
Registry that stores prompts as files in a directory structure.
__init__
Initialize the directory prompt registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory_path
|
Path
|
Path to directory where prompts will be stored |
required |
force_reindex
|
bool
|
Whether to force rebuilding the index from disk |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If directory_path is not a directory |
get
Retrieve a prompt by name and version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the prompt to retrieve |
required |
version
|
str | None
|
Version of the prompt (optional) |
None
|
Returns:
Type | Description |
---|---|
Prompt
|
The requested Prompt object |
Raises:
Type | Description |
---|---|
PromptNotFoundError
|
If prompt doesn't exist |
set
Store a prompt in the registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
Prompt
|
The Prompt object to store |
required |
overwrite
|
bool
|
Whether to overwrite existing prompt |
False
|
Raises:
Type | Description |
---|---|
InvalidPromptError
|
If prompt exists and overwrite=False |
banks.registries.file.FilePromptRegistry
A prompt registry storing all prompt data in a single JSON file.
__init__
Initialize the file prompt registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
registry_index
|
Path
|
Path to the JSON file that will store the prompts |
required |
Note
Creates parent directories if they don't exist.
get
Retrieve a prompt by name and version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the prompt to retrieve |
required |
version
|
str | None
|
Version of the prompt (optional) |
None
|
Returns:
Type | Description |
---|---|
Prompt
|
The requested Prompt object |
Raises:
Type | Description |
---|---|
PromptNotFoundError
|
If the requested prompt doesn't exist |
set
Store a prompt in the registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
Prompt
|
The Prompt object to store |
required |
overwrite
|
bool
|
Whether to overwrite an existing prompt |
False
|
Raises:
Type | Description |
---|---|
InvalidPromptError
|
If prompt exists and overwrite=False |
banks.registries.redis.RedisPromptRegistry
A prompt registry that stores prompts in Redis.
__init__
Initialize the Redis prompt registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
redis_url
|
str
|
Redis connection URL |
'redis://localhost:6379'
|
prefix
|
str
|
Key prefix for storing prompts in Redis |
'banks:prompt:'
|
get
Get a prompt by name and version.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the prompt |
required |
version
|
str | None
|
Version of the prompt (optional) |
None
|
Returns:
Type | Description |
---|---|
Prompt
|
Prompt instance |
Raises:
Type | Description |
---|---|
PromptNotFoundError
|
If prompt doesn't exist |
set
Store a prompt in Redis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
Prompt
|
Prompt instance to store |
required |
overwrite
|
bool
|
Whether to overwrite existing prompt |
False
|
Raises:
Type | Description |
---|---|
InvalidPromptError
|
If prompt exists and overwrite=False |