-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Prompt role blocks debug output + new prompt implementation example #578
Merged
slundberg
merged 7 commits into
guidance-ai:main
from
cpcdoy:feature/debug_llm_role_blocks
Jan 11, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9b58f8d
Add: Usage of new debug output for easier prompt implementation
cpcdoy 02689d0
Add: debug parameter with a nice centered orange text display
cpcdoy 69acbbb
Add: nicer block color + formatting
cpcdoy 2184837
Add: render latest debug color
cpcdoy b08ee82
Merge branch 'main' into feature/debug_llm_role_blocks
slundberg 4584bb2
Convert the syntax to use with blocks, and fix some context block ord…
slundberg 47b3339
Switch to using attribute instead of variable to track indentation
slundberg 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
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,37 +1,95 @@ | ||
import guidance | ||
from ._block import block | ||
from ._set_attribute import set_attribute | ||
|
||
nodisp_start = "<||_#NODISP_||>" | ||
nodisp_end = "<||_/NODISP_||>" | ||
span_start = "<||_html:<span style='background-color: rgba(255, 180, 0, 0.3); border-radius: 3px;'>_||>" | ||
span_end = "<||_html:</span>_||>" | ||
|
||
|
||
@guidance | ||
def role_opener(lm, role_name, **kwargs): | ||
indent = getattr(lm, "indent_roles", True) | ||
if not hasattr(lm, "get_role_start"): | ||
raise Exception(f"You need to use a chat model in order the use role blocks like `with {role_name}():`! Perhaps you meant to use the {type(lm).__name__}Chat class?") | ||
lm += f"<||_html:<div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>{role_name.lower()}</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>_||>" | ||
lm += "<||_#NODISP_||>" + lm.get_role_start(role_name, **kwargs) + "<||_/NODISP_||>" | ||
raise Exception( | ||
f"You need to use a chat model in order the use role blocks like `with {role_name}():`! Perhaps you meant to use the {type(lm).__name__}Chat class?" | ||
) | ||
|
||
# Block start container (centers elements) | ||
if indent: | ||
lm += f"<||_html:<div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); justify-content: center; align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>{role_name.lower()}</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>_||>" | ||
|
||
# Start of either debug or HTML no disp block | ||
if indent: | ||
lm += nodisp_start | ||
else: | ||
lm += span_start | ||
|
||
lm += lm.get_role_start(role_name, **kwargs) | ||
|
||
# End of either debug or HTML no disp block | ||
if indent: | ||
lm += nodisp_end | ||
else: | ||
lm += span_end | ||
|
||
return lm | ||
|
||
|
||
@guidance | ||
def role_closer(lm, role_name, **kwargs): | ||
lm += "<||_html:</div></div>_||>" + "<||_#NODISP_||>" + lm.get_role_end(role_name) + "<||_/NODISP_||>" | ||
indent = getattr(lm, "indent_roles", True) | ||
# Start of either debug or HTML no disp block | ||
if indent: | ||
lm += nodisp_start | ||
else: | ||
lm += span_start | ||
|
||
lm += lm.get_role_end(role_name) | ||
|
||
# End of either debug or HTML no disp block | ||
if indent: | ||
lm += nodisp_end | ||
else: | ||
lm += span_end | ||
|
||
# End of top container | ||
if indent: | ||
lm += "<||_html:</div></div>_||>" | ||
|
||
return lm | ||
|
||
|
||
def role(role_name, text=None, **kwargs): | ||
if text is None: | ||
return block(opener=role_opener(role_name, **kwargs), closer=role_closer(role_name, **kwargs)) | ||
return block( | ||
opener=role_opener(role_name, **kwargs), | ||
closer=role_closer(role_name, **kwargs), | ||
) | ||
else: | ||
assert False | ||
#return self.append(open_text + text + close_text) | ||
# return self.append(open_text + text + close_text) | ||
|
||
|
||
def system(text=None, **kwargs): | ||
return role("system", text, **kwargs) | ||
|
||
|
||
def user(text=None, **kwargs): | ||
return role("user", text, **kwargs) | ||
|
||
|
||
def assistant(text=None, **kwargs): | ||
return role("assistant", text, **kwargs) | ||
|
||
|
||
def function(text=None, **kwargs): | ||
return role("function", text, **kwargs) | ||
|
||
|
||
def instruction(text=None, **kwargs): | ||
return role("instruction", text, **kwargs) | ||
return role("instruction", text, **kwargs) | ||
|
||
def indent_roles(indent=True): | ||
return set_attribute("indent_roles", indent) |
This file contains 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,21 @@ | ||
import guidance | ||
from ._block import block | ||
|
||
@guidance | ||
def set_attr_opener(lm, name, value): | ||
if hasattr(lm, name): | ||
lm = lm.setattr("__save" + name, getattr(lm, name)) | ||
return lm.setattr(name, value) | ||
|
||
@guidance | ||
def set_attr_closer(lm, name): | ||
if hasattr(lm, "__save" + name): | ||
return lm.setattr(name, lm["__save" + name]).delattr("__save" + name) | ||
else: | ||
return lm.delattr(name) | ||
|
||
def set_attribute(name, value=True): | ||
return block( | ||
opener=set_attr_opener(name, value), | ||
closer=set_attr_closer(name), | ||
) |
This file contains 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,21 @@ | ||
import guidance | ||
from ._block import block | ||
|
||
@guidance | ||
def set_opener(lm, name, value): | ||
if name in lm: | ||
lm = lm.set("__save" + name, lm[name]) | ||
return lm.set(name, value) | ||
|
||
@guidance | ||
def set_closer(lm, name): | ||
if "__save" + name in lm: | ||
return lm.set(name, lm["__save" + name]).remove("__save" + name) | ||
else: | ||
return lm.remove(name) | ||
|
||
def set_var(name, value=True): | ||
return block( | ||
opener=set_opener(name, value), | ||
closer=set_closer(name), | ||
) |
This file contains 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from ._llama import Llama, LlamaChat | ||
from ._llama import Llama, LlamaChat | ||
from ._transformers import Transformers, TransformersChat |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you implemented this approach to set the same values as in the
set_attribute
method you did for blocks, but I don't see it used anywhere for now. Is this something you're planning to use later on?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I just checked your commit history and you were using variables instead of attributes for the blocks before, so maybe a forgotten file, unless you think it's still useful for other purposes?