Skip to content

Commit 73cbd36

Browse files
Merge pull request #8 from shankarpandala/feature/dev
Feature/dev
2 parents a87bd93 + 26606c1 commit 73cbd36

File tree

7 files changed

+78
-48
lines changed

7 files changed

+78
-48
lines changed

lazygitgpt/agents/cli_agent.py

+38-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
1-
import json
2-
from langchain.prompts import ChatPromptTemplate
3-
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
4-
import re
5-
1+
from langchain.chains import ConversationalRetrievalChain
2+
from langchain.agents import Tool
3+
from langchain.tools import DuckDuckGoSearchRun
4+
from langchain.agents import initialize_agent
65
from lazygitgpt.llms import chat_model
76
from lazygitgpt.datasources.repos import read_repository_contents
87
from lazygitgpt.git.operations import update_files
8+
from lazygitgpt.retrievers.retrievalqa import retriever
9+
from lazygitgpt.memory.memory import memory
10+
11+
search = DuckDuckGoSearchRun()
12+
13+
def generate_response(prompt):
14+
inputs = {'chat_history': '', 'question': prompt}
15+
qa = ConversationalRetrievalChain.from_llm(chat_model, retriever=retriever, memory=memory)
16+
result = qa(inputs)
17+
return result["answer"]
18+
19+
# tools = [
20+
# Tool(
21+
# name='DuckDuckGo Search',
22+
# func= search.run,
23+
# description="Useful for when you need to do a search on the internet to find information that another tool can't find. be specific with your input."
24+
# ),
25+
# Tool(
26+
# name='Conversational Retrieval',
27+
# func=generate_response,
28+
# description="This is Conversational Retrieval chain which has content of the entire repository."
29+
# )
30+
# ]
931

10-
output_schema = ResponseSchema(name='filename', description='contents', type='string')
11-
output_parser = StructuredOutputParser(response_schemas=[output_schema])
12-
format_instructions = output_parser.get_format_instructions()
13-
template_string = """You are an expert programmer.
14-
You are reviewing a code repository.
15-
Read the code and make changes to the code as per the user requirements.
16-
user requirements: {user_requirements}
17-
code repository: {code_repository}
18-
Output the contents of the file that you changed as per the format instructions : {format_instructions}
19-
"""
32+
# zero_shot_agent = initialize_agent(
33+
# agent="zero-shot-react-description",
34+
# tools=tools,
35+
# llm=chat_model,
36+
# verbose=True,
37+
# max_iterations=30,
38+
# retriever=retriever
39+
# )
2040

21-
def generate_response(prompt, sources=read_repository_contents()):
22-
sources_str = json.dumps(sources, indent=4)
23-
prompt_template = ChatPromptTemplate.from_template(template_string)
24-
messages = prompt_template.format_messages(user_requirements = prompt,
25-
code_repository = sources_str,
26-
format_instructions=format_instructions)
27-
response = chat_model(messages)
28-
response_json = response.to_json()
29-
data = response_json['kwargs']['content']
30-
return data
41+
# def run(prompt):
42+
# reponse = zero_shot_agent.run(prompt)
43+
# return reponse

lazygitgpt/cli.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env python3
2-
32
import click
43
from .git.operations import clone_repository, checkout_branch, create_branch
5-
from .agents.cli_agent import generate_response
4+
from .agents.cli_agent import generate_response#, run
65

76
@click.group()
87
def cli():

lazygitgpt/datasources/repos.py

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import os
22
import glob
33
import json
4+
from git import Repo
5+
from langchain.document_loaders import GitLoader
6+
from langchain.document_loaders.generic import GenericLoader
7+
from langchain.document_loaders.parsers import LanguageParser
8+
from langchain.text_splitter import Language
9+
from langchain.text_splitter import RecursiveCharacterTextSplitter
410

5-
def read_repository_contents(directory_path=os.getcwd(), file_pattern="*"):
6-
"""
7-
Reads all files in the specified directory matching the file pattern,
8-
and creates a JSON object with file names and their contents.
11+
def read_repository_contents():
12+
repo_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13+
Repo.git_dir=repo_path
14+
repo = Repo(Repo.git_dir)
15+
branch = repo.head.reference
916

10-
Args:
11-
directory_path (str): Path to the directory containing the files.
12-
file_pattern (str): Pattern to match files. Defaults to '*' (all files).
13-
14-
Returns:
15-
str: A JSON string containing the file names and their contents.
16-
"""
17-
data = {}
18-
for file_path in glob.glob(f"{directory_path}/{file_pattern}"):
19-
if os.path.isfile(file_path):
20-
try:
21-
with open(file_path, 'r', encoding='utf-8') as file:
22-
data[file_path] = file.read()
23-
except Exception as e:
24-
print(f"Error reading file: {file_path} - {e}")
25-
26-
return json.dumps(data, indent=4)
17+
loader = GitLoader(repo_path, branch=branch)
18+
docs = loader.load()
19+
loader = GenericLoader.from_filesystem(
20+
repo_path,
21+
glob="**/*",
22+
suffixes=[".py"],
23+
parser=LanguageParser(language=Language.PYTHON, parser_threshold=500),
24+
)
25+
documents = loader.load()
26+
python_splitter = RecursiveCharacterTextSplitter.from_language(
27+
language=Language.PYTHON, chunk_size=2000, chunk_overlap=200
28+
)
29+
texts = python_splitter.split_documents(documents)
30+
return texts

lazygitgpt/memory/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .memory import memory

lazygitgpt/memory/memory.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from langchain.memory import ConversationBufferMemory
2+
3+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
4+

lazygitgpt/retrievers/retrievalqa.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from langchain.embeddings.openai import OpenAIEmbeddings
2+
from langchain.vectorstores import Chroma
3+
from lazygitgpt.datasources.repos import read_repository_contents
4+
5+
db = Chroma.from_documents(read_repository_contents(), OpenAIEmbeddings(disallowed_special=()))
6+
retriever = db.as_retriever(
7+
search_type="mmr", # Also test "similarity"
8+
search_kwargs={"k": 1000},
9+
)

lazygitgpt/vectorstores/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)