class Ollama::Handlers::Markdown

ollama.generate(model: ‘llama3.1’, prompt: ‘Hello World’, &Markdown)
@example Displaying a response as markdown
desired.
suitable for interactive terminal applications where styled text output is
It supports both continuous and single-message display modes, making it
formatted markdown style using ANSI escape codes for terminal rendering.
This class is designed to display streaming or non-streaming responses in a
A handler that processes responses by rendering them as ANSI-markdown output.

def call(response)

Returns:
  • (self) - returns the handler instance itself after processing the

Parameters:
  • response (Ollama::Response) -- the response object containing content
def call(response)
  if content = response.response || response.message&.content
    if @stream
      @content << content
      markdown_content = Kramdown::ANSI.parse(@content)
      @output.print clear_screen, move_home, markdown_content
    else
      markdown_content = Kramdown::ANSI.parse(content)
      @output.print markdown_content
    end
  end
  self
end

def initialize(output: $stdout, stream: true)

Parameters:
  • stream (TrueClass, FalseClass) -- whether to enable streaming mode, defaults to true
  • output (IO) -- the output stream to be used for handling responses, defaults to $stdout
def initialize(output: $stdout, stream: true)
  super(output:)
  @stream      = stream
  @output.sync = @stream
  @content     = ''
end