package llm import ( "bytes" "eta/eta_api/utils" "fmt" "io" "mime/multipart" "net/http" "os" ) type UpdateTempDocsResp struct { Code int `json:"code"` Msg string `json:"msg"` Data struct { Id string `json:"id"` FailedFiles []interface{} `json:"failed_files"` } `json:"data"` } func UpdateTempDocs(filePath string) { postUrl := utils.LLM_SERVER + "/knowledge_base/upload_temp_docs" params := make(map[string]string) //params[`prev_id`] = `` params[`chunk_size`] = `750` params[`chunk_overlap`] = `150` params[`zh_title_enhance`] = `true` files := make(map[string]string) files[`files`] = filePath result, err := PostFormData(postUrl, params, files) if err != nil { return } str := string(result) fmt.Println(str) return } func ChatByFile(question string) { // 没有问题那就直接返回 if question == `` { return } } // PostFormData sends a POST request with form-data func PostFormData(url string, params map[string]string, files map[string]string) ([]byte, error) { body := &bytes.Buffer{} writer := multipart.NewWriter(body) for key, val := range params { if err := writer.WriteField(key, val); err != nil { return nil, err } } for fieldName, filePath := range files { file, err := os.Open(filePath) if err != nil { return nil, err } defer file.Close() part, err := writer.CreateFormFile(fieldName, filePath) if err != nil { return nil, err } _, err = io.Copy(part, file) if err != nil { return nil, err } } err := writer.Close() if err != nil { return nil, err } req, err := http.NewRequest("POST", url, body) if err != nil { return nil, err } req.Header.Set("Content-Type", writer.FormDataContentType()) client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() result, err := io.ReadAll(resp.Body) return result, nil }