File Service steps process files by extracting contents, images, and pages. The output is a file object that can be used in subsequent steps.

Output Structure

File Service steps return the following object:

interface FileSchema {
  id: string; // Unique identifier for the file
  file_id: string; // File identifier used in the storage system
  file_filename: string; // Name of the file (e.g. document.pdf)
  file_size: number; // Size of the file in bytes (e.g. 1024)
  file_mimetype: string; // MIME type of the file (e.g. application/pdf)
  file_url: string; // URL to access the file
  file_format?: 'DOCUMENT' | null; // Format of the file (e.g. DOCUMENT)
  file_status?: 'PROCESSING' | 'PROCESSED' | 'FAILED'; // Current status of the file (e.g. PROCESSED)
  file_status_message?: string; // Status message providing additional information about the file status
  document?: Document;
  document_extract_contents?: boolean; // Flag indicating whether to extract contents from the document
  document_extract_images?: boolean; // Flag indicating whether to extract images from the document
  document_extract_pages?: boolean; // Flag indicating whether to extract pages from the document
  document_pages?: number[]; // Array of page numbers to extract from the document (e.g. [1,2,3])
}

interface Document {
  filename: string; // Name of the document file
  filetype: string; // Type of the document file
  contents: DocumentContent[]; // Array of document contents (only available if extract_contents is true)
  images: DocumentImage[]; // Array of document images (only available if extract_images is true)
  extracted_pages?: string; // Extracted pages from the document (only available if extract_pages is true)
  time?: number; // Time taken to process the document
}

interface DocumentContent {
  id: number; // Unique identifier for the content
  type: string; // Type of the content element
  content: string; // Content text
  content_as_html?: string; // Content text in HTML format
  page: number; // Page number where the content is located in the document
  parent?: number; // Parent content identifier
}

interface DocumentImage {
  id: string; // Unique identifier for the image
  file_id: string; // File identifier associated with the image
  image_url: string; // URL of the image
  type: string; // Type of the image
  page: number; // Page number where the image is located in the document
  height: number; // Height of the image
  width: number; // Width of the image
}

Accessing File Service Output

Let’s see how to access the output of a File Service step.

In code steps:

CODE_STEP
// 1. Basic file information
const fileInfo = {
  name: FILE_STEP.output.file_filename,
  size: FILE_STEP.output.file_size,
  type: FILE_STEP.output.file_mimetype,
  url: FILE_STEP.output.file_url,
};

In non-code steps like model messages, HTTP request URLs, request bodies, and other configuration fields:

{{FILE_STEP.output.file_filename}}
{{FILE_STEP.output.file_size}}
{{FILE_STEP.output.file_mimetype}}
{{FILE_STEP.output.file_url}}

Accessing File Contents and Images

To access file contents or images, you must enable Enable Contents and Enable Images in the file service step configuration.

Without these properties enabled, when you try to access contents or images, they will be empty.

In code steps:

CODE_STEP
// Accessing contents
const contents = util.getFileContents(FILE_STEP.output);
contents.forEach((content) => {
  console.log(`Page ${content.page}: ${content.content}`);
});

// Accessing images
const images = util.getFileImages(FILE_STEP.output);
images.forEach((imageUrl) => {
  console.log(`Image URL: ${imageUrl}`);
});

In model messages:

[
  {
    "role": "system",
    "content": "You are a document analyzer."
  },
  {
    "role": "user",
    "content": "Analyze this document: {{FILE_STEP.output:contents}}"
  }
]
[
  {
    "role": "system",
    "content": "You are a document analyzer."
  },
  {
    "role": "user",
    "content": "What do you see in these images?: {{FILE_STEP.output:images}}"
  }
]