ChatPreconfiguredModelBuilder provides a fluent interface for building and executing chat conversations with language models. It supports messages, tools, streaming, and various callbacks.
Configuration Methods
model
Set the LLM model to use for the chat.The model identifier string (e.g., ‘gpt-4o’, ‘claude-3-5-sonnet’) or LLM instance
Returns the builder instance for method chaining
system
Set the system prompt for the conversation.The system prompt to guide the model’s behavior
Returns the builder instance for method chaining
prompt
Set a simple user prompt (alternative to using messages).The user prompt text or Prompt instance
Returns the builder instance for method chaining
messages
Set the conversation messages.Array of Message instances (TextMessage, ToolCallMessage, ToolResultMessage)
Returns the builder instance for method chaining
addMessage
Add a single message to the conversation.The message to add
Returns the builder instance for method chaining
addMessages
Add multiple messages to the conversation.Array of messages to add
Returns the builder instance for method chaining
Tool Methods
tools
Register tools that the model can call.Array of tools (closures or InvokableTool instances) with string keys as tool names
Returns the builder instance for method chaining
toolChoice
Set the tool choice strategy.Tool choice mode: ToolChoice::Auto, ToolChoice::Required, or a specific tool name
Returns the builder instance for method chaining
forceTool
Force the model to use a tool.The tool name to force, or ToolChoice::Required to require any tool
Returns the builder instance for method chaining
interrupt
Set a callback to interrupt execution before tool calls.Closure that receives a ToolCall and returns bool. True interrupts execution.
Returns the builder instance for method chaining
onToolError
Set a callback for tool execution errors.Closure that receives the Throwable exception
Returns the builder instance for method chaining
Callback Methods
onMessage
Set a callback for completed messages.Closure that receives each completed Message
Returns the builder instance for method chaining
onMessageProgress
Set a callback for streaming message progress.Closure that receives partial Messages during streaming
Returns the builder instance for method chaining
onTokenStats
Set a callback for token usage statistics.Closure that receives TokenStats with usage information
Returns the builder instance for method chaining
Configuration Methods
attempts
Set the maximum number of retry attempts for tool errors.Maximum number of attempts (default: 3)
Returns the builder instance for method chaining
debugger
Attach a debugger for logging conversation details.FileDebugger instance or null to disable debugging
Returns the builder instance for method chaining
schema
Set a JSON schema for structured output validation.JSON schema array defining the expected output structure
Returns the builder instance for method chaining
Execution Methods
stream
Execute the chat with streaming responses.Collection of messages from the conversation, including responses and tool calls
send
Execute the chat without streaming (waits for complete response).Collection of messages from the conversation
build
Build a Prompt object from the current configuration.A Prompt object containing the system message, messages, tools, and configuration