Skip to content

Commit b575f0c

Browse files
committed
chore: Update Docker configuration files and workflows
1 parent c6ad396 commit b575f0c

7 files changed

Lines changed: 59 additions & 143 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
__pycache__
33
*.egg-info
44
.env
5+
*.log

docker-compose.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
version: '3.8'
1+
name: struct
22

33
services:
44
struct:
55
build: .
66
volumes:
77
- .:/app
88
entrypoint: ["python", "struct_module/main.py"]
9+
env_file:
10+
- .env
911
command: [
1012
"--log=DEBUG",
1113
"--dry-run",
1214
"--vars=project_name=MyProject,author_name=JohnDoe",
1315
"--backup=/app/backup",
1416
"--file-strategy=rename",
1517
"--log-file=/app/logfile.log",
16-
"/app/project_structure.yaml",
17-
"/app/project"
18+
"/app/example/structure.yaml",
19+
"/app/example_project"
1820
]

example/structure.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ structure:
1111
- LICENSE:
1212
file: https://raw.githubusercontent.com/nishanths/license/master/LICENSE
1313
- .gitignore:
14-
prompt: |
14+
user_prompt: |
1515
Generate the content for a .gitignore file that is appropriate for a Python project. Include common directories and file types that should be ignored.

struct_module/main.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,64 @@
55
from string import Template
66
import time
77
import yaml
8-
import openai
8+
from openai import OpenAI
99
from dotenv import load_dotenv
1010

1111
load_dotenv()
1212

1313
openai_api_key = os.getenv("OPENAI_API_KEY")
14+
openai_model = os.getenv("OPENAI_MODEL")
1415

1516
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.")
1918

2019
class FileItem:
2120
def __init__(self, properties):
2221
self.name = properties.get("name")
2322
self.content = properties.get("content")
2423
self.remote_location = properties.get("file")
2524
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+
]
3563
)
36-
self.content = response.choices[0].text
64+
65+
self.content = completion.choices[0].message.content
3766
logging.debug(f"Generated content: {self.content}")
3867

3968
def fetch_content(self):
@@ -94,8 +123,8 @@ def validate_configuration(structure):
94123
raise ValueError("Each name in the 'structure' item must be a string.")
95124
if isinstance(content, dict):
96125
# 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.")
99128
# Check if 'file' key is present and its value is a string
100129
if 'file' in content and not isinstance(content['file'], str):
101130
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
125154
file_item = FileItem({"name": name, "content": content})
126155

127156
file_item.apply_template_variables(template_vars)
128-
file_item.process_prompt()
157+
file_item.process_prompt(dry_run)
129158
file_item.create(base_path, dry_run, backup_path, file_strategy)
130159

131160
def main():

tests/__init__.py

Whitespace-only changes.

tests/test_prompt.py

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

tests/test_script.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
from unittest.mock import patch, MagicMock
99
from struct_module.main import FileItem, validate_configuration, create_structure
1010

11+
# Mock the environment variables for OpenAI
12+
@pytest.fixture(autouse=True)
13+
def mock_env_vars(monkeypatch):
14+
monkeypatch.setenv("OPENAI_API_KEY", "test-api-key")
15+
monkeypatch.setenv("OPENAI_MODEL", "gpt-3.5-turbo")
16+
1117
# Test for FileItem.fetch_content
1218
@patch('struct_module.requests.get')
1319
def test_fetch_remote_content(mock_get):

0 commit comments

Comments
 (0)