collection.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package fastgpt
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "time"
  7. )
  8. type CollectionResp struct {
  9. Code int `json:"code"`
  10. StatusText string `json:"statusText"`
  11. Message string `json:"message"`
  12. Data string `json:"data"`
  13. }
  14. type CreateCollectionReq struct {
  15. DatasetId string `json:"datasetId"`
  16. ParentId string `json:"parentId,optional"`
  17. Name string `json:"name"`
  18. Type string `json:"type"`
  19. Metadata interface{} `json:"metadata,optional"`
  20. }
  21. type CreateTextCollectionReq struct {
  22. DatasetId string `json:"datasetId"`
  23. ParentId string `json:"parentId,optional"`
  24. Name string `json:"name"`
  25. Text string `json:"text"`
  26. Metadata interface{} `json:"metadata,optional"`
  27. TrainingType string `json:"trainingType,optional"`
  28. ChunkSize uint64 `json:"chunkSize,optional"`
  29. ChunkSplitter string `json:"chunkSplitter,optional"`
  30. QaPrompt string `json:"qaPrompt,optional"`
  31. }
  32. type CreateTextCollectionResp struct {
  33. Code int `json:"code"`
  34. StatusText string `json:"statusText"`
  35. Message string `json:"message"`
  36. Data struct {
  37. CollectionID string `json:"collectionId"`
  38. Results struct {
  39. InsertLen int `json:"insertLen"`
  40. OverToken []any `json:"overToken"`
  41. Repeat []any `json:"repeat"`
  42. Error []any `json:"error"`
  43. } `json:"results"`
  44. } `json:"data"`
  45. }
  46. type CreateLinkCollectionReq struct {
  47. DatasetId string `json:"datasetId"`
  48. ParentId string `json:"parentId,optional"`
  49. Name string `json:"name"`
  50. Link string `json:"link"`
  51. Metadata interface{} `json:"metadata,optional"`
  52. TrainingType string `json:"trainingType,optional"`
  53. ChunkSize uint64 `json:"chunkSize,optional"`
  54. ChunkSplitter string `json:"chunkSplitter,optional"`
  55. QaPrompt string `json:"qaPrompt,optional"`
  56. }
  57. type CreateLinkCollectionResp struct {
  58. Code int `json:"code"`
  59. StatusText string `json:"statusText"`
  60. Message string `json:"message"`
  61. Data struct {
  62. CollectionID string `json:"collectionId"`
  63. } `json:"data"`
  64. }
  65. type CreateFileCollectionReq struct {
  66. DatasetId string `json:"datasetId"`
  67. ParentId string `json:"parentId,optional"`
  68. Metadata interface{} `json:"metadata,optional"`
  69. TrainingType string `json:"trainingType,optional"`
  70. ChunkSize uint64 `json:"chunkSize,optional"`
  71. ChunkSplitter string `json:"chunkSplitter,optional"`
  72. QaPrompt string `json:"qaPrompt,optional"`
  73. }
  74. type CreateFileCollectionResp struct {
  75. Code int `json:"code"`
  76. StatusText string `json:"statusText,optional"`
  77. Message string `json:"message,optional"`
  78. Data struct {
  79. CollectionID string `json:"collectionId,optional"`
  80. Results struct {
  81. InsertLen int `json:"insertLen"`
  82. OverToken []any `json:"overToken"`
  83. Repeat []any `json:"repeat"`
  84. Error []any `json:"error"`
  85. } `json:"results,optional"`
  86. } `json:"data,optional"`
  87. }
  88. type GetCollectionListReq struct {
  89. PageNum int `json:"pageNum,optional"`
  90. PageSize int `json:"pageSize,optional"`
  91. DatasetId string `json:"datasetId"`
  92. ParentId string `json:"parentId,optional"`
  93. SearchText string `json:"searchText,optional"`
  94. }
  95. type CollectionInfo struct {
  96. ID string `json:"_id"`
  97. ParentID string `json:"parentId"`
  98. TeamID string `json:"teamId"`
  99. TmbID string `json:"tmbId"`
  100. DatasetID struct {
  101. ID string `json:"_id"`
  102. ParentID string `json:"parentId"`
  103. TeamID string `json:"teamId"`
  104. TmbID string `json:"tmbId"`
  105. Type string `json:"type"`
  106. Status string `json:"status"`
  107. Avatar string `json:"avatar"`
  108. Name string `json:"name"`
  109. VectorModel string `json:"vectorModel"`
  110. AgentModel string `json:"agentModel"`
  111. Intro string `json:"intro"`
  112. Permission string `json:"permission"`
  113. UpdateTime time.Time `json:"updateTime"`
  114. } `json:"datasetId"`
  115. Type string `json:"type"`
  116. Name string `json:"name"`
  117. TrainingType string `json:"trainingType"`
  118. ChunkSize uint64 `json:"chunkSize"`
  119. ChunkSplitter string `json:"chunkSplitter"`
  120. QaPrompt string `json:"qaPrompt"`
  121. RawTextLength uint64 `json:"rawTextLength"`
  122. HashRawText string `json:"hashRawText"`
  123. CreateTime time.Time `json:"createTime"`
  124. UpdateTime time.Time `json:"updateTime"`
  125. CanWrite bool `json:"canWrite"`
  126. SourceName string `json:"sourceName"`
  127. }
  128. type CollectionListResp struct {
  129. Code int `json:"code"`
  130. StatusText string `json:"statusText"`
  131. Message string `json:"message"`
  132. Data struct {
  133. PageNum int `json:"pageNum"`
  134. PageSize int `json:"pageSize"`
  135. Data []CollectionSimpleInfo `json:"data"`
  136. Total int `json:"total"`
  137. } `json:"data"`
  138. }
  139. type CollectionSimpleInfo struct {
  140. ID string `json:"_id"`
  141. ParentID string `json:"parentId"`
  142. TmbID string `json:"tmbId"`
  143. Type string `json:"type"`
  144. Name string `json:"name"`
  145. UpdateTime time.Time `json:"updateTime"`
  146. DataAmount uint64 `json:"dataAmount"`
  147. TrainingAmount uint64 `json:"trainingAmount"`
  148. }
  149. type CollectionDetailResp struct {
  150. Code int `json:"code"`
  151. StatusText string `json:"statusText"`
  152. Message string `json:"message"`
  153. Data CollectionInfo `json:"data"`
  154. }
  155. type UpdateCollectionReq struct {
  156. ID string `json:"id"`
  157. ParentId string `json:"parentId"`
  158. Name string `json:"name"`
  159. }
  160. // GetCollectionDetail 获取合集详情
  161. func GetCollectionDetail(id string) (collection *CollectionDetailResp, err error) {
  162. resp, err := NewResty().
  163. R().
  164. SetResult(&collection).
  165. SetQueryParam("id", id).
  166. Get("core/dataset/collection/detail")
  167. if err != nil {
  168. return nil, err
  169. }
  170. if resp.IsError() {
  171. return nil, errors.New(resp.String())
  172. }
  173. return
  174. }
  175. // GetCollectionList 获取合集列表
  176. func GetCollectionList(params *GetCollectionListReq) (collectionList *CollectionListResp, err error) {
  177. resp, err := NewResty().
  178. R().
  179. SetResult(&collectionList).
  180. SetBody(params).
  181. Post("core/dataset/collection/list")
  182. if err != nil {
  183. return nil, err
  184. }
  185. if resp.IsError() {
  186. return nil, errors.New(resp.String())
  187. }
  188. return
  189. }
  190. // CreateEmptyCollection 创建空合集
  191. func CreateEmptyCollection(data *CreateCollectionReq) (collection *CollectionResp, err error) {
  192. resp, err := NewResty().
  193. R().
  194. SetResult(&collection).
  195. SetBody(data).
  196. Post("core/dataset/collection/create")
  197. if err != nil {
  198. return nil, err
  199. }
  200. if resp.IsError() {
  201. return nil, errors.New(resp.String())
  202. }
  203. return
  204. }
  205. // CreateTextCollection 创建文本合集
  206. func CreateTextCollection(data *CreateTextCollectionReq) (collection *CreateTextCollectionResp, err error) {
  207. resp, err := NewResty().
  208. R().
  209. SetResult(&collection).
  210. SetBody(*data).
  211. Post("core/dataset/collection/create/text")
  212. if err != nil {
  213. fmt.Printf(err.Error())
  214. return nil, err
  215. }
  216. if resp.IsError() {
  217. return nil, errors.New(resp.String())
  218. }
  219. return
  220. }
  221. // CreateLinkCollection 创建链接合集
  222. func CreateLinkCollection(data *CreateLinkCollectionReq) (collection *CreateLinkCollectionResp, err error) {
  223. resp, err := NewResty().
  224. R().
  225. SetResult(&collection).
  226. SetBody(*data).
  227. Post("core/dataset/collection/create/link")
  228. if err != nil {
  229. fmt.Printf(err.Error())
  230. return nil, err
  231. }
  232. if resp.IsError() {
  233. return nil, errors.New(resp.String())
  234. }
  235. return
  236. }
  237. // CreateFileCollection 创建文件合集
  238. func CreateFileCollection(data *map[string]string, filePath string) (collection *CreateFileCollectionResp, err error) {
  239. dataString, _ := json.Marshal(*data)
  240. resp, err := NewResty().
  241. R().
  242. SetResult(&collection).
  243. SetFormData(map[string]string{"data": string(dataString)}).
  244. SetFile("file", filePath).
  245. Post("core/dataset/collection/create/localFile")
  246. fmt.Printf("%v, %v", err, resp.String())
  247. if err != nil {
  248. return nil, err
  249. }
  250. if resp.IsError() {
  251. return nil, errors.New(resp.String())
  252. }
  253. return
  254. }
  255. // UpdateCollection 修改合集
  256. func UpdateCollection(data *UpdateCollectionReq) (collection *CollectionResp, err error) {
  257. resp, err := NewResty().
  258. R().
  259. SetResult(&collection).
  260. SetBody(*data).
  261. Put("core/dataset/collection/update")
  262. if err != nil {
  263. return nil, err
  264. }
  265. if resp.IsError() {
  266. return nil, errors.New(resp.String())
  267. }
  268. return
  269. }
  270. // DeleteCollection 删除合集
  271. func DeleteCollection(id string) (collection *CollectionResp, err error) {
  272. resp, err := NewResty().
  273. R().
  274. SetResult(&collection).
  275. SetQueryParam("id", id).
  276. Delete("core/dataset/collection/delete")
  277. if err != nil {
  278. return nil, err
  279. }
  280. if resp.IsError() {
  281. return nil, errors.New(resp.String())
  282. }
  283. return
  284. }