forked from zhouyangtingwen/dify-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 15
/
api_messages.go
103 lines (90 loc) · 2.97 KB
/
api_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
package dify
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
)
const (
FeedbackLike = "like"
FeedbackDislike = "dislike"
)
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"`
}
/* 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) {
httpReq, err := api.createBaseRequest(ctx, http.MethodGet, "/v1/messages", nil)
if err != nil {
return
}
query := httpReq.URL.Query()
query.Set("conversation_id", req.ConversationID)
query.Set("user", req.User)
if req.FirstID != "" {
query.Set("first_id", req.FirstID)
}
if req.Limit > 0 {
query.Set("limit", strconv.FormatInt(int64(req.Limit), 10))
}
httpReq.URL.RawQuery = query.Encode()
err = api.c.sendJSONRequest(httpReq, &resp)
return
}
/* 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.MessageID == "" {
err = errors.New("MessagesFeedbacksRequest.MessageID Illegal")
return
}
url := fmt.Sprintf("/v1/messages/%s/feedbacks", req.MessageID)
req.MessageID = ""
httpReq, err := api.createBaseRequest(ctx, http.MethodPost, url, req)
if err != nil {
return
}
err = api.c.sendJSONRequest(httpReq, &resp)
return
}