Skip to content
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

(feat) Support Preserving Variable Values #115

Merged
merged 5 commits into from
Mar 21, 2024

Conversation

mwootendev
Copy link
Contributor

Makes a minor change to the VariableAssignmentCommand to include a new boolean preserve property, which is set by placing a ? before the = in the assignment.

Updating the SamplingContext to test for the new preserve property before processing the assignment. The value being assigned is only associated with the variable if the variable did not already exist in the context. Without the preserve property, the value will always be assigned.

Example Wildcards:

vartest:
  prompt:
    - '${subject?=!{man|woman}} ${weather=!{sun|rain}} ${drink?=!{__vartest/drink__}} a ${subject} standing in the ${weather} drinking ${drink}'
  drink:
    - coffee
    - tea
  winter:
    - '${weather=snow} ${drink=hot chocolate} __vartest/prompt__'

Example Prompts:

${subject=boy} __vartest/prompt__
a boy standing in the rain drinking tea

${subject=cowboy} ${weather=sun} ${drink=sasparilla} __vartest/prompt__
a cowboy standing in the sun drinking sasparilla

__vartest/winter__
a woman standing in the snow drinking hot chocolate

${subject=boy} ${weather=rain} ${drink=iced tea} __vartest/winter__
a boy standing in the snow drinking hot chocolate

The functionality is similar to default values for variables, but will allow the variable values to propagate to other nested prompts and retain some consistency.

Updates the variable assignment command to support preserving the value
of a variable if one is already declared. The new syntax adds an
optional `?` preceding the `=` to indicate that the assignment should
only occur if the variable does not have a value.
@codecov-commenter
Copy link

codecov-commenter commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e377980) 97.43% compared to head (8e74296) 97.44%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #115   +/-   ##
=======================================
  Coverage   97.43%   97.44%           
=======================================
  Files          77       77           
  Lines        3358     3369   +11     
=======================================
+ Hits         3272     3283   +11     
  Misses         86       86           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@akx akx self-requested a review December 21, 2023 07:52
Copy link
Collaborator

@akx akx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Beyond the inline comments, could you also update the documentation to include this feature?

Comment on lines 393 to 401
@pytest.mark.parametrize(
("immediate", "preserve"),
[
(False, False),
(False, True),
(True, False),
(True, True),
],
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have the same combinatorial effect, plus nicer test IDs:

Suggested change
@pytest.mark.parametrize(
("immediate", "preserve"),
[
(False, False),
(False, True),
(True, False),
(True, True),
],
)
@pytest.mark.parametrize("immediate", (False, True), ids=("delayed", "immediate"))
@pytest.mark.parametrize("preserve", (False, True), ids=("not-preserved", "preserved"))

src/dynamicprompts/commands/variable_commands.py Outdated Show resolved Hide resolved
@mwootendev
Copy link
Contributor Author

Thanks!

Beyond the inline comments, could you also update the documentation to include this feature?

Where should this be added? It looks like all of the variable functionality is actually documented in the sd-dynamic-prompts project.

Renamed the `preserve` property of `VariableAssignmentCommand` to
overwrite, changed its default to True, and reversed how it was used
that existing values are now preserved when it is not set.
@akx
Copy link
Collaborator

akx commented Dec 29, 2023

Where should this be added? It looks like all of the variable functionality is actually documented in the sd-dynamic-prompts project.

Heck, that's a good question. I opened (EDIT: and merged) #117 and adieyal/sd-dynamic-prompts#701 to improve and sync the docs between the two repos, so please rebase and add the new stuff into docs/SYNTAX.md :)

@mwootendev
Copy link
Contributor Author

Where should this be added? It looks like all of the variable functionality is actually documented in the sd-dynamic-prompts project.

Heck, that's a good question. I opened (EDIT: and merged) #117 and adieyal/sd-dynamic-prompts#701 to improve and sync the docs between the two repos, so please rebase and add the new stuff into docs/SYNTAX.md :)

I updated the pull request with a new section in SYNTAX.md

@mwootendev
Copy link
Contributor Author

@akx @adieyal are there additional changes needed?

@mwootendev mwootendev requested a review from akx January 9, 2024 14:20
Copy link
Collaborator

@akx akx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. So sorry for the review delay!

@akx akx merged commit 05455df into adieyal:main Mar 21, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants