open-guardrail-fastify
v1.6.2
Published
Fastify plugin adapter for open-guardrail — guard HTTP request/response with pipelines
Downloads
3,064
Maintainers
Readme
open-guardrail-fastify
Fastify plugin adapter for open-guardrail.
Install
pnpm add open-guardrail-fastify open-guardrail-coreUsage
import Fastify from 'fastify';
import { pipe, promptInjection, pii } from 'open-guardrail';
import { createGuardrailPlugin, createOutputGuard } from 'open-guardrail-fastify';
const app = Fastify();
// Guard all incoming requests
app.register(createGuardrailPlugin({
input: pipe(promptInjection({ action: 'block' })),
fieldName: 'message', // default
// inputFrom: (request) => request.body.prompt,
// onBlocked: (result, request, reply) => reply.code(400).send({ err: 'nope' }),
}));
// Guard LLM output before responding
const guardOutput = createOutputGuard({
output: pipe(pii({ entities: ['email'], action: 'mask' })),
});
app.post('/chat', async (request, reply) => {
const llmResponse = await getLLMResponse(request.body.message);
const safe = await guardOutput(llmResponse);
reply.send({ reply: safe });
});Options
| Option | Type | Default | Description |
|---|---|---|---|
| input | Pipeline | — | Input guard pipeline |
| fieldName | string | 'message' | Body field to guard |
| inputFrom | (request) => string | — | Custom text extractor |
| onBlocked | (result, request, reply) => void | — | Custom block handler |
License
MIT
