chroma.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "time"
  10. "github.com/beego/beego/v2/server/web"
  11. )
  12. // ChromaController 知识库搜索控制器
  13. type ChromaController struct {
  14. web.Controller
  15. }
  16. // SearchRequest 搜索请求结构体
  17. type SearchRequest struct {
  18. QueryStr string `json:"query_str"`
  19. NResults int `json:"n_results"`
  20. }
  21. // SearchResponse 搜索响应结构体
  22. type SearchResponse struct {
  23. Total int `json:"total"`
  24. Files []FileInfo `json:"files"`
  25. }
  26. // FileInfo 文件信息结构体
  27. type FileInfo struct {
  28. DocumentID string `json:"document_id"`
  29. FileName string `json:"file_name"`
  30. FilePath string `json:"file_path"`
  31. SourceFile string `json:"source_file"`
  32. HasTitle bool `json:"has_title"`
  33. }
  34. // AdvancedSearch 知识库文件高级搜索接口
  35. func (c *ChromaController) AdvancedSearch() {
  36. // 获取查询参数
  37. queryStr := c.GetString("query_str")
  38. nResultsStr := c.GetString("n_results", "50")
  39. // 参数验证
  40. if queryStr == "" {
  41. c.Data["json"] = map[string]interface{}{
  42. "code": 400,
  43. "message": "查询字符串不能为空",
  44. "data": nil,
  45. }
  46. c.ServeJSON()
  47. return
  48. }
  49. // 转换n_results参数
  50. nResults, err := strconv.Atoi(nResultsStr)
  51. if err != nil || nResults <= 0 {
  52. nResults = 50 // 默认值
  53. }
  54. // 构建请求参数
  55. searchReq := SearchRequest{
  56. QueryStr: queryStr,
  57. NResults: nResults,
  58. }
  59. // 调用外部API
  60. result, err := c.callAdvancedSearchAPI(searchReq)
  61. if err != nil {
  62. c.Data["json"] = map[string]interface{}{
  63. "code": 500,
  64. "message": fmt.Sprintf("搜索失败: %v", err),
  65. "data": nil,
  66. }
  67. c.ServeJSON()
  68. return
  69. }
  70. fmt.Println("知识库:", result)
  71. // 返回成功结果
  72. c.Data["json"] = map[string]interface{}{
  73. "code": 200,
  74. "message": "搜索成功",
  75. "data": result,
  76. }
  77. c.ServeJSON()
  78. }
  79. // callAdvancedSearchAPI 调用外部高级搜索API
  80. func (c *ChromaController) callAdvancedSearchAPI(req SearchRequest) (*SearchResponse, error) {
  81. // 构建请求URL
  82. apiURL := "https://aqai.shudaodsj.com:22000/admin/api/v1/knowledge/files/advanced-search"
  83. // 构建查询参数
  84. params := url.Values{}
  85. params.Add("query_str", req.QueryStr)
  86. params.Add("n_results", strconv.Itoa(req.NResults))
  87. // 创建HTTP请求
  88. fullURL := apiURL + "?" + params.Encode()
  89. httpReq, err := http.NewRequest("GET", fullURL, nil)
  90. if err != nil {
  91. return nil, fmt.Errorf("创建请求失败: %v", err)
  92. }
  93. // 设置请求头
  94. httpReq.Header.Set("Content-Type", "application/json")
  95. httpReq.Header.Set("User-Agent", "shudao-chat-go/1.0")
  96. // 创建HTTP客户端
  97. client := &http.Client{
  98. Timeout: 30 * time.Second,
  99. }
  100. // 发送请求
  101. resp, err := client.Do(httpReq)
  102. if err != nil {
  103. return nil, fmt.Errorf("请求失败: %v", err)
  104. }
  105. defer resp.Body.Close()
  106. // 检查响应状态码
  107. if resp.StatusCode != http.StatusOK {
  108. return nil, fmt.Errorf("API返回错误状态码: %d", resp.StatusCode)
  109. }
  110. // 读取响应体
  111. body, err := io.ReadAll(resp.Body)
  112. if err != nil {
  113. return nil, fmt.Errorf("读取响应失败: %v", err)
  114. }
  115. // 解析响应JSON
  116. var searchResp SearchResponse
  117. err = json.Unmarshal(body, &searchResp)
  118. if err != nil {
  119. return nil, fmt.Errorf("解析响应JSON失败: %v", err)
  120. }
  121. return &searchResp, nil
  122. }