search.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package fastgpt
  2. import (
  3. "errors"
  4. )
  5. type SearchReq struct {
  6. DatasetID string `json:"datasetId"` //知识库ID
  7. Text string `json:"text"` //需要测试的文本
  8. Limit uint64 `json:"limit"` //最大 tokens 数量
  9. Similarity float64 `json:"similarity,optional"` //最低相关度(0~1,可选)
  10. SearchMode string `json:"searchMode"` //搜索模式:embedding | fullTextRecall | mixedRecall
  11. UsingReRank bool `json:"usingReRank"` //使用重排
  12. }
  13. type SearchResp struct {
  14. Code int `json:"code"`
  15. StatusText string `json:"statusText"`
  16. Message string `json:"message"`
  17. Data struct {
  18. List []struct {
  19. ID string `json:"id"`
  20. DatasetID string `json:"datasetId"`
  21. CollectionID string `json:"collectionId"`
  22. SourceName string `json:"sourceName"`
  23. Q string `json:"q"`
  24. A string `json:"a"`
  25. ChunkIndex int `json:"chunkIndex"`
  26. Score []struct {
  27. Type string `json:"type"`
  28. Value float64 `json:"value"`
  29. Index uint64 `json:"index"`
  30. } `json:"score"`
  31. } `json:"list,optional"`
  32. Duration string `json:"duration"`
  33. SearchMode string `json:"searchMode"`
  34. Limit uint64 `json:"limit"`
  35. Similarity uint64 `json:"similarity"`
  36. UsingReRank bool `json:"usingReRank"`
  37. UsingSimilarityFilter bool `json:"usingSimilarityFilter"`
  38. } `json:"data,optional"`
  39. }
  40. // SearchText 搜索数据
  41. func SearchText(params *SearchReq) (dataList *SearchResp, err error) {
  42. resp, err := NewResty().
  43. R().
  44. SetResult(&dataList).
  45. SetBody(params).
  46. Post("core/dataset/searchTest")
  47. if err != nil {
  48. return nil, err
  49. }
  50. if resp.IsError() {
  51. return nil, errors.New(resp.String())
  52. }
  53. return
  54. }