mirror of
https://github.com/coaidev/coai.git
synced 2025-05-20 21:40:15 +09:00
update apikey filter
This commit is contained in:
parent
2ff7276688
commit
a661b6c33c
@ -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
24
api/selector.go
Normal 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, "|")
|
||||
}
|
Loading…
Reference in New Issue
Block a user