How to use ClearRun with the Vercel AI SDK

Call generateText from the AI SDK, then pass the prompt and text to clearrun.evaluate() on the server. Return the answer and the verdict to your page.

Last updated

Steps

  1. Install

    npm install ai @clearrun/sdk
  2. Score in a server route

    import { generateText } from 'ai';
    import { createKernelClient } from '@clearrun/sdk';
    const clearrun = createKernelClient(); // reads CLEARRUN_API_KEY
    
    export async function POST(req) {
      const { prompt } = await req.json();
      const { text } = await generateText({ model: process.env.AI_MODEL, prompt });
      const { verdict } = await clearrun.evaluate({ prompt, responseText: text });
      return Response.json({ text, score: verdict.score, verdictId: verdict.verdictId });
    }

Streaming

Score after the stream finishes, on the full text.

Keep the key on the server

The ClearRun API does not accept calls from browser pages, on purpose, because the key must stay secret. See Does the ClearRun SDK run in the browser?