-
Notifications
You must be signed in to change notification settings - Fork 3
Create 14_templated_instructions.py #49
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
046d250
Create 13_templated_instructions.py
3dc3793
rename to example 14
b5573a7
Update README.md
6d45fbe
Merge branch 'main' into pierre-example-instruction-variables
933fd8d
Merge branch 'main' into pierre-example-instruction-variables
9333d9c
fix: lint replace List
guillaq 2e92bd6
feat: add code field to template output for better context
ee673ac
Merge branch 'main' into pierre-example-instruction-variables
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 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,180 @@ | ||
| """ | ||
| This example demonstrates how to use templated instructions that adapt based on input variables. | ||
| The template variables are automatically populated from the input model's fields. | ||
|
|
||
| The templating uses Jinja2 syntax (server-side rendering): | ||
| - {{ variable }} for variable substitution | ||
| - {% if condition %} ... {% endif %} for conditionals | ||
| - {% for item in items %} ... {% endfor %} for loops | ||
| - {{ loop.index }} for loop indices | ||
|
|
||
| For full Jinja2 template syntax documentation, see: | ||
| https://jinja.palletsprojects.com/en/stable/ | ||
|
|
||
| It showcases: | ||
| 1. Simple variable substitution | ||
| 2. Conditional logic | ||
| 3. Loops | ||
| 4. Nested conditionals | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
| import workflowai | ||
| from workflowai import Model, Run | ||
|
|
||
|
|
||
| class CodeReviewInput(BaseModel): | ||
| """Input model for the code review agent.""" | ||
|
|
||
| language: str = Field( | ||
| description="The programming language of the code to review", | ||
| examples=["python", "javascript", "typescript"], | ||
| ) | ||
| code: str = Field( | ||
| description="The code to review", | ||
| ) | ||
| style_guide: str = Field( | ||
| description="The style guide to follow", | ||
| examples=["PEP 8", "Google Style", "Airbnb"], | ||
| ) | ||
| is_production: bool = Field( | ||
| description="Whether this is a production code review", | ||
| default=False, | ||
| ) | ||
| required_checks: list[str] = Field( | ||
| description="List of specific checks to perform", | ||
| default=["code style", "performance", "maintainability"], | ||
| ) | ||
| security_level: str = Field( | ||
| description="Required security level", | ||
| default="standard", | ||
| examples=["standard", "high"], | ||
| ) | ||
|
|
||
|
|
||
| class CodeReviewOutput(BaseModel): | ||
| """Output model containing the code review results.""" | ||
|
|
||
| overall_assessment: str = Field( | ||
| description="Overall assessment of the code quality", | ||
| ) | ||
| style_violations: list[str] = Field( | ||
| description="List of style guide violations", | ||
| ) | ||
| security_issues: list[str] = Field( | ||
| description="List of security concerns", | ||
| default_factory=list, | ||
| ) | ||
| suggested_improvements: list[str] = Field( | ||
| description="List of suggested improvements", | ||
| ) | ||
|
|
||
|
|
||
| @workflowai.agent( | ||
| id="templated-code-reviewer", | ||
| model=Model.CLAUDE_3_5_SONNET_LATEST, | ||
| ) | ||
| async def review_code(review_input: CodeReviewInput) -> Run[CodeReviewOutput]: | ||
| """ | ||
| Review code based on specified parameters and guidelines. | ||
|
|
||
| You are a code reviewer for {{ language }} code. | ||
| Please review the code according to the {{ style_guide }} style guide. | ||
|
|
||
| {% if is_production %} | ||
| This is a PRODUCTION code review - please be extra thorough and strict about best practices. | ||
| {% else %} | ||
| This is a development code review - focus on maintainability and clarity. | ||
| {% endif %} | ||
|
|
||
| Required checks to perform: | ||
| {% for check in required_checks %}{{ loop.index }}. {{ check }} | ||
| {% endfor %} | ||
|
|
||
| {% if security_level == "high" %} | ||
| Additional security requirements: | ||
| - Must follow secure coding practices | ||
| - Check for potential security vulnerabilities | ||
| - Ensure all inputs are properly sanitized | ||
| {% endif %} | ||
|
|
||
| Guidelines: | ||
| 1. Check for adherence to {{ style_guide }} conventions | ||
| 2. Look for potential bugs and performance issues | ||
| 3. Suggest improvements while keeping the {{ language }} best practices in mind | ||
|
|
||
| {% if language == "python" %} | ||
| Python-specific checks: | ||
| - Type hints usage | ||
| - PEP 8 compliance | ||
| - Docstring format | ||
| {% elif language == "javascript" or language == "typescript" %} | ||
| JavaScript/TypeScript-specific checks: | ||
| - ESLint rules compliance | ||
| - Modern ES6+ features usage | ||
| - Browser compatibility | ||
| {% endif %} | ||
|
|
||
| Please analyze the following code and provide: | ||
| 1. An overall assessment | ||
| 2. Style guide violations | ||
| 3. Security issues (if any) | ||
| 4. Suggested improvements | ||
|
|
||
| The code is: | ||
| {{ code }} | ||
| """ | ||
| ... | ||
|
|
||
|
|
||
| async def main(): | ||
| # Example 1: Python code review for development | ||
| print("\nExample 1: Python Development Code Review") | ||
| print("-" * 50) | ||
| python_code = """ | ||
| def calculate_sum(numbers): | ||
| result = 0 | ||
| for n in numbers: | ||
| result = result + n | ||
| return result | ||
| """ | ||
|
|
||
| run = await review_code( | ||
| CodeReviewInput( | ||
| language="python", | ||
| code=python_code, | ||
| style_guide="PEP 8", | ||
| required_checks=["type hints", "docstring", "performance"], | ||
| ), | ||
| ) | ||
| print(run) | ||
|
|
||
| # Example 2: TypeScript production code with high security | ||
| print("\nExample 2: TypeScript Production Code Review (High Security)") | ||
| print("-" * 50) | ||
| typescript_code = """ | ||
| function processUserData(data: any) { | ||
| const userId = data.id; | ||
| const query = `SELECT * FROM users WHERE id = ${userId}`; | ||
| return executeQuery(query); | ||
| } | ||
| """ | ||
|
|
||
| run = await review_code( | ||
| CodeReviewInput( | ||
| language="typescript", | ||
| code=typescript_code, | ||
| style_guide="Airbnb", | ||
| is_production=True, | ||
| security_level="high", | ||
| required_checks=["security", "type safety", "SQL injection"], | ||
| ), | ||
| ) | ||
| print(run) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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.