Jade/RequestDeepseek.go
MrBounty edd5c58a7b Lots of fix
Can now add new api key again, and some minor bugs
2024-08-14 17:52:30 +02:00

124 lines
4.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"] = "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 Deepseeks systems."
DeepseekErrorCodes["503"] = "Provider error: Deepseeks server is temporarily overloaded."
}
func RequestDeepseek(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)
url := "https://api.deepseek.com/chat/completions"
var apiKey string
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 {
key
} filter .company.name = <str>$0 AND .<keys[is Setting].<setting[is User] = global currentUser
)
select filtered_keys.key limit 1
`, &apiKey, "deepseek")
if err != nil {
logErrorCode.Println("08-00-0000")
return "JADE internal error: 08-00-0000. Please contact the support."
}
} else {
apiKey = testApiKey
}
requestBody := OpenaiChatCompletionRequest{
Model: model,
Messages: Message2RequestMessage(messages, context),
MaxTokens: maxTokens,
Temperature: temperature,
}
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."
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
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."
}
for key, value := range DeepseekErrorCodes {
if strings.Contains(resp.Status, key) {
return value
}
}
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."
}
if testApiKey != "" {
return chatCompletionResponse.Choices[0].Message.Content
}
var usedModelInfo ModelInfo
usedModelInfo, found := getModelInfoByID(llm.Model.ID)
if !found {
logErrorCode.Println("08-00-0006")
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 {
logErrorCode.Println("08-03-0007 -", resp.Status, "-", string(body))
return "JADE internal error: 08-03-0007. Please contact the support."
}
return chatCompletionResponse.Choices[0].Message.Content
}