package main import ( "bytes" "encoding/json" "io" "net/http" "strings" "github.com/gofiber/fiber/v2" ) type GoogleRequestMessage struct { Role string `json:"role"` Parts []GooglePart `json:"parts"` } type GooglePart struct { Text string `json:"text"` } type GoogleChatCompletionRequest struct { Messages []GoogleRequestMessage `json:"contents"` } type GoogleChatCompletionResponse struct { Candidates []GoogleCandidate `json:"candidates"` UsageMetadata GoogleUsageMetadata `json:"usageMetadata"` } type GoogleCandidate struct { Content struct { Parts []GooglePart `json:"parts"` Role string `json:"role"` } `json:"content"` FinishReason string `json:"finishReason"` Index int `json:"index"` SafetyRatings []GoogleSafetyRating `json:"safetyRatings"` } type GoogleSafetyRating struct { Category string `json:"category"` Probability string `json:"probability"` } type GoogleUsageMetadata struct { PromptTokenCount int32 `json:"promptTokenCount"` CandidatesTokenCount int32 `json:"candidatesTokenCount"` TotalTokenCount int32 `json:"totalTokenCount"` } var GoogleErrorCodes map[string]string // TODO: Update func init() { GoogleErrorCodes = make(map[string]string) 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 { model := llm.Model.ModelID // TODO: Use those parameters // 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("03-00-0006") return "JADE internal error: 03-00-0006. Please contact the support." } var inputCost float32 = float32(chatCompletionResponse.UsageMetadata.PromptTokenCount) * usedModelInfo.InputPrice var outputCost float32 = float32(chatCompletionResponse.UsageMetadata.CandidatesTokenCount) * usedModelInfo.OutputPrice addUsage(c, inputCost, outputCost, chatCompletionResponse.UsageMetadata.PromptTokenCount, chatCompletionResponse.UsageMetadata.CandidatesTokenCount, model) if len(chatCompletionResponse.Candidates) == 0 { logErrorCode.Println("Google -", resp.Status, "-", string(body)) return "JADE internal error: 03-03-0007. Please contact the support." } return chatCompletionResponse.Candidates[0].Content.Parts[0].Text }