|
5 | 5 | from string import Template |
6 | 6 | import time |
7 | 7 | import yaml |
8 | | -import openai |
| 8 | +from openai import OpenAI |
9 | 9 | from dotenv import load_dotenv |
10 | 10 |
|
11 | 11 | load_dotenv() |
12 | 12 |
|
13 | 13 | openai_api_key = os.getenv("OPENAI_API_KEY") |
| 14 | +openai_model = os.getenv("OPENAI_MODEL") |
14 | 15 |
|
15 | 16 | if not openai_api_key: |
16 | | - raise ValueError("OpenAI API key not found. Please set it in the .env file.") |
17 | | - |
18 | | -openai.api_key = openai_api_key |
| 17 | + logging.warning("OpenAI API key not found. Skipping processing prompt.") |
19 | 18 |
|
20 | 19 | class FileItem: |
21 | 20 | def __init__(self, properties): |
22 | 21 | self.name = properties.get("name") |
23 | 22 | self.content = properties.get("content") |
24 | 23 | self.remote_location = properties.get("file") |
25 | 24 | self.permissions = properties.get("permissions") |
26 | | - self.prompt = properties.get("prompt") |
27 | | - |
28 | | - def process_prompt(self): |
29 | | - if self.prompt: |
30 | | - logging.debug(f"Processing prompt: {self.prompt}") |
31 | | - response = openai.Completion.create( |
32 | | - engine="davinci", |
33 | | - prompt=self.prompt, |
34 | | - max_tokens=100 |
| 25 | + |
| 26 | + self.system_prompt = properties.get("system_prompt") |
| 27 | + self.user_prompt = properties.get("user_prompt") |
| 28 | + self.openai_client = OpenAI( |
| 29 | + api_key=openai_api_key |
| 30 | + ) |
| 31 | + |
| 32 | + if not openai_model: |
| 33 | + logging.info("OpenAI model not found. Using default model.") |
| 34 | + self.openai_model = "gpt-3.5-turbo" |
| 35 | + else: |
| 36 | + logging.debug(f"Using OpenAI model: {openai_model}") |
| 37 | + self.openai_model = openai_model |
| 38 | + |
| 39 | + def process_prompt(self, dry_run=False): |
| 40 | + if self.user_prompt: |
| 41 | + logging.debug(f"Using user prompt: {self.user_prompt}") |
| 42 | + |
| 43 | + if not openai_api_key: |
| 44 | + logging.warning("Skipping processing prompt as OpenAI API key is not set.") |
| 45 | + return |
| 46 | + |
| 47 | + if not self.system_prompt: |
| 48 | + system_prompt = "You are a software developer working on a project. You need to create a file with the following content:" |
| 49 | + else: |
| 50 | + system_prompt = self.system_prompt |
| 51 | + |
| 52 | + if dry_run: |
| 53 | + logging.info("[DRY RUN] Would generate content using OpenAI API.") |
| 54 | + self.content = "[DRY RUN] Generating content using OpenAI" |
| 55 | + return |
| 56 | + |
| 57 | + completion = self.openai_client.chat.completions.create( |
| 58 | + model=self.openai_model, |
| 59 | + messages=[ |
| 60 | + {"role": "system", "content": system_prompt}, |
| 61 | + {"role": "user", "content": self.user_prompt} |
| 62 | + ] |
35 | 63 | ) |
36 | | - self.content = response.choices[0].text |
| 64 | + |
| 65 | + self.content = completion.choices[0].message.content |
37 | 66 | logging.debug(f"Generated content: {self.content}") |
38 | 67 |
|
39 | 68 | def fetch_content(self): |
@@ -94,8 +123,8 @@ def validate_configuration(structure): |
94 | 123 | raise ValueError("Each name in the 'structure' item must be a string.") |
95 | 124 | if isinstance(content, dict): |
96 | 125 | # Check that any of the keys 'content', 'file' or 'prompt' is present |
97 | | - if 'content' not in content and 'file' not in content and 'prompt' not in content: |
98 | | - raise ValueError(f"Dictionary item '{name}' must contain either 'content' or 'file' or 'prompt' key.") |
| 126 | + if 'content' not in content and 'file' not in content and 'user_prompt' not in content: |
| 127 | + raise ValueError(f"Dictionary item '{name}' must contain either 'content' or 'file' or 'user_prompt' key.") |
99 | 128 | # Check if 'file' key is present and its value is a string |
100 | 129 | if 'file' in content and not isinstance(content['file'], str): |
101 | 130 | raise ValueError(f"The 'file' value for '{name}' must be a string.") |
@@ -125,7 +154,7 @@ def create_structure(base_path, structure, dry_run=False, template_vars=None, ba |
125 | 154 | file_item = FileItem({"name": name, "content": content}) |
126 | 155 |
|
127 | 156 | file_item.apply_template_variables(template_vars) |
128 | | - file_item.process_prompt() |
| 157 | + file_item.process_prompt(dry_run) |
129 | 158 | file_item.create(base_path, dry_run, backup_path, file_strategy) |
130 | 159 |
|
131 | 160 | def main(): |
|
0 commit comments