Non-streaming chat completions
Set streamMode to false for non-streaming chat response.
Send a completion
import { ServiceProvider } from '@preternatural-js/client-sdk'
const result = await client.streamChat({
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,
streamMode: false
})Read the response
The current return type is string | ChatCompletion. Narrow it before reading the completion:
if (typeof result === 'string') {
throw new Error('Expected a chat completion')
}
const answer = result.choices[0]?.message.content
if (answer) {
renderAnswer(answer)
}Handle customer access errors
import {
CustomerAccessInvalidatedError
} from '@preternatural-js/client-sdk'
try {
const result = await client.streamChat(request)
if (typeof result === 'string') {
throw new Error('Expected a chat completion')
}
return result
} catch (error) {
if (error instanceof CustomerAccessInvalidatedError) {
redirectToSignIn()
}
throw error
}