From 880d74579cba7ff4786f73e12f512abc9493c617 Mon Sep 17 00:00:00 2001 From: sahari Date: Sat, 29 Jun 2024 18:29:11 -0700 Subject: [PATCH] feat: add markdown parser --- app/run.go | 30 +++++++++++++++++------------- go.mod | 1 + go.sum | 2 ++ internal/parser/markdown.go | 17 +++++++++++++++++ templates/index.html | 15 +++++++++++---- 5 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 internal/parser/markdown.go diff --git a/app/run.go b/app/run.go index f4f87de..4fa704e 100644 --- a/app/run.go +++ b/app/run.go @@ -6,10 +6,10 @@ import ( "html/template" "log" "net/http" - "strings" "jenn-ai/internal/bedrock" "jenn-ai/internal/ollama" + "jenn-ai/internal/parser" "github.com/gin-gonic/gin" ) @@ -32,13 +32,15 @@ func (mc *ModelConfig) runModel(ctx context.Context, region string) gin.HandlerF } completion = input + response - processPrompt := strings.ReplaceAll(message, "\n", "
") - processPrompt = strings.ReplaceAll(processPrompt, " ", ``) - processResponse := strings.ReplaceAll(response, "\n", "
") - processResponse = strings.ReplaceAll(processResponse, " ", ``) + // parse markdown + parsed, err := parser.ParseMD(response) + if err != nil { + log.Fatal(err) + } + c.HTML(http.StatusOK, "chat.html", gin.H{ - "Human": template.HTML("You:
" + processPrompt), - "Response": template.HTML("JennAI:
" + processResponse), + "Human": template.HTML("You:
" + message), + "Response": template.HTML("JennAI:
" + parsed), }) case "Ollama": model := ollama.NewModel(modelID, mc.Temperature, mc.TopP, mc.TopK, mc.MaxTokens) @@ -49,13 +51,15 @@ func (mc *ModelConfig) runModel(ctx context.Context, region string) gin.HandlerF } completion = input + response - processPrompt := strings.ReplaceAll(message, "\n", "
") - processPrompt = strings.ReplaceAll(processPrompt, " ", ``) - processResponse := strings.ReplaceAll(response, "\n", "
") - processResponse = strings.ReplaceAll(processResponse, " ", ``) + // parse markdown + parsed, err := parser.ParseMD(response) + if err != nil { + log.Fatal(err) + } + c.HTML(http.StatusOK, "chat.html", gin.H{ - "Human": template.HTML("You:
" + processPrompt), - "Response": template.HTML("JennAI:
" + processResponse), + "Human": template.HTML("You:
" + message), + "Response": template.HTML("JennAI:
" + parsed), }) default: fmt.Println("No Model Platform selected or unsupported") diff --git a/go.mod b/go.mod index 82c3298..b0aa562 100644 --- a/go.mod +++ b/go.mod @@ -50,6 +50,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect + github.com/yuin/goldmark v1.7.4 // indirect golang.org/x/arch v0.3.0 // indirect golang.org/x/crypto v0.17.0 // indirect golang.org/x/net v0.19.0 // indirect diff --git a/go.sum b/go.sum index 783e43a..314984a 100644 --- a/go.sum +++ b/go.sum @@ -123,6 +123,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2 github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg= +github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= diff --git a/internal/parser/markdown.go b/internal/parser/markdown.go new file mode 100644 index 0000000..200199b --- /dev/null +++ b/internal/parser/markdown.go @@ -0,0 +1,17 @@ +package parser + +import ( + "bytes" + + "github.com/yuin/goldmark" +) + +// ParseMD is used to parse markdown +func ParseMD(source string) (string, error) { + var buf bytes.Buffer + md := goldmark.New() + if err := md.Convert([]byte(source), &buf); err != nil { + return "", err + } + return buf.String(), nil +} diff --git a/templates/index.html b/templates/index.html index 2daa951..1c40e6e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -30,12 +30,19 @@ -
+
{{template "chat.html" .}}
-
- {{template "prompt.html" .}} -
+ +
+ {{template "prompt.html" .}} +
{{end}}