dslmodel built with Pydantic and DSPy. Generates model instances with prompts and Jinja
Project description
dslmodel
dslmodel
is a framework for declarative model creation using templates and concurrent execution, built on top of the pydantic
and dspy
library. It provides tools to generate models with dynamic fields and execute tasks concurrently.
Installing
To install this package, run:
pip install dslmodel
Using
To view the CLI help information, run:
dsl --help
Example Usage
Defining Models
You can define models with dynamic fields using Jinja templates, and then instantiate these models using the provided tools.
from typing import List
from pydantic import Field
from dslmodel import DSLModel
class Participant(DSLModel):
"""Represents a participant in a meeting."""
name: str = Field("{{ fake_name() }}", description="Name of the participant.")
role: str = Field("{{ fake_job() }}", description="Role of the participant.")
class Meeting(DSLModel):
"""Represents a meeting, its participants, and other details."""
name: str = Field(..., description="Name of the meeting.")
participants: List[Participant] = Field(..., description="List of participants.")
Generating Data from a Template
Here's how you can use a Jinja template to dynamically generate meeting details, including participants with fake data:
meeting_template = """
Fortune 500 Meeting about {{ fake_bs() }}
Participants:
{% for participant in participants %}
- {{ participant.name }} ({{ participant.role }})
{% endfor %}
"""
# Create participants
participants = [Participant() for _ in range(5)]
# Generate the meeting instance
meeting_instance = Meeting.from_prompt(meeting_template, participants=participants)
# Output the meeting in YAML format
print(meeting_instance.to_yaml())
Concurrent Execution with run_dsls
You can generate multiple participants concurrently using the run_dsls
function:
from dslmodel.utils.model_tools import run_dsls
def create_participants_concurrently():
tasks = [(Participant, "{{ fake_name() }} - {{ fake_job() }}") for _ in range(5)]
# Run the tasks concurrently
results = run_dsls(tasks, max_workers=5)
for i, result in enumerate(results):
print(f"Participant {i+1}: {result}")
# Create participants concurrently
create_participants_concurrently()
Saving and Loading Models
You can save your generated model instances to a file and reload them as needed:
# Save the meeting instance to a YAML file
meeting_instance.save(file_path="meeting_output.yaml")
# Load the meeting instance from the saved YAML file
loaded_meeting = Meeting.from_yaml("meeting_output.yaml")
# Display loaded content
print(loaded_meeting.to_yaml())
Contributing
Prerequisites
1. Set up Git to use SSH
- Generate an SSH key and add the SSH key to your GitHub account.
- Configure SSH to automatically load your SSH keys:
cat << EOF >> ~/.ssh/config Host * AddKeysToAgent yes IgnoreUnknown UseKeychain UseKeychain yes ForwardAgent yes EOF
2. Install Docker
- Install Docker Desktop.
- Linux only:
- Export your user's user id and group id so that files created in the Dev Container are owned by your user:
cat << EOF >> ~/.bashrc export UID=$(id --user) export GID=$(id --group) EOF
- Export your user's user id and group id so that files created in the Dev Container are owned by your user:
- Linux only:
3. Install VS Code or PyCharm
- Install VS Code and VS Code's Dev Containers extension. Alternatively, install PyCharm.
- Optional: install a Nerd Font such as FiraCode Nerd Font and configure VS Code or configure PyCharm to use it.
Development environments
The following development environments are supported:
- ⭐️ GitHub Codespaces: click on Code and select Create codespace to start a Dev Container with GitHub Codespaces.
- ⭐️ Dev Container (with container volume): click on Open in Dev Containers to clone this repository in a container volume and create a Dev Container with VS Code.
- Dev Container: clone this repository, open it with VS Code, and run Ctrl/⌘ + ⇧ + P → Dev Containers: Reopen in Container.
- PyCharm: clone this repository, open it with PyCharm, and configure Docker Compose as a remote interpreter with the
dev
service. - Terminal: clone this repository, open it with your terminal, and run
docker compose up --detach dev
to start a Dev Container in the background, and then rundocker compose exec dev zsh
to open a shell prompt in the Dev Container.
Developing
- Run
poe
from within the development environment to print a list of Poe the Poet tasks available to run on this project. - Run
poetry add {package}
from within the development environment to install a run time dependency and add it topyproject.toml
andpoetry.lock
. Add--group test
or--group dev
to install a CI or development dependency, respectively. - Run
poetry update
from within the development environment to upgrade all dependencies to the latest versions allowed bypyproject.toml
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dslmodel-2024.10.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0363fd4465c5a543b558a00fa0c417886c01dd4d20370ba8012d97d8bdb281df |
|
MD5 | f02a06cd329db31c15f7b9b6c1b5194d |
|
BLAKE2b-256 | 9ed43cf2c46402adecf5d5d428d37faeb5fcdc6ebcc0f3e8ae4455d27c36c07a |