From 947ac9c7138cb7532596213f89970cd29e529b17 Mon Sep 17 00:00:00 2001 From: Adrien Date: Tue, 14 May 2024 12:17:46 +0200 Subject: [PATCH] Added GPT-4 Omni and some fix --- Chat.go | 2 +- Request.go | 67 ++++++++++++++++++++++++++++++----------------- RequestOpenai.go | 28 +++++++++++++++++--- database.go | 2 -- static/.DS_Store | Bin 6148 -> 8196 bytes utils.go | 32 +++++++++++----------- 6 files changed, 85 insertions(+), 46 deletions(-) diff --git a/Chat.go b/Chat.go index ecaecb4..5bed563 100644 --- a/Chat.go +++ b/Chat.go @@ -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) } diff --git a/Request.go b/Request.go index 4ec679c..865913c 100644 --- a/Request.go +++ b/Request.go @@ -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() { + // 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 + } + + // Call the selected addMessageFunc in a goroutine go func() { defer wg.Done() - response := addOpenaiMessage(lastSelectedModelIds[idx], idx == 0) - InsertedIDs = append(InsertedIDs, response) + if addMessageFunc != nil { + addMessageFunc(lastSelectedModelIds[idx], idx == 0) + } + done <- struct{}{} }() - } 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) - }() - } + + // 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 both goroutines to finish + // Wait for all goroutines to finish wg.Wait() + fmt.Println("Done!") + return c.SendString(generateChatHTML()) } diff --git a/RequestOpenai.go b/RequestOpenai.go index 4aa2846..0476a0e 100644 --- a/RequestOpenai.go +++ b/RequestOpenai.go @@ -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", diff --git a/database.go b/database.go index 29dae1f..16955bb 100644 --- a/database.go +++ b/database.go @@ -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 diff --git a/static/.DS_Store b/static/.DS_Store index c4e8735952b41d190b1c700ce280a840b13dffaf..272507cacdb7ebb5ad960df29e294b6c304a1579 100644 GIT binary patch literal 8196 zcmeHMU2GIp6u#fIr85I9Q*7xL)^(xs)7n5=eoDc}(x1@sV})JX(pJju&HxjpGi7IX zi&k{~c@j;GQ4=H@_0bi3T_la}dvt*sX!m6ca4sH{{f7gj!)JmyR# zy|kBiyVAK)K1(@{m6;usa{-n^tPFX)Bv2V`GP9CC!HocO-o>Wt{Z*c-RXDBd(M4x_zHvleXcD7_RJ%ukvo`nvxcpk z_R#}VrtTdZDwwXBv5b_Ro-mXeq?DQJ`bfjF<6|ezMFb0c`u57Q|Hw$soqBcCb8?oUL z)9fjBhP}+rvNzfL>^!@`zGfHMckFw1nO$K&vtQZo08}hO6{=B>2pX^)Yte>wY`}K( zp&z@j2m3IBQHNiLOxMTSRIQA1 zOYwa-##&SjvY2d9x2jq>WtlRwEYeV~s@oF^&v}jcwq3EFn8tHP{xi6Hj(x~JVV|*Y zh`T?spV%+#Dk`uLb;Q+1tiozEp_%x)37fG6o#;Xjda;w(yAMgEFo+=>Muxa+;0PR? zKn^Y*!K1|B$MFR5_Zd8k=kPqv;uXA#*YLW} y)cNH&NxvK?dF>yDG>(%g_elh@C?sj9{pBA5=vQ8N{&U>t|Lq!{|2{SU)7>BWYcL7` delta 179 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{MGjdEU6q~50$jCG?VE1GL8J5ZX0%DB2CaVb) zYD!gC>zbJv=_nYQ8JpGWC{$Y-8R#gOSQ^#Va&m|&>strKXXoVR<@Ze9Cm_$*yLq>m z597v$+boOOIXDEFfrbNt05_0u1=+K)@H_Klei=&zkT4?y0~3^H0ntDX$VDJWGHj0L HnZpbK{Z1i! diff --git a/utils.go b/utils.go index ecc9cf5..a78c717 100644 --- a/utils.go +++ b/utils.go @@ -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" - } - 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" + for i := range ModelsInfos { + if ModelsInfos[i].ID == model { + return ModelsInfos[i].Icon + } } 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 +}