magentic icon indicating copy to clipboard operation
magentic copied to clipboard

Support litellm router features?

Open benwhalley opened this issue 1 year ago • 3 comments

litellm includes some nice features to rate limit calls, and switch between deployments based on https://docs.litellm.ai/docs/routing

I wonder if it would be possible to configure magentic to accept a router config?

e.g. on line 12 here, rather than just importing litellm and then using litellm.completion you could create a router instance based on a MAGENTIC_LITELLM_ROUTER config and below on line 61 or 94 use router.completion or router.acompletion rather than litellm.acompletion?

https://github.com/jackmpcollins/magentic/blob/5b4b1012e630bd01baa0ef8bf3caf86fb68ae993/src/magentic/chat_model/litellm_chat_model.py

My initial use case is just setting RPM limits, but I can see it would also be nice to be able to track token usage.

benwhalley avatar Aug 20 '24 10:08 benwhalley

It's not currently possible but it could definitely be added. The code you link is old. The newer version would be even more simple to update.

https://github.com/jackmpcollins/magentic/blob/6747d99dc37f0becbfbb12fdfdc17392da343ee0/src/magentic/chat_model/litellm_chat_model.py#L153-L155

I think a good approach would be to add a router: litellm.Router | None = None parameter to LitellmChatModel.__init__. Then in (a)complete use router.completion instead of litellm.completion if router was provided (assuming they meet the same interface, which appears to be the case).

jackmpcollins avatar Aug 21 '24 09:08 jackmpcollins

would you be open to a pr if I have a go at this?

benwhalley avatar Aug 21 '24 09:08 benwhalley

would you be open to a pr if I have a go at this?

@benwhalley Absolutely!

jackmpcollins avatar Aug 21 '24 20:08 jackmpcollins