File size: 639 Bytes
9be5ab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { MODEL_ENDPOINTS } from "$env/static/private";
import { sum } from "$lib/utils/sum";

const endpoints: Array<{ endpoint: string; authorization: string; weight: number }> =
	JSON.parse(MODEL_ENDPOINTS);
const totalWeight = sum(endpoints.map((e) => e.weight));

/**
 * Find a random load-balanced endpoint
 */
export function modelEndpoint(): { endpoint: string; authorization: string; weight: number } {
	let random = Math.random() * totalWeight;
	for (const endpoint of endpoints) {
		if (random < endpoint.weight) {
			return endpoint;
		}
		random -= endpoint.weight;
	}

	throw new Error("Invalid config, no endpoint found");
}