mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-31 07:54:59 +00:00
rf: centralize base64 image handling (#2595)
contains simple fixes to warnings and errors, removes a broken / outdated test, runs go mod tidy, and as the actual change, centralizes base64 image handling Signed-off-by: Dave Lee <dave@gray101.com>
This commit is contained in:
parent
4156a4f15f
commit
12513ebae0
12 changed files with 60 additions and 98 deletions
|
@ -1,6 +1,7 @@
|
|||
package library
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -17,14 +18,17 @@ import (
|
|||
var skipLibraryPath = os.Getenv("LOCALAI_SKIP_LIBRARY_PATH") != ""
|
||||
|
||||
// LoadExtractedLibs loads the extracted libraries from the asset dir
|
||||
func LoadExtractedLibs(dir string) {
|
||||
func LoadExtractedLibs(dir string) error {
|
||||
// Skip this if LOCALAI_SKIP_LIBRARY_PATH is set
|
||||
if skipLibraryPath {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error = nil
|
||||
for _, libDir := range []string{filepath.Join(dir, "backend-assets", "lib"), filepath.Join(dir, "lib")} {
|
||||
LoadExternal(libDir)
|
||||
err = errors.Join(err, LoadExternal(libDir))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// LoadLDSO checks if there is a ld.so in the asset dir and if so, prefixes the grpc process with it.
|
||||
|
@ -57,9 +61,10 @@ func LoadLDSO(assetDir string, args []string, grpcProcess string) ([]string, str
|
|||
}
|
||||
|
||||
// LoadExternal sets the LD_LIBRARY_PATH to include the given directory
|
||||
func LoadExternal(dir string) {
|
||||
func LoadExternal(dir string) error {
|
||||
// Skip this if LOCALAI_SKIP_LIBRARY_PATH is set
|
||||
if skipLibraryPath {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
lpathVar := "LD_LIBRARY_PATH"
|
||||
|
@ -67,6 +72,7 @@ func LoadExternal(dir string) {
|
|||
lpathVar = "DYLD_FALLBACK_LIBRARY_PATH" // should it be DYLD_LIBRARY_PATH ?
|
||||
}
|
||||
|
||||
var setErr error = nil
|
||||
if _, err := os.Stat(dir); err == nil {
|
||||
ldLibraryPath := os.Getenv(lpathVar)
|
||||
if ldLibraryPath == "" {
|
||||
|
@ -74,6 +80,7 @@ func LoadExternal(dir string) {
|
|||
} else {
|
||||
ldLibraryPath = fmt.Sprintf("%s:%s", ldLibraryPath, dir)
|
||||
}
|
||||
os.Setenv(lpathVar, ldLibraryPath)
|
||||
setErr = errors.Join(setErr, os.Setenv(lpathVar, ldLibraryPath))
|
||||
}
|
||||
return setErr
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package model_test
|
||||
|
||||
import (
|
||||
"github.com/mudler/LocalAI/pkg/model"
|
||||
. "github.com/mudler/LocalAI/pkg/model"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
|
@ -44,7 +43,7 @@ var llama3TestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"user": {
|
||||
"template": llama3,
|
||||
"expected": "<|start_header_id|>user<|end_header_id|>\n\nA long time ago in a galaxy far, far away...<|eot_id|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "user",
|
||||
RoleName: "user",
|
||||
|
@ -59,7 +58,7 @@ var llama3TestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"assistant": {
|
||||
"template": llama3,
|
||||
"expected": "<|start_header_id|>assistant<|end_header_id|>\n\nA long time ago in a galaxy far, far away...<|eot_id|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "assistant",
|
||||
RoleName: "assistant",
|
||||
|
@ -74,7 +73,7 @@ var llama3TestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"function_call": {
|
||||
"template": llama3,
|
||||
"expected": "<|start_header_id|>assistant<|end_header_id|>\n\nFunction call:\n{\"function\":\"test\"}<|eot_id|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "assistant",
|
||||
RoleName: "assistant",
|
||||
|
@ -89,7 +88,7 @@ var llama3TestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"function_response": {
|
||||
"template": llama3,
|
||||
"expected": "<|start_header_id|>tool<|end_header_id|>\n\nFunction response:\nResponse from tool<|eot_id|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "tool",
|
||||
RoleName: "tool",
|
||||
|
@ -107,7 +106,7 @@ var chatMLTestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"user": {
|
||||
"template": chatML,
|
||||
"expected": "<|im_start|>user\nA long time ago in a galaxy far, far away...<|im_end|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "user",
|
||||
RoleName: "user",
|
||||
|
@ -122,7 +121,7 @@ var chatMLTestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"assistant": {
|
||||
"template": chatML,
|
||||
"expected": "<|im_start|>assistant\nA long time ago in a galaxy far, far away...<|im_end|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "assistant",
|
||||
RoleName: "assistant",
|
||||
|
@ -137,7 +136,7 @@ var chatMLTestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"function_call": {
|
||||
"template": chatML,
|
||||
"expected": "<|im_start|>assistant\n<tool_call>\n{\"function\":\"test\"}\n</tool_call><|im_end|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "assistant",
|
||||
RoleName: "assistant",
|
||||
|
@ -152,7 +151,7 @@ var chatMLTestMatch map[string]map[string]interface{} = map[string]map[string]in
|
|||
"function_response": {
|
||||
"template": chatML,
|
||||
"expected": "<|im_start|>tool\n<tool_response>\nResponse from tool\n</tool_response><|im_end|>",
|
||||
"data": model.ChatMessageTemplateData{
|
||||
"data": ChatMessageTemplateData{
|
||||
SystemPrompt: "",
|
||||
Role: "tool",
|
||||
RoleName: "tool",
|
||||
|
@ -175,7 +174,7 @@ var _ = Describe("Templates", func() {
|
|||
for key := range chatMLTestMatch {
|
||||
foo := chatMLTestMatch[key]
|
||||
It("renders correctly `"+key+"`", func() {
|
||||
templated, err := modelLoader.EvaluateTemplateForChatMessage(foo["template"].(string), foo["data"].(model.ChatMessageTemplateData))
|
||||
templated, err := modelLoader.EvaluateTemplateForChatMessage(foo["template"].(string), foo["data"].(ChatMessageTemplateData))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(templated).To(Equal(foo["expected"]), templated)
|
||||
})
|
||||
|
@ -189,7 +188,7 @@ var _ = Describe("Templates", func() {
|
|||
for key := range llama3TestMatch {
|
||||
foo := llama3TestMatch[key]
|
||||
It("renders correctly `"+key+"`", func() {
|
||||
templated, err := modelLoader.EvaluateTemplateForChatMessage(foo["template"].(string), foo["data"].(model.ChatMessageTemplateData))
|
||||
templated, err := modelLoader.EvaluateTemplateForChatMessage(foo["template"].(string), foo["data"].(ChatMessageTemplateData))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(templated).To(Equal(foo["expected"]), templated)
|
||||
})
|
||||
|
|
|
@ -103,7 +103,10 @@ func (ml *ModelLoader) startProcess(grpcProcess, id string, serverAddress string
|
|||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
<-c
|
||||
grpcControlProcess.Stop()
|
||||
err := grpcControlProcess.Stop()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error while shutting down grpc process")
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
|
|
|
@ -42,9 +42,12 @@ func GetImageURLAsBase64(s string) (string, error) {
|
|||
return encoded, nil
|
||||
}
|
||||
|
||||
// if the string instead is prefixed with "data:image/jpeg;base64,", drop it
|
||||
if strings.HasPrefix(s, "data:image/jpeg;base64,") {
|
||||
return strings.ReplaceAll(s, "data:image/jpeg;base64,", ""), nil
|
||||
// if the string instead is prefixed with "data:image/...;base64,", drop it
|
||||
dropPrefix := []string{"data:image/jpeg;base64,", "data:image/png;base64,"}
|
||||
for _, prefix := range dropPrefix {
|
||||
if strings.HasPrefix(s, prefix) {
|
||||
return strings.ReplaceAll(s, prefix, ""), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("not valid string")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,13 +7,20 @@ import (
|
|||
)
|
||||
|
||||
var _ = Describe("utils/base64 tests", func() {
|
||||
It("GetImageURLAsBase64 can strip data url prefixes", func() {
|
||||
It("GetImageURLAsBase64 can strip jpeg data url prefixes", func() {
|
||||
// This one doesn't actually _care_ that it's base64, so feed "bad" data in this test in order to catch a change in that behavior for informational purposes.
|
||||
input := "data:image/jpeg;base64,FOO"
|
||||
b64, err := GetImageURLAsBase64(input)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(b64).To(Equal("FOO"))
|
||||
})
|
||||
It("GetImageURLAsBase64 can strip png data url prefixes", func() {
|
||||
// This one doesn't actually _care_ that it's base64, so feed "bad" data in this test in order to catch a change in that behavior for informational purposes.
|
||||
input := "data:image/png;base64,BAR"
|
||||
b64, err := GetImageURLAsBase64(input)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(b64).To(Equal("BAR"))
|
||||
})
|
||||
It("GetImageURLAsBase64 returns an error for bogus data", func() {
|
||||
input := "FOO"
|
||||
b64, err := GetImageURLAsBase64(input)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue