add realtime proxy

This commit is contained in:
Sakurasan
2024-10-06 00:44:48 +08:00
parent eef24913e0
commit 236dffa256
9 changed files with 329 additions and 278 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"log"
"math/rand"
"strings"
"time"
"github.com/Sakurasan/to"
@@ -73,6 +74,37 @@ func SelectKeyCache(apitype string) (Key, error) {
return keys[idx], nil
}
func SelectKeyCacheByModel(model string) (Key, error) {
var keys []Key
items := KeysCache.Items()
for _, item := range items {
if strings.HasPrefix(model, "gpt-") {
if item.Object.(Key).ApiType == "openai" {
keys = append(keys, item.Object.(Key))
}
if item.Object.(Key).ApiType == "azure" {
keys = append(keys, item.Object.(Key))
}
if item.Object.(Key).ApiType == "github" {
keys = append(keys, item.Object.(Key))
}
}
if strings.HasPrefix(model, "o1-") || model == "chatgpt-4o-latest" {
if item.Object.(Key).ApiType == "openai" {
keys = append(keys, item.Object.(Key))
}
}
}
if len(keys) == 0 {
return Key{}, errors.New("No key found")
} else if len(keys) == 1 {
return keys[0], nil
}
rand.Seed(time.Now().UnixNano())
idx := rand.Intn(len(keys))
return keys[idx], nil
}
func LoadAuthCache() {
AuthCache = cache.New(cache.NoExpiration, cache.NoExpiration)
users, err := GetAllUsers()