Removed the TestProvider key, created an logErrorCode and better error code per provider
This commit is contained in:
parent
25ae1337ff
commit
c6608fbea6
@ -8,17 +8,17 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var logMissingErrorCode *log.Logger
|
||||
var logErrorCode *log.Logger
|
||||
|
||||
func init() {
|
||||
logFile, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
logMissingErrorCode = log.New(logFile, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
logErrorCode = log.New(logFile, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
}
|
||||
|
||||
func MissingErrorCodeLogsHandler(c *fiber.Ctx) error {
|
||||
func ErrorCodeLogsHandler(c *fiber.Ctx) error {
|
||||
if !IsUserAdmin(c) {
|
||||
return c.SendString("That's for admin, how did you manage to come here ?")
|
||||
}
|
||||
|
@ -40,69 +40,14 @@ var AnthropicErrorCodes map[string]string
|
||||
|
||||
func init() {
|
||||
AnthropicErrorCodes = make(map[string]string)
|
||||
AnthropicErrorCodes["400"] = "Invalid Request - Please contact the support."
|
||||
AnthropicErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid."
|
||||
AnthropicErrorCodes["403"] = "Current API key does not have permission to use the specified resource."
|
||||
AnthropicErrorCodes["404"] = "The requested resource was not found."
|
||||
AnthropicErrorCodes["413"] = "Request exceeds the maximum allowed number of bytes."
|
||||
AnthropicErrorCodes["429"] = "Your account has hit a rate limit."
|
||||
AnthropicErrorCodes["500"] = "An unexpected error has occurred internal to Anthropic’s systems."
|
||||
AnthropicErrorCodes["529"] = "Anthropic’s server is temporarily overloaded."
|
||||
}
|
||||
|
||||
func TestAnthropicKey(apiKey string) bool {
|
||||
url := "https://api.anthropic.com/v1/messages"
|
||||
|
||||
AnthropicMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := AnthropicChatCompletionRequest{
|
||||
Model: "claude-3-haiku-20240307",
|
||||
Messages: AnthropicMessages,
|
||||
MaxTokens: 10,
|
||||
Temperature: 0,
|
||||
Context: "",
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("content-Type", "application/json")
|
||||
req.Header.Set("anthropic-version", "2023-06-01")
|
||||
req.Header.Set("x-api-key", apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse AnthropicChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Content == nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
AnthropicErrorCodes["400"] = "Provider error: Invalid Request - Please contact the support."
|
||||
AnthropicErrorCodes["401"] = "Provider error: Invalid Authentication - Ensure that the API key is still valid."
|
||||
AnthropicErrorCodes["403"] = "Provider error: Current API key does not have permission to use the specified resource."
|
||||
AnthropicErrorCodes["404"] = "Provider error: The requested resource was not found."
|
||||
AnthropicErrorCodes["413"] = "Provider error: Request exceeds the maximum allowed number of bytes."
|
||||
AnthropicErrorCodes["429"] = "Provider error: Your account has hit a rate limit."
|
||||
AnthropicErrorCodes["500"] = "Provider error: An unexpected error has occurred internal to Anthropic’s systems."
|
||||
AnthropicErrorCodes["529"] = "Provider error: Anthropic’s server is temporarily overloaded."
|
||||
}
|
||||
|
||||
func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
@ -141,12 +86,13 @@ func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("01-01-0001")
|
||||
return "JADE internal error: 01-01-0001. Please contact the support."
|
||||
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("01-02-0002")
|
||||
return "JADE internal error: 01-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -157,12 +103,14 @@ func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("01-02-0003")
|
||||
return "JADE internal error: 01-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("01-01-0004")
|
||||
return "JADE internal error: 01-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -175,6 +123,7 @@ func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse AnthropicChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("01-01-0005")
|
||||
return "JADE internal error: 01-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -188,6 +137,7 @@ func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("01-00-0006")
|
||||
return "JADE internal error: 01-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -196,7 +146,7 @@ func RequestAnthropic(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.InputTokens, chatCompletionResponse.Usage.OutputTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Content) == 0 {
|
||||
logMissingErrorCode.Println("Anthropic -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("01-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 01-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -25,11 +25,13 @@ func RequestHuggingface(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("10-01-0001")
|
||||
return "JADE internal error: 10-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("10-02-0002")
|
||||
return "JADE internal error: 10-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -39,12 +41,14 @@ func RequestHuggingface(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("10-02-0003")
|
||||
return "JADE internal error: 10-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("10-01-0004")
|
||||
return "JADE internal error: 10-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -57,7 +61,7 @@ func RequestHuggingface(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logMissingErrorCode.Println("Custom -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("10-01-0005 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 10-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -14,69 +14,13 @@ var DeepseekErrorCodes map[string]string
|
||||
|
||||
func init() {
|
||||
DeepseekErrorCodes = make(map[string]string)
|
||||
DeepseekErrorCodes["400"] = "Invalid Request - Please contact the support."
|
||||
DeepseekErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid."
|
||||
DeepseekErrorCodes["402"] = "You have run out of balance. Please check your account's balance."
|
||||
DeepseekErrorCodes["422"] = "Request exceeds the maximum allowed number of bytes."
|
||||
DeepseekErrorCodes["429"] = "Your account has hit a rate limit. You are sending requests too quickly."
|
||||
DeepseekErrorCodes["500"] = "An unexpected error has occurred internal to Deepseek’s systems."
|
||||
DeepseekErrorCodes["503"] = "Deepseek’s server is temporarily overloaded."
|
||||
}
|
||||
|
||||
func TestDeepseekKey(apiKey string) bool {
|
||||
url := "https://api.deepseek.com/chat/completions"
|
||||
|
||||
// Convert messages to OpenAI format
|
||||
deepseekMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "deepseek-chat",
|
||||
Messages: deepseekMessages,
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
DeepseekErrorCodes["400"] = "Provider error: Invalid Request - Please contact the support."
|
||||
DeepseekErrorCodes["401"] = "Provider error: Invalid Authentication - Ensure that the API key is still valid."
|
||||
DeepseekErrorCodes["402"] = "Provider error: You have run out of balance. Please check your account's balance."
|
||||
DeepseekErrorCodes["422"] = "Provider error: Request exceeds the maximum allowed number of bytes."
|
||||
DeepseekErrorCodes["429"] = "Provider error: Your account has hit a rate limit. You are sending requests too quickly."
|
||||
DeepseekErrorCodes["500"] = "Provider error: An unexpected error has occurred internal to Deepseek’s systems."
|
||||
DeepseekErrorCodes["503"] = "Provider error: Deepseek’s server is temporarily overloaded."
|
||||
}
|
||||
|
||||
func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
@ -110,11 +54,13 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("08-01-0001")
|
||||
return "JADE internal error: 08-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("08-02-0002")
|
||||
return "JADE internal error: 08-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -124,12 +70,14 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("08-02-0003")
|
||||
return "JADE internal error: 08-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("08-01-0004")
|
||||
return "JADE internal error: 08-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -142,6 +90,7 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("08-01-0005")
|
||||
return "JADE internal error: 08-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -155,6 +104,7 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("08-00-0006")
|
||||
return "JADE internal error: 08-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -163,7 +113,7 @@ func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Deepseek -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("08-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 08-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -10,60 +10,6 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestFireworkKey(apiKey string) bool {
|
||||
url := "https://api.fireworks.ai/inference/v1/chat/completions"
|
||||
|
||||
// Convert messages to OpenAI format
|
||||
fireworkMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "accounts/fireworks/models/llama-v2-7b-chat",
|
||||
Messages: fireworkMessages,
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
model := llm.Model.ModelID
|
||||
temperature := float64(llm.Temperature)
|
||||
@ -81,6 +27,7 @@ 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."
|
||||
}
|
||||
|
||||
@ -95,11 +42,13 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("09-01-0001")
|
||||
return "JADE internal error: 09-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("09-02-0002")
|
||||
return "JADE internal error: 09-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -109,12 +58,14 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("09-02-0003")
|
||||
return "JADE internal error: 09-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("09-01-0004")
|
||||
return "JADE internal error: 09-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -127,6 +78,7 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("09-01-0005")
|
||||
return "JADE internal error: 09-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -140,6 +92,7 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("09-00-0006")
|
||||
return "JADE internal error: 09-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -148,7 +101,7 @@ func RequestFirework(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Fireworks -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("09-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 09-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -54,66 +54,12 @@ var GoogleErrorCodes map[string]string
|
||||
// TODO: Update
|
||||
func init() {
|
||||
GoogleErrorCodes = make(map[string]string)
|
||||
GoogleErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid."
|
||||
GoogleErrorCodes["403"] = "Accessing the API from an unsupported country, region, or territory."
|
||||
GoogleErrorCodes["429"] = "Rate limit reached for requests - You are sending requests too quickly."
|
||||
GoogleErrorCodes["429"] = "You have run out of credits or hit your maximum monthly spend - Buy more credits or learn how to increase your limits."
|
||||
GoogleErrorCodes["500"] = "Issue on Provider servers - Retry your request after a brief wait and contact the provider if the issue persists."
|
||||
GoogleErrorCodes["503"] = "Servers are experiencing high traffic - Please retry your requests after a brief wait."
|
||||
}
|
||||
|
||||
func TestGoogleKey(apiKey string) bool {
|
||||
url := "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=" + apiKey
|
||||
|
||||
googlePart := GooglePart{
|
||||
Text: "Hello",
|
||||
}
|
||||
// Convert messages to OpenAI format
|
||||
googleMessages := []GoogleRequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Parts: []GooglePart{googlePart},
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := GoogleChatCompletionRequest{
|
||||
Messages: googleMessages,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse GoogleChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.UsageMetadata.CandidatesTokenCount == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
GoogleErrorCodes["401"] = "Provider error: Invalid Authentication - Ensure that the API key is still valid."
|
||||
GoogleErrorCodes["403"] = "Provider error: Accessing the API from an unsupported country, region, or territory."
|
||||
GoogleErrorCodes["429"] = "Provider error: Rate limit reached for requests - You are sending requests too quickly."
|
||||
GoogleErrorCodes["429"] = "Provider error: You have run out of credits or hit your maximum monthly spend - Buy more credits or learn how to increase your limits."
|
||||
GoogleErrorCodes["500"] = "Provider error: Issue on Provider servers - Retry your request after a brief wait and contact the provider if the issue persists."
|
||||
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 {
|
||||
@ -162,11 +108,13 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("03-01-0001")
|
||||
return "JADE internal error: 03-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("03-02-0002")
|
||||
return "JADE internal error: 03-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -175,12 +123,14 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("03-02-0003")
|
||||
return "JADE internal error: 03-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("03-01-0004")
|
||||
return "JADE internal error: 03-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -193,6 +143,7 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse GoogleChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("03-01-0005")
|
||||
return "JADE internal error: 03-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -206,6 +157,7 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("03-00-0006")
|
||||
return "JADE internal error: 03-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -214,7 +166,7 @@ func RequestGoogle(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.UsageMetadata.PromptTokenCount, chatCompletionResponse.UsageMetadata.CandidatesTokenCount, model)
|
||||
|
||||
if len(chatCompletionResponse.Candidates) == 0 {
|
||||
logMissingErrorCode.Println("Google -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("Google -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 03-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -14,67 +14,13 @@ var GroqErrorCodes map[string]string
|
||||
|
||||
func init() {
|
||||
GroqErrorCodes = make(map[string]string)
|
||||
GroqErrorCodes["400"] = "Invalid Request - Please contact the support."
|
||||
GroqErrorCodes["404"] = "The requested resource could not be found. Most likely the model is not available anymore."
|
||||
GroqErrorCodes["422"] = "The request was well-formed but could not be followed due to semantic errors. Verify the data provided for correctness and completeness."
|
||||
GroqErrorCodes["429"] = "Too many requests were sent in a given timeframe. Implement request throttling and respect rate limits."
|
||||
GroqErrorCodes["500"] = "A generic error occurred on Gorq's server. Try the request again later or contact support if the issue persists."
|
||||
GroqErrorCodes["502"] = "The server received an invalid response from an upstream server. This may be a temporary issue; retrying the request might resolve it."
|
||||
GroqErrorCodes["503"] = "The server is not ready to handle the request, often due to maintenance or overload. Wait before retrying the request."
|
||||
}
|
||||
|
||||
func TestGroqKey(apiKey string) bool {
|
||||
url := "https://api.groq.com/openai/v1/chat/completions"
|
||||
|
||||
// Convert messages to Qroq format
|
||||
groqMessages := []Message{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "llama3-8b-8192",
|
||||
Messages: Message2RequestMessage(groqMessages, ""),
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
GroqErrorCodes["400"] = "Provider error: Invalid Request - Please contact the support."
|
||||
GroqErrorCodes["404"] = "Provider error: The requested resource could not be found. Most likely the model is not available anymore."
|
||||
GroqErrorCodes["422"] = "Provider error: The request was well-formed but could not be followed due to semantic errors. Verify the data provided for correctness and completeness."
|
||||
GroqErrorCodes["429"] = "Provider error: Too many requests were sent in a given timeframe. Implement request throttling and respect rate limits."
|
||||
GroqErrorCodes["500"] = "Provider error: A generic error occurred on Gorq's server. Try the request again later or contact support if the issue persists."
|
||||
GroqErrorCodes["502"] = "Provider error: The server received an invalid response from an upstream server. This may be a temporary issue; retrying the request might resolve it."
|
||||
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 {
|
||||
@ -94,6 +40,7 @@ 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."
|
||||
}
|
||||
|
||||
@ -108,11 +55,13 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("04-01-0001")
|
||||
return "JADE internal error: 04-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("04-02-0002")
|
||||
return "JADE internal error: 04-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -122,12 +71,14 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("04-02-0003")
|
||||
return "JADE internal error: 04-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("04-01-0004")
|
||||
return "JADE internal error: 04-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -140,6 +91,7 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("04-01-0005")
|
||||
return "JADE internal error: 04-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -153,6 +105,7 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("04-00-0006")
|
||||
return "JADE internal error: 04-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -161,7 +114,7 @@ func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Groq -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("04-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 04-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -10,60 +10,6 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestMistralKey(apiKey string) bool {
|
||||
url := "https://api.mistral.ai/v1/chat/completions"
|
||||
|
||||
// Convert messages to Mistral format
|
||||
mistralMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "open-mistral-7b",
|
||||
Messages: mistralMessages,
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
model := llm.Model.ModelID
|
||||
temperature := float64(llm.Temperature)
|
||||
@ -81,6 +27,7 @@ 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."
|
||||
}
|
||||
|
||||
@ -95,11 +42,13 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("02-01-0001")
|
||||
return "JADE internal error: 02-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("02-02-0002")
|
||||
return "JADE internal error: 02-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -109,12 +58,14 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("02-02-0003")
|
||||
return "JADE internal error: 02-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("02-01-0004")
|
||||
return "JADE internal error: 02-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -127,6 +78,7 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("02-01-0005")
|
||||
return "JADE internal error: 02-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -140,6 +92,7 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("02-00-0006")
|
||||
return "JADE internal error: 02-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -148,7 +101,7 @@ func RequestMistral(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Mistral -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("02-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 02-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -10,61 +10,6 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestNimKey(apiKey string) bool {
|
||||
url := "https://integrate.api.nvidia.com/v1/chat/completions"
|
||||
|
||||
// Convert messages to OpenAI format
|
||||
nimMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "meta/llama3-8b-instruct",
|
||||
Messages: nimMessages,
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
model := llm.Model.ModelID
|
||||
temperature := float64(llm.Temperature)
|
||||
@ -82,6 +27,7 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
select filtered_keys.key limit 1
|
||||
`, &apiKey, "nim")
|
||||
if err != nil {
|
||||
logErrorCode.Println("05-0")
|
||||
return "JADE internal error: 05-00-0000. Please contact the support."
|
||||
}
|
||||
|
||||
@ -96,11 +42,13 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("05-01-0001")
|
||||
return "JADE internal error: 05-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("05-02-0002")
|
||||
return "JADE internal error: 05-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -110,12 +58,14 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("05-02-0003")
|
||||
return "JADE internal error: 05-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("05-01-0004")
|
||||
return "JADE internal error: 05-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -128,6 +78,7 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("05-01-0005")
|
||||
return "JADE internal error: 05-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -141,6 +92,7 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("05-00-0006")
|
||||
return "JADE internal error: 05-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -149,7 +101,7 @@ func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Nim -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("05-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 05-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -42,67 +42,13 @@ var OpenaiErrorCodes map[string]string
|
||||
|
||||
func init() {
|
||||
OpenaiErrorCodes = make(map[string]string)
|
||||
OpenaiErrorCodes["400"] = "Invalid Request - Please contact the support."
|
||||
OpenaiErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid."
|
||||
OpenaiErrorCodes["403"] = "Accessing the API from an unsupported country, region, or territory."
|
||||
OpenaiErrorCodes["404"] = "Model not found."
|
||||
OpenaiErrorCodes["429"] = "Rate limit reached for requests - You are sending requests too quickly OR You have run out of credits or hit your maximum monthly spend - Buy more credits or learn how to increase your limits."
|
||||
OpenaiErrorCodes["500"] = "Issue on Provider servers - Retry your request after a brief wait and contact the provider if the issue persists."
|
||||
OpenaiErrorCodes["503"] = "Servers are experiencing high traffic - Please retry your requests after a brief wait."
|
||||
}
|
||||
|
||||
func TestOpenaiKey(apiKey string) bool {
|
||||
url := "https://api.openai.com/v1/chat/completions"
|
||||
|
||||
// Convert messages to OpenAI format
|
||||
openaiMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "gpt-3.5-turbo",
|
||||
Messages: openaiMessages,
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
OpenaiErrorCodes["400"] = "Provider error: Invalid Request - Please contact the support."
|
||||
OpenaiErrorCodes["401"] = "Provider error: Invalid Authentication - Ensure that the API key is still valid."
|
||||
OpenaiErrorCodes["403"] = "Provider error: Accessing the API from an unsupported country, region, or territory."
|
||||
OpenaiErrorCodes["404"] = "Provider error: Model not found."
|
||||
OpenaiErrorCodes["429"] = "Provider error: Rate limit reached for requests - You are sending requests too quickly OR You have run out of credits or hit your maximum monthly spend - Buy more credits or learn how to increase your limits."
|
||||
OpenaiErrorCodes["500"] = "Provider error: Issue on Provider servers - Retry your request after a brief wait and contact the provider if the issue persists."
|
||||
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 {
|
||||
@ -122,6 +68,7 @@ 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."
|
||||
}
|
||||
|
||||
@ -136,11 +83,13 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("00-01-0001")
|
||||
return "JADE internal error: 00-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("00-02-0002")
|
||||
return "JADE internal error: 00-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -150,12 +99,14 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("00-02-0003")
|
||||
return "JADE internal error: 00-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("00-01-0004")
|
||||
return "JADE internal error: 00-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -168,6 +119,7 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("00-01-0005")
|
||||
return "JADE internal error: 00-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -181,6 +133,7 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("00-00-0006")
|
||||
return "JADE internal error: 00-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -189,7 +142,7 @@ func RequestOpenai(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Openai -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("00-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 00-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -10,60 +10,6 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestPerplexityKey(apiKey string) bool {
|
||||
url := "https://api.perplexity.ai/chat/completions"
|
||||
|
||||
// Convert messages to OpenAI format
|
||||
perplexityMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "llama-3-8b-instruct",
|
||||
Messages: perplexityMessages,
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
model := llm.Model.ModelID
|
||||
temperature := float64(llm.Temperature)
|
||||
@ -81,6 +27,7 @@ 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."
|
||||
}
|
||||
|
||||
@ -95,11 +42,13 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("06-01-0001")
|
||||
return "JADE internal error: 06-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("06-02-0002")
|
||||
return "JADE internal error: 06-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -109,12 +58,14 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("06-02-0003")
|
||||
return "JADE internal error: 06-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("06-01-0004")
|
||||
return "JADE internal error: 06-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -127,6 +78,7 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("06-01-0005")
|
||||
return "JADE internal error: 06-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -140,6 +92,7 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("06-00-0006")
|
||||
return "JADE internal error: 06-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -148,7 +101,7 @@ func RequestPerplexity(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Perplexity -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("06-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 06-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
@ -10,23 +10,6 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var TogetherErrorCodes map[string]string
|
||||
|
||||
func init() {
|
||||
TogetherErrorCodes = make(map[string]string)
|
||||
TogetherErrorCodes["400"] = "Invalid Request - Please contact the support."
|
||||
TogetherErrorCodes["401"] = "Invalid Authentication - Ensure that the API key is still valid."
|
||||
TogetherErrorCodes["403"] = "Set max_tokens to a lower number. Or leave it empty for using max value."
|
||||
TogetherErrorCodes["404"] = "Model not found."
|
||||
TogetherErrorCodes["429"] = "Rate limit reached for requests - You are sending requests too quickly."
|
||||
TogetherErrorCodes["500"] = "Issue on Together AI servers - Retry your request after a brief wait and contact Together AI if the issue persists."
|
||||
TogetherErrorCodes["503"] = "Servers are experiencing high traffic - Please retry your requests after a brief wait."
|
||||
TogetherErrorCodes["504"] = "Servers are experiencing high traffic - Please retry your requests after a brief wait."
|
||||
TogetherErrorCodes["520"] = "An unexpected error has occurred internal to Together’s systems."
|
||||
TogetherErrorCodes["524"] = "An unexpected error has occurred internal to Together’s systems."
|
||||
TogetherErrorCodes["529"] = "An unexpected error has occurred internal to Together’s systems."
|
||||
}
|
||||
|
||||
type TogetherChatCompletionResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
@ -42,58 +25,21 @@ type TogetherChoice struct {
|
||||
Index int `json:"index"`
|
||||
}
|
||||
|
||||
func TestTogetherKey(apiKey string) bool {
|
||||
url := "https://api.together.xyz/v1/completions"
|
||||
var TogetherErrorCodes map[string]string
|
||||
|
||||
// Convert messages to OpenAI format
|
||||
togetherMessages := []RequestMessage{
|
||||
{
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
},
|
||||
}
|
||||
|
||||
requestBody := OpenaiChatCompletionRequest{
|
||||
Model: "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
||||
Messages: togetherMessages,
|
||||
Temperature: 0,
|
||||
MaxTokens: 10,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var chatCompletionResponse OpenaiChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if chatCompletionResponse.Usage.CompletionTokens == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
func init() {
|
||||
TogetherErrorCodes = make(map[string]string)
|
||||
TogetherErrorCodes["400"] = "Provider error: Invalid Request - Please contact the support."
|
||||
TogetherErrorCodes["401"] = "Provider error: nvalid Authentication - Ensure that the API key is still valid."
|
||||
TogetherErrorCodes["403"] = "Provider error: et max_tokens to a lower number. Or leave it empty for using max value."
|
||||
TogetherErrorCodes["404"] = "Provider error: odel not found."
|
||||
TogetherErrorCodes["429"] = "Provider error: ate limit reached for requests - You are sending requests too quickly."
|
||||
TogetherErrorCodes["500"] = "Provider error: ssue on Together AI servers - Retry your request after a brief wait and contact Together AI if the issue persists."
|
||||
TogetherErrorCodes["503"] = "Provider error: ervers are experiencing high traffic - Please retry your requests after a brief wait."
|
||||
TogetherErrorCodes["504"] = "Provider error: ervers are experiencing high traffic - Please retry your requests after a brief wait."
|
||||
TogetherErrorCodes["520"] = "Provider error: n unexpected error has occurred internal to Together’s systems."
|
||||
TogetherErrorCodes["524"] = "Provider error: n unexpected error has occurred internal to Together’s systems."
|
||||
TogetherErrorCodes["529"] = "Provider error: n unexpected error has occurred internal to Together’s systems."
|
||||
}
|
||||
|
||||
func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
@ -113,6 +59,7 @@ 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."
|
||||
}
|
||||
|
||||
@ -127,11 +74,13 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
logErrorCode.Println("07-01-0001")
|
||||
return "JADE internal error: 07-01-0001. Please contact the support."
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
logErrorCode.Println("07-02-0002")
|
||||
return "JADE internal error: 07-02-0002. Please contact the support."
|
||||
}
|
||||
|
||||
@ -141,12 +90,14 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logErrorCode.Println("07-02-0003")
|
||||
return "JADE internal error: 07-02-0003. Please contact the support."
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logErrorCode.Println("07-01-0004")
|
||||
return "JADE internal error: 07-01-0004. Please contact the support."
|
||||
}
|
||||
|
||||
@ -159,6 +110,7 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
var chatCompletionResponse TogetherChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
logErrorCode.Println("07-01-0005")
|
||||
return "JADE internal error: 07-01-0005. Please contact the support."
|
||||
}
|
||||
|
||||
@ -172,6 +124,7 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
LIMIT 1
|
||||
`, &usedModelInfo, model)
|
||||
if err != nil {
|
||||
logErrorCode.Println("07-00-0006")
|
||||
return "JADE internal error: 07-00-0006. Please contact the support."
|
||||
}
|
||||
|
||||
@ -180,7 +133,7 @@ func RequestTogether(c *fiber.Ctx, llm LLM, messages []Message) string {
|
||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
if len(chatCompletionResponse.Choices) == 0 {
|
||||
logMissingErrorCode.Println("Together -", resp.Status, "-", string(body))
|
||||
logErrorCode.Println("07-03-0007 -", resp.Status, "-", string(body))
|
||||
return "JADE internal error: 07-03-0007. Please contact the support."
|
||||
}
|
||||
|
||||
|
60
main.go
60
main.go
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
@ -120,7 +121,7 @@ func main() {
|
||||
})
|
||||
|
||||
app.Get("/ws", websocket.New(handleWebSocket))
|
||||
app.Get("/missingErrorLogs", MissingErrorCodeLogsHandler)
|
||||
app.Get("/errorLogs", ErrorCodeLogsHandler)
|
||||
|
||||
// Start server
|
||||
if err := app.Listen(":8080"); err != nil {
|
||||
@ -143,17 +144,30 @@ func addKeys(c *fiber.Ctx) error {
|
||||
"deepseek": c.FormValue("deepseek_key"),
|
||||
}
|
||||
|
||||
testFunctions := map[string]func(string) bool{
|
||||
"openai": TestOpenaiKey,
|
||||
"anthropic": TestAnthropicKey,
|
||||
"mistral": TestMistralKey,
|
||||
"groq": TestGroqKey,
|
||||
"google": TestGoogleKey,
|
||||
"nim": TestNimKey,
|
||||
"perplexity": TestPerplexityKey,
|
||||
"fireworks": TestFireworkKey,
|
||||
"together": TestTogetherKey,
|
||||
"deepseek": TestDeepseekKey,
|
||||
requestFunctions := map[string]func(*fiber.Ctx, LLM, []Message) string{
|
||||
"openai": RequestOpenai,
|
||||
"anthropic": RequestAnthropic,
|
||||
"mistral": RequestMistral,
|
||||
"groq": RequestGroq,
|
||||
"google": RequestGoogle,
|
||||
"nim": RequestNim,
|
||||
"perplexity": RequestPerplexity,
|
||||
"fireworks": RequestFirework,
|
||||
"together": RequestTogether,
|
||||
"deepseek": RequestDeepseek,
|
||||
}
|
||||
|
||||
testModels := map[string]string{
|
||||
"openai": "gpt-3.5-turbo",
|
||||
"anthropic": "claude-3-haiku-20240307",
|
||||
"mistral": "open-mistral-7b",
|
||||
"groq": "llama3-8b-8192",
|
||||
"google": "gemini-pro",
|
||||
"nim": "meta/llama3-8b-instruct",
|
||||
"perplexity": "llama-3-8b-instruct",
|
||||
"fireworks": "accounts/fireworks/models/llama-v2-7b-chat",
|
||||
"together": "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
||||
"deepseek": "deepseek-chat",
|
||||
}
|
||||
|
||||
openaiExists, anthropicExists, mistralExists, groqExists, googleExists, perplexityExists, fireworksExists, nimExists, togetherExists, deepseekExists := getExistingKeysNew(c)
|
||||
@ -171,9 +185,29 @@ func addKeys(c *fiber.Ctx) error {
|
||||
"deepseek": deepseekExists,
|
||||
}
|
||||
|
||||
var messages = []Message{
|
||||
{
|
||||
Content: "You are useful",
|
||||
Role: "context",
|
||||
},
|
||||
{
|
||||
Content: "hello",
|
||||
Role: "user",
|
||||
},
|
||||
}
|
||||
|
||||
for company, key := range keys {
|
||||
if key != "" {
|
||||
if !testFunctions[company](key) {
|
||||
var llm LLM = LLM{
|
||||
Model: ModelInfo{ModelID: testModels[company]},
|
||||
Temperature: 0,
|
||||
MaxToken: 10,
|
||||
Context: "",
|
||||
}
|
||||
|
||||
var responseText string = requestFunctions[company](c, llm, messages)
|
||||
|
||||
if responseText == "" || strings.Contains(responseText, "JADE internal error") || strings.Contains(responseText, "Provider error") {
|
||||
return c.SendString(fmt.Sprintf("Invalid %s API Key\n", company))
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user