Non-streaming chat completions
client.chat.complete() sends one request and waits for the whole response. temperature and maxTokens default to 0.7 and 1000.
Send a completion
import { ServiceProvider } from '@preternatural-js/client-sdk'
const result = await client.chat.complete({
messages: [
{
role: 'system',
content: 'Answer in one paragraph.'
},
{
role: 'user',
content: 'Why does Saturn have rings?'
}
],
scope: {
provider: ServiceProvider.openai,
modelIdentifier: 'gpt-4o-mini',
productId: 'your-product-id'
},
temperature: 0.7,
maxTokens: 500
})Read the response
The result is an OpenAI-compatible ChatCompletion. The text is in choices[0].message.content:
const answer = result.choices[0]?.message.content
if (answer) {
renderAnswer(answer)
}Handle customer access errors
When the customer is revoked, deleted, or suspended, complete() rejects with CustomerAccessInvalidatedError and the client clears the session:
import {
CustomerAccessInvalidatedError
} from '@preternatural-js/client-sdk'
try {
const result = await client.chat.complete(request)
renderAnswer(result.choices[0]?.message.content ?? '')
} catch (error) {
if (error instanceof CustomerAccessInvalidatedError) {
redirectToSignIn()
} else {
throw error
}
}