View the full code and setup instructions in our examples repository
Overview
The process involves three main steps:- Initializing and indexing the codebase
- Finding relevant code snippets for a query
- Generating answers using RAG
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: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:- Load or create the vector index
- Find relevant code snippets
- Generate a detailed answer
- Return both the answer and file references
Example Usage
Here’s what the output looks like: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.
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.