Skip to content

Commit

Permalink
breaking up files
Browse files Browse the repository at this point in the history
  • Loading branch information
rsivilli committed Oct 4, 2024
1 parent 8055ba2 commit f4bdc1b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
38 changes: 38 additions & 0 deletions lazy_dev_ai/files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

import importlib
from pathlib import Path
from string import Template

# Load default base template for use from an internal resource file
with importlib.resources.open_text("lazy_dev_ai.templates", "default_base.txt") as file:
default_template = Template(file.read())

def load_template(file: str | Path|None =None) -> Template:
# Use the default template if no specific file is given
if file is None:
return default_template
# Ensures the file path is valid and can be read
file_path = Path(file)
if not file_path.exists():
raise FileNotFoundError(f"The file {file_path} does not exist.")

with file_path.open("r", encoding="utf-8") as f:
template_content = f.read()

return Template(template_content)


def load_file(file:str | Path) -> str:
file_path = Path(file)
if not file_path.exists():
raise FileNotFoundError(f"The file {file_path} does not exist.")
with file_path.open("r", encoding="utf-8") as f:
content = f.read()
return content


def write_file(file:str|Path, contents:str):
file_path = Path(file)
with file_path.open("w", encoding="utf-8") as f:
f.write(contents)

36 changes: 1 addition & 35 deletions lazy_dev_ai/llm.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from lazy_dev_ai.files import load_file,load_template,write_file
from openai import OpenAI
from pydantic import BaseModel, Field, ConfigDict, ValidationError
from pathlib import Path
from string import Template
from enum import Enum
import importlib.resources

default_template=None
client = None

# Load default base template for use from an internal resource file
with importlib.resources.open_text("lazy_dev_ai.templates", "default_base.txt") as file:
default_template = Template(file.read())

class OpenAIRole(str, Enum):
SYSTEM = "system"
Expand Down Expand Up @@ -48,36 +44,6 @@ def getClient(api_key:str=None, organization:str=None, project:str=None)->OpenAI
return client


def load_template(file: str | Path|None =None) -> Template:
# Use the default template if no specific file is given
if file is None:
return default_template
# Ensures the file path is valid and can be read
file_path = Path(file)
if not file_path.exists():
raise FileNotFoundError(f"The file {file_path} does not exist.")

with file_path.open("r", encoding="utf-8") as f:
template_content = f.read()

return Template(template_content)


def load_file(file:str | Path) -> str:
file_path = Path(file)
if not file_path.exists():
raise FileNotFoundError(f"The file {file_path} does not exist.")
with file_path.open("r", encoding="utf-8") as f:
content = f.read()
return content


def write_file(file:str|Path, contents:str):
file_path = Path(file)
with file_path.open("w", encoding="utf-8") as f:
f.write(contents)


def apply_code_template(code_file:str|Path,prompt_file:str|Path=None,prompt:str=None,model: str = "gpt-4-turbo",template_file:str|Path=None,max_retries:int = 3)->CodeChangeResponse:
# Ensure a valid prompt is provided before proceeding
if prompt is None and prompt_file is None:
Expand Down

0 comments on commit f4bdc1b

Please sign in to comment.