Skip to main content
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.
View the full code and setup instructions in our examples repository
While this example uses the Codegen codebase, you can adapt it to any repository by changing the repository URL

Overview

The process involves three main steps:
  1. Initializing and indexing the codebase
  2. Finding relevant code snippets for a query
  3. Generating answers using RAG
Let’s walk through each step using Codegen.

Step 1: Initializing the Codebase

First, we initialize the codebase and create a vector index for semantic search:
The vector index is persisted to disk, so subsequent queries will be much faster. See semantic code search to learn more about VectorIndex.

Step 2: Finding Relevant Code

Next, we use the vector index to find code snippets relevant to a query:
VectorIndex automatically chunks large files for better search results. We clean up the chunk references to show clean file paths.

Step 3: Generating Answers

Finally, we use GPT-4 to generate answers based on the relevant code:

Putting It All Together

Here’s how the components work together to answer questions:
This will:
  1. Load or create the vector index
  2. Find relevant code snippets
  3. Generate a detailed answer
  4. Return both the answer and file references

Example Usage

Here’s what the output looks like:
Output:

Extensions

While this example demonstrates a simple RAG-based bot, you can extend it to build a more powerful code agent that can:
  • Do more sophisticated code retrieval
  • Make code changes using Codegen’s edit APIs
  • Gather further context from Slack channels
  • … etc.
Check out our Code Agent tutorial to learn how to build an intelligent agent with access to Codegen’s full suite of tools

Learn More

Semantic Code Search

Learn how to use VectorIndex for semantic code search and embeddings.

Build a Code Agent

Create a more powerful agent with multi-step reasoning and code manipulation.

OpenAI Embeddings

Learn about OpenAI’s text embeddings and how they work.

RAG Best Practices

Understand RAG patterns and best practices for better results.