Spaces:
Running
Running
package main | |
import ( | |
"bytes" | |
"fmt" | |
"encoding/json" | |
"syscall/js" | |
"github.com/ollama/ollama/api" | |
"github.com/ollama/ollama/template" | |
) | |
func formatChatTemplate() js.Func { | |
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any { | |
if len(args) != 2 { | |
return fmt.Errorf("expected 2 arguments, got %d", len(args)) | |
} | |
inputConv := args[0].String() | |
inputTmpl := args[1].String() | |
var messages []api.Message | |
if err := json.Unmarshal([]byte(inputConv), &messages); err != nil { | |
return string(err.Error()) | |
} | |
for _, message := range messages { | |
fmt.Printf("%s: %s\n", message.Role, message.Content) | |
} | |
tmpl, err := template.Parse(inputTmpl) | |
if err != nil { | |
return string(err.Error()) | |
} | |
var buf bytes.Buffer | |
err = tmpl.Execute(&buf, template.Values{ | |
Messages: messages, | |
}) | |
if err != nil { | |
return string(err.Error()) | |
} | |
return string(buf.String()) | |
}) | |
return jsonFunc | |
} | |
func main() { | |
fmt.Println("Go Program Started") | |
js.Global().Set("formatChatTemplate", formatChatTemplate()) | |
<-make(chan struct{}) | |
} | |