Local LLM based AI-Assisted Code Review Comments in Xcode - Part 1
In this series an Xcode Source Editor Extension is built, that analyzes
Swift code using a locally running
language model (via llama.cpp) and inserting generated review comments directly
into the source code as inline annotations are covered.
In the first part a locally running LLM is installed.
Prerequisites
- A Mac with Apple Silicon (M1, M2, M3, or later)
- Xcode 15.0 or newer
- macOS 14.0 or newer
- Approximately 5 GB of available disk space for the model
Setting Up the Local LLM
Install llama.cpp
Open a terminal and change to a directory where you want to save llama.cpp.
Run the commands listed below one by one:
# Clone the llama.cpp repository
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
# Prepare the build directory
mkdir build
cd build
# Build with Metal acceleration for Apple Silicon
cmake .. -DGGML_METAL=ON && cmake --build . --config Release
# Prepare for downloading models
cd ..
mkdir -p models
# Download a coding-focused model (CodeLlama 7B)
# This is optimized for code analysis and fits in 8GB RAM
curl -L -o models/codellama-7b-instruct.Q4_K_M.gguf \
https://huggingface.co/TheBloke/CodeLlama-7B-Instruct-GGUF/resolve/main/codellama-7b-instruct.Q4_K_M.gguf
Test the Model
Run the following command to test that the model works:
./build/bin/llama-cli -m models/codellama-7b-instruct.Q4_K_M.gguf \
-p "Review this Swift code for issues: func getData() { let x = 0 / 0 }" \
-n 128
▶ Play video in full resolution
This is an example output from the model performing the prompt
"Review this Swift code for issues: func getData() { let x = 0 / 0 }":
▄▄ ▄▄
██ ██
██ ██ ▀▀█▄ ███▄███▄ ▀▀█▄ ▄████ ████▄ ████▄
██ ██ ▄█▀██ ██ ██ ██ ▄█▀██ ██ ██ ██ ██ ██
██ ██ ▀█▄██ ██ ██ ██ ▀█▄██ ██ ▀████ ████▀ ████▀
██ ██
▀▀ ▀▀
build : b8586-64ac9ab66
model : codellama-7b-instruct.Q4_K_M.gguf
modalities : text
available commands:
/exit or Ctrl+C stop or exit
/regen regenerate the last response
/clear clear the chat history
/read <file> add a text file
/glob <pattern> add text files using globbing pattern
> Review this Swift code for issues: func getData() { let x = 0 / 0 }
This Swift code is illegal because division by zero is not allowed.
To fix this error, you should change the value of x to a valid number.
func getData() { let x = 1 / 1 }
<|im_end|>
[ Prompt: 131.5 t/s | Generation: 26.3 t/s ]
>
What's next
With the local model now set up and ready for use, the next part will focus on integrating it into your development workflow. It will cover how to implement an Xcode Source Editor Extension that leverages the model to analyze Swift code and provide inline review feedback directly within the editor.