109 lines
2.5 KiB
Go
109 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/yuin/goldmark"
|
|
highlighting "github.com/yuin/goldmark-highlighting"
|
|
)
|
|
|
|
func markdownToHTML(markdownText string) string {
|
|
var buf bytes.Buffer
|
|
md := goldmark.New(
|
|
goldmark.WithExtensions(
|
|
highlighting.NewHighlighting(highlighting.WithStyle("monokai")),
|
|
),
|
|
)
|
|
if err := md.Convert([]byte(markdownText), &buf); err != nil {
|
|
panic(err) // Handle the error appropriately
|
|
}
|
|
|
|
return addCopyButtonsToCode(buf.String())
|
|
}
|
|
|
|
func addCopyButtonsToCode(htmlContent string) string {
|
|
buttonHTML := `<button class="copy-button" onclick="copyCode(this)"><i class="fa-solid fa-copy"></i></button>`
|
|
|
|
// Regular expression pattern to match <pre> elements and insert the button right before <code>
|
|
pattern := `(<pre[^>]*>)`
|
|
|
|
// Compile the regular expression
|
|
re := regexp.MustCompile(pattern)
|
|
|
|
// Replace each matched <pre> element with the updated HTML
|
|
updatedHTML := re.ReplaceAllString(htmlContent, "$1"+buttonHTML)
|
|
|
|
return updatedHTML
|
|
}
|
|
|
|
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"
|
|
}
|
|
return "openai"
|
|
}
|
|
|
|
func model2Name(model string) string {
|
|
for i := range ModelsInfos {
|
|
if ModelsInfos[i].ID == model {
|
|
return ModelsInfos[i].Name
|
|
}
|
|
}
|
|
return "OpenAI"
|
|
}
|
|
|
|
func getExistingKeys() (bool, bool, bool) {
|
|
if edgeClient == nil {
|
|
return false, false, false
|
|
}
|
|
|
|
var openaiExists bool
|
|
var anthropicExists bool
|
|
var mistralExists bool
|
|
|
|
err := edgeClient.QuerySingle(edgeCtx, `
|
|
select exists (
|
|
select global currentUser.setting.keys
|
|
filter .company = "openai"
|
|
);
|
|
`, &openaiExists)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addOpenaiKey: ", err)
|
|
}
|
|
|
|
err = edgeClient.QuerySingle(edgeCtx, `
|
|
select exists (
|
|
select global currentUser.setting.keys
|
|
filter .company = "anthropic"
|
|
);
|
|
`, &anthropicExists)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addOpenaiKey: ", err)
|
|
}
|
|
|
|
err = edgeClient.QuerySingle(edgeCtx, `
|
|
select exists (
|
|
select global currentUser.setting.keys
|
|
filter .company = "mistral"
|
|
);
|
|
`, &mistralExists)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addOpenaiKey: ", err)
|
|
}
|
|
|
|
return openaiExists, anthropicExists, mistralExists
|
|
}
|