Class DifyDocumentConverter

java.lang.Object
io.agentscope.core.rag.integration.dify.DifyDocumentConverter

public class DifyDocumentConverter extends Object
Converter for transforming Dify API responses to AgentScope Document objects.

This class handles the conversion between Dify's retrieve response format and AgentScope's internal Document representation, ensuring seamless integration with the RAG system.

Dify API response format:


 {
   "query": { "content": "..." },
   "records": [
     {
       "segment": {
         "id": "...",
         "document_id": "...",
         "content": "...",
         "document": { "id": "...", "name": "..." }
       },
       "score": 0.95
     }
   ]
 }
 

Example usage:


 DifyResponse response = difyClient.retrieve(...).block();
 List<Document> documents = DifyDocumentConverter.fromDifyResponse(response);
 
  • Method Details

    • fromDifyResponse

      public static List<io.agentscope.core.rag.model.Document> fromDifyResponse(DifyResponse response)
      Converts Dify API response to a list of Documents.

      This method extracts records from the Dify response and converts each record to an AgentScope Document object. If the response is null or empty, an empty list is returned.

      Parameters:
      response - the Dify API response
      Returns:
      a list of Document objects (never null)
    • fromDifyRecord

      public static io.agentscope.core.rag.model.Document fromDifyRecord(DifyResponse.Record record)
      Converts a single Dify record to a Document.

      This method extracts the segment content, score, and metadata from a Dify record and constructs an AgentScope Document object. If the record is invalid or missing required fields, null is returned.

      Parameters:
      record - the Dify record from API response
      Returns:
      a Document object, or null if conversion fails