File size: 884 Bytes
c10f8f8
 
 
 
 
 
 
 
 
 
5c71dae
c10f8f8
 
 
530fd3f
c10f8f8
5c71dae
c10f8f8
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
export const getBestProvider = async (model: string, provider?: string) => {
  const response = await fetch(`https://router.huggingface.co/v1/models/${model}`)
  const { data } = await response.json()
  let bestProvider = null;
  if (provider === "auto") {
    const sortedProviders = data.providers.sort((a: any, b: any) => {
      if (a.status === "live" && b.status !== "live") return -1
      if (a.status !== "live" && b.status === "live") return 1
      return a?.pricing?.output - b?.pricing?.output + a?.pricing?.input - b?.pricing?.input
    })
    bestProvider = sortedProviders[0]
  } else {
    const providerData = data.providers.find((p: any) => p.provider === provider)
    if (providerData?.status === "live") {
      bestProvider = providerData
    } else {
      bestProvider = data.providers?.find((p: any) => p.status === "live")
    }
  }

  return bestProvider
}