2024-10-04 16:32:29 +00:00
package templates
import (
"bytes"
"text/template"
2024-10-17 15:34:20 +00:00
"github.com/Masterminds/sprig/v3"
2024-10-04 16:32:29 +00:00
)
2024-10-22 07:34:05 +00:00
type MultiModalOptions struct {
TotalImages int
TotalAudios int
TotalVideos int
ImagesInMessage int
AudiosInMessage int
VideosInMessage int
}
type MultimodalContent struct {
ID int
}
const DefaultMultiModalTemplate = "{{ range .Audio }}[audio-{{.ID}}]{{end}}{{ range .Images }}[img-{{.ID}}]{{end}}{{ range .Video }}[vid-{{.ID}}]{{end}}{{.Text}}"
func TemplateMultiModal ( templateString string , opts MultiModalOptions , text string ) ( string , error ) {
if templateString == "" {
templateString = DefaultMultiModalTemplate
}
2024-10-04 16:32:29 +00:00
// compile the template
2024-10-17 15:34:20 +00:00
tmpl , err := template . New ( "template" ) . Funcs ( sprig . FuncMap ( ) ) . Parse ( templateString )
2024-10-04 16:32:29 +00:00
if err != nil {
return "" , err
}
2024-10-22 07:34:05 +00:00
videos := [ ] MultimodalContent { }
for i := 0 ; i < opts . VideosInMessage ; i ++ {
videos = append ( videos , MultimodalContent { ID : i + ( opts . TotalVideos - opts . VideosInMessage ) } )
}
audios := [ ] MultimodalContent { }
for i := 0 ; i < opts . AudiosInMessage ; i ++ {
audios = append ( audios , MultimodalContent { ID : i + ( opts . TotalAudios - opts . AudiosInMessage ) } )
}
images := [ ] MultimodalContent { }
for i := 0 ; i < opts . ImagesInMessage ; i ++ {
images = append ( images , MultimodalContent { ID : i + ( opts . TotalImages - opts . ImagesInMessage ) } )
}
2024-10-04 16:32:29 +00:00
result := bytes . NewBuffer ( nil )
// execute the template
err = tmpl . Execute ( result , struct {
2024-10-22 07:34:05 +00:00
Audio [ ] MultimodalContent
Images [ ] MultimodalContent
Video [ ] MultimodalContent
Text string
2024-10-04 16:32:29 +00:00
} {
2024-10-22 07:34:05 +00:00
Audio : audios ,
Images : images ,
Video : videos ,
Text : text ,
2024-10-04 16:32:29 +00:00
} )
return result . String ( ) , err
}