Skip to main content
Polyglot provides a clean, fluent API for building inference requests. You can configure messages, models, tools, response formats, and provider-specific options — all through a consistent interface that works across every supported LLM provider.

Basic Request

The simplest way to get a response is to pass a string message directly:
Messages::fromString() wraps a plain string as a user message. You can also use Messages::fromArray() to pass an array of role/content pairs.

The with() Method

When you need to configure multiple request fields at once, use the combined with() method. It accepts all core request parameters in a single call:
All parameters on with() are optional — pass only what you need:

Focused Helper Methods

For better readability, use the dedicated fluent helpers instead of packing everything into a single with() call:
Each helper returns a new immutable instance, so you can safely branch from a shared base:
The full list of fluent helpers:
  • withMessages(...) — set the conversation messages
  • withModel(...) — override the model
  • withTools(...) — attach tool/function definitions
  • withToolChoice(...) — control tool selection
  • withResponseFormat(...) — specify the response format
  • withOptions(...) — set provider-specific options
  • withStreaming(...) — enable or disable streaming
  • withMaxTokens(...) — set the maximum token count
  • withCachedContext(...) — attach reusable cached context
  • withRetryPolicy(...) — configure retry behavior
  • withResponseCachePolicy(...) — configure response caching

Using the Messages Class

For complex conversations, use the Messages class to build message sequences with a more expressive API:
The asDeveloper() method maps to OpenAI’s developer role and is automatically normalized for providers that do not support it.

Message Formats

The withMessages() method requires a Messages object. You can create one from different input formats using the factory methods on Messages:
  • Messages::fromString($text) — wraps a plain string as a single user message
  • Messages::fromArray($array) — converts an array of role/content pairs
  • Messages fluent API — build messages with asSystem(), asUser(), asDeveloper(), asAssistant()

Multimodal Content

For providers that support vision, you can include images in your messages:

Using InferenceRequest Directly

If your application already constructs request objects — for example, when deserializing stored requests or building them in a pipeline — you can pass them in directly:
InferenceRequest objects are immutable value objects. Use with() or the dedicated mutators (withMessages(), withModel(), etc.) to derive modified copies. Note that InferenceRequest::with() accepts typed objects (Messages, ToolDefinitions, ToolChoice, ResponseFormat) rather than primitive arrays or strings: