Added GPT-4 Omni and some fix

This commit is contained in:
Adrien Bouvais 2024-05-14 12:17:46 +02:00
parent 07180d5176
commit 947ac9c713
6 changed files with 85 additions and 46 deletions

View File

@ -305,7 +305,7 @@ func RedoMessageHandler(c *fiber.Ctx) error {
selectedModelIds = append(selectedModelIds, ModelsInfos[ModelInfo].ID)
}
}
lastSelectedModelIds = selectedModelIds
lastSelectedModelIds = removeDuplicate(selectedModelIds)
return c.SendString(messageOut)
}

View File

@ -1,9 +1,11 @@
package main
import (
"context"
"fmt"
"log"
"sync"
"time"
"github.com/edgedb/edgedb-go"
"github.com/flosch/pongo2"
@ -61,45 +63,62 @@ func addUsage(inputCost float32, outputCost float32, inputToken int32, outputTok
}
func GenerateMultipleMessages(c *fiber.Ctx) error {
insertArea()
// Create a wait group to synchronize the goroutines
var wg sync.WaitGroup
var InsertedIDs []edgedb.UUID
// Add the length of lastSelectedModelIds goroutines to the wait group
wg.Add(len(lastSelectedModelIds))
for i := range lastSelectedModelIds {
idx := i
if model2Icon(lastSelectedModelIds[i]) == "openai" {
go func() {
defer wg.Done()
response := addOpenaiMessage(lastSelectedModelIds[idx], idx == 0)
InsertedIDs = append(InsertedIDs, response)
}()
} else if model2Icon(lastSelectedModelIds[i]) == "anthropic" {
go func() {
defer wg.Done()
response := addAnthropicMessage(lastSelectedModelIds[idx], idx == 0)
InsertedIDs = append(InsertedIDs, response)
}()
} else if model2Icon(lastSelectedModelIds[i]) == "mistral" {
go func() {
defer wg.Done()
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)
}()
}
// Create a context with a 1-minute timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel() // Ensure the context is cancelled to free resources
// Use a channel to signal the completion of addxxxMessage
done := make(chan struct{}, 1)
// Determine which message function to call based on the model
var addMessageFunc func(modelID string, selected bool) edgedb.UUID
switch model2Icon(lastSelectedModelIds[idx]) {
case "openai":
addMessageFunc = addOpenaiMessage
case "anthropic":
addMessageFunc = addAnthropicMessage
case "mistral":
addMessageFunc = addMistralMessage
case "groq":
addMessageFunc = addGroqMessage
}
// Wait for both goroutines to finish
// Call the selected addMessageFunc in a goroutine
go func() {
defer wg.Done()
if addMessageFunc != nil {
addMessageFunc(lastSelectedModelIds[idx], idx == 0)
}
done <- struct{}{}
}()
// Use select to wait on multiple channel operations
select {
case <-ctx.Done(): // Context's deadline is exceeded
// Insert a bot message indicating a timeout
insertBotMessage(lastSelectedModelIds[idx]+" too long to answer", idx == 0, lastSelectedModelIds[idx])
case <-done: // addMessageFunc completed within the deadline
// No action needed, the function completed successfully
}
}()
}
// Wait for all goroutines to finish
wg.Wait()
fmt.Println("Done!")
return c.SendString(generateChatHTML())
}

View File

@ -47,9 +47,9 @@ func init() {
modelInfo := ModelInfo{
ID: "gpt-3.5-turbo",
Name: "GPT-3.5",
Name: "GPT-3.5 Turbo",
Icon: "openai",
MaxToken: 4096,
MaxToken: 16385,
InputPrice: 0.50 / 1000000,
OutputPrice: 1.50 / 1000000,
}
@ -57,16 +57,38 @@ func init() {
ModelsInfos = append(ModelsInfos, modelInfo)
modelInfo = ModelInfo{
ID: "gpt-4-turbo",
ID: "gpt-4",
Name: "GPT-4",
Icon: "openai",
MaxToken: 8192,
InputPrice: 30.00 / 1000000,
OutputPrice: 60.00 / 1000000,
}
ModelInfosList = append(ModelInfosList, modelInfo)
ModelsInfos = append(ModelsInfos, modelInfo)
modelInfo = ModelInfo{
ID: "gpt-4-turbo",
Name: "GPT-4 Turbo",
Icon: "openai",
MaxToken: 128000,
InputPrice: 10.00 / 1000000,
OutputPrice: 30.00 / 1000000,
}
ModelInfosList = append(ModelInfosList, modelInfo)
ModelsInfos = append(ModelsInfos, modelInfo)
modelInfo = ModelInfo{
ID: "gpt-4o",
Name: "GPT-4 Omni",
Icon: "openai",
MaxToken: 128000,
InputPrice: 5.00 / 1000000,
OutputPrice: 15.00 / 1000000,
}
ModelInfosList = append(ModelInfosList, modelInfo)
ModelsInfos = append(ModelsInfos, modelInfo)
companyInfo := CompanyInfo{
ID: "openai",
Name: "OpenAI",

View File

@ -100,8 +100,6 @@ func getCurrentUser() User {
var result User
err := edgeClient.QuerySingle(edgeCtx, "SELECT global currentUser LIMIT 1;", &result)
if err != nil {
fmt.Println("Error in edgedb.QuerySingle: in getCurrentUser")
fmt.Println(err)
return User{}
}
return result

BIN
static/.DS_Store vendored

Binary file not shown.

View File

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"regexp"
"strings"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
@ -40,21 +39,10 @@ func addCopyButtonsToCode(htmlContent string) string {
}
func model2Icon(model string) string {
if model == "gpt-3.5-turbo" {
return "openai"
for i := range ModelsInfos {
if ModelsInfos[i].ID == model {
return ModelsInfos[i].Icon
}
if model == "gpt-4" {
return "openai"
}
// If model name contain claude-3 retrun anthropic
if strings.Contains(model, "claude-3") {
return "anthropic"
}
if strings.Contains(model, "mistral") || strings.Contains(model, "mixtral") {
return "mistral"
}
if strings.Contains(model, "llama3") || strings.Contains(model, "gemma") {
return "groq"
}
return "bouvai2"
}
@ -124,3 +112,15 @@ func getExistingKeys() (bool, bool, bool, bool) {
return openaiExists, anthropicExists, mistralExists, groqExists
}
func removeDuplicate(s []string) []string {
m := make(map[string]bool)
var result []string
for _, str := range s {
if !m[str] {
m[str] = true
result = append(result, str)
}
}
return result
}