-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi-completion-messages.go
114 lines (93 loc) · 2.86 KB
/
api-completion-messages.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
package dify
import (
"encoding/json"
"fmt"
)
type CompletionMessagesPayload struct {
Inputs any `json:"inputs"`
ResponseMode string `json:"response_mode,omitempty"`
User string `json:"user"`
ConversationID string `json:"conversation_id,omitempty"`
}
type CompletionMessagesResponse struct {
Event string `json:"event"`
TaskID string `json:"task_id"`
ID string `json:"id"`
MessageID string `json:"message_id"`
Mode string `json:"mode"`
Answer string `json:"answer"`
Metadata any `json:"metadata"`
CreatedAt int `json:"created_at"`
}
func PrepareCompletionPayload(payload map[string]interface{}) (string, error) {
jsonData, err := json.Marshal(payload)
if err != nil {
return "", err
}
return string(jsonData), nil
}
func (dc *DifyClient) CompletionMessages(inputs string, conversation_id string, files []any) (result CompletionMessagesResponse, err error) {
var payload CompletionMessagesPayload
if len(inputs) == 0 {
return result, fmt.Errorf("inputs is required")
} else {
var tryDecode map[string]interface{}
err := json.Unmarshal([]byte(inputs), &tryDecode)
if err != nil {
return result, fmt.Errorf("inputs should be a valid JSON string")
}
payload.Inputs = tryDecode
}
payload.ResponseMode = RESPONSE_MODE_BLOCKING
payload.User = dc.User
if conversation_id != "" {
payload.ConversationID = conversation_id
}
if len(files) > 0 {
// TODO TBD
return result, fmt.Errorf("files are not supported")
}
api := dc.GetAPI(API_COMPLETION_MESSAGES)
code, body, err := SendPostRequestToAPI(dc, api, payload)
err = CommonRiskForSendRequest(code, err)
if err != nil {
return result, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return result, fmt.Errorf("failed to unmarshal the response: %v", err)
}
return result, nil
}
func (dc *DifyClient) CompletionMessagesStreaming(inputs string, conversation_id string, files []any) (result string, err error) {
var payload CompletionMessagesPayload
if len(inputs) == 0 {
return "", fmt.Errorf("inputs is required")
} else {
var tryDecode map[string]interface{}
err := json.Unmarshal([]byte(inputs), &tryDecode)
if err != nil {
return "", fmt.Errorf("inputs should be a valid JSON string")
}
payload.Inputs = tryDecode
}
payload.ResponseMode = RESPONSE_MODE_STREAMING
payload.User = dc.User
if conversation_id != "" {
payload.ConversationID = conversation_id
}
if len(files) > 0 {
// TODO TBD
return "", fmt.Errorf("files are not supported")
}
api := dc.GetAPI(API_COMPLETION_MESSAGES)
code, body, err := SendPostRequestToAPI(dc, api, payload)
err = CommonRiskForSendRequest(code, err)
if err != nil {
return result, err
}
// if !strings.Contains(resp.Header.Get("Content-Type"), "text/event-stream") {
// return "", fmt.Errorf("response is not a streaming response")
// }
return string(body), nil
}