-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathluis.go
125 lines (109 loc) · 3.35 KB
/
luis.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
package luis
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/url"
)
//Luis :
type Luis struct {
appid string
versionid string
client *Client
}
//NewLuis :
func NewLuis(key string, appid string) *Luis {
if len(key) == 0 {
return nil
}
e := new(Luis)
e.appid = appid
e.client = NewClient(key)
res, err := e.GetAppInfo()
if err != nil {
fmt.Println(err, "Cannot get app info")
return nil
}
app := NewAppInfo(res)
fmt.Println(app)
e.versionid = app.Endpoints.PRODUCTION.VersionID
fmt.Println(e.versionid)
return e
}
//GetAppInfo :Get App detail info and refresh app version id.
func (l *Luis) GetAppInfo() ([]byte, *ErrorResponse) {
url := getAppInfo(l.appid)
fmt.Println("app detail URL=", url)
return l.client.Connect("GET", url, nil, true)
}
//IntelList :Get All Intent List of this app
//Retreives information about the intent models.
func (l *Luis) IntelList() ([]byte, *ErrorResponse) {
url := getIntentListURL(l.appid, l.versionid)
fmt.Println("intent list URL=", url)
return l.client.Connect("GET", url, nil, true)
}
//ActionChannels :get Available Action Channels
//gets a list of all available action channels for the application
func (l *Luis) ActionChannels() ([]byte, *ErrorResponse) {
url := getActionChannels(l.appid, l.versionid)
return l.client.Connect("GET", url, nil, true)
}
//Predict :get Train Model Predictions
//gets the trained model predictions for the input examples
func (l *Luis) Predict(utterance string) ([]byte, *ErrorResponse) {
outUrl := getPredictURL(l.appid, l.versionid)
utterance = url.QueryEscape(utterance)
outUrl = outUrl + "?subscription-key=" + l.client.key + "&q=" + utterance
return l.client.Connect("GET", outUrl, nil, true)
}
//Train :Training Status
//gets the training status of all models of the specified application
func (l *Luis) Train() ([]byte, *ErrorResponse) {
url := getTrainURL(l.appid, l.versionid)
return l.client.Connect("POST", url, nil, true)
}
//AddLabel :Add Label
// Adds a labeled example to the specified application
func (l *Luis) AddLabel(example ExamplePayload) ([]byte, *ErrorResponse) {
url := getAddExampleURL(l.appid, l.versionid)
bytExample, err := json.Marshal(example)
if err != nil {
log.Println("err:", err)
return nil, nil
}
return l.client.Connect("POST", url, bytes.NewBuffer(bytExample), true)
}
//Publish :Publishes a specific version of the application
func (l *Luis) Publish(payload PublishPayload) ([]byte, *ErrorResponse) {
url := getPublishURL(l.appid, l.versionid)
bytPayload, err := json.Marshal(payload)
if err != nil {
log.Println("err:", err)
return nil, nil
}
return l.client.Connect("POST", url, bytes.NewBuffer(bytPayload), true)
}
//Version :Get application version
func (l *Luis) Version(versionID string) ([]byte, *ErrorResponse) {
outUrl := getVersionURL(l.appid, l.versionid)
versionID = url.QueryEscape(versionID)
outUrl = outUrl + "/" + versionID + "/"
return l.client.Connect("GET", outUrl, nil, true)
}
//Version :Get application version
func (l *Luis) Versions() ([]byte, *ErrorResponse) {
outUrl := getVersionURL(l.appid, l.versionid)
outUrl = outUrl + "/"
return l.client.Connect("GET", outUrl, nil, true)
}
func (l *Luis) GetCurrentProductionVersion() (string, *ErrorResponse) {
if l.versionid != "" {
return l.versionid, nil
}
return "", &ErrorResponse{
ErrorCode: 500,
Err: fmt.Errorf("No production model.\n"),
}
}