gateway
gateway copied to clipboard
Added Support for the new /fim/completions route on Mistral AI API
Support new /fim/completions route on Mistral AI API:
Description:
/fim/completionsendpoint work in the same way as the/chat/completionsroute. It handles extra parameters such assuffix,promptalso.- Implemented
fimCompletionsHandlerto handle requests to the/fim/completionsendpoint. - Updated the API configuration to include the new
fimCompleteendpoint. - implemented response and stream chunk transformations for the
fimCompletefunction in Mistral AI. - Modified index.ts to register the new route
v1//fim/completionswith request validation.
Motivation:
- To add support in portkey gateway for the new
/fim/completionsroute on Mistral AI API
Related Issues:
- #381
Thank you for the contribution @keshavkrishna will review this soon
Hey @keshavkrishna! Will you be able to take a look at the comments? And please update your branch with the latest main branch changes
Yes I will take care of these comments by this weekend. Is that timeline okay?
Hey. Is it not possible to reuse the existing /chat/completions route? We can passthrough all the extra key as it is
Requests to the existing /chat/completions route are redirected to the provider's base URL with the endpoint chat/completions. In this scenario, we need to direct requests to the provider's base URL with the endpoint fim/completions.
We can accept a new header x-portkey-mistral-fim-completion: true and route it conditionally to the the mistral fim completions route. Adding a new router for provider specific routes is not the most feasible solution here because it will increase the number of routers in future.
EDIT: Adding extra details
- In this function, gateway already passes providerOptions. Using it, you can check for a new param called
providerOptions.mistralFimCompletion. If its true, you can change the endpoint to the mistral fim endpoint. https://github.com/Portkey-AI/gateway/blob/9724792edf7ff7954d068f00b25f90b5009a3cd0/src/providers/mistral-ai/api.ts#L8 - In the
chatCompleteconfig, you can add the extra fields which are required (like suffix, prompt, etc.). The response structure seems to be OpenAI compliant. So that should not be a problem
Sure, I'll implement this
@VisargD
If I am not getting mistralFimCompletion in providerOptions. following is provider option i am getting
{
provider: 'mistral-ai',
apiKey: ' SOME_API_KEY_HERE',
overrideParams: {},
retry: {},
cache: {}
}
so i have passed requestHeaders in getEndpioint, and i am checking by
if (requestHeaders['x-portkey-mistral-fim-completion'] === 'true')
Is that okay??
@VisargD
If I am not getting
mistralFimCompletioninproviderOptions. following is provider option i am getting{ provider: 'mistral-ai', apiKey: ' SOME_API_KEY_HERE', overrideParams: {}, retry: {}, cache: {} }so i have passed requestHeaders in
getEndpioint, and i am checking byif (requestHeaders['x-portkey-mistral-fim-completion'] === 'true')Is that okay??
Hey! specific headers checks might not be required. Here is what you can do:
- In this function, add one more condition for mistral-ai provider and add the mapping for
mistralFimCompletionin it. Check the existing conditions for other providers for reference. Once this is done, you will always get the mistralFimCompletion param in providerOptions. https://github.com/Portkey-AI/gateway/blob/3bb3a80385258ab2b2262ac28f6e3f2f47e0e3f1/src/handlers/handlerUtils.ts#L969
Please let me know if you want an example
Yes it is. We are using chatComplete transforms as per visarg's comment.
@keshavkrishna - Can you please run npm run format and also update with main branch? I can merge it now
@VisargD as requested i have updated my branch and also formatted it