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:
with() are optional — pass only what you need:
| Parameter | Type | Description |
|---|---|---|
messages | ?Messages | The messages to send to the LLM |
model | ?string | Model identifier (overrides preset default) |
tools | ?ToolDefinitions | Tool/function definitions for the model |
toolChoice | ?ToolChoice | Tool selection preference |
responseFormat | ?ResponseFormat | Response format specification |
options | ?array | Provider-specific request options |
Focused Helper Methods
For better readability, use the dedicated fluent helpers instead of packing everything into a singlewith() call:
withMessages(...)— set the conversation messageswithModel(...)— override the modelwithTools(...)— attach tool/function definitionswithToolChoice(...)— control tool selectionwithResponseFormat(...)— specify the response formatwithOptions(...)— set provider-specific optionswithStreaming(...)— enable or disable streamingwithMaxTokens(...)— set the maximum token countwithCachedContext(...)— attach reusable cached contextwithRetryPolicy(...)— configure retry behaviorwithResponseCachePolicy(...)— 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
ThewithMessages() 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 messageMessages::fromArray($array)— converts an array of role/content pairsMessagesfluent API — build messages withasSystem(),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: