-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmain.py
69 lines (58 loc) · 2.85 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#main.py
import os
import tempfile
from dotenv import load_dotenv
from langchain import PromptTemplate, LLMChain
from langchain.llms import OpenAI
from config import WHITE, GREEN, RESET_COLOR, model_name
from utils import format_user_question
from file_processing import clone_github_repo, load_and_index_files
from questions import ask_question, QuestionContext
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
def main():
github_url = input("Enter the GitHub URL of the repository: ")
repo_name = github_url.split("/")[-1]
print("Cloning the repository...")
with tempfile.TemporaryDirectory() as local_path:
if clone_github_repo(github_url, local_path):
index, documents, file_type_counts, filenames = load_and_index_files(local_path)
if index is None:
print("No documents were found to index. Exiting.")
exit()
print("Repository cloned. Indexing files...")
llm = OpenAI(api_key=OPENAI_API_KEY, temperature=0.2)
template = """
Repo: {repo_name} ({github_url}) | Conv: {conversation_history} | Docs: {numbered_documents} | Q: {question} | FileCount: {file_type_counts} | FileNames: {filenames}
Instr:
1. Answer based on context/docs.
2. Focus on repo/code.
3. Consider:
a. Purpose/features - describe.
b. Functions/code - provide details/samples.
c. Setup/usage - give instructions.
4. Unsure? Say "I am not sure".
Answer:
"""
prompt = PromptTemplate(
template=template,
input_variables=["repo_name", "github_url", "conversation_history", "question", "numbered_documents", "file_type_counts", "filenames"]
)
llm_chain = LLMChain(prompt=prompt, llm=llm)
conversation_history = ""
question_context = QuestionContext(index, documents, llm_chain, model_name, repo_name, github_url, conversation_history, file_type_counts, filenames)
while True:
try:
user_question = input("\n" + WHITE + "Ask a question about the repository (type 'exit()' to quit): " + RESET_COLOR)
if user_question.lower() == "exit()":
break
print('Thinking...')
user_question = format_user_question(user_question)
answer = ask_question(user_question, question_context)
print(GREEN + '\nANSWER\n' + answer + RESET_COLOR + '\n')
conversation_history += f"Question: {user_question}\nAnswer: {answer}\n"
except Exception as e:
print(f"An error occurred: {e}")
break
else:
print("Failed to clone the repository.")