Skip to content

Commit

Permalink
Merge pull request #2 from shankarpandala/feature/dev
Browse files Browse the repository at this point in the history
Add new files and dependencies
  • Loading branch information
shankarpandala authored Nov 26, 2023
2 parents 9e684fd + cbccd49 commit 7654f86
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 140 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

*.pkl
*.bin
*.ipynb
16 changes: 16 additions & 0 deletions folderstructure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
from pathlib import Path

def create_folder_structure_md(path, indent=0):
markdown = ""
path = Path(path)
if path.is_file() and not path.name.startswith('.') and path.name != 'folderstructure.py':
markdown += " " * indent + "- " + path.name + "\n"
elif path.is_dir() and not path.name.startswith('.') and path.name != 'folderstructure.py':
markdown += " " * indent + "- " + path.name + "/\n"
for child in path.iterdir():
markdown += create_folder_structure_md(child, indent + 1)
return markdown

# Replace 'your_directory_path' with the path of the directory you want to convert to markdown
print(create_folder_structure_md(os.getcwd()))
1 change: 1 addition & 0 deletions lazygitgpt/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .cli_agent import generate_response
30 changes: 30 additions & 0 deletions lazygitgpt/agents/cli_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json
from langchain.prompts import ChatPromptTemplate
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
import re

from lazygitgpt.llms import chat_model
from lazygitgpt.datasources.repos import read_repository_contents
from lazygitgpt.git.operations import update_files

output_schema = ResponseSchema(name='filename', description='contents', type='string')
output_parser = StructuredOutputParser(response_schemas=[output_schema])
format_instructions = output_parser.get_format_instructions()
template_string = """You are an expert programmer.
You are reviewing a code repository.
Read the code and make changes to the code as per the user requirements.
user requirements: {user_requirements}
code repository: {code_repository}
Output the contents of the file that you changed as per the format instructions : {format_instructions}
"""

def generate_response(prompt, sources=read_repository_contents()):
sources_str = json.dumps(sources, indent=4)
prompt_template = ChatPromptTemplate.from_template(template_string)
messages = prompt_template.format_messages(user_requirements = prompt,
code_repository = sources_str,
format_instructions=format_instructions)
response = chat_model(messages)
response_json = response.to_json()
data = response_json['kwargs']['content']
return data
87 changes: 0 additions & 87 deletions lazygitgpt/ai_operations.py

This file was deleted.

4 changes: 2 additions & 2 deletions lazygitgpt/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3

import click
from lazygitgpt.git_operations import clone_repository, checkout_branch, create_branch
from lazygitgpt.ai_operations import generate_response
from .git.operations import clone_repository, checkout_branch, create_branch
from .agents.cli_agent import generate_response

@click.group()
def cli():
Expand Down
1 change: 1 addition & 0 deletions lazygitgpt/datasources/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .repos import read_repository_contents
26 changes: 26 additions & 0 deletions lazygitgpt/datasources/repos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
import glob
import json

def read_repository_contents(directory_path=os.getcwd(), file_pattern="*"):
"""
Reads all files in the specified directory matching the file pattern,
and creates a JSON object with file names and their contents.
Args:
directory_path (str): Path to the directory containing the files.
file_pattern (str): Pattern to match files. Defaults to '*' (all files).
Returns:
str: A JSON string containing the file names and their contents.
"""
data = {}
for file_path in glob.glob(f"{directory_path}/{file_pattern}"):
if os.path.isfile(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
data[file_path] = file.read()
except Exception as e:
print(f"Error reading file: {file_path} - {e}")

return json.dumps(data, indent=4)
1 change: 1 addition & 0 deletions lazygitgpt/git/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .operations import update_files, clone_repository, checkout_branch, create_branch
113 changes: 63 additions & 50 deletions lazygitgpt/git_operations.py → lazygitgpt/git/operations.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,64 @@
import os
from git import Repo, GitCommandError

def clone_repository(repo_url):
"""
Clones a git repository from the given URL to the current working directory.
:param repo_url: URL of the repository to clone.
"""
try:
# Get the name of the repository by parsing the URL
repo_name = os.path.basename(repo_url)
# If the URL ends with '.git', remove it
if repo_name.endswith('.git'):
repo_name = repo_name[:-4]
# Use the current working directory as the destination
destination = os.path.join(os.getcwd(), repo_name)
Repo.clone_from(repo_url, destination)
print(f"Repository cloned successfully to {destination}")
except GitCommandError as e:
print(f"Error cloning repository: {e}")

def checkout_branch(branch_name):
"""
Checks out a specified branch in the current working directory.
:param branch_name: Name of the branch to checkout.
"""
try:
# Use the current working directory as the repository path
repo_path = os.getcwd()
repo = Repo(repo_path)
repo.git.checkout(branch_name)
print(f"Checked out to branch {branch_name}")
except GitCommandError as e:
print(f"Error checking out branch: {e}")

def create_branch(branch_name):
"""
Creates a new branch in the current working directory.
:param branch_name: Name of the branch to create.
"""
try:
# Use the current working directory as the repository path
repo_path = os.getcwd()
repo = Repo(repo_path)
repo.git.checkout('-b', branch_name)
print(f"Created and checked out to new branch {branch_name}")
except GitCommandError as e:
import os
from git import Repo, GitCommandError

def update_files(response):
try:
for filename, contents in response.items():
file_path = os.path.join(os.getcwd(), filename)

with open(file_path, 'w', encoding='utf-8') as file:
file.write(contents)

print(f"Updated file: {filename}")

except Exception as e:
print(f"Error updating files: {e}")

def clone_repository(repo_url):
"""
Clones a git repository from the given URL to the current working directory.
:param repo_url: URL of the repository to clone.
"""
try:
# Get the name of the repository by parsing the URL
repo_name = os.path.basename(repo_url)
# If the URL ends with '.git', remove it
if repo_name.endswith('.git'):
repo_name = repo_name[:-4]
# Use the current working directory as the destination
destination = os.path.join(os.getcwd(), repo_name)
Repo.clone_from(repo_url, destination)
print(f"Repository cloned successfully to {destination}")
except GitCommandError as e:
print(f"Error cloning repository: {e}")

def checkout_branch(branch_name):
"""
Checks out a specified branch in the current working directory.
:param branch_name: Name of the branch to checkout.
"""
try:
# Use the current working directory as the repository path
repo_path = os.getcwd()
repo = Repo(repo_path)
repo.git.checkout(branch_name)
print(f"Checked out to branch {branch_name}")
except GitCommandError as e:
print(f"Error checking out branch: {e}")

def create_branch(branch_name):
"""
Creates a new branch in the current working directory.
:param branch_name: Name of the branch to create.
"""
try:
# Use the current working directory as the repository path
repo_path = os.getcwd()
repo = Repo(repo_path)
repo.git.checkout('-b', branch_name)
print(f"Created and checked out to new branch {branch_name}")
except GitCommandError as e:
print(f"Error creating new branch: {e}")
1 change: 1 addition & 0 deletions lazygitgpt/llms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .openai import chat_model
3 changes: 3 additions & 0 deletions lazygitgpt/llms/openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from langchain.chat_models import ChatOpenAI

chat_model = ChatOpenAI(model="gpt-4-1106-preview", temperature=0.0)
Empty file added lazygitgpt/memory/__init__.py
Empty file.
Empty file added lazygitgpt/parsers/__init__.py
Empty file.
Empty file added lazygitgpt/prompts/__init__.py
Empty file.
Empty file.
Empty file added lazygitgpt/utils/__init__.py
Empty file.
Empty file.
6 changes: 5 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
gitpython>=3.1.0
openai>=0.10.0
click
langchain
langchain
duckduckgo-search
llmx
cohere
tiktoken

0 comments on commit 7654f86

Please sign in to comment.