Skip to content

Function: init()

Prefactor TypeScript SDK


Prefactor TypeScript SDK / @prefactor/packages/ai / init

init(config?, middlewareConfig?): LanguageModelV3Middleware

Defined in: packages/ai/src/init.ts:144

Initialize the Prefactor AI middleware and return it for use with wrapLanguageModel.

This is the main entry point for the SDK. Call this function to create a middleware instance that you can pass to the Vercel AI SDK’s wrapLanguageModel function.

Partial<{ }>

Optional configuration object for transport settings

MiddlewareConfig

Optional middleware-specific configuration

LanguageModelV3Middleware

Middleware object to use with wrapLanguageModel

import { init, shutdown } from '@prefactor/ai';
import { generateText, wrapLanguageModel } from 'ai';
import { anthropic } from '@ai-sdk/anthropic';
// Initialize with HTTP transport config
const middleware = init({
transportType: 'http',
httpConfig: {
apiUrl: 'https://app.prefactorai.com',
apiToken: process.env.PREFACTOR_API_TOKEN!,
agentIdentifier: '1.0.0',
},
});
// Wrap your model with the middleware
const model = wrapLanguageModel({
model: anthropic('claude-3-haiku-20240307'),
middleware,
});
const result = await generateText({
model,
prompt: 'Hello!',
});
await shutdown();
const middleware = init({
transportType: 'http',
httpConfig: {
apiUrl: 'https://app.prefactorai.com',
apiToken: process.env.PREFACTOR_API_TOKEN!,
agentId: process.env.PREFACTOR_AGENT_ID,
agentIdentifier: '1.0.0',
},
});
const middleware = init(
{
transportType: 'http',
httpConfig: {
apiUrl: 'https://app.prefactorai.com',
apiToken: process.env.PREFACTOR_API_TOKEN!,
agentIdentifier: '1.0.0',
},
},
{ captureContent: false } // Don't capture prompts/responses
);