This tutorial demonstrates how to build a Slack bot that can answer code questions using simple RAG (Retrieval Augmented Generation) over a codebase. The bot uses semantic search to find relevant code snippets and generates detailed answers using OpenAI’s APIs.
First, we initialize the codebase and create a vector index for semantic search:
Copy
Ask AI
from codegen import Codebasefrom codegen.extensions import VectorIndexdef initialize_codebase(): """Initialize and index the codebase.""" # Initialize codebase with smart caching codebase = Codebase.from_repo( "codegen-sh/codegen-sdk", language="python", tmp_dir="/root" ) # Initialize vector index index = VectorIndex(codebase) # Try to load existing index or create new one index_path = "/root/E.pkl" try: index.load(index_path) except FileNotFoundError: # Create new index if none exists index.create() index.save(index_path) return codebase, index
The vector index is persisted to disk, so subsequent queries will be much faster.
See semantic code search to learn more about VectorIndex.
Next, we use the vector index to find code snippets relevant to a query:
Copy
Ask AI
def find_relevant_code(index: VectorIndex, query: str) -> list[tuple[str, float]]: """Find code snippets relevant to the query.""" # Get top 10 most relevant files results = index.similarity_search(query, k=10) # Clean up chunk references from index cleaned_results = [] for filepath, score in results: if "#chunk" in filepath: filepath = filepath.split("#chunk")[0] cleaned_results.append((filepath, score)) return cleaned_results
VectorIndex automatically chunks large files for better search results. We clean up the chunk references to show clean file paths.
Finally, we use GPT-4 to generate answers based on the relevant code:
Copy
Ask AI
from openai import OpenAIdef generate_answer(query: str, context: str) -> str: """Generate an answer using RAG.""" prompt = f"""You are a code expert. Given the following code context and question,provide a clear and accurate answer.Note: Keep it short and sweet - 2 paragraphs max.Question: {query}Relevant code:{context}Answer:""" client = OpenAI() response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a code expert. Answer questions about the given repo based on RAG'd results."}, {"role": "user", "content": prompt}, ], temperature=0, ) return response.choices[0].message.content
answer, files = answer_question("How does VectorIndex handle large files?")print("Answer:", answer)print("\nRelevant files:")for filepath, score in files: print(f"• {filepath} (score: {score:.2f})")
Output:
Copy
Ask AI
Answer:VectorIndex handles large files by automatically chunking them into smaller piecesusing tiktoken. Each chunk is embedded separately and can be searched independently,allowing for more precise semantic search results.Relevant files:• src/codegen/extensions/vector_index.py (score: 0.92)• src/codegen/extensions/tools/semantic_search.py (score: 0.85)