-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqa_interface.py
More file actions
270 lines (218 loc) Β· 9.31 KB
/
qa_interface.py
File metadata and controls
270 lines (218 loc) Β· 9.31 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import sys
import os
from typing import List, Optional
import logging
from retriever.use_rag_index import query_code, index_code
import json
import requests
from dotenv import load_dotenv
# load environment variables
load_dotenv()
logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger(__name__)
class CodeQAInterface:
"""Interactive Q&A system for code quality queries using Anthropic Claude."""
def __init__(self):
"""Initialize the Q&A interface with Anthropic Claude."""
self.api_key = os.getenv("ANTHROPIC_API_KEY")
self.model_name = os.getenv("ANTHROPIC_MODEL", "claude-3-haiku-20240307")
self.context_chunks = 5 # Number of code chunks to retrieve
self.api_base = "https://api.anthropic.com/v1/messages"
if not self.api_key:
logger.warning("ANTHROPIC_API_KEY not found in environment. Please set it in .env file.")
else:
self._test_anthropic_connection()
def _test_anthropic_connection(self):
"""Test if Anthropic API key is valid."""
try:
# test with a simple message using requests
response = self._make_api_call("Hi", max_tokens=10)
if response:
logger.info(f"β
Anthropic Claude ({self.model_name}) is ready!")
else:
logger.error("β Failed to connect to Anthropic API")
except Exception as e:
logger.error(f"β Error testing Anthropic connection: {e}")
def _make_api_call(self, prompt: str, max_tokens: int = 2048) -> Optional[str]:
"""Make a direct API call to Anthropic using requests."""
if not self.api_key:
return None
headers = {
"anthropic-version": "2023-06-01",
"content-type": "application/json",
"x-api-key": self.api_key
}
data = {
"model": self.model_name,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": max_tokens,
"temperature": 0.2
}
try:
# make direct HTTP request without any proxy
response = requests.post(
self.api_base,
headers=headers,
json=data,
timeout=30,
proxies={} # Empty dict to explicitly avoid proxy
)
if response.status_code == 200:
result = response.json()
return result['content'][0]['text']
else:
logger.error(f"API error: {response.status_code} - {response.text}")
return None
except Exception as e:
logger.error(f"Request failed: {e}")
return None
def _query_claude(self, prompt: str) -> Optional[str]:
"""
Query Claude with the given prompt.
Args:
prompt: The prompt to send to Claude
Returns:
Claude's response or None if error
"""
if not self.api_key:
return self._placeholder_llm_response(prompt)
response = self._make_api_call(prompt)
if response:
return response
else:
return self._placeholder_llm_response(prompt)
def _placeholder_llm_response(self, prompt: str) -> str:
"""
Placeholder response when Claude is not available.
Args:
prompt: The prompt (for context)
Returns:
A placeholder response
"""
return (
"π Analysis Summary:\n"
"Based on the retrieved code chunks, here are the key insights:\n"
"β’ The code implements the requested functionality\n"
"β’ Consider reviewing the implementation for best practices\n"
"β’ Additional context may be needed for a more detailed analysis\n\n"
"Note: This is a placeholder response. Set ANTHROPIC_API_KEY in .env for actual Claude analysis."
)
def _format_prompt(self, question: str, code_chunks: List[str]) -> str:
"""
Format the prompt for Claude with question and relevant code.
Args:
question: The user's question
code_chunks: Relevant code chunks from the retriever
Returns:
Formatted prompt string
"""
prompt = f"""You are a senior software engineer analyzing code. Answer the following question based on the provided code chunks.
Question: {question}
Relevant Code Chunks:
"""
for i, chunk in enumerate(code_chunks, 1):
prompt += f"\n--- Code Chunk {i} ---\n{chunk}\n"
prompt += "\nProvide a clear, concise answer focusing on the question asked. Include specific code references when relevant."
return prompt
def answer_question(self, question: str) -> str:
"""
Process a question and return an answer using embeddings and Claude.
Args:
question: The user's question about the code
Returns:
The answer string
"""
# retrieve relevant code chunks
logger.info("π Searching for relevant code...")
code_chunks = query_code(question, k=self.context_chunks)
if not code_chunks:
return "β No relevant code chunks found. Please ensure code has been indexed."
logger.info(f"β
Found {len(code_chunks)} relevant code chunks")
# format prompt and query LLM
prompt = self._format_prompt(question, code_chunks)
logger.info("π€ Analyzing code with Claude and generating answer...")
answer = self._query_claude(prompt)
if answer:
return answer
else:
return "β Failed to generate answer. Please check Claude API connection."
def run_interactive_session(self):
"""Run the interactive Q&A session."""
print("\n" + "="*60)
print("π Code Quality Intelligence Agent - Interactive Q&A")
print(" Powered by Anthropic Claude")
print("="*60)
print("\nCommands:")
print(" β’ Type your question about the code")
print(" β’ Type 'quit' or 'exit' to end the session")
print(" β’ Type 'help' for more information")
print("\n" + "-"*60 + "\n")
while True:
try:
# get user input
question = input("β Your question: ").strip()
# check for exit commands
if question.lower() in ['quit', 'exit', 'q']:
print("\nπ Thank you for using Code Quality Q&A. Goodbye!")
break
# check for help command
if question.lower() == 'help':
self._show_help()
continue
# skip empty questions
if not question:
continue
# process the question
print("\n" + "-"*60)
answer = self.answer_question(question)
print("\nπ‘ Answer:")
print(answer)
print("\n" + "-"*60 + "\n")
except KeyboardInterrupt:
print("\n\nπ Session interrupted. Goodbye!")
break
except Exception as e:
logger.error(f"Error processing question: {e}")
print("β An error occurred. Please try again.")
def _show_help(self):
"""Display help information."""
help_text = """
π Help Information
==================
This interactive Q&A system allows you to ask questions about your codebase.
The system uses semantic search to find relevant code chunks and then uses
Claude to generate comprehensive answers.
Example Questions:
β’ "How does the authentication system work?"
β’ "What are the main security vulnerabilities in this code?"
β’ "Explain the data flow in the payment module"
β’ "What design patterns are used in this codebase?"
β’ "How is error handling implemented?"
Tips:
β’ Be specific in your questions for better results
β’ The system searches through indexed code chunks
β’ Ensure your code has been properly indexed before querying
Configuration:
β’ Using Anthropic Claude API (direct HTTP)
β’ Model: claude-3-haiku-20240307 (fast and efficient)
β’ Retrieves top 5 most relevant code chunks by default
"""
print(help_text)
def main():
"""Main entry point for the Q&A interface."""
# check if code has been indexed
try:
test_chunks = query_code("test", k=1)
if not test_chunks:
print("β οΈ Warning: No code appears to be indexed yet.")
print(" Run the main code quality agent first to index your codebase.")
print()
except Exception:
print("β οΈ Warning: Code index not initialized.")
print(" Run the main code quality agent first to index your codebase.")
print()
# create and run the Q&A interface
qa_interface = CodeQAInterface()
qa_interface.run_interactive_session()
if __name__ == "__main__":
main()