Repository navigation
LCORE-4742: Implement prompt store operations #3119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| """Store operations for the versioned prompt catalog.""" | ||
|
|
||
| from typing import Optional | ||
| from uuid import uuid4 | ||
|
|
||
| from app.database import get_session | ||
| from log import get_logger | ||
| from models.database.prompts import Prompt, PromptVersion | ||
| from prompts.exceptions import PromptNotFoundError, PromptValidationError | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| def _validate_variables(variables: list[str]) -> None: | ||
| """Reject duplicate variable names. | ||
|
|
||
| Parameters: | ||
| variables: Declared variable names to validate. | ||
|
|
||
| Raises: | ||
| PromptValidationError: If the list contains duplicate names. | ||
|
|
||
| """ | ||
| if len(variables) != len(set(variables)): | ||
| raise PromptValidationError("The variables contain duplicate names") | ||
|
|
||
|
|
||
| def create_prompt(name: str, template: str, variables: list[str]) -> PromptVersion: | ||
| """Create a new prompt with its initial version. | ||
|
|
||
| Parameters: | ||
| name: Display name for the prompt. | ||
| template: Mustache template text. | ||
| variables: Declared variable names for the template. | ||
|
|
||
| Returns: | ||
| PromptVersion: The initial version row. | ||
|
|
||
| Raises: | ||
| PromptValidationError: If variables contain duplicates. | ||
|
|
||
| """ | ||
| # TODO: template validation # pylint: disable=fixme | ||
|
|
||
| _validate_variables(variables) | ||
|
|
||
| prompt_id = f"pmpt_{uuid4()}" | ||
| version = 1 | ||
|
|
||
| with get_session() as session: | ||
| prompt = Prompt(id=prompt_id, name=name, default_version=version) | ||
| prompt_version = PromptVersion( | ||
| prompt_id=prompt_id, version=version, template=template, variables=variables | ||
| ) | ||
|
|
||
| session.add(prompt) | ||
| session.add(prompt_version) | ||
| session.commit() | ||
| logger.debug("Created prompt id=%s with version %s", prompt_id, version) | ||
| session.refresh(prompt_version) | ||
|
|
||
| return prompt_version | ||
|
|
||
|
|
||
| def get_prompt(prompt_id: str, version: Optional[int] = None) -> PromptVersion: | ||
| """Retrieve a prompt version by identifier. | ||
|
|
||
| When version is None the prompt's default version is used. | ||
|
|
||
| Parameters: | ||
| prompt_id: The prompt identifier. | ||
| version: Specific version number, or None for the default. | ||
|
|
||
| Returns: | ||
| PromptVersion: The matching version row. | ||
|
|
||
| Raises: | ||
| PromptNotFoundError: If the prompt or requested version does not exist. | ||
|
|
||
| """ | ||
| with get_session() as session: | ||
|
|
||
| # Grab default version from Prompt if version is not provided | ||
| if version is None: | ||
| prompt = session.query(Prompt).filter_by(id=prompt_id).first() | ||
| if prompt is None: | ||
| logger.debug("Prompt id=%s not found", prompt_id) | ||
| raise PromptNotFoundError(f"Prompt id={prompt_id} not found") | ||
|
|
||
| version = prompt.default_version | ||
|
|
||
| prompt_version = ( | ||
| session.query(PromptVersion) | ||
| .filter_by(prompt_id=prompt_id, version=version) | ||
| .first() | ||
| ) | ||
|
|
||
| if prompt_version is None: | ||
| logger.debug("Version %s of prompt id=%s not found", version, prompt_id) | ||
| raise PromptNotFoundError( | ||
| f"Version {version} of prompt id={prompt_id} not found" | ||
| ) | ||
|
|
||
| return prompt_version | ||
|
|
||
|
|
||
| def list_prompts() -> list[Prompt]: | ||
| """List all prompts in the catalog. | ||
|
|
||
| Returns: | ||
| list[Prompt]: All prompt identity rows. | ||
|
|
||
| """ | ||
| with get_session() as session: | ||
| prompts = session.query(Prompt).all() | ||
| return prompts | ||
|
|
||
|
|
||
| def update_prompt( | ||
| prompt_id: str, template: str, variables: list[str], *, default: bool = False | ||
| ) -> PromptVersion: | ||
| """Create a new immutable version for an existing prompt. | ||
|
|
||
| Parameters: | ||
| prompt_id: The prompt identifier. | ||
| template: Mustache template text for the new version. | ||
| variables: Declared variable names for the template. | ||
| default: If True, set the new version as the prompt's default. | ||
|
|
||
| Returns: | ||
| PromptVersion: The newly created version row. | ||
|
|
||
| Raises: | ||
| PromptNotFoundError: If the prompt does not exist. | ||
| PromptValidationError: If variables contain duplicates. | ||
|
|
||
| """ | ||
| # TODO: validate template # pylint: disable=fixme | ||
|
|
||
| _validate_variables(variables) | ||
|
|
||
| with get_session() as session: | ||
| max_prompt_version = ( | ||
| session.query(PromptVersion) | ||
| .filter_by(prompt_id=prompt_id) | ||
| .order_by(PromptVersion.version.desc()) | ||
| .first() | ||
| ) | ||
| if max_prompt_version is None: | ||
| logger.debug("No available version found for prompt id=%s", prompt_id) | ||
| raise PromptNotFoundError( | ||
| f"No available version found for prompt id={prompt_id}" | ||
| ) | ||
|
|
||
| max_version = max_prompt_version.version | ||
|
|
||
| prompt_version = PromptVersion( | ||
| prompt_id=prompt_id, | ||
| version=max_version + 1, | ||
|
asimurka marked this conversation as resolved.
Resolved
|
||
| template=template, | ||
| variables=variables, | ||
| ) | ||
| session.add(prompt_version) | ||
| logger.debug( | ||
| "Created version %s for prompt id=%s", prompt_version.version, prompt_id | ||
| ) | ||
|
|
||
| if default: | ||
| prompt = session.query(Prompt).filter_by(id=prompt_id).first() | ||
| if prompt is None: | ||
| logger.debug("Prompt id=%s not found", prompt_id) | ||
| raise PromptNotFoundError(f"Prompt id={prompt_id} not found") | ||
|
|
||
| prompt.default_version = prompt_version.version | ||
|
|
||
| session.commit() | ||
| session.refresh(prompt_version) | ||
|
|
||
| return prompt_version | ||
|
|
||
|
|
||
| def delete_prompt(prompt_id: str) -> None: | ||
| """Delete a prompt and all its versions. | ||
|
|
||
| Parameters: | ||
| prompt_id: The prompt identifier. | ||
|
|
||
| Raises: | ||
| PromptNotFoundError: If the prompt does not exist. | ||
|
|
||
| """ | ||
| with get_session() as session: | ||
| prompt = session.query(Prompt).filter_by(id=prompt_id).first() | ||
| if prompt is None: | ||
| raise PromptNotFoundError(f"Prompt id={prompt_id} not found") | ||
|
|
||
| # This cover the situation when FK enforcement is not on | ||
| session.query(PromptVersion).filter_by(prompt_id=prompt_id).delete() | ||
| session.delete(prompt) | ||
| session.commit() | ||
| logger.debug("Deleted prompt id=%s", prompt_id) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Unit tests for the prompts module.""" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.