Module: Ask::LLM::ProviderConfig

Overview

Shared contract for provider request/response transformation.

Every provider includes this module and implements the core methods that define its wire format. The provider's #chat method orchestrates between building requests, making HTTP calls, and parsing responses.

This separation makes each wire-format concern testable in isolation and adding a new provider mechanical — you implement four methods and the provider works.

Instance Method Summary collapse

Instance Method Details

#build_request(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params) ⇒ Hash

Build a provider-native request payload from internal message format.

Parameters:

  • messages (Array<Hash>)

    normalized messages with :role, :content, :tool_calls, :tool_call_id

  • model (String)

    model ID

  • tools (Array<Hash>, nil) (defaults to: nil)

    tool definitions

  • temperature (Float, nil) (defaults to: nil)

    sampling temperature

  • stream (Boolean) (defaults to: nil)

    whether streaming will be used

  • schema (Hash, nil) (defaults to: nil)

    JSON schema for structured output

Returns:

  • (Hash)

    provider-native request body

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/ask/llm/provider_config.rb', line 25

def build_request(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params)
  raise NotImplementedError, "#{self.class} must implement #build_request"
end

#format_message(msg) ⇒ Hash

Format a single message for this provider's wire format.

Parameters:

  • msg (Hash)

    message with :role, :content, :tool_calls, :tool_call_id

Returns:

  • (Hash)

    provider-native message format



60
61
62
# File 'lib/ask/llm/provider_config.rb', line 60

def format_message(msg)
  msg
end

#format_tools(tools) ⇒ Array

Format tool definitions for this provider's wire format.

Parameters:

  • tools (Array)

    tool definitions (Ask::Tool instances or Hashes)

Returns:

  • (Array)

    provider-native tool format



52
53
54
# File 'lib/ask/llm/provider_config.rb', line 52

def format_tools(tools)
  tools
end

#parse_response(body, model) ⇒ Ask::Message

Parse a non-streaming response into an Ask::Message.

Parameters:

  • body (Hash)

    parsed response body

  • model (String)

    model ID

Returns:

  • (Ask::Message)

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/ask/llm/provider_config.rb', line 34

def parse_response(body, model)
  raise NotImplementedError, "#{self.class} must implement #parse_response"
end

#parse_stream(raw, stream, model) {|Ask::Chunk| ... } ⇒ Object

Parse raw stream data and yield Ask::Chunks.

Parameters:

  • raw (String)

    raw data from the stream callback

  • stream (Ask::Stream)

    the accumulating stream

  • model (String)

    model ID

Yields:

  • (Ask::Chunk)

    optional per-chunk callback

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/ask/llm/provider_config.rb', line 44

def parse_stream(raw, stream, model, &block)
  raise NotImplementedError, "#{self.class} must implement #parse_stream"
end