diff --git a/Chat.go b/Chat.go
index e4f90c5..edeae79 100644
--- a/Chat.go
+++ b/Chat.go
@@ -326,9 +326,9 @@ func LoadUsageKPIHandler(c *fiber.Ctx) error {
}
func LoadKeysHandler(c *fiber.Ctx) error {
- openaiExists, anthropicExists, mistralExists := getExistingKeys()
+ openaiExists, anthropicExists, mistralExists, groqExists := getExistingKeys()
- out, err := pongo2.Must(pongo2.FromFile("views/partials/popover-keys.html")).Execute(pongo2.Context{"IsLogin": checkIfLogin(), "OpenaiExists": openaiExists, "AnthropicExists": anthropicExists, "MistralExists": mistralExists})
+ out, err := pongo2.Must(pongo2.FromFile("views/partials/popover-keys.html")).Execute(pongo2.Context{"IsLogin": checkIfLogin(), "OpenaiExists": openaiExists, "AnthropicExists": anthropicExists, "MistralExists": mistralExists, "GroqExists": groqExists})
if err != nil {
c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error rendering template",
@@ -338,7 +338,7 @@ func LoadKeysHandler(c *fiber.Ctx) error {
}
func LoadModelSelectionHandler(c *fiber.Ctx) error {
- openaiExists, anthropicExists, mistralExists := getExistingKeys()
+ openaiExists, anthropicExists, mistralExists, groqExists := getExistingKeys()
var CompanyInfosAvailable []CompanyInfo
@@ -373,6 +373,17 @@ func LoadModelSelectionHandler(c *fiber.Ctx) error {
CompanyInfosAvailable = append(CompanyInfosAvailable, mistralCompanyInfo)
}
+ if groqExists {
+ var groqCompanyInfo CompanyInfo
+ for _, info := range CompanyInfos {
+ if info.ID == "groq" {
+ groqCompanyInfo = info
+ break
+ }
+ }
+ CompanyInfosAvailable = append(CompanyInfosAvailable, groqCompanyInfo)
+ }
+
CheckedModels := []string{"gpt-3.5-turbo"} // Default model
out, err := pongo2.Must(pongo2.FromFile("views/partials/popover-models.html")).Execute(pongo2.Context{
"CompanyInfos": CompanyInfosAvailable,
diff --git a/Request.go b/Request.go
index f96d611..752bd23 100644
--- a/Request.go
+++ b/Request.go
@@ -88,6 +88,12 @@ func GenerateMultipleMessages(c *fiber.Ctx) error {
response := addMistralMessage(lastSelectedModelIds[idx], idx == 0)
InsertedIDs = append(InsertedIDs, response)
}()
+ } else if model2Icon(lastSelectedModelIds[i]) == "groq" {
+ go func() {
+ defer wg.Done()
+ response := addGroqMessage(lastSelectedModelIds[idx], idx == 0)
+ InsertedIDs = append(InsertedIDs, response)
+ }()
}
}
diff --git a/RequestGroq.go b/RequestGroq.go
new file mode 100644
index 0000000..7a929e4
--- /dev/null
+++ b/RequestGroq.go
@@ -0,0 +1,255 @@
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+
+ "github.com/edgedb/edgedb-go"
+)
+
+type GroqChatCompletionRequest struct {
+ Model string `json:"model"`
+ Messages []GroqMessage `json:"messages"`
+ Temperature float64 `json:"temperature"`
+}
+
+type GroqMessage struct {
+ Role string `json:"role"`
+ Content string `json:"content"`
+}
+
+type GroqChatCompletionResponse struct {
+ ID string `json:"id"`
+ Object string `json:"object"`
+ Created int64 `json:"created"`
+ Model string `json:"model"`
+ Usage GroqUsage `json:"usage"`
+ Choices []GroqChoice `json:"choices"`
+}
+
+type GroqUsage struct {
+ PromptTokens int32 `json:"prompt_tokens"`
+ CompletionTokens int32 `json:"completion_tokens"`
+ TotalTokens int32 `json:"total_tokens"`
+}
+
+type GroqChoice struct {
+ Message GroqMessage `json:"message"`
+ FinishReason string `json:"finish_reason"`
+ Index int `json:"index"`
+}
+
+func init() {
+ var ModelInfosList = []ModelInfo{}
+
+ modelInfo := ModelInfo{
+ ID: "llama3-8b-8192",
+ Name: "Llama 8B",
+ Icon: "groq",
+ MaxToken: 8192,
+ InputPrice: 0.00 / 1000000,
+ OutputPrice: 0.00 / 1000000,
+ }
+ ModelInfosList = append(ModelInfosList, modelInfo)
+ ModelsInfos = append(ModelsInfos, modelInfo)
+
+ modelInfo = ModelInfo{
+ ID: "llama3-70b-8192",
+ Name: "Llama 70B",
+ Icon: "groq",
+ MaxToken: 8192,
+ InputPrice: 0.00 / 1000000,
+ OutputPrice: 0.00 / 1000000,
+ }
+ ModelInfosList = append(ModelInfosList, modelInfo)
+ ModelsInfos = append(ModelsInfos, modelInfo)
+
+ modelInfo = ModelInfo{
+ ID: "gemma-7b-it",
+ Name: "Gemma 7B",
+ Icon: "groq",
+ MaxToken: 8192,
+ InputPrice: 0.00 / 1000000,
+ OutputPrice: 0.00 / 1000000,
+ }
+ ModelInfosList = append(ModelInfosList, modelInfo)
+ ModelsInfos = append(ModelsInfos, modelInfo)
+
+ companyInfo := CompanyInfo{
+ ID: "groq",
+ Name: "Groq",
+ Icon: "icons/groq.png",
+ ModelInfos: ModelInfosList,
+ }
+ CompanyInfos = append(CompanyInfos, companyInfo)
+}
+
+func addGroqMessage(modelID string, selected bool) edgedb.UUID {
+ Messages := getAllMessages()
+
+ chatCompletion, err := RequestGroq(modelID, Messages, 0.7)
+ if err != nil {
+ fmt.Println("Error:", err)
+ } else if len(chatCompletion.Choices) == 0 {
+ fmt.Println(chatCompletion)
+ fmt.Println("No response from Groq")
+ id := insertBotMessage("No response from Groq", selected, modelID)
+ return id
+ } else {
+ Content := chatCompletion.Choices[0].Message.Content
+ id := insertBotMessage(Content, selected, modelID)
+ return id
+ }
+ return edgedb.UUID{}
+}
+
+func EdgeMessages2GroqMessages(messages []Message) []GroqMessage {
+ groqMessages := make([]GroqMessage, len(messages))
+ for i, msg := range messages {
+ var role string
+ switch msg.Role {
+ case "user":
+ role = "user"
+ case "bot":
+ role = "assistant"
+ default:
+ role = "system"
+ }
+ groqMessages[i] = GroqMessage{
+ Role: role,
+ Content: msg.Content,
+ }
+ }
+ return groqMessages
+}
+
+func TestGroqKey(apiKey string) bool {
+ url := "https://api.groq.com/openai/v1/chat/completions"
+
+ // Convert messages to Qroq format
+ groqMessages := []GroqMessage{
+ {
+ Role: "user",
+ Content: "Hello",
+ },
+ }
+
+ requestBody := GroqChatCompletionRequest{
+ Model: "llama3-8b-8192",
+ Messages: groqMessages,
+ Temperature: 0,
+ }
+
+ 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 GroqChatCompletionResponse
+ err = json.Unmarshal(body, &chatCompletionResponse)
+ fmt.Println(chatCompletionResponse)
+ if err != nil {
+ return false
+ }
+ if chatCompletionResponse.Usage.CompletionTokens == 0 {
+ return false
+ }
+ return true
+}
+
+func RequestGroq(model string, messages []Message, temperature float64) (GroqChatCompletionResponse, error) {
+ var apiKey string
+ err := edgeClient.QuerySingle(edgeCtx, `
+ with
+ filtered_keys := (
+ select Key {
+ key
+ } filter .company =