data.go 4.9 KB

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