Prompt Chain

Build Dynamic Prompts with Vercel AI SDK

A promptfoo example dynamically constructing prompts by persona, task type, and context via a custom Vercel AI SDK provider.

Works with vercel

91
Spark score
out of 100
Updated 29 days ago
Version code-scan-action-0.1
Models
gpt 4o

Add to Favorites

Why it matters

Leverage the Vercel AI SDK to dynamically construct prompts and integrate them with promptfoo for robust testing and reporting. This asset demonstrates how to build sophisticated AI applications by combining powerful SDKs.

Outcomes

What it gets done

01

Integrate Vercel AI SDK for dynamic prompt generation.

02

Utilize promptfoo's provider prompt reporting.

03

Demonstrate prompt construction for AI applications.

04

Test and refine AI prompt logic.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/pfoo-vercel-ai-sdk | bash

Overview

Vercel Ai Sdk

This promptfoo example uses a custom Vercel AI SDK provider to dynamically construct prompts from persona, task type, domain, audience, and RAG-style context variables. Use it when evaluating dynamic, structured prompt-construction logic rather than a fixed prompt template; requires implementing the construction logic in a custom provider.

What it does

This promptfoo example demonstrates dynamic prompt construction using a custom provider (file://./aiSdkProvider.mjs) built on the Vercel AI SDK. Instead of a fixed prompt template, the provider builds the actual prompt sent to the LLM (gpt-4o-mini) based on variables like persona (expert, coder, analyst), task_type (explain, compare, troubleshoot), domain, audience, and optional RAG-style context, and reports back the constructed prompt.

When to use - and when NOT to

Use this example when your application composes prompts dynamically from structured inputs (persona, task type, audience, retrieved context) rather than filling a single static template, and you want to evaluate that composition logic alongside the resulting LLM output. It requires a custom provider script to implement the prompt-construction logic; it is not a fixed-prompt eval and needs adaptation if your composition rules differ.

Inputs and outputs

The shared prompt is just '{{topic}}', with all the real construction logic implemented inside aiSdkProvider.mjs based on additional test variables:

providers:
  - id: file://./aiSdkProvider.mjs
    config:
      model: gpt-4o-mini
      temperature: 0.7

tests:
  - description: Explain with context (RAG simulation)
    vars:
      topic: transformer architecture
      persona: expert
      task_type: explain
      domain: machine learning
      audience: ML engineers
      context: |
        From "Attention Is All You Need" (2017):
        The Transformer uses self-attention to compute representations...
    assert:
      - type: icontains
        value: attention
      - type: llm-rubric
        value: references or builds upon the provided context

Other tests vary persona/task_type combinations - a coder persona explaining async/await, an analyst comparing SQL vs NoSQL with a structured pros/cons format, and an expert troubleshooting Node.js memory leaks - each checked with a mix of icontains keyword checks and llm-rubric quality checks.

Integrations

Uses the Vercel AI SDK inside a custom promptfoo provider script (aiSdkProvider.mjs) to call gpt-4o-mini, with promptfoo's icontains and llm-rubric assertion types for grading.

Who it's for

Developers building applications with dynamic, persona/task-aware prompt construction (e.g. multi-mode assistants or RAG pipelines) who want to evaluate both the constructed prompt logic and the resulting model output using the Vercel AI SDK.

Source README

yaml-language-server: $schema=https://promptfoo.dev/config-schema.json

Vercel AI SDK Example

Demonstrates dynamic prompt construction with the Vercel AI SDK.

The provider builds prompts based on persona, task type, and context,

then reports the actual prompt sent to the LLM.

Setup:

cd examples/vercel-ai-sdk

npm install

export OPENAI_API_KEY=sk-...

Run:

npx promptfoo@latest eval

description: Vercel AI SDK with dynamic prompt construction

providers:

  • id: file://./aiSdkProvider.mjs
    config:
    model: gpt-4o-mini
    temperature: 0.7

prompts:

  • '{{topic}}'

tests:

Expert persona with explain task

  • description: Quantum computing for students
    vars:
    topic: quantum entanglement
    persona: expert
    task_type: explain
    domain: quantum physics
    audience: college students
    assert:
    • type: llm-rubric
      value: explains quantum entanglement clearly with appropriate examples

Coder persona with explain task

  • description: Async/await for junior devs
    vars:
    topic: async/await patterns in JavaScript
    persona: coder
    task_type: explain
    domain: JavaScript
    audience: junior developers
    assert:
    • type: icontains
      value: async
    • type: icontains
      value: await

Analyst persona with comparison task

  • description: Compare SQL vs NoSQL
    vars:
    topic: SQL databases vs NoSQL databases
    persona: analyst
    task_type: compare
    domain: database systems
    format: structured comparison with pros/cons
    assert:
    • type: icontains
      value: SQL
    • type: llm-rubric
      value: provides balanced comparison of both database types

Expert with troubleshooting task

  • description: Debug memory leaks
    vars:
    topic: memory leaks in Node.js applications
    persona: coder
    task_type: troubleshoot
    domain: Node.js performance
    audience: senior engineers
    assert:
    • type: icontains
      value: memory
    • type: llm-rubric
      value: provides actionable debugging steps

With RAG-style context injection

  • description: Explain with context (RAG simulation)
    vars:
    topic: transformer architecture
    persona: expert
    task_type: explain
    domain: machine learning
    audience: ML engineers
    context: |
    From "Attention Is All You Need" (2017):
    The Transformer uses self-attention to compute representations
    of its input and output without using sequence-aligned RNNs or
    convolution.
    assert:
    • type: icontains
      value: attention
    • type: llm-rubric
      value: references or builds upon the provided context

FAQ

Common questions

Discussion

Questions & comments ยท 0

Sign In Sign in to leave a comment.