From edd5c58a7b802feed439de68ef443c552db456df Mon Sep 17 00:00:00 2001 From: MrBounty Date: Wed, 14 Aug 2024 17:52:30 +0200 Subject: [PATCH] Lots of fix Can now add new api key again, and some minor bugs --- Authentification.go | 3 ++- EdgeDatabase.go | 36 ++++++++++++++++++++++++++++-- Request.go | 7 +++--- RequestAnthropic.go | 28 ++++++++++++----------- RequestCustomEndpoint.go | 2 +- RequestDeepseek.go | 28 ++++++++++++----------- RequestFirework.go | 29 ++++++++++++------------ RequestGoogle.go | 28 ++++++++++++----------- RequestGroq.go | 29 ++++++++++++------------ RequestMistral.go | 29 ++++++++++++------------ RequestNim.go | 29 ++++++++++++------------ RequestOpenai.go | 29 ++++++++++++------------ RequestPerplexity.go | 29 ++++++++++++------------ RequestTogetherai.go | 29 ++++++++++++------------ main.go | 4 ++-- views/layouts/main.html | 15 ++++++++++++- views/partials/chat-input.html | 8 ++++--- views/partials/message-bot.html | 2 +- views/partials/message-user.html | 2 +- views/partials/popover-models.html | 13 ----------- 20 files changed, 214 insertions(+), 165 deletions(-) diff --git a/Authentification.go b/Authentification.go index d278856..b6296bb 100644 --- a/Authentification.go +++ b/Authentification.go @@ -261,7 +261,8 @@ func handleCallbackSignup(c *fiber.Ctx) error { INSERT Conversation { name := 'Default', user := global currentUser, - position := 1 + position := 1, + selected := true, }`) if err != nil { fmt.Println("Error creating default conversation") diff --git a/EdgeDatabase.go b/EdgeDatabase.go index 6a6542c..9579e6e 100644 --- a/EdgeDatabase.go +++ b/EdgeDatabase.go @@ -105,8 +105,30 @@ type CompanyInfo struct { Icon string `edgedb:"icon"` } -var edgeCtx context.Context -var edgeGlobalClient *edgedb.Client +var ( + edgeCtx context.Context + edgeGlobalClient *edgedb.Client + allModelInfo []ModelInfo + allCompany []CompanyInfo +) + +func getModelInfoByID(id edgedb.UUID) (ModelInfo, bool) { + for _, model := range allModelInfo { + if model.ID == id { + return model, true + } + } + return ModelInfo{}, false +} + +func getCompanyInfoByID(id edgedb.UUID) (CompanyInfo, bool) { + for _, company := range allCompany { + if company.ID == id { + return company, true + } + } + return CompanyInfo{}, false +} func init() { var ctx = context.Background() @@ -118,6 +140,16 @@ func init() { edgeCtx = ctx edgeGlobalClient = client + + err = edgeGlobalClient.Query(edgeCtx, `SELECT ModelInfo { id, inputPrice, outputPrice, modelID, name, company}`, &allModelInfo) + if err != nil { + panic("Can't get all ModelInfo") + } + + err = edgeGlobalClient.Query(edgeCtx, `SELECT Company { id, icon, name}`, &allCompany) + if err != nil { + panic("Can't get all Company") + } } func checkIfLogin(c *fiber.Ctx) bool { diff --git a/Request.go b/Request.go index 19f1099..edd4d2d 100644 --- a/Request.go +++ b/Request.go @@ -53,6 +53,7 @@ func GeneratePlaceholderHTML(c *fiber.Ctx, message string, selectedLLMIds []stri key }, modelInfo : { + id, modelID, company : { icon, @@ -138,7 +139,7 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error { defer cancel() // Ensure the context is cancelled to free resources // Determine which message function to call based on the model - var addMessageFunc func(c *fiber.Ctx, llm LLM, messages []Message) string + var addMessageFunc func(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string switch selectedLLMs[idx].Model.Company.Name { case "openai": addMessageFunc = RequestOpenai @@ -149,7 +150,7 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error { case "groq": addMessageFunc = RequestGroq case "huggingface": - addMessageFunc = RequestHuggingface + addMessageFunc = RequestCustomEndpoint case "google": addMessageFunc = RequestGoogle case "perplexity": @@ -164,7 +165,7 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error { addMessageFunc = RequestDeepseek } - var content string = addMessageFunc(c, selectedLLMs[idx], messages) + var content string = addMessageFunc(c, selectedLLMs[idx], messages, "") var messageUUID edgedb.UUID = insertBotMessage(c, content, selectedLLMs[idx].ID) var message Message diff --git a/RequestAnthropic.go b/RequestAnthropic.go index 23adc3c..a1cdd9a 100644 --- a/RequestAnthropic.go +++ b/RequestAnthropic.go @@ -50,7 +50,7 @@ func init() { AnthropicErrorCodes["529"] = "Provider error: Anthropic’s server is temporarily overloaded." } -func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context @@ -63,15 +63,20 @@ func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string { var apiKey struct { Key string `edgedb:"key"` } - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` SELECT Key { key } FILTER .$0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("01-00-0006") return "JADE internal error: 01-00-0006. Please contact the support." } diff --git a/RequestCustomEndpoint.go b/RequestCustomEndpoint.go index 6509565..63ba6c8 100644 --- a/RequestCustomEndpoint.go +++ b/RequestCustomEndpoint.go @@ -10,7 +10,7 @@ import ( "github.com/gofiber/fiber/v2" ) -func RequestHuggingface(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestCustomEndpoint(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { url := llm.Endpoint.Endpoint temperature := float64(llm.Temperature) context := llm.Context diff --git a/RequestDeepseek.go b/RequestDeepseek.go index a59329f..26aea73 100644 --- a/RequestDeepseek.go +++ b/RequestDeepseek.go @@ -23,7 +23,7 @@ func init() { DeepseekErrorCodes["503"] = "Provider error: Deepseek’s server is temporarily overloaded." } -func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context @@ -32,7 +32,8 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string { url := "https://api.deepseek.com/chat/completions" var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -41,8 +42,12 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "deepseek") - if err != nil { - return "JADE internal error: 08-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("08-00-0000") + return "JADE internal error: 08-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } requestBody := OpenaiChatCompletionRequest{ @@ -94,16 +99,13 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 08-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Message.Content + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("08-00-0006") return "JADE internal error: 08-00-0006. Please contact the support." } diff --git a/RequestFirework.go b/RequestFirework.go index 0b16bf1..1fca79b 100644 --- a/RequestFirework.go +++ b/RequestFirework.go @@ -10,14 +10,15 @@ import ( "github.com/gofiber/fiber/v2" ) -func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -26,9 +27,12 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "fireworks") - if err != nil { - logErrorCode.Println("09-00-0000") - return "JADE internal error: 09-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("09-00-0000") + return "JADE internal error: 09-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://api.fireworks.ai/inference/v1/chat/completions" @@ -82,16 +86,13 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 09-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Message.Content + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("09-00-0006") return "JADE internal error: 09-00-0006. Please contact the support." } diff --git a/RequestGoogle.go b/RequestGoogle.go index 5ef9092..b203807 100644 --- a/RequestGoogle.go +++ b/RequestGoogle.go @@ -62,7 +62,7 @@ func init() { GoogleErrorCodes["503"] = "Provider error: Servers are experiencing high traffic - Please retry your requests after a brief wait." } -func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID // TODO: Use those parameters @@ -71,7 +71,8 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string { //maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -80,8 +81,12 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "google") - if err != nil { - return "JADE internal error: 03-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("03-00-0000") + return "JADE internal error: 03-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://generativelanguage.googleapis.com/v1beta/models/" + model + ":generateContent?key=" + apiKey @@ -147,16 +152,13 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 03-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Candidates[0].Content.Parts[0].Text + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("03-00-0006") return "JADE internal error: 03-00-0006. Please contact the support." } diff --git a/RequestGroq.go b/RequestGroq.go index da13d38..2c18e97 100644 --- a/RequestGroq.go +++ b/RequestGroq.go @@ -23,14 +23,15 @@ func init() { GroqErrorCodes["503"] = "Provider error: The server is not ready to handle the request, often due to maintenance or overload. Wait before retrying the request." } -func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -39,9 +40,12 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "groq") - if err != nil { - logErrorCode.Println("04-00-0000") - return "JADE internal error: 04-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("04-00-0000") + return "JADE internal error: 04-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://api.groq.com/openai/v1/chat/completions" @@ -95,16 +99,13 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 04-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Message.Content + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("04-00-0006") return "JADE internal error: 04-00-0006. Please contact the support." } diff --git a/RequestMistral.go b/RequestMistral.go index 8ba0297..3fc96ec 100644 --- a/RequestMistral.go +++ b/RequestMistral.go @@ -10,14 +10,15 @@ import ( "github.com/gofiber/fiber/v2" ) -func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -26,9 +27,12 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "mistral") - if err != nil { - logErrorCode.Println("02-00-0000") - return "JADE internal error: 02-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("02-00-0000") + return "JADE internal error: 02-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://api.mistral.ai/v1/chat/completions" @@ -82,16 +86,13 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 02-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Message.Content + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("02-00-0006") return "JADE internal error: 02-00-0006. Please contact the support." } diff --git a/RequestNim.go b/RequestNim.go index ae3e70e..03dea19 100644 --- a/RequestNim.go +++ b/RequestNim.go @@ -10,14 +10,15 @@ import ( "github.com/gofiber/fiber/v2" ) -func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestNim(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -26,9 +27,12 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "nim") - if err != nil { - logErrorCode.Println("05-00-0000") - return "JADE internal error: 05-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("05-00-0000") + return "JADE internal error: 05-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://integrate.api.nvidia.com/v1/chat/completions" @@ -82,16 +86,13 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 05-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Message.Content + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("05-00-0006") return "JADE internal error: 05-00-0006. Please contact the support." } diff --git a/RequestOpenai.go b/RequestOpenai.go index b1c17b8..2df79a5 100644 --- a/RequestOpenai.go +++ b/RequestOpenai.go @@ -51,14 +51,15 @@ func init() { OpenaiErrorCodes["503"] = "Provider error: Servers are experiencing high traffic - Please retry your requests after a brief wait." } -func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -67,9 +68,12 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "openai") - if err != nil { - logErrorCode.Println("00-00-0000") - return "JADE internal error: 00-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("00-00-0000") + return "JADE internal error: 00-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://api.openai.com/v1/chat/completions" @@ -123,16 +127,13 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 00-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Message.Content + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("00-00-0006") return "JADE internal error: 00-00-0006. Please contact the support." } diff --git a/RequestPerplexity.go b/RequestPerplexity.go index 57f3249..e6b5a27 100644 --- a/RequestPerplexity.go +++ b/RequestPerplexity.go @@ -10,14 +10,15 @@ import ( "github.com/gofiber/fiber/v2" ) -func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -26,9 +27,12 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "perplexity") - if err != nil { - logErrorCode.Println("06-00-0000") - return "JADE internal error: 06-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("06-00-0000") + return "JADE internal error: 06-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://api.perplexity.ai/chat/completions" @@ -82,16 +86,13 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 06-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Message.Content + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("06-00-0006") return "JADE internal error: 06-00-0006. Please contact the support." } diff --git a/RequestTogetherai.go b/RequestTogetherai.go index 3be794e..656bec4 100644 --- a/RequestTogetherai.go +++ b/RequestTogetherai.go @@ -45,14 +45,15 @@ func init() { TogetherErrorCodes["529"] = "Provider error: n unexpected error has occurred internal to Together’s systems." } -func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string { +func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message, testApiKey string) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) var apiKey string - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` + if testApiKey == "" { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` with filtered_keys := ( select Key { @@ -61,9 +62,12 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string { ) select filtered_keys.key limit 1 `, &apiKey, "together") - if err != nil { - logErrorCode.Println("07-00-0000") - return "JADE internal error: 07-00-0000. Please contact the support." + if err != nil { + logErrorCode.Println("07-00-0000") + return "JADE internal error: 07-00-0000. Please contact the support." + } + } else { + apiKey = testApiKey } url := "https://api.together.xyz/v1/completions" @@ -117,16 +121,13 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string { return "JADE internal error: 07-01-0005. Please contact the support." } + if testApiKey != "" { + return chatCompletionResponse.Choices[0].Text + } + var usedModelInfo ModelInfo - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - SELECT ModelInfo { - inputPrice, - outputPrice - } - FILTER .modelID = $0 - LIMIT 1 - `, &usedModelInfo, model) - if err != nil { + usedModelInfo, found := getModelInfoByID(llm.Model.ID) + if !found { logErrorCode.Println("07-00-0006") return "JADE internal error: 07-00-0006. Please contact the support." } diff --git a/main.go b/main.go index d45b725..8c3d4f5 100644 --- a/main.go +++ b/main.go @@ -147,7 +147,7 @@ func addKeys(c *fiber.Ctx) error { "deepseek": c.FormValue("deepseek_key"), } - requestFunctions := map[string]func(*fiber.Ctx, LLM, []Message) string{ + requestFunctions := map[string]func(*fiber.Ctx, LLM, []Message, string) string{ "openai": RequestOpenai, "anthropic": RequestAnthropic, "mistral": RequestMistral, @@ -208,7 +208,7 @@ func addKeys(c *fiber.Ctx) error { Context: "", } - var responseText string = requestFunctions[company](c, llm, messages) + var responseText string = requestFunctions[company](c, llm, messages, key) if responseText == "" || strings.Contains(responseText, "JADE internal error") || strings.Contains(responseText, "Provider error") { return c.SendString(fmt.Sprintf("Invalid %s API Key\n", company)) diff --git a/views/layouts/main.html b/views/layouts/main.html index 900a197..0b0a27b 100644 --- a/views/layouts/main.html +++ b/views/layouts/main.html @@ -23,7 +23,7 @@