MCPcopy Create free account
hub / github.com/castai/openwebui-content-sync / AddFileToKnowledge

Method AddFileToKnowledge

internal/openwebui/client.go:228–283  ·  view source on GitHub ↗

AddFileToKnowledge adds a file to a knowledge source

(ctx context.Context, knowledgeID, fileID string)

Source from the content-addressed store, hash-verified

226
227// AddFileToKnowledge adds a file to a knowledge source
228func (c *Client) AddFileToKnowledge(ctx context.Context, knowledgeID, fileID string) error {
229 url := fmt.Sprintf("%s/api/v1/knowledge/%s/file/add", c.baseURL, knowledgeID)
230
231 logrus.Debugf("Adding file to knowledge: fileID=%s, knowledgeID=%s", fileID, knowledgeID)
232 logrus.Debugf("Add file URL: %s", url)
233
234 payload := map[string]string{
235 "file_id": fileID,
236 }
237
238 jsonData, err := json.Marshal(payload)
239 if err != nil {
240 return fmt.Errorf("failed to marshal payload: %w", err)
241 }
242
243 // logrus.Debugf("Add file payload: %s", string(jsonData))
244
245 req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
246 if err != nil {
247 return fmt.Errorf("failed to create request: %w", err)
248 }
249
250 req.Header.Set("Content-Type", "application/json")
251 if c.apiKey != "" {
252 req.Header.Set("Authorization", "Bearer "+c.apiKey)
253 logrus.Debugf("Using API key for add file request")
254 } else {
255 logrus.Debugf("No API key provided for add file request")
256 }
257
258 logrus.Debugf("Sending add file to knowledge request...")
259 resp, err := c.client.Do(req)
260 if err != nil {
261 return fmt.Errorf("failed to send request: %w", err)
262 }
263 defer resp.Body.Close()
264
265 logrus.Debugf("Add file to knowledge response status: %d %s", resp.StatusCode, resp.Status)
266
267 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
268 body, _ := io.ReadAll(resp.Body)
269 logrus.Errorf("Add file to knowledge failed with status %d: %s", resp.StatusCode, string(body))
270 return fmt.Errorf("add file to knowledge failed with status %d: %s", resp.StatusCode, string(body))
271 }
272
273 // Read response body for debugging
274 body, err := io.ReadAll(resp.Body)
275 if err != nil {
276 logrus.Warnf("Failed to read add file response body: %v", err)
277 } else {
278 logrus.Debugf("Add file to knowledge response body: %s", string(body))
279 }
280
281 logrus.Debugf("Successfully added file %s to knowledge %s", fileID, knowledgeID)
282 return nil
283}
284
285// waitForFileProcessing waits for a file to finish processing with adaptive polling

Callers 1

Calls

no outgoing calls

Tested by 1