Skip to content

Commit c0345eb

Browse files
authored
Added patchflow to generate usage example (#1034)
* added patchflow to generate usage example * fix: changed name of patchflow
1 parent 48b21fb commit c0345eb

File tree

8 files changed

+126
-12
lines changed

8 files changed

+126
-12
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ on:
1313
- generatereadme-*
1414
- generatedocstring-*
1515
- generateunittests-*
16+
- generatecodeusageexample-*
1617
- resolveissue-*
18+
1719
- demo*
1820

1921
# Credits to https://blog.maximeheckel.com/posts/building-perfect-github-action-frontend-teams/#you-are-terminated
@@ -231,6 +233,14 @@ jobs:
231233
--github_api_key=${{ secrets.SCM_GITHUB_KEY }} \
232234
--folder_path=tests/cicd/generate_docstring \
233235
--disable_telemetry
236+
237+
- name: Generate Code Usage Example
238+
run: |
239+
poetry run patchwork GenerateCodeUsageExample --log debug \
240+
--patched_api_key=${{ secrets.PATCHED_API_KEY }} \
241+
--github_api_key=${{ secrets.SCM_GITHUB_KEY }} \
242+
--folder_path=tests/cicd/generate_docstring \
243+
--disable_telemetry
234244
235245
- name: Generate README
236246
run: |
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import json
2+
from pathlib import Path
3+
import yaml
4+
5+
from patchwork.common.utils.step_typing import validate_steps_with_inputs
6+
from patchwork.step import Step
7+
from patchwork.steps import (
8+
LLM,
9+
CallCode2Prompt,
10+
ModifyCode,
11+
PR
12+
)
13+
14+
_DEFAULT_INPUT_FILE = Path(__file__).parent / "defaults.yml"
15+
_DEFAULT_PROMPT_JSON = Path(__file__).parent / "default_prompt.json"
16+
17+
class GenerateCodeUsageExample(Step):
18+
def __init__(self, inputs):
19+
super().__init__(inputs)
20+
21+
final_inputs = yaml.safe_load(_DEFAULT_INPUT_FILE.read_text())
22+
if final_inputs is None:
23+
final_inputs = {}
24+
25+
final_inputs.update(inputs)
26+
27+
final_inputs["prompt_id"] = "GenerateUsageExample"
28+
if "folder_path" not in final_inputs.keys():
29+
final_inputs["folder_path"] = Path.cwd()
30+
else:
31+
final_inputs["folder_path"] = Path(final_inputs["folder_path"])
32+
33+
if "prompt_template_file" not in final_inputs:
34+
final_inputs["prompt_template_file"] = _DEFAULT_PROMPT_JSON
35+
36+
final_inputs["pr_title"] = f"PatchWork Usage Example generated"
37+
final_inputs["branch_prefix"] = f"{self.__class__.__name__.lower()}-"
38+
39+
validate_steps_with_inputs(
40+
set(final_inputs.keys()).union({"prompt_values","files_to_patch"}), LLM, CallCode2Prompt,ModifyCode,PR
41+
)
42+
self.inputs = final_inputs
43+
44+
def run(self):
45+
outputs = CallCode2Prompt(self.inputs).run()
46+
new_file_name = f"usage_example.{self.inputs['test_file_extension']}"
47+
new_file_path = Path(outputs['uri']).with_name(new_file_name)
48+
Path(outputs['uri']).rename(new_file_path)
49+
outputs['uri'] = str(new_file_path)
50+
self.inputs["response_partitions"] = {"patch": ["```", "\n", "```"]}
51+
self.inputs["files_to_patch"] = self.inputs["prompt_values"] = [outputs]
52+
outputs = LLM(self.inputs).run()
53+
self.inputs.update(outputs)
54+
outputs = ModifyCode(self.inputs).run()
55+
self.inputs.update(outputs)
56+
number = len(self.inputs["modified_code_files"])
57+
self.inputs["pr_header"] = f"This pull request adds usage example."
58+
outputs = PR(self.inputs).run()
59+
self.inputs.update(outputs)
60+
61+
return self.inputs
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Contents of GenerateUsageExample Code
2+
3+
### Inputs
4+
- The code reads default inputs from a YAML file (`defaults.yml`) and a JSON file (`default_prompt.json`).
5+
- The code takes user inputs and updates the default inputs accordingly.
6+
- The code expects inputs like `folder_path`, `prompt_template_file`, `test_file_extension`, etc.
7+
8+
### Outputs
9+
- The code generates a usage example based on the provided inputs.
10+
- It utilizes other steps like `CallCode2Prompt`, `ModifyCode`, and `PR` to create and process the example.
11+
- The final output includes modified code files and information for creating a pull request.

patchwork/patchflows/GenerateCodeUsageExample/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{
3+
"id": "GenerateUsageExample",
4+
"prompts": [
5+
{
6+
"role": "system",
7+
"content": "You are a skilled technical writer specializing in creating concise, clear, and runnable usage examples for libraries, frameworks, and projects. Users will provide a description of the code or project, and you will generate examples that demonstrate practical use cases. Ensure examples are well-commented and runnable, with no extra explanation or formatting outside of the example code itself."
8+
},
9+
{
10+
"role": "user",
11+
"content": "Description: {{fullContent}}"
12+
}
13+
]
14+
}
15+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# CallLLM Inputs
2+
# openai_api_key: required-for-chatgpt
3+
# google_api_key: required-for-gemini
4+
# model: gpt-4o
5+
# client_base_url: https://api.openai.com/v1
6+
# Example HF model
7+
# client_base_url: https://api-inference.huggingface.co/models/codellama/CodeLlama-70b-Instruct-hf/v1
8+
# model: codellama/CodeLlama-70b-Instruct-hf
9+
# model_temperature: 0.2
10+
# model_top_p: 0.95
11+
# model_max_tokens: 2000
12+
13+
# folder_path : path/to/folder/with/class
14+
15+
# Default value
16+
test_file_extension : py
17+
18+
# CommitChanges Inputs
19+
disable_branch: false
20+
21+
# CreatePR Inputs
22+
disable_pr: false
23+
force_pr_creation: true
24+
# github_api_key: required-for-github-scm
25+
# gitlab_api_key: required-for-gitlab-scm

patchwork/patchflows/GenerateUnitTests/README.md

Lines changed: 0 additions & 12 deletions
This file was deleted.

patchwork/patchflows/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from .GenerateUnitTests.GenerateUnitTests import GenerateUnitTests
77
from .PRReview.PRReview import PRReview
88
from .ResolveIssue.ResolveIssue import ResolveIssue
9+
from .GenerateUnitTests.GenerateUnitTests import GenerateUnitTests
10+
from .GenerateCodeUsageExample.GenerateCodeUsageExample import GenerateCodeUsageExample
911
from .SonarFix.SonarFix import SonarFix
1012

1113
__all__ = [
@@ -17,5 +19,7 @@
1719
"GenerateDocstring",
1820
"GenerateUnitTests",
1921
"GenerateDiagram",
22+
"GenerateCodeUsageExample",
2023
"SonarFix",
2124
]
25+

0 commit comments

Comments
 (0)