aider icon indicating copy to clipboard operation
aider copied to clipboard

feat: add LiteLLM support

Open aleclarson opened this issue 1 year ago • 3 comments

🚨 Feedback wanted!

When all of --openai-api-key, --openai-api-base, and --openrouter are not passed and the user has installed LiteLLM with pip install litellm, Aider will default to using the LiteLLM client, which has support for many models, including Claude 3, Gemini 1.5 Pro, and Command R+.

You also need to install model-specific clients, which LiteLLM will use under the hood. For example, you'll need to pip install anthropic to use Claude 3. To use Gemini 1.5 Pro, you'll need to pip install google-generativeai first.

Since LiteLLM loads the nearest .env file, it's recommended to place your model-specific API key in there. For example, LiteLLM expects an ANTHROPIC_API_KEY environment variable to exist when using Claude 3. Of course, you can define the environment variable however you please, so an .env file is optional. Find your preferred model on the LiteLLM supported models page to determine which environment variable is expected by LiteLLM based on which model you're using.

  • Does this add dependencies to Aider?
    No, the user must run pip install litellm for Aider to support LiteLLM.

  • Can I use any model supported by LiteLLM?
    Yes, the supported models are loaded directly from LiteLLM's Github repository. As long as you keep LiteLLM up-to-date, any new models can be used as long as LiteLLM supports them.

  • What model is used for the summarizer and repo-map?
    If you're using Claude 3 Opus or GPT-4, Aider will use GPT-3.5 Turbo for the chat summarizer and repo-map features. Otherwise, the --model you defined is what's used.

  • Has this been tested extensively?
    No, I'm looking for people to try it out and leave feedback on this pull request.

Model aliases

When defining the --model name, you can use these aliases:

# claude-3
opus   => claude-3-opus-20240229
sonnet => claude-3-sonnet-20240229
haiku  => claude-3-haiku-20240307

# gemini-1.5-pro
gemini => gemini-1.5-pro-preview-0409

# gpt-3.5
gpt-3.5           => gpt-3.5-turbo-0613
gpt-3.5-turbo     => gpt-3.5-turbo-0613
gpt-3.5-turbo-16k => gpt-3.5-turbo-16k-0613

# gpt-4
gpt-4     => gpt-4-0613
gpt-4-32k => gpt-4-32k-0613

The gpt-3.5 and gpt-4 aliases were already supported, but they're also supported when using the --litellm flag or when LiteLLM is used by default.

Additional improvements

I've added some environment variables to improve .env file support, so I can run aider with dotenv run aider to use them:

  • the AIDER_MODEL environment variable is now supported
  • the AIDER_EDIT_FORMAT environment variable is now supported

aleclarson avatar Apr 11 '24 22:04 aleclarson

This is looking really great, thanks for putting in all the work!

My main question is why not just use litellm, and ditch the openai client? Direct calls to openai are handled by litellm, as are OpenRouter and Azure. I had been planning on going down that path with a litellm integration.

paul-gauthier avatar Apr 12 '24 21:04 paul-gauthier

My main question is why not just use litellm, and ditch the openai client?

That could make sense in the future. For now, it's better to make the transition smooth for everyone by still allowing the previous usage, just in case there's a subtle difference between the two approaches? Also, I figured it was easier to get a less drastic change merged sooner.

aleclarson avatar Apr 15 '24 17:04 aleclarson

I really appreciate the work you put in here. Just wanted to give you a heads up that I've also got a litellm branch going. I am leaning towards a wholesale replacement of the openai client with litellm. I'm far enough into my branch now that it looks feasible.

You can see my work in progress in #564.

paul-gauthier avatar Apr 17 '24 22:04 paul-gauthier