dify.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package dify
  2. import (
  3. "errors"
  4. "github.com/go-resty/resty/v2"
  5. )
  6. type SessionResponse struct {
  7. Limit int `json:"limit"`
  8. HasMore bool `json:"has_more"`
  9. Data []SessionDatum `json:"data"`
  10. }
  11. type SessionDatum struct {
  12. ID string `json:"id"`
  13. Name string `json:"name"`
  14. Inputs Inputs `json:"inputs"`
  15. Status string `json:"status"`
  16. CreatedAt int64 `json:"created_at"`
  17. }
  18. type Inputs map[string]string
  19. type HistoryResponse struct {
  20. Limit int `json:"limit"`
  21. HasMore bool `json:"has_more"`
  22. Data []History `json:"data"`
  23. }
  24. type History struct {
  25. ID string `json:"id"`
  26. ConversationId string `json:"conversation_id"`
  27. Inputs Inputs `json:"inputs"`
  28. Query string `json:"query"`
  29. Answer string `json:"answer"`
  30. CreatedAt int64 `json:"created_at"`
  31. }
  32. type DeleteSessionResponse struct {
  33. Result string `json:"result"`
  34. }
  35. type RenameSessionResp struct {
  36. ID string `json:"id"`
  37. Name string `json:"name"`
  38. Inputs Inputs `json:"inputs"`
  39. Introduction string `json:"introduction"`
  40. CreatedAt int64 `json:"created_at"`
  41. }
  42. // GetSessionList 获取会话列表
  43. func GetSessionList(baseUrl, token, user, lastId, limit string) (*SessionResponse, error) {
  44. data := &SessionResponse{}
  45. resp, err := NewDifyResty(baseUrl, token).
  46. R().
  47. SetResult(&data).
  48. SetQueryParam("user", user).
  49. SetQueryParam("last_id", lastId).
  50. SetQueryParam("limit", limit).
  51. Get("/conversations")
  52. if err != nil {
  53. return nil, err
  54. }
  55. if resp.IsError() {
  56. return nil, errors.New(resp.String())
  57. }
  58. return data, nil
  59. }
  60. // DeleteSession 删除会话
  61. func DeleteSession(baseUrl, token, user, conversationId string) (*DeleteSessionResponse, error) {
  62. data := &DeleteSessionResponse{}
  63. body := make(map[string]string)
  64. body["user"] = user
  65. _, err := NewDifyResty(baseUrl, token).
  66. R().
  67. SetResult(&data).
  68. SetBody(body).
  69. Delete("/conversations/" + conversationId)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return data, nil
  74. }
  75. // RenameSession 会话重命名
  76. func RenameSession(baseUrl, token, user, name, conversationId string) (*DeleteSessionResponse, error) {
  77. data := &DeleteSessionResponse{}
  78. body := make(map[string]string)
  79. body["user"] = user
  80. body["name"] = name
  81. resp, err := NewDifyResty(baseUrl, token).
  82. R().
  83. SetResult(&data).
  84. SetBody(body).
  85. Post("/conversations/" + conversationId + "/name")
  86. if err != nil {
  87. return nil, err
  88. }
  89. if resp.IsError() {
  90. return nil, errors.New(resp.String())
  91. }
  92. return data, nil
  93. }
  94. // GetChatHistory 获取历史会话
  95. func GetChatHistory(baseUrl, token, user, conversationId, firstId, limit string) (*HistoryResponse, error) {
  96. data := &HistoryResponse{}
  97. resp, err := NewDifyResty(baseUrl, token).
  98. R().
  99. SetResult(&data).
  100. SetQueryParam("user", user).
  101. SetQueryParam("conversation_id", conversationId).
  102. SetQueryParam("first_id", firstId).
  103. SetQueryParam("limit", limit).
  104. Get("/messages")
  105. if err != nil {
  106. return nil, err
  107. }
  108. if resp.IsError() {
  109. return nil, errors.New(resp.String())
  110. }
  111. return data, nil
  112. }
  113. // NewDifyResty 实例化dify实例
  114. func NewDifyResty(baseUrl, token string) *resty.Client {
  115. client := resty.New()
  116. client.SetRetryCount(2).
  117. SetHeader("Content-Type", "application/json").
  118. SetBaseURL(baseUrl).
  119. SetHeader("Authorization", "Bearer "+token)
  120. return client
  121. }