-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathinput.go
160 lines (145 loc) · 4.18 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package predict
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/mitchellh/go-homedir"
"github.com/vincent-petithory/dataurl"
"github.com/replicate/cog/pkg/util/mime"
)
type Input struct {
String *string
File *string
Array *[]any
ChatMessage *json.RawMessage
}
type Inputs map[string]Input
var jsonSerializableSchemas = map[string]bool{
"#/components/schemas/CommonChatSchemaDeveloperMessage": true,
"#/components/schemas/CommonChatSchemaSystemMessage": true,
"#/components/schemas/CommonChatSchemaUserMessage": true,
"#/components/schemas/CommonChatSchemaAssistantMessage": true,
"#/components/schemas/CommonChatSchemaToolMessage": true,
"#/components/schemas/CommonChatSchemaFunctionMessage": true,
}
func NewInputs(keyVals map[string][]string, schema *openapi3.T) (Inputs, error) {
var inputComponent *openapi3.SchemaRef
for name, component := range schema.Components.Schemas {
if name == "Input" {
inputComponent = component
break
}
}
input := Inputs{}
for key, vals := range keyVals {
if len(vals) == 1 {
val := vals[0]
// Check if we should explicitly parse the JSON based on a known schema
if inputComponent != nil {
properties, err := inputComponent.JSONLookup("properties")
if err != nil {
return input, err
}
propertiesSchemas := properties.(openapi3.Schemas)
messages, err := propertiesSchemas.JSONLookup("messages")
// If there is an error it means messages was not found, this is valid for an OpenAPI schema.
if err == nil {
messagesSchemas := messages.(*openapi3.Schema)
found := false
for _, schemaRef := range messagesSchemas.Items.Value.AnyOf {
if _, ok := jsonSerializableSchemas[schemaRef.Ref]; ok {
found = true
message := json.RawMessage(val)
input[key] = Input{ChatMessage: &message}
break
}
}
if found {
continue
}
}
}
switch {
case strings.HasPrefix(val, "@"):
val = val[1:]
input[key] = Input{File: &val}
default:
input[key] = Input{String: &val}
}
} else if len(vals) > 1 {
var anyVals = make([]any, len(vals))
for i, v := range vals {
anyVals[i] = v
}
input[key] = Input{Array: &anyVals}
}
}
return input, nil
}
func NewInputsWithBaseDir(keyVals map[string]string, baseDir string) Inputs {
input := Inputs{}
for key, val := range keyVals {
if strings.HasPrefix(val, "@") {
val = filepath.Join(baseDir, val[1:])
input[key] = Input{File: &val}
} else {
input[key] = Input{String: &val}
}
}
return input
}
func (inputs *Inputs) toMap() (map[string]any, error) {
keyVals := map[string]any{}
for key, input := range *inputs {
switch {
case input.String != nil:
// Directly assign the string value
keyVals[key] = *input.String
case input.File != nil:
// Single file handling: read content and convert to a data URL
dataURL, err := fileToDataURL(*input.File)
if err != nil {
return keyVals, err
}
keyVals[key] = dataURL
case input.Array != nil:
// Handle array, potentially containing file paths
dataURLs := make([]string, len(*input.Array))
for i, elem := range *input.Array {
if str, ok := elem.(string); ok && strings.HasPrefix(str, "@") {
filePath := str[1:] // Remove '@' prefix
dataURL, err := fileToDataURL(filePath)
if err != nil {
return keyVals, err
}
dataURLs[i] = dataURL
} else if ok {
// Directly use the string if it's not a file path
dataURLs[i] = str
}
}
keyVals[key] = dataURLs
case input.ChatMessage != nil:
keyVals[key] = *input.ChatMessage
}
}
return keyVals, nil
}
// Helper function to read file content and convert to a data URL
func fileToDataURL(filePath string) (string, error) {
// Expand home directory if necessary
expandedVal, err := homedir.Expand(filePath)
if err != nil {
return "", fmt.Errorf("error expanding homedir for '%s': %w", filePath, err)
}
content, err := os.ReadFile(expandedVal)
if err != nil {
return "", err
}
mimeType := mime.TypeByExtension(filepath.Ext(expandedVal))
dataURL := dataurl.New(content, mimeType).String()
return dataURL, nil
}