package main import ( "bytes" "encoding/json" "io" "net/http" "strings" "github.com/gofiber/fiber/v2" ) type OpenaiChatCompletionRequest struct { Model string `json:"model"` Messages []RequestMessage `json:"messages"` MaxTokens int `json:"max_tokens"` Temperature float64 `json:"temperature"` } type OpenaiChatCompletionResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Usage OpenaiUsage `json:"usage"` Choices []OpenaiChoice `json:"choices"` } type OpenaiUsage struct { PromptTokens int32 `json:"prompt_tokens"` CompletionTokens int32 `json:"completion_tokens"` TotalTokens int32 `json:"total_tokens"` } type OpenaiChoice struct { Message Message `json:"message"` FinishReason string `json:"finish_reason"` Index int `json:"index"` } var OpenaiErrorCodes map[string]string func init() { OpenaiErrorCodes = make(map[string]string) 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 { 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 { logErrorCode.Println("00-00-0006") return "JADE internal error: 00-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("00-03-0007 -", resp.Status, "-", string(body)) return "JADE internal error: 00-03-0007. Please contact the support." } return chatCompletionResponse.Choices[0].Message.Content }