-
Notifications
You must be signed in to change notification settings - Fork 17
/
api.go
460 lines (383 loc) · 12.2 KB
/
api.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package dify
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"strings"
)
var (
chatMessages = "/v1/chat-messages"
messagesFeedbacks = "/v1/messages/{message_id}/feedbacks"
messages = "/v1/messages"
conversations = "/v1/conversations"
conversationsRename = "/v1/conversations/{conversation_id}/name"
parameters = "/v1/parameters"
)
type ErrorResponse struct {
Code string `json:"code"`
Message string `json:"message"`
Params string `json:"params"`
}
type ChatMessageRequest struct {
Inputs map[string]interface{} `json:"inputs"`
Query string `json:"query"`
ResponseMode string `json:"response_mode"`
ConversationID string `json:"conversation_id,omitempty"`
User string `json:"user"`
}
type ChatMessageResponse struct {
ID string `json:"id"`
Answer string `json:"answer"`
ConversationID string `json:"conversation_id"`
CreatedAt int `json:"created_at"`
}
type ChatMessageStreamResponse struct {
Event string `json:"event"`
TaskID string `json:"task_id"`
ID string `json:"id"`
Answer string `json:"answer"`
CreatedAt int64 `json:"created_at"`
ConversationID string `json:"conversation_id"`
}
type ChatMessageStreamChannelResponse struct {
ChatMessageStreamResponse
Err error `json:"-"`
}
type MessagesFeedbacksRequest struct {
MessageID string `json:"message_id,omitempty"`
Rating string `json:"rating,omitempty"`
User string `json:"user"`
}
type MessagesFeedbacksResponse struct {
HasMore bool `json:"has_more"`
Data []MessagesFeedbacksDataResponse `json:"data"`
}
type MessagesFeedbacksDataResponse struct {
ID string `json:"id"`
Username string `json:"username"`
PhoneNumber string `json:"phone_number"`
AvatarURL string `json:"avatar_url"`
DisplayName string `json:"display_name"`
ConversationID string `json:"conversation_id"`
LastActiveAt int64 `json:"last_active_at"`
CreatedAt int64 `json:"created_at"`
}
type MessagesRequest struct {
ConversationID string `json:"conversation_id"`
FirstID string `json:"first_id,omitempty"`
Limit int `json:"limit"`
User string `json:"user"`
}
type MessagesResponse struct {
Limit int `json:"limit"`
HasMore bool `json:"has_more"`
Data []MessagesDataResponse `json:"data"`
}
type MessagesDataResponse struct {
ID string `json:"id"`
ConversationID string `json:"conversation_id"`
Inputs map[string]interface{} `json:"inputs"`
Query string `json:"query"`
Answer string `json:"answer"`
Feedback interface{} `json:"feedback"`
CreatedAt int64 `json:"created_at"`
}
type ConversationsRequest struct {
LastID string `json:"last_id,omitempty"`
Limit int `json:"limit"`
User string `json:"user"`
}
type ConversationsResponse struct {
Limit int `json:"limit"`
HasMore bool `json:"has_more"`
Data []ConversationsDataResponse `json:"data"`
}
type ConversationsDataResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Inputs map[string]string `json:"inputs"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
}
type ConversationsRenamingRequest struct {
ConversationID string `json:"conversation_id,omitempty"`
Name string `json:"name"`
User string `json:"user"`
}
type ConversationsRenamingResponse struct {
Result string `json:"result"`
}
type ParametersRequest struct {
User string `json:"user"`
}
type ParametersResponse struct {
OpeningStatement string `json:"opening_statement"`
SuggestedQuestions []interface{} `json:"suggested_questions"`
SuggestedQuestionsAfterAnswer struct {
Enabled bool `json:"enabled"`
} `json:"suggested_questions_after_answer"`
MoreLikeThis struct {
Enabled bool `json:"enabled"`
} `json:"more_like_this"`
UserInputForm []map[string]interface{} `json:"user_input_form"`
}
// type ParametersUserInputFormResponse struct {
// TextInput []ParametersTextInputResponse `json:"text-input"`
// }
// type ParametersTextInputResponse struct {
// Label string `json:"label"`
// Variable string `json:"variable"`
// Required bool `json:"required"`
// MaxLength int `json:"max_length"`
// Default string `json:"default"`
// }
const (
FeedbackLike = "like"
FeedbackDislike = "dislike"
)
type Api struct {
c *Client
}
func (api *Api) buildRequestApi(requestUrl string) string {
return api.c.GetHost() + requestUrl
}
func (api *Api) createBaseRequest(ctx context.Context, method string, url string, req ...interface{}) (r *http.Request, err error) {
if r, err = api.c.NewHttpRequest(ctx, method, url, req...); err == nil {
api.c.SetHttpRequest(r).
SetHttpRequestHeader("Authorization", "Bearer " + api.c.GetApiSecretKey()).
SetHttpRequestHeader("Cache-Control", "no-cache").
SetHttpRequestHeader("Content-Type", "application/json; charset=utf-8")
}
return
}
func (api *Api) createGetRequest(ctx context.Context, url string, req ...interface{}) (*http.Request, error) {
return api.createBaseRequest(ctx, http.MethodGet, url, req...)
}
func (api *Api) createPostRequest(ctx context.Context, url string, req ...interface{}) (*http.Request, error) {
return api.createBaseRequest(ctx, http.MethodPost, url, req...)
}
func (api *Api) createChatMessageHttpRequest(ctx context.Context, req *ChatMessageRequest) (r *http.Request, err error) {
r, err = api.createPostRequest(ctx, api.buildRequestApi(chatMessages), req)
return
}
/* Create chat message
* Create a new conversation message or continue an existing dialogue.
*/
func (api *Api) ChatMessages(ctx context.Context, req *ChatMessageRequest) (resp *ChatMessageResponse, err error) {
if req == nil {
err = errors.New("ChatMessages.ChatMessageRequest Illegal")
return
}
req.ResponseMode = "blocking"
var r *http.Request
if r, err = api.createChatMessageHttpRequest(ctx, req); err != nil {
return
}
var _resp ChatMessageResponse
err = api.c.SetHttpRequest(r).SendRequest(&_resp)
resp = &_resp
return
}
func (api *Api) ChatMessagesStreamRaw(ctx context.Context, req *ChatMessageRequest) (resp *http.Response, err error) {
if req == nil {
err = errors.New("ChatMessagesStreamRaw.ChatMessageRequest Illegal")
return
}
req.ResponseMode = "streaming"
var r *http.Request
if r, err = api.createChatMessageHttpRequest(ctx, req); err != nil {
return
}
return api.c.SetHttpRequest(r).SendRequestStream()
}
func (api *Api) ChatMessagesStream(ctx context.Context, req *ChatMessageRequest) (streamChannel chan ChatMessageStreamChannelResponse, err error) {
if req == nil {
err = errors.New("ChatMessagesStream.ChatMessageRequest Illegal")
return
}
var resp *http.Response
if resp, err = api.ChatMessagesStreamRaw(ctx, req); err != nil {
return
}
streamChannel = make(chan ChatMessageStreamChannelResponse)
go api.chatMessagesStreamHandle(ctx, resp, streamChannel)
return
}
func (api *Api) chatMessagesStreamHandle(ctx context.Context, resp *http.Response, streamChannel chan ChatMessageStreamChannelResponse) {
var (
body = resp.Body
reader = bufio.NewReader(body)
err error
line []byte
)
defer resp.Body.Close()
defer close(streamChannel)
if line, _, err = reader.ReadLine(); err == nil {
var errResp ErrorResponse
var _err error
if _err = json.Unmarshal(line, &errResp); _err == nil {
streamChannel <-ChatMessageStreamChannelResponse{
Err: errors.New(string(line)),
}
return
}
}
for {
select {
case <-ctx.Done():
return
default:
if line, err = reader.ReadBytes('\n'); err != nil {
streamChannel <-ChatMessageStreamChannelResponse{
Err: errors.New("Error reading line: " + err.Error()),
}
return
}
if !bytes.HasPrefix(line, []byte("data:")) {
continue
}
line = bytes.TrimPrefix(line, []byte("data:"))
line = bytes.TrimSpace(line)
var resp ChatMessageStreamChannelResponse
if err = json.Unmarshal(line, &resp); err != nil {
streamChannel <-ChatMessageStreamChannelResponse{
Err: errors.New("Error unmarshalling event: " + err.Error()),
}
return
} else if resp.Answer == "" {
return
}
streamChannel <-resp
}
}
}
/* Message terminal user feedback, like
* Rate received messages on behalf of end-users with likes or dislikes.
* This data is visible in the Logs & Annotations page and used for future model fine-tuning.
*/
func (api *Api) MessagesFeedbacks(ctx context.Context, req *MessagesFeedbacksRequest) (resp *MessagesFeedbacksResponse, err error) {
if req == nil {
err = errors.New("MessagesFeedbacks.MessagesFeedbacksRequest Illegal")
return
}
if req.MessageID == "" {
err = errors.New("MessagesFeedbacksRequest.MessageID Illegal")
return
}
var url = api.buildRequestApi(messagesFeedbacks)
url = strings.ReplaceAll(url, "{message_id}", req.MessageID)
req.MessageID = ""
var r *http.Request
if r, err = api.createPostRequest(ctx, url, req); err != nil {
return
}
var _resp MessagesFeedbacksResponse
err = api.c.SetHttpRequest(r).SendRequest(&_resp)
resp = &_resp
return
}
/* Get the chat history message
* The first page returns the latest limit bar, which is in reverse order.
*/
func (api *Api) Messages(ctx context.Context, req *MessagesRequest) (resp *MessagesResponse, err error) {
if req == nil {
err = errors.New("Messages.MessagesRequest Illegal")
return
}
var u = url.Values{}
u.Set("conversation_id", req.ConversationID)
u.Set("user", req.User)
if req.FirstID != "" {
u.Set("first_id", req.FirstID)
}
if req.Limit > 0 {
var l = int64(req.Limit)
u.Set("limit", strconv.FormatInt(l, 10))
}
var r *http.Request
if r, err = api.createGetRequest(ctx, api.buildRequestApi(messages), u); err != nil {
return
}
var _resp MessagesResponse
err = api.c.SetHttpRequest(r).SendRequest(&_resp)
resp = &_resp
return
}
/* Get conversation list
* Gets the session list of the current user. By default, the last 20 sessions are returned.
*/
func (api *Api) Conversations(ctx context.Context, req *ConversationsRequest) (resp *ConversationsResponse, err error) {
if req == nil {
err = errors.New("Conversations.ConversationsRequest Illegal")
return
}
if req.User == "" {
err = errors.New("ConversationsRequest.User Illegal")
return
}
if req.Limit == 0 {
req.Limit = 20
}
var u = url.Values{}
u.Set("last_id", req.LastID)
u.Set("user", req.User)
var l = int64(req.Limit)
u.Set("limit", strconv.FormatInt(l, 10))
var r *http.Request
if r, err = api.createGetRequest(ctx, api.buildRequestApi(conversations), u); err != nil {
return
}
var _resp ConversationsResponse
err = api.c.SetHttpRequest(r).SendRequest(&_resp)
resp = &_resp
return
}
/* Conversation renaming
* Rename conversations; the name is displayed in multi-session client interfaces.
*/
func (api *Api) ConversationsRenaming(ctx context.Context, req *ConversationsRenamingRequest) (resp *ConversationsRenamingResponse, err error) {
if req == nil {
err = errors.New("ConversationsRenaming.ConversationsRenamingRequest Illegal")
return
}
var url = api.buildRequestApi(conversationsRename)
url = strings.ReplaceAll(url, "{conversation_id}", req.ConversationID)
req.ConversationID = ""
var r *http.Request
if r, err = api.createPostRequest(ctx, url, req); err != nil {
return
}
var _resp ConversationsRenamingResponse
err = api.c.SetHttpRequest(r).SendRequest(&_resp)
resp = &_resp
return
}
/* Obtain application parameter information
* Retrieve configured Input parameters, including variable names, field names, types, and default values.
* Typically used for displaying these fields in a form or filling in default values after the client loads.
*/
func (api *Api) Parameters(ctx context.Context, req *ParametersRequest) (resp *ParametersResponse, err error) {
if req == nil {
err = errors.New("Parameters.ParametersRequest Illegal")
return
}
if req.User == "" {
err = errors.New("ParametersRequest.User Illegal")
return
}
var u = url.Values{}
u.Set("user", req.User)
var r *http.Request
if r, err = api.createGetRequest(ctx, api.buildRequestApi(parameters), u); err != nil {
return
}
var _resp ParametersResponse
err = api.c.SetHttpRequest(r).SendRequest(&_resp)
resp = &_resp
return
}