Candil.RequestBuilder (Candil v2.0.0)

Copy Markdown View Source

Shared request body builders for LLM providers.

These functions normalise messages and build the JSON compatible request body maps for OpenAI-compatible, Anthropic, and Ollama APIs. Both the Candil.Inference (non-streaming) and Candil.Stream (SSE streaming) modules delegate here to avoid code duplication.

Options

  • :temperature — sampling temperature (default: 0.7)
  • :max_tokens — maximum tokens to generate (default: 512)
  • :stop — list of stop sequences (OpenAI only, default: [])
  • :system — system prompt string (prepended to messages)
  • :stream — enable SSE streaming (default: false)
  • :tools — list of tool definitions for function calling (default: [])
  • :tool_choice — controls tool invocation (OpenAI only)

Function calling

Pass a list of tool definitions in :tools. Each tool is a map with :name, :description, and :parameters (JSON Schema). The provider will respond with a tool_calls field in the response when it wants to invoke a tool.

tools = [
  %{
    name: "get_weather",
    description: "Get the current weather for a location",
    parameters: %{
      "type" => "object",
      "properties" => %{
        "location" => %{"type" => "string", "description" => "City name"}
      },
      "required" => ["location"]
    }
  }
]

Candil.chat(:gpt4, [%{role: "user", content: "Weather in Madrid?"}], tools: tools)

The response will include a tool_calls key with the tool name and parsed arguments, which your code can then act on.

All functions return a plain map ready for Jason.encode!/1.

Summary

Types

tool()

@type tool() :: %{name: String.t(), description: String.t(), parameters: map()}