页面简介
Learn how to use Aider, an AI pair programming tool, with Cerebras Inference for ultra-fast code editing and generation in your terminal.
正文摘录
Aider is an AI pair programming tool that runs in your terminal. It allows you to edit code in your local Git repository using natural language commands. By connecting Aider to Cerebras Inference, you can leverage ultra-fast inference speeds for an enhanced coding experience. Prerequisites Before you begin, ensure you have: Cerebras API Key - Get a free API key here Python 3.10 to 3.12 - Current Aider releases do not support Python 3.13 Git - Aider works with Git repositories to track changes A code editor - While Aider runs in the terminal, you’ll want an editor to view your files Installation and Setup 1Install AiderInstall Aider using pip. This will download and install the latest version of Aider and its dependencies.pip install aider-chat Alternatively, you can use pipx for an isolated installation:pipx install aider-chat Using pipx is recommended for isolated environments, as it installs Aider in its own virtual environment.2Set up your Cerebras API keyAider needs your Cerebras API key to authenticate requests. Set it as an environment variable so Aider can access it.macOS/LinuxWindows (PowerShell)Windows (CMD)export CEREBRAS_API_KEY=your-cerebras-api-key-here For a permanent setup, add this line to your shell configuration file (~/.bashrc, ~/.zshrc, etc.).3Initialize your project directoryAider works with Git repositories to track changes. Navigate to your project and ensure it’s a Git repository.cd /path/to/your/project If your project is not already a Git repository, initialize one:git init git add . git commit -m "Initial commit" Aider uses Git to track all changes it makes, allowing you to easily review and revert modifications.4Create an Aider configuration fileCreate an .aider.conf.yml file in your project root for easier usage. This eliminates the need to specify connection details every time you launch Aider.You can create this file via a terminal command:cat > .aider.conf.yml <<EOF model: cerebras/gpt-oss-120b openai-api-key: env:CEREBRAS_API_KEY EOF With this configuration, you can simply run aider without additional flags. Using Aider with Cerebras 1Start Aider with a Cerebras modelLaunch Aider and specify a Cerebras model using the OpenAI-compatible API. If you created a configuration file, simply run aider. Otherwise, use the full command:With config fileWithout config fileaider This command tells Aider to use the gpt-oss-120b model from Cerebras and connect to Cerebras’s API endpoint.The cerebras/ prefix tells Aider to use LiteLLM routing to Cerebras, which automatically configures the correct API endpoint. The --openai-api-key flag is used because Aider expects this parameter name for OpenAI-compatible providers.2Create a new file from scratchSince we are starting with an empty project, let’s ask Aider to create a file. Aider can generate code and create files based on your instructions.Paste this prompt into Aider:Create a python script named fibonacci.py that calculates the fibonacci sequence using a recursive approach. Aider will create fibonacci.py with the implementation. You don’t need to manually create the file first.3Refine the codeNow, let’s ask Aider to improve the implementation. The model will analyze the existing code and apply your requested changes.Convert the fibonacci function to use iteration instead of recursion for better performance. Also add a main block to run it. Aider will: Analyze the recursive implementation Rewrite it to use iteration Add the main execution block Commit the changes to Git 4Run the codeYou can run shell commands directly within Aider to verify the changes./run python fibonacci.py Aider will run the command and show the output:Running python fibonacci.py The 10th Fibonacci number is: 34 You can choose to add this output to the chat so Aider can see the result and fix any errors if they occur.5Review and undoYou can inspect the changes Aider made or undo them if you’re not satisfied.To see the last change:/diff To undo the last change:/undo Aider will revert the changes:Removed: 83b2890 refactor: use iteration in fibonacci Now at: 8f1f75c feat: add fibonacci script You can use /undo to undo and discard each aider commit. Recommended Models Cerebras offers several models that work well with Aider. Choose based on your needs for speed versus capability: gpt-oss-120b: Largest model for the most demanding tasks For the best experience with Aider, we recommend using cerebras/gpt-oss-120b as it provides excellent code understanding and generation capabilities while maintaining fast inference speeds. Advanced Usage Using Different Edit Formats Aider supports multiple edit formats. For Cerebras models, the default “whole” format works well, but you can experiment with others: To use whole file editing (recommended for Cerebras): aider --model cerebras/gpt-oss-120b --edit-format whole To use diff-based editing for smaller changes: aider --model cerebras/gpt-oss-120b --edit-format diff Architect Mode For high-level design discussions before coding, use architect mode. This is useful for planning changes, discussing architecture, or exploring different approaches: aider --architect --model cerebras/gpt-oss-120b In architect mode, Aider won’t make direct code changes but will help you think through design decisions. Streaming Responses Enable streaming to see responses as they’re generated, providing immediate feedback: aider --model cerebras/gpt-oss-120b --stream Working with Multiple Models You can use different models for different tasks. For example, use a larger model for complex edits and a smaller one for simple changes: aider \ --model cerebras/gpt-oss-120b \ --editor-model cerebras/gpt-oss-120b Example Session Here’s a complete example of using Aider with Cerebras to add a new feature to a Python project. First, start Aider from your terminal: aider --model cerebras/gpt-oss-120b Then, inside the Aider chat, run the following commands and prompts: # Add relevant files /add calculator.py /add test_calculator.py # Request a new feature Add a new function to calculate the factorial of a number, with proper error handling for negative inputs # Aider generates and applies the code # Review the changes /diff # Request tests Now add comprehensive unit tests for the factorial function, including edge cases # Review the test file changes /diff test_calculator.py # If satisfied, changes are already committed # View the commit /git log -1 # Continue with more requests or exit /exit Troubleshooting Aider can't connect to Cerebras APIEnsure your API key is correctly set and accessible:echo $CEREBRAS_API_KEY If empty, set it again:export CEREBRAS_API_KEY=your-key-here Verify the API base URL is correct: https://api.cerebras.ai/v1If you’re still having issues, try running Aider with verbose logging:aider --verbose --model gpt-oss-120b Model isn't editing files properlySome models may struggle with code editing, or the edit format may not be optimal. Try: Using a more capable model like gpt-oss-120b Switching edit formats: --edit-format whole Being more specific in your requests with examples Breaking complex changes into smaller, incremental steps Adding more context files with /add You can also check Aider’s LLM leaderboards to see how different models perform.Changes aren't being committed to GitAider requires a Git repository to function properly. Ensure: You’re in a Git repository: git status You have an initial commit: git log Your Git configuration is set up: git config user.name and git config user.email If needed, initialize Git:git init git config user.name "Your Name" git config user.email "your.email@example.com" git add . git commit -m "Initial commit" Then restart Aider.Rate limiting or quota errorsIf you encounter rate limits or quota errors: Check your Cerebras dashboard for current usage Consider upgrading your plan for higher limits Reduce unnecessary context and output tokens Add delays between requests if using Aider programmatically Break large changes into smaller sessions Cerebras offers generous rate limits, but very intensive usage may require a paid plan.Aider is slow or unresponsiveIf Aider feels slow: Ensure you’re using a Cerebras model (they’re optimized for speed) Reduce the number of files in context with /drop filename Use streaming mode: --stream Check your internet connection Reduce the amount of repository context sent with each request Cerebras models are typically very fast, so slowness often indicates network issues or too much context. FAQ Which Cerebras model should I use with Aider?For the best experience, use gpt-oss-120b. It provides excellent code understanding and generation capabilities while maintaining fast inference speeds.For the best current experience, use gpt-oss-120b and keep the repository context focused on the files relevant to your task.Check Aider’s LLM leaderboards to see how different models perform on code editing tasks.Can I use Aider with multiple files?Yes! Use the /add command to include multiple files in the conversation:/add src/main.py /add src/utils.py /add tests/test_main.py Aider will maintain context across all added files. You can also add entire directories:/add src/ Use /drop filename to remove files from context if needed.Does Aider work with non-Python projects?Absolutely! Aider works with any programming language including JavaScript, TypeScript, Java, C++, Go, Rust, and more. It uses Git for version control and can edit any text-based files.The model’s ability to work with different languages depends on its training, but Cerebras models like gpt-oss-120b have strong multi-language capabilities.How do I undo changes made by Aider?Since Aider commits changes to Git, you can use standard Git commands:To view commit history:git log To undo the last commit but keep the changes in your files:git reset --soft HEAD~1 To undo the last commit and discard the changes:git reset --hard HEAD~1 To revert a specific commit:git revert <commit-hash> You can also use /undo within an Aider session to undo