Skip to main content

Pydantic + DSPy instances from 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.

Open in Dev Containers Open in GitHub Codespaces

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
  1. Generate an SSH key and add the SSH key to your GitHub account.
  2. 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
  1. Install Docker Desktop.
3. Install VS Code or PyCharm
  1. Install VS Code and VS Code's Dev Containers extension. Alternatively, install PyCharm.
  2. 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:

  1. ⭐️ GitHub Codespaces: click on Code and select Create codespace to start a Dev Container with GitHub Codespaces.
  2. ⭐️ 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.
  3. Dev Container: clone this repository, open it with VS Code, and run Ctrl/⌘ + + PDev Containers: Reopen in Container.
  4. PyCharm: clone this repository, open it with PyCharm, and configure Docker Compose as a remote interpreter with the dev service.
  5. 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 run docker 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 to pyproject.toml and poetry.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 by pyproject.toml.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dslmodel-2024.10.3.3.tar.gz (35.8 kB view hashes)

Uploaded Source

Built Distribution

dslmodel-2024.10.3.3-py3-none-any.whl (39.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page