update apikey filter

This commit is contained in:
Zhang Minghan 2023-09-02 15:30:13 +08:00
parent 2ff7276688
commit a661b6c33c
2 changed files with 40 additions and 0 deletions

View File

@ -41,6 +41,22 @@ func GetChatGPTResponse(message []types.ChatGPTMessage, token int) (string, erro
return data.(string), nil
}
func TestKey(key string) bool {
res, err := utils.Post(viper.GetString("openai.anonymous_endpoint")+"/chat/completions", map[string]string{
"Content-Type": "application/json",
"Authorization": "Bearer " + key,
}, types.ChatGPTRequest{
Model: "gpt-3.5-turbo",
Messages: []types.ChatGPTMessage{{Role: "user", Content: "hi"}},
MaxToken: 2,
})
if err != nil || res == nil {
panic(err)
}
return res.(map[string]interface{})["choices"] != nil
}
func GetAnonymousResponse(message string) (string, string, error) {
keyword, source := ChatWithWeb([]types.ChatGPTMessage{{Role: "user", Content: message}}, false)
resp, err := GetChatGPTResponse(source, 1000)

24
api/selector.go Normal file
View File

@ -0,0 +1,24 @@
package api
import "strings"
func FilterKeys(keys string) string {
stack := make(chan string, len(strings.Split(keys, "|")))
for _, key := range strings.Split(keys, "|") {
go func(key string) {
if TestKey(key) {
stack <- key
} else {
stack <- ""
}
}(key)
}
var result string
for i := 0; i < len(strings.Split(keys, "|")); i++ {
if res := <-stack; res != "" {
result += res + "|"
}
}
return strings.Trim(result, "|")
}