package main import ( "bytes" "encoding/json" "io" "net/http" "strings" "github.com/gofiber/fiber/v2" ) 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 } func RequestDeepseek(c *fiber.Ctx, llm LLM, messages []Message) string { model := llm.Model.ModelID temperature := float64(llm.Temperature) context := llm.Context maxTokens := int(llm.MaxToken) 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, ` with filtered_keys := ( select Key { key } filter .company.name = $0 AND .$0 LIMIT 1 `, &usedModelInfo, model) if err != nil { return "JADE internal error: 08-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("Deepseek -", resp.Status, "-", string(body)) return "JADE internal error: 08-03-0007. Please contact the support." } return chatCompletionResponse.Choices[0].Message.Content }