| name | bx-ai-chatting |
| description | Use this skill when writing BoxLang AI chat code: aiChat(), aiChatAsync(), aiChatStream(), parameters (temperature, max_tokens, model), provider selection, API keys, return formats, multi-turn conversations, and error handling. |
bx-ai: Chatting with AI
Core BIF: aiChat()
aiChat( message, params={}, options={} )
message — string or array of message structs
params — model parameters (temperature, max_tokens, model, top_p, stop, etc.)
options — provider options (provider, apiKey, returnFormat, timeout)
- Returns: string by default; array or struct depending on
returnFormat
Simple Usage
answer = aiChat( "What is the capital of France?" )
code = aiChat(
"Write a Fibonacci function in BoxLang",
{ temperature: 0.2, max_tokens: 500 }
)
result = aiChat(
"Summarize this text: ...",
{ temperature: 0.5 },
{ provider: "claude" }
)
Parameters Reference
params = {
temperature : 0.7,
max_tokens : 1000,
model : "gpt-4o",
top_p : 1.0,
stop : ["\n\n"]
}
Temperature guide:
0.0–0.3 — Facts, code, data extraction
0.5–0.7 — General chat, balanced
0.8–1.0 — Creative writing, brainstorming
Return Formats
text = aiChat( "Hello" )
response = aiChat( "Hello", {}, { returnFormat: "full" } )
println( response.content )
println( response.usage.total )
choices = aiChat( "Tell a joke", { n: 3 }, { returnFormat: "choices" } )
Multi-Turn Conversations
messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is 2+2?" },
{ role: "assistant", content: "4" },
{ role: "user", content: "Multiply that by 10." }
]
result = aiChat( messages )
Async Chat
future = aiChatAsync( "Explain quantum entanglement" )
response = future.get()
response = future.get( 30, "seconds" )
Streaming Responses
aiChatStream(
"Write a short story about a robot",
{},
{},
( chunk ) -> {
print( chunk )
}
)
Provider Configuration
result = aiChat( "Hello", {}, { provider: "openai", apiKey: "sk-..." } )
result = aiChat( "Hello", {}, { provider: "claude", apiKey: "sk-ant-..." } )
result = aiChat( "Hello", {}, { provider: "gemini" } )
result = aiChat( "Hello", {}, { provider: "ollama" } )
Error Handling
try {
result = aiChat( "Hello", {}, { provider: "openai" } )
} catch ( bxModules.bxai.exceptions.AIProviderException e ) {
logError( "AI provider error: #e.message#" )
} catch ( bxModules.bxai.exceptions.AITimeoutException e ) {
return getDefaultResponse()
}
Common Pitfalls
- ❌ Do NOT pass
model: as a top-level BIF argument — put it in params
- ❌ Do NOT use both
temperature and top_p simultaneously
- ❌ Do NOT forget to handle provider exceptions in production code
- ✅ Set
returnFormat: "full" when you need token counts or finish reasons
- ✅ Use
aiChatAsync() for long-running requests in web handlers