Added Groq
This commit is contained in:
parent
404d61e95e
commit
d6ebfdc712
17
Chat.go
17
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,
|
||||
|
@ -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)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
|
255
RequestGroq.go
Normal file
255
RequestGroq.go
Normal file
@ -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 = <str>$0
|
||||
)
|
||||
select filtered_keys.key limit 1
|
||||
`, &apiKey, "groq")
|
||||
if err != nil {
|
||||
return GroqChatCompletionResponse{}, fmt.Errorf("error getting Groq API key: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println("API key:", apiKey)
|
||||
|
||||
url := "https://api.groq.com/openai/v1/chat/completions"
|
||||
|
||||
// Convert messages to Qroq format
|
||||
groqMessages := EdgeMessages2GroqMessages(messages)
|
||||
|
||||
requestBody := GroqChatCompletionRequest{
|
||||
Model: model,
|
||||
Messages: groqMessages,
|
||||
Temperature: temperature,
|
||||
}
|
||||
|
||||
fmt.Println(requestBody)
|
||||
|
||||
jsonBody, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return GroqChatCompletionResponse{}, fmt.Errorf("error marshaling JSON: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return GroqChatCompletionResponse{}, fmt.Errorf("error creating request: %w", err)
|
||||
}
|
||||
|
||||
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 GroqChatCompletionResponse{}, fmt.Errorf("error sending request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return GroqChatCompletionResponse{}, fmt.Errorf("error reading response body: %w", err)
|
||||
}
|
||||
|
||||
var chatCompletionResponse GroqChatCompletionResponse
|
||||
err = json.Unmarshal(body, &chatCompletionResponse)
|
||||
if err != nil {
|
||||
return GroqChatCompletionResponse{}, fmt.Errorf("error unmarshaling JSON: %w", err)
|
||||
}
|
||||
|
||||
var usedModelInfo ModelInfo
|
||||
for mi := range ModelsInfos {
|
||||
if ModelsInfos[mi].ID == model {
|
||||
usedModelInfo = ModelsInfos[mi]
|
||||
}
|
||||
}
|
||||
var inputCost float32 = float32(chatCompletionResponse.Usage.PromptTokens) * usedModelInfo.InputPrice
|
||||
var outputCost float32 = float32(chatCompletionResponse.Usage.CompletionTokens) * usedModelInfo.OutputPrice
|
||||
addUsage(inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||
|
||||
return chatCompletionResponse, nil
|
||||
}
|
66
main.go
66
main.go
@ -75,6 +75,7 @@ func addKeys(c *fiber.Ctx) error {
|
||||
openaiKey := c.FormValue("openai_key")
|
||||
anthropicKey := c.FormValue("anthropic_key")
|
||||
mistralKey := c.FormValue("mistral_key")
|
||||
groqKey := c.FormValue("groq_key")
|
||||
var Exists bool
|
||||
|
||||
// Handle OpenAI key
|
||||
@ -273,5 +274,70 @@ func addKeys(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Groq key
|
||||
if groqKey != "" {
|
||||
// Check if the OpenAI key already exists
|
||||
err := edgeClient.QuerySingle(edgeCtx, `
|
||||
select exists (
|
||||
select global currentUser.setting.keys
|
||||
filter .company = "groq" AND .key = <str>$0
|
||||
);
|
||||
`, &Exists, openaiKey)
|
||||
if err != nil {
|
||||
fmt.Println("Error in edgedb.QuerySingle: in addGroqKey: ", err)
|
||||
return c.SendString("")
|
||||
}
|
||||
if Exists {
|
||||
fmt.Println("Groq key already exists")
|
||||
return c.SendString("")
|
||||
}
|
||||
|
||||
if !TestGroqKey(groqKey) {
|
||||
fmt.Println("Invalid Groq API Key")
|
||||
return c.SendString("Invalid Groq API Key\n")
|
||||
}
|
||||
|
||||
// Check if the company key already exists
|
||||
err = edgeClient.QuerySingle(edgeCtx, `
|
||||
select exists (
|
||||
select global currentUser.setting.keys
|
||||
filter .company = "groq"
|
||||
);
|
||||
`, &Exists)
|
||||
if err != nil {
|
||||
fmt.Println("Error in edgedb.QuerySingle: in addGroqKey")
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
if Exists {
|
||||
err = edgeClient.Execute(edgeCtx, `
|
||||
UPDATE Key filter .company = "groq" AND .key = <str>$0
|
||||
SET {
|
||||
key := <str>$0,
|
||||
}
|
||||
`, groqKey)
|
||||
if err != nil {
|
||||
fmt.Println("Error in edgedb.QuerySingle: in addGroqKey")
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
err = edgeClient.Execute(edgeCtx, `
|
||||
UPDATE global currentUser.setting
|
||||
SET {
|
||||
keys += (
|
||||
INSERT Key {
|
||||
company := "groq",
|
||||
key := <str>$0,
|
||||
name := "Groq API Key",
|
||||
}
|
||||
)
|
||||
}`, groqKey)
|
||||
if err != nil {
|
||||
fmt.Println("Error in edgedb.QuerySingle: in addGroqKey")
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.SendString("<script>window.location.reload()</script>")
|
||||
}
|
||||
|
BIN
static/icons/groq.png
Normal file
BIN
static/icons/groq.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
21
utils.go
21
utils.go
@ -53,6 +53,9 @@ func model2Icon(model string) string {
|
||||
if strings.Contains(model, "mistral") || strings.Contains(model, "mixtral") {
|
||||
return "mistral"
|
||||
}
|
||||
if strings.Contains(model, "llama3") || strings.Contains(model, "gemma") {
|
||||
return "groq"
|
||||
}
|
||||
return "bouvai2"
|
||||
}
|
||||
|
||||
@ -65,14 +68,15 @@ func model2Name(model string) string {
|
||||
return "You"
|
||||
}
|
||||
|
||||
func getExistingKeys() (bool, bool, bool) {
|
||||
func getExistingKeys() (bool, bool, bool, bool) {
|
||||
if edgeClient == nil {
|
||||
return false, false, false
|
||||
return false, false, false, false
|
||||
}
|
||||
|
||||
var openaiExists bool
|
||||
var anthropicExists bool
|
||||
var mistralExists bool
|
||||
var groqExists bool
|
||||
|
||||
err := edgeClient.QuerySingle(edgeCtx, `
|
||||
select exists (
|
||||
@ -107,5 +111,16 @@ func getExistingKeys() (bool, bool, bool) {
|
||||
mistralExists = false
|
||||
}
|
||||
|
||||
return openaiExists, anthropicExists, mistralExists
|
||||
err = edgeClient.QuerySingle(edgeCtx, `
|
||||
select exists (
|
||||
select global currentUser.setting.keys
|
||||
filter .company = "groq"
|
||||
);
|
||||
`, &groqExists)
|
||||
if err != nil {
|
||||
fmt.Println("Error in edgedb.QuerySingle checking for mistral: ", err)
|
||||
groqExists = false
|
||||
}
|
||||
|
||||
return openaiExists, anthropicExists, mistralExists, groqExists
|
||||
}
|
||||
|
@ -25,8 +25,4 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function clearTextArea() {
|
||||
document.querySelector('.textarea').value = '';
|
||||
}
|
||||
</script>
|
||||
<script> function clearTextArea() { setTimeout(function () { document.querySelector('.textarea').value = ''; }, 200); } </script>
|
@ -39,6 +39,16 @@
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
<div class="field has-addons">
|
||||
<p class="control has-icons-left is-expanded">
|
||||
<input class="input is-small {% if GroqExists %}is-success{% endif %}" type="text"
|
||||
placeholder="Groq API key" {%if not IsLogin %}disabled{% endif %} name="groq_key"
|
||||
autocomplete="off">
|
||||
<span class="icon is-small is-left">
|
||||
<i class="fas fa-lock"></i>
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
<div class="field has-addons">
|
||||
<p class="control">
|
||||
<button {% if not IsLogin %}disabled{% endif %} type="submit" class="button is-small">
|
||||
|
@ -7,9 +7,13 @@
|
||||
<div class="dropdown-menu" id="dropdown-menu4" role="menu">
|
||||
<div class="dropdown-content">
|
||||
<div class="dropdown-item">
|
||||
<a class="button is-small is-info is-outlined" href="/signout">
|
||||
<a class="button is-small is-info is-outlined mb-1" href="/signout">
|
||||
Log out
|
||||
</a>
|
||||
<a class="button is-small is-info is-outlined" href="https://artificialanalysis.ai/models"
|
||||
target="_blank">
|
||||
Compare models
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user