Skip to content

[Inference Providers] provider="auto" #1390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 30, 2025
Merged

Conversation

hanouticelina
Copy link
Contributor

Same as huggingface/huggingface_hub#3011.

This PR adds support for auto selection of the provider. Previously the default value was hf-inference (HF Inference API provider), now we default to "auto", meaning we will select the first of the providers available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.

you can test with:

import { chatCompletion } from "../src";

const res = await chatCompletion({
	// provider="auto",
	model: "deepseek-ai/DeepSeek-V3-0324",
	messages: [
		{
			role: "user",
			content: "What is the capital of France?",
		},
	],
	accessToken: process.env.HF_TOKEN,
});
console.log(res.choices[0].message.content);
Defaulting to 'auto' which will select the first provider available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.
Auto-selected provider: sambanova
The capital of France is **Paris**. It is known for its iconic landmarks such as the Eiffel Tower...blabla

the selected provider should be be the first in inferenceProviderMapping mapping here: https://huggingface.co/api/models/deepseek-ai/DeepSeek-V3-0324?expand=inferenceProviderMapping

@@ -94,3 +112,27 @@ export async function getInferenceProviderMapping(
}
return null;
}

export async function resolveProvider(
Copy link
Contributor Author

@hanouticelina hanouticelina Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be done in getProviderHelper as we did for the python client, but that would make the function async and we would have to update the snippets generation as well

}
inferenceProviderMapping = await resp
.json()
.then((json) => json.inferenceProviderMapping)
.catch(() => null);

if (inferenceProviderMapping) {
inferenceProviderMappingCache.set(modelId, inferenceProviderMapping);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if provider="auto", we call fetchInferenceProviderMappingForModel 2 times: once to resolve the provider and a second time in makeRequestOptions. this cache avoids the extra HTTP call.

Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from a question about the type name, lgtm!

Copy link
Contributor

@SBrandeis SBrandeis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean!

Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@julien-c
Copy link
Member

Are we sure we're not keeping the name InferenceProvider? Feels better to me honestly (and we mention in the docstring that it can be a "policy" or a "virtual provider")

Will keep diff smaller as @SBrandeis mentions too

@hanouticelina
Copy link
Contributor Author

Discussed internally, let's keep InferenceProviderOrPolicy and merge the PR!

@hanouticelina hanouticelina merged commit eaa1b9c into main Apr 30, 2025
4 of 5 checks passed
@hanouticelina hanouticelina deleted the auto-select-provider branch April 30, 2025 09:18
Deep-unlearning pushed a commit that referenced this pull request May 13, 2025
Same as huggingface/huggingface_hub#3011.

This PR adds support for auto selection of the provider. Previously the
default value was `hf-inference` (HF Inference API provider), now we
default to "auto", meaning we will select the first of the providers
available for the model, sorted by the user's order in
https://hf.co/settings/inference-providers.

you can test with:
```ts
import { chatCompletion } from "../src";

const res = await chatCompletion({
	// provider="auto",
	model: "deepseek-ai/DeepSeek-V3-0324",
	messages: [
		{
			role: "user",
			content: "What is the capital of France?",
		},
	],
	accessToken: process.env.HF_TOKEN,
});
console.log(res.choices[0].message.content);
```
```
Defaulting to 'auto' which will select the first provider available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.
Auto-selected provider: sambanova
The capital of France is **Paris**. It is known for its iconic landmarks such as the Eiffel Tower...blabla
```
the selected provider should be be the first in
`inferenceProviderMapping` mapping here:
https://huggingface.co/api/models/deepseek-ai/DeepSeek-V3-0324?expand=inferenceProviderMapping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants