Skip to content

Commit 12e2658

Browse files
authored
Enhance prompt processing with existing file content support (#43)
### Overview This PR improves the prompt-based file generation functionality by allowing the OpenAI-assisted file generation process to consider and modify existing file content when applicable. ### Changes - **Added** support for passing existing file content to the OpenAI prompt within `file_item.py`. - **Updated** default OpenAI model to `gpt-4.1` from `gpt-3.5-turbo`. - **Modified** `generate.py` to read existing file content and pass it to the `process_prompt()` method. - **Created** a new example configuration file `example/gpt.yaml` demonstrating usage with user prompts. ### Justification This change provides more intelligent and context-aware file generation by allowing existing content to be passed into the prompt. It helps to enhance productivity and avoid overwriting relevant content unintentionally. ### Impact - Improves integration with OpenAI by enabling content-aware generation. - Prevents accidental data loss when regenerating files. - Demonstrates usage with an example YAML file, aiding users in understanding configuration.
1 parent cf8f980 commit 12e2658

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

example/gpt.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
files:
2+
- .github/workflows/run_struct.yaml:
3+
user_prompt: |
4+
make sure that token is set on secrets and value is TOKEN
5+
make sure that password is set on secrets and value is PASSWORD

struct_module/commands/generate.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,19 @@ def _create_structure(self, args):
9090
}
9191
)
9292

93+
# Determine the full file path
94+
file_path_to_create = os.path.join(args.base_path, name)
95+
existing_content = None
96+
if os.path.exists(file_path_to_create):
97+
self.logger.info(f"File already exists: {file_path_to_create}")
98+
with open(file_path_to_create, 'r') as existing_file:
99+
existing_content = existing_file.read()
100+
101+
file_item.process_prompt(
102+
args.dry_run,
103+
existing_content=existing_content
104+
)
93105
file_item.apply_template_variables(template_vars)
94-
file_item.process_prompt(args.dry_run)
95106

96107
file_item.create(
97108
args.base_path,

struct_module/file_item.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,16 @@ def _configure_openai(self):
4747
self.openai_client = OpenAI(api_key=openai_api_key)
4848
if not openai_model:
4949
self.logger.debug("OpenAI model not found. Using default model.")
50-
self.openai_model = "gpt-3.5-turbo"
50+
self.openai_model = "gpt-4.1"
5151
else:
5252
self.logger.debug(f"Using OpenAI model: {openai_model}")
5353
self.openai_model = openai_model
5454

5555
def _get_file_directory(self):
5656
return os.path.dirname(self.name)
5757

58-
def process_prompt(self, dry_run=False):
58+
def process_prompt(self, dry_run=False, existing_content=None):
5959
if self.user_prompt:
60-
self.logger.debug(f"Using user prompt: {self.user_prompt}")
61-
6260
if not self.openai_client or not openai_api_key:
6361
self.logger.warning("Skipping processing prompt as OpenAI API key is not set.")
6462
return
@@ -68,17 +66,25 @@ def process_prompt(self, dry_run=False):
6866
else:
6967
system_prompt = self.system_prompt
7068

69+
# If existing_content is provided, append it to the user prompt
70+
user_prompt = self.user_prompt
71+
if existing_content:
72+
user_prompt += f"\n\nCurrent file content (if any):\n```\n{existing_content}\n```\n\nPlease modify existing content so that it meets the new requirements. Your output should be plain text, without any code blocks or formatting. Do not include any explanations or comments. Just provide the final content of the file."
73+
74+
self.logger.debug(f"Using system prompt: {system_prompt}")
75+
self.logger.debug(f"Using user prompt: {user_prompt}")
76+
7177
if dry_run:
7278
self.logger.info("[DRY RUN] Would generate content using OpenAI API.")
7379
self.content = "[DRY RUN] Generating content using OpenAI"
7480
return
7581

76-
if not self.openai_client or not openai_api_key:
82+
if self.openai_client and openai_api_key:
7783
completion = self.openai_client.chat.completions.create(
7884
model=self.openai_model,
7985
messages=[
8086
{"role": "system", "content": system_prompt},
81-
{"role": "user", "content": self.user_prompt}
87+
{"role": "user", "content": user_prompt}
8288
]
8389
)
8490

0 commit comments

Comments
 (0)