OpenAI
Chat Completions using OpenAI LLM provider can be done in two ways:
- chat completion methods in
@preternatural-js/client-sdk - the official OpenAI SDK configured to use the Preternatural gateway
Non-streaming completion
Set streamMode to false for non-streaming chat completions.
const result = await client.streamChat({
scope: {
provider: ServiceProvider.openai,
modelIdentifier: 'gpt-4o-mini',
productId: 'your-product-id',
},
messages: [
{
role: 'user',
content: 'Explain quantum computing in one paragraph.',
},
],
maxTokens: 500,
temperature: 0.7,
streamMode: false,
})
if (typeof result === 'string') {
throw new Error('Expected a non-streaming chat completion')
}
const message = result.choices[0]?.message.contentWhen streamMode is false, streamChat() returns an OpenAI-compatible ChatCompletion object.
Streaming completion
Set streamMode to true and provide a callback to process generated text as it arrives.
const text = await client.streamChat(
{
scope: {
provider: ServiceProvider.openai,
modelIdentifier: 'gpt-4o-mini',
productId: 'your-product-id',
},
messages: [
{
role: 'user',
content: 'Write a short story about a lunar gardener.',
},
],
maxTokens: 500,
temperature: 0.7,
streamMode: true,
},
(chunk) => {
console.log(chunk)
},
)Using the OpenAI SDK
import OpenAI from 'openai'
import { ServiceProvider } from '@preternatural-js/client-sdk'
const gateway = await client.createProviderGatewayConfig({
provider: ServiceProvider.openai,
modelIdentifier: 'gpt-4o-mini',
productId: 'your-product-id',
})
const openai = new OpenAI({
apiKey: 'PRETERNATURAL_GATEWAY_AUTHENTICATES_REQUESTS',
baseURL: gateway.baseURL,
defaultHeaders: gateway.headers,
fetch: gateway.fetchImpl,
maxRetries: 0,
})You don’t need to pass the actual OpenAI API Key. Authentication is handled by the headers returned by the Client SDK.
Non-streaming completion
const completion = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [
{
role: 'user',
content: 'Explain quantum computing in one paragraph.',
},
],
max_tokens: 500,
temperature: 0.7,
})
const message = completion.choices[0]?.message.contentStreaming completion
const stream = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [
{
role: 'user',
content: 'Write a short story about a lunar gardener.',
},
],
max_tokens: 500,
temperature: 0.7,
stream: true,
})
for await (const event of stream) {
const text = event.choices[0]?.delta.content
if (text) {
console.log(text)
}
}