@@ -4,8 +4,10 @@ import (
44 "bytes"
55 "context"
66 "encoding/json"
7+ "fmt"
78 "net/http"
89 "net/url"
10+ "strings"
911 "time"
1012
1113 "github.com/pkg/errors"
@@ -16,35 +18,52 @@ type Model struct {
1618 // ID is the ID of the model.
1719 ID string `json:"id,omitempty"`
1820 // Name is the human readable name of the Model.
19- Name string `json:"name,omitempty "`
21+ Name string `json:"name"`
2022 // Options are optional Model settings to adjust the behaviour
2123 // of this Model within Suggestionbox.
2224 Options * ModelOptions `json:"options,omitempty"`
2325 // Choices are the options this Model will select from.
2426 Choices []Choice `json:"choices,omitempty"`
2527}
2628
29+ // NewModel makes a new Model.
30+ func NewModel (id , name string , choices ... Choice ) Model {
31+ return Model {
32+ ID : id ,
33+ Name : name ,
34+ Choices : choices ,
35+ }
36+ }
37+
2738// Feature represents a single feature, to describe an input or a choice
2839// for example age:28 or location:"London".
2940type Feature struct {
3041 // Key is the name of the Feature.
31- Key string `json:"key,omitempty "`
42+ Key string `json:"key"`
3243 // Value is the string value of this Feature.
33- Value string `json:"value,omitempty "`
44+ Value string `json:"value"`
3445 // Type is the type of the Feature.
3546 // Can be "number", "text", "keyword", "list", "image_url" or "image_base64"..
36- Type string `json:"type,omitempty "`
47+ Type string `json:"type"`
3748}
3849
3950// Choice is an option with features.
4051type Choice struct {
4152 // ID is a unique ID for this choice.
42- ID string `json:"id,omitempty "`
53+ ID string `json:"id"`
4354 // Features holds all the Feature objects that describe
4455 // this choice.
4556 Features []Feature `json:"features,omitempty"`
4657}
4758
59+ // NewChoice creates a new Choice.
60+ func NewChoice (id string , features ... Feature ) Choice {
61+ return Choice {
62+ ID : id ,
63+ Features : features ,
64+ }
65+ }
66+
4867// ModelOptions describes the behaviours of a Model.
4968type ModelOptions struct {
5069 // Expiration is the time to wait for the reward before it expires.
@@ -104,3 +123,60 @@ func (c *Client) CreateModel(ctx context.Context, model Model) (Model, error) {
104123 }
105124 return response .Model , nil
106125}
126+
127+ // FeatureNumber makes a numerical Feature.
128+ func FeatureNumber (key string , value float64 ) Feature {
129+ return Feature {
130+ Type : "number" ,
131+ Key : key ,
132+ Value : fmt .Sprintf ("%v" , value ),
133+ }
134+ }
135+
136+ // FeatureText makes a textual Feature that will be tokenized.
137+ // Use FeatureKeyword for values that should not be tokenized.
138+ func FeatureText (key string , text string ) Feature {
139+ return Feature {
140+ Type : "text" ,
141+ Key : key ,
142+ Value : text ,
143+ }
144+ }
145+
146+ // FeatureKeyword makes a textual Feature that will not be tokenized.
147+ // Use FeatureList to provide multiple keywords in a single Feature.
148+ // Use Text for bodies of text that should be tokenized.
149+ func FeatureKeyword (key string , keyword string ) Feature {
150+ return Feature {
151+ Type : "keyword" ,
152+ Key : key ,
153+ Value : keyword ,
154+ }
155+ }
156+
157+ // FeatureList makes a Feature made up of multiple keywords.
158+ func FeatureList (key string , keywords ... string ) Feature {
159+ return Feature {
160+ Type : "list" ,
161+ Key : key ,
162+ Value : strings .Join (keywords , "," ),
163+ }
164+ }
165+
166+ // FeatureImageURL makes a Feature that points to a hosted image.
167+ func FeatureImageURL (key string , url string ) Feature {
168+ return Feature {
169+ Type : "image_url" ,
170+ Key : key ,
171+ Value : url ,
172+ }
173+ }
174+
175+ // FeatureImageBase64 makes a Feature that is base 64 encoded.
176+ func FeatureImageBase64 (key string , data string ) Feature {
177+ return Feature {
178+ Type : "image_base64" ,
179+ Key : key ,
180+ Value : data ,
181+ }
182+ }
0 commit comments