-
Notifications
You must be signed in to change notification settings - Fork 115
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(prompts): add util for variable name extraction #1046
Conversation
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.
Disclaimer: Experimental PR review
PR Summary
Introduced a new TemplateParser class to centralize template parsing logic and added variable name extraction functionality for both text and chat prompts.
- Added
TemplateParser
class inlangfuse/model.py
withfind_variable_names()
andcompile_template()
methods for robust template handling - Added
variable_names()
method toTextPromptClient
returningList[str]
of template variables - Added
variable_names()
method toChatPromptClient
returningList[ChatMessageVariables]
with role-specific variables - Added comprehensive test suite in
tests/test_prompt_compilation.py
covering edge cases like whitespace, special characters and JSON content
3 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile
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.
Disclaimer: Experimental PR review
PR Summary
(updates since last review)
Refactored variable name extraction in prompt clients by converting variable_names()
method to a variables
property and simplified chat message variable handling to return a flat list.
- Changed
variable_names()
tovariables
property inTextPromptClient
andChatPromptClient
for better API design - Simplified chat message variable extraction to return
List[str]
instead of role-based grouping - Fixed mutable default argument issue in
TemplateParser.compile_template()
- Added handling for
None
data parameter incompile_template
2 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
@rhighs - I have adjusted the Do you have a strong opinion on this? If so, could you explain exactly why you'd benefit from that? |
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.
Disclaimer: Experimental PR review
PR Summary
(updates since last review)
Based on the recent changes, I'll provide a concise summary focusing only on the latest updates not covered in previous reviews:
Simplified template parsing implementation and improved test coverage for the TemplateParser class.
- Added extensive test cases in
test_prompt_compilation.py
for unescaped JSON handling and complex template scenarios - Improved
TemplateParser.compile_template()
to handle unescaped JSON content without character escaping - Added test coverage for edge cases like
content
as variable name and empty string replacements - Added test cases for variable case sensitivity and JSON template handling
The changes focus on improving robustness and test coverage of the template parsing functionality, particularly around JSON content handling.
2 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
def _get_langchain_prompt_string(content: str): | ||
return re.sub(r"{{\s*(\w+)\s*}}", r"{\g<1>}", content) |
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.
logic: regex pattern \w+ only matches word chars - may need to support other valid variable name chars
LGTM, no strong opinions tbh |
Important
Introduces
TemplateParser
for template variable extraction and compilation, replacing existing methods inBasePromptClient
, and adds tests for new functionality.TemplateParser
class inmodel.py
for extracting and compiling template variables.BasePromptClient._compile_template_string
withTemplateParser.compile_template
inTextPromptClient
andChatPromptClient
.variable_names()
method toTextPromptClient
andChatPromptClient
to extract variable names from templates.test_prompt.py
andtest_prompt_compilation.py
forTemplateParser
methodscompile_template
andfind_variable_names
.This description was created by
for 81e9006. It will automatically update as commits are pushed.