package main import ( "bytes" "encoding/json" "io" "net/http" "strings" "github.com/gofiber/fiber/v2" ) 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 } func RequestGroq(c *fiber.Ctx, llm LLM, messages []Message) 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, ` with filtered_keys := ( select Key { key } filter .company.name = $0 AND .$0 LIMIT 1 `, &usedModelInfo, model) if err != nil { return "JADE internal error: 04-00-0006. Please contact the support." } var inputCost float32 = float32(chatCompletionResponse.Usage.PromptTokens) * usedModelInfo.InputPrice var outputCost float32 = float32(chatCompletionResponse.Usage.CompletionTokens) * usedModelInfo.OutputPrice addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model) if len(chatCompletionResponse.Choices) == 0 { logMissingErrorCode.Println("Groq -", resp.Status, "-", string(body)) return "JADE internal error: 04-03-0007. Please contact the support." } return chatCompletionResponse.Choices[0].Message.Content }