data.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package fastgpt
  2. import (
  3. "errors"
  4. )
  5. type DataResp struct {
  6. Code int `json:"code"`
  7. StatusText string `json:"statusText"`
  8. Message string `json:"message"`
  9. Data string `json:"data,optional"`
  10. }
  11. type CreateBulkDataReq struct {
  12. CollectionID string `json:"collectionId"`
  13. TrainingMode string `json:"trainingMode"`
  14. Prompt string `json:"prompt,optional"`
  15. BillID string `json:"billId,optional"`
  16. Data []DataQuestion `json:"data"`
  17. }
  18. type DataQuestion struct {
  19. Q string `json:"q"`
  20. A string `json:"a,optional"`
  21. Indexes []Index `json:"indexes,omitempty,optional"`
  22. }
  23. type Index struct {
  24. DataId string `json:"dataId,optional"`
  25. DefaultIndex bool `json:"defaultIndex,optional"`
  26. Text string `json:"text"`
  27. }
  28. type CreateBulkDataResp struct {
  29. Code int `json:"code"`
  30. StatusText string `json:"statusText"`
  31. Data struct {
  32. InsertLen uint64 `json:"insertLen"`
  33. OverToken []any `json:"overToken"`
  34. Repeat []any `json:"repeat"`
  35. Error []any `json:"error"`
  36. } `json:"data"`
  37. }
  38. type GetDataListReq struct {
  39. PageNum int `json:"pageNum,optional"`
  40. PageSize int `json:"pageSize,optional"`
  41. CollectionId string `json:"collectionId,optional"`
  42. SearchText string `json:"searchText,optional"`
  43. }
  44. type DataInfo struct {
  45. ID string `json:"id"`
  46. TeamID string `json:"teamId"`
  47. Q string `json:"q"`
  48. A string `json:"a"`
  49. ChunkIndex uint64 `json:"chunkIndex"`
  50. Indexes []struct {
  51. DefaultIndex bool `json:"defaultIndex"`
  52. DataID string `json:"dataId"`
  53. Text string `json:"text"`
  54. ID string `json:"_id"`
  55. } `json:"indexes"`
  56. DatasetID string `json:"datasetId"`
  57. CollectionID string `json:"collectionId"`
  58. SourceName string `json:"sourceName"`
  59. SourceID string `json:"sourceId"`
  60. IsOwner bool `json:"isOwner"`
  61. CanWrite bool `json:"canWrite"`
  62. }
  63. type DataListResp struct {
  64. Code int `json:"code"`
  65. StatusText string `json:"statusText"`
  66. Message string `json:"message"`
  67. Data struct {
  68. PageNum int `json:"pageNum"`
  69. PageSize int `json:"pageSize"`
  70. Data []struct {
  71. ID string `json:"_id"`
  72. DatasetID string `json:"datasetId"`
  73. CollectionID string `json:"collectionId"`
  74. Q string `json:"q"`
  75. A string `json:"a"`
  76. ChunkIndex uint64 `json:"chunkIndex"`
  77. } `json:"data,optional"`
  78. Total int `json:"total"`
  79. } `json:"data,optional"`
  80. }
  81. type DataDetail struct {
  82. Code int `json:"code"`
  83. StatusText string `json:"statusText"`
  84. Message string `json:"message"`
  85. Data DataInfo `json:"data,optional"`
  86. }
  87. type UpdateDataReq struct {
  88. DataId string `json:"dataId" validate:"required"`
  89. Q string `json:"q" validate:"required"`
  90. A string `json:"a" validate:"required"`
  91. Indexes []Index `json:"indexes,omitempty,optional"`
  92. }
  93. // GetDataDetail 获取数据详情
  94. func GetDataDetail(id string) (data *DataDetail, err error) {
  95. resp, err := NewResty().
  96. R().
  97. SetResult(&data).
  98. SetQueryParam("id", id).
  99. Get("core/dataset/data/detail")
  100. if err != nil {
  101. return nil, err
  102. }
  103. if resp.IsError() {
  104. return nil, errors.New(resp.String())
  105. }
  106. return
  107. }
  108. // GetDataList 获取数据列表
  109. func GetDataList(params *GetDataListReq) (dataList *DataListResp, err error) {
  110. resp, err := NewResty().
  111. R().
  112. SetResult(&dataList).
  113. SetBody(params).
  114. Post("core/dataset/data/list")
  115. if err != nil {
  116. return nil, err
  117. }
  118. if resp.IsError() {
  119. return nil, errors.New(resp.String())
  120. }
  121. return
  122. }
  123. // CreateBulkData 批量创建数据
  124. func CreateBulkData(data *CreateBulkDataReq) (response *CreateBulkDataResp, err error) {
  125. resp, err := NewResty().
  126. R().
  127. SetResult(&response).
  128. SetBody(*data).
  129. Post("core/dataset/data/pushData")
  130. if err != nil {
  131. return nil, err
  132. }
  133. if resp.IsError() {
  134. return nil, errors.New(resp.String())
  135. }
  136. return
  137. }
  138. // DiyCreateBulkData 批量创建数据
  139. func DiyCreateBulkData(token string, data *CreateBulkDataReq) (response *CreateBulkDataResp, err error) {
  140. resp, err := NewDiyResty(token).
  141. R().
  142. SetResult(&response).
  143. SetBody(*data).
  144. Post("core/dataset/data/pushData")
  145. if err != nil {
  146. return nil, err
  147. }
  148. if resp.IsError() {
  149. return nil, errors.New(resp.String())
  150. }
  151. return
  152. }
  153. // UpdateData 修改数据
  154. func UpdateData(data *UpdateDataReq) (response *DataResp, err error) {
  155. resp, err := NewResty().
  156. R().
  157. SetResult(&response).
  158. SetBody(*data).
  159. Put("core/dataset/data/update")
  160. if err != nil {
  161. return nil, err
  162. }
  163. if resp.IsError() {
  164. return nil, errors.New(resp.String())
  165. }
  166. return
  167. }
  168. // DeleteData 删除数据
  169. func DeleteData(id string) (response *DataResp, err error) {
  170. resp, err := NewResty().
  171. R().
  172. SetResult(&response).
  173. SetQueryParam("id", id).
  174. Delete("core/dataset/data/delete")
  175. if err != nil {
  176. return nil, err
  177. }
  178. if resp.IsError() {
  179. return nil, errors.New(resp.String())
  180. }
  181. return
  182. }