AI: Generate captions using the Ollama API #5011 #5123

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-07-21 10:43:49 +02:00
parent f67ba0e634
commit ae42af54d8
16 changed files with 313 additions and 41 deletions

View File

@@ -33,6 +33,15 @@ func TypeLowerUnderscore(s string) string {
return strings.ReplaceAll(TypeLower(s), " ", "_")
}
// TypeLowerDash converts a string to a lowercase type string and replaces spaces with dashes.
func TypeLowerDash(s string) string {
if s == "" {
return s
}
return strings.ReplaceAll(TypeLower(s), " ", "-")
}
// ShortType omits invalid runes, ensures a maximum length of 8 characters, and returns the result.
func ShortType(s string) string {
if s == "" {

View File

@@ -82,6 +82,24 @@ func TestTypeLowerUnderscore(t *testing.T) {
})
}
func TestTypeLowerDash(t *testing.T) {
t.Run("Undefined", func(t *testing.T) {
assert.Equal(t, "", TypeLowerDash(" \t "))
})
t.Run("ClientCredentials", func(t *testing.T) {
assert.Equal(t, "client-credentials", TypeLowerDash(" Client Credentials幸"))
})
t.Run("OllamaModel", func(t *testing.T) {
assert.Equal(
t,
"ollama-model:7b",
TypeLowerDash("Ollama Model:7b"))
})
t.Run("Empty", func(t *testing.T) {
assert.Equal(t, "", TypeLowerDash(""))
})
}
func TestShortType(t *testing.T) {
t.Run("Clip", func(t *testing.T) {
result := ShortType(" 幸福 Hanzi are logograms developed for the writing of Chinese! Expressions in an index may not ...!")

View File

@@ -45,6 +45,18 @@ func DataUrl(r io.Reader) string {
return fmt.Sprintf("data:%s;base64,%s", mimeType, EncodeBase64String(data))
}
// DataBase64 generates a base64 encoded string of the binary data from the specified io.Reader.
func DataBase64(r io.Reader) string {
// Read binary data.
data, err := io.ReadAll(r)
if err != nil || len(data) == 0 {
return ""
}
return EncodeBase64String(data)
}
// ReadUrl reads binary data from a regular file path,
// fetches its data from a remote http or https URL,
// or decodes a base64 data URL as created by DataUrl.

View File

@@ -6,6 +6,7 @@ type Type = string
const (
File Type = "file"
Data Type = "data"
Base64 Type = "base64"
Http Type = "http"
Https Type = "https"
Websocket Type = "wss"