-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporag.py
168 lines (140 loc) · 5.72 KB
/
reporag.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from reporag.reporag.main import main
from dotenv import load_dotenv
import os
import sys
from typing import Dict, Any, Optional
def validate_repo_url(url: str) -> bool:
"""Validate the repository URL format."""
parts = url.split('/')
return len(parts) == 2 and all(part.strip() for part in parts)
def get_env_or_input(env_var: str, prompt: str) -> str:
"""Get value from environment variable or user input."""
value = os.getenv(env_var)
if not value:
value = input(prompt).strip()
return value
def get_user_input() -> Dict[str, Any]:
"""Get and validate user input for repository processing."""
print("\n=== Repoprompter Interactive Mode ===\n")
# Get and validate repository URL
while True:
repo_url = input("Enter the GitHub repository URL (format: owner/repo): ").strip()
if validate_repo_url(repo_url):
break
print("Invalid repository format. Please use 'owner/repo' format.")
# Get tokens
github_token = get_env_or_input(
'GITHUB_ACCESS_TOKEN',
"GitHub access token not found in .env. Please enter your token: "
)
# Get output file name (now required for all modes)
while True:
output_file = input("\nEnter the output file name for the prompt (e.g., output_prompt.txt): ").strip()
if output_file:
break
print("Output file name cannot be empty.")
# Choose mode with input validation
while True:
print("\nAvailable modes:")
print("1. Generate prompt file only")
print("2. Generate prompt file and start RAG mode")
mode = input("Enter your choice (1/2): ").strip()
if mode in ['1', '2']:
break
print("Invalid choice. Please enter 1 or 2.")
result = {
'repo_url': repo_url,
'github_token': github_token,
'output_file': output_file,
'mode': 'prompt' if mode == '1' else 'rag'
}
if mode == '2':
result['groq_token'] = get_env_or_input(
'GROQ_API_KEY',
"Groq API key not found in .env. Please enter your key: "
)
return result
def interactive_rag_session(rag_instance: Any) -> None:
"""Run an interactive RAG session with improved error handling."""
print("\n=== RAG Interactive Mode ===")
print("Available commands:")
print("- exit: Exit the RAG session")
print("- help: Show this help message")
print("- clear: Clear the conversation history")
print("- Any other input will be treated as a question about the repository\n")
while True:
try:
question = input("\nQuestion: ").strip()
if not question:
print("Please enter a question or command.")
continue
command = question.lower()
if command == 'exit':
print("Exiting RAG session...")
rag_instance.clear_vector_store() # Clear the vector store
rag_instance.clear_cache() # Clear the cache
break
elif command == 'help':
print("\nAvailable commands:")
print("- exit: Exit the RAG session")
print("- help: Show this help message")
print("- clear: Clear the conversation history")
print("- Any other input will be treated as a question about the repository")
continue
elif command == 'clear':
rag_instance.clear_memory()
print("Conversation history cleared.")
continue
print("\nProcessing your question...")
answer = rag_instance.query(question)
print("\nAnswer:", answer)
except KeyboardInterrupt:
print("\nExiting RAG session...")
rag_instance.clear_vector_store() # Clear the vector store
rag_instance.clear_cache() # Clear the cache
break
except Exception as e:
print(f"\nError processing question: {str(e)}")
print("Please try another question or type 'exit' to quit.")
def main_interactive() -> None:
"""Main interactive function with improved error handling."""
try:
# Load environment variables
load_dotenv(override=True)
# Get user inputs
inputs = get_user_input()
print("\nInitializing...")
if inputs['mode'] == 'prompt':
# Generate prompt file only
result = main(
repo_url=inputs['repo_url'],
access_token=inputs['github_token'],
output_file=inputs['output_file']
)
if isinstance(result, str) and result.startswith("Error"):
raise Exception(result)
print(f"\nPrompt successfully generated and saved to {inputs['output_file']}")
else:
# Generate prompt file and initialize RAG
result = main(
repo_url=inputs['repo_url'],
access_token=inputs['github_token'],
groq_api_key=inputs['groq_token'],
output_file=inputs['output_file'],
rag_mode=True
)
if isinstance(result, str) and result.startswith("Error"):
raise Exception(result)
print(f"\nPrompt file generated and saved to {inputs['output_file']}")
print("RAG system initialized successfully.")
# Start interactive RAG session
interactive_rag_session(result)
except KeyboardInterrupt:
print("\nOperation cancelled by user.")
sys.exit(0)
except Exception as e:
print(f"\nError: {str(e)}")
print("Please check your inputs and try again.")
sys.exit(1)
if __name__ == "__main__":
main_interactive()