-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathapp.py
More file actions
226 lines (183 loc) · 8.44 KB
/
app.py
File metadata and controls
226 lines (183 loc) · 8.44 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os
import gc
import tempfile
import uuid
import pandas as pd
from typing import Optional, Dict, Any
import logging
from gitingest import ingest
from llama_index.core import Settings, PromptTemplate, VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.node_parser import MarkdownNodeParser
import streamlit as st
from dotenv import load_dotenv
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
load_dotenv()
# Constants
MAX_REPO_SIZE = 100 * 1024 * 1024 # 100MB
SUPPORTED_REPO_TYPES = ['.py', '.md', '.ipynb', '.js', '.ts', '.json']
class GitHubRAGError(Exception):
"""Custom exception for GitHub RAG application errors"""
pass
def validate_github_url(url: str) -> bool:
"""Validate GitHub repository URL"""
return url.startswith(('https://github.com/', 'http://github.com/'))
def get_repo_name(url: str) -> str:
"""Extract repository name from URL"""
try:
return url.split('/')[-1].replace('.git', '')
except Exception as e:
raise GitHubRAGError(f"Invalid repository URL: {str(e)}")
def reset_chat():
"""Reset chat session and clean up resources"""
try:
st.session_state.messages = []
st.session_state.context = None
gc.collect()
logger.info("Chat session reset successfully")
except Exception as e:
logger.error(f"Error resetting chat: {str(e)}")
raise GitHubRAGError("Failed to reset chat session")
def process_with_gitingets(github_url: str) -> tuple:
"""Process GitHub repository using gitingest"""
try:
summary, tree, content = ingest(github_url)
if not all([summary, tree, content]):
raise GitHubRAGError("Failed to process repository: Missing data")
return summary, tree, content
except Exception as e:
logger.error(f"Error processing repository: {str(e)}")
raise GitHubRAGError(f"Failed to process repository: {str(e)}")
def create_query_engine(content_path: str, repo_name: str) -> Any:
"""Create and configure query engine"""
try:
loader = SimpleDirectoryReader(input_dir=content_path)
docs = loader.load_data()
node_parser = MarkdownNodeParser()
index = VectorStoreIndex.from_documents(
documents=docs,
transformations=[node_parser],
show_progress=True
)
qa_prompt_tmpl_str = """
You are an AI assistant specialized in analyzing GitHub repositories.
Repository structure:
{tree}
---------------------
Context information from the repository:
{context_str}
---------------------
Given the repository structure and context above, provide a clear and precise answer to the query.
Focus on the repository's content, code structure, and implementation details.
If the information is not available in the context, respond with 'I don't have enough information about that aspect of the repository.'
Query: {query_str}
Answer: """
qa_prompt_tmpl = PromptTemplate(qa_prompt_tmpl_str)
query_engine = index.as_query_engine(streaming=True)
query_engine.update_prompts(
{"response_synthesizer:text_qa_template": qa_prompt_tmpl}
)
return query_engine
except Exception as e:
logger.error(f"Error creating query engine: {str(e)}")
raise GitHubRAGError(f"Failed to create query engine: {str(e)}")
# Initialize session state
if "id" not in st.session_state:
st.session_state.id = uuid.uuid4()
st.session_state.file_cache = {}
st.session_state.messages = []
session_id = st.session_state.id
# Sidebar
with st.sidebar:
st.header("Add your GitHub repository!")
github_url = st.text_input(
"Enter GitHub repository URL",
placeholder="https://github.com/username/repo",
help="Enter a valid GitHub repository URL"
)
load_repo = st.button("Load Repository", type="primary")
if github_url and load_repo:
try:
# Validate URL
if not validate_github_url(github_url):
st.error("Please enter a valid GitHub repository URL")
st.stop()
repo_name = get_repo_name(github_url)
file_key = f"{session_id}-{repo_name}"
if file_key not in st.session_state.file_cache:
with st.spinner("Processing your repository..."):
with tempfile.TemporaryDirectory() as temp_dir:
try:
summary, tree, content = process_with_gitingets(github_url)
# Write content to temporary file
content_path = os.path.join(temp_dir, f"{repo_name}_content.md")
with open(content_path, "w", encoding="utf-8") as f:
f.write(content)
# Create and cache query engine
query_engine = create_query_engine(temp_dir, repo_name)
st.session_state.file_cache[file_key] = query_engine
st.success("Repository loaded successfully! Ready to chat.")
logger.info(f"Successfully processed repository: {repo_name}")
except GitHubRAGError as e:
st.error(str(e))
logger.error(f"Error processing repository {repo_name}: {str(e)}")
st.stop()
except Exception as e:
st.error("An unexpected error occurred while processing the repository")
logger.error(f"Unexpected error: {str(e)}")
st.stop()
else:
st.info("Repository already loaded. Ready to chat!")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
logger.error(f"Error in repository loading process: {str(e)}")
st.stop()
# Main content
col1, col2 = st.columns([6, 1])
with col1:
st.header("Chat with GitHub using RAG </>")
with col2:
st.button("Clear Chat ↺", on_click=reset_chat, help="Clear chat history and reset session")
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("What's up?"):
try:
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Process and display assistant response
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
try:
repo_name = get_repo_name(github_url)
file_key = f"{session_id}-{repo_name}"
query_engine = st.session_state.file_cache.get(file_key)
if query_engine is None:
raise GitHubRAGError("Please load a repository first!")
response = query_engine.query(prompt)
if hasattr(response, 'response_gen'):
for chunk in response.response_gen:
if isinstance(chunk, str):
full_response += chunk
message_placeholder.markdown(full_response + "▌")
else:
full_response = str(response)
message_placeholder.markdown(full_response)
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
except GitHubRAGError as e:
st.error(str(e))
logger.error(f"Error in chat processing: {str(e)}")
except Exception as e:
st.error("An unexpected error occurred while processing your query")
logger.error(f"Unexpected error in chat: {str(e)}")
except Exception as e:
st.error("An error occurred in the chat system")
logger.error(f"Chat system error: {str(e)}")