Prompt Chain

Orchestrate Multi-Turn Conversations

Promptfoo example demonstrating multi-turn conversation evaluation using the built-in _conversation variable in Nunjucks prompt templates.


73
Spark score
out of 100
Updated 15 days ago
Version 0.121.19
Models
gpt 3 5

Add to Favorites

Why it matters

Automate complex, multi-turn conversational interactions with AI. This asset manages sequential prompts and responses to achieve sophisticated dialogue flows.

Outcomes

What it gets done

01

Manage multi-turn chatbot interactions

02

Process and summarize sequential AI outputs

03

Extract key information from extended dialogues

04

Automate complex prompt chaining

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/pfoo-config-multi-turn | bash

Steps

Steps in the chain

01
Initialize the multi-turn conversation example
02
Set up environment variable
03
Review and configure files
04
Use conversation variables in template
05
Run evaluation
06
View results

Overview

Config Multi Turn

A promptfoo example demonstrating how to reference prior conversation turns - prompt, input, and output - inside a Nunjucks prompt template using the built-in _conversation variable, for multi-turn evaluation scenarios. Use it as a starting reference when building a promptfoo evaluation that needs to reference earlier turns of a conversation.

What it does

This is a promptfoo example demonstrating multi-turn conversation evaluation, showing how to use the built-in _conversation variable inside a Nunjucks prompt template to reference previous turns. The variable is typed as an array of completions:

type Completion = {
  prompt: string | object;
  input: string;
  output: string;
};

type Conversation = Completion[];

When looping through _conversation, completion.prompt gives access to the full prior prompt (e.g. completion.prompt[completion.prompt.length - 1].content is the last user message in a chat-formatted prompt), completion.input gives the last part of the prompt (equal to that same last-message content in chat format, or the raw last prompt element otherwise), and completion.output gives the assistant's response to that turn.

npx promptfoo@latest init --example config-multi-turn
cd config-multi-turn

When to use - and when NOT to

Use it as a reference for building promptfoo evaluations that need to reference conversation history - grading a response based on what was said in earlier turns, or constructing follow-up prompts that reference prior outputs.

It is a starter example, not a production eval - you're expected to edit prompt.json and promptfooconfig.yaml to fit your own multi-turn scenario before running it for real.

Inputs and outputs

Input is the OPENAI_API_KEY environment variable plus the example's prompt.json and promptfooconfig.yaml configuration, edited to define your multi-turn prompt logic. Output is produced by running promptfoo eval, with results viewable via promptfoo view.

Who it's for

Developers building promptfoo evaluations for multi-turn conversational prompts who need a working reference for accessing prior turns' prompts, inputs, and outputs inside a Nunjucks template.

Source README

config-multi-turn (Multiple Turn Conversation)

You can run this example with:

npx promptfoo@latest init --example config-multi-turn
cd config-multi-turn

Usage

To get started, set your OPENAI_API_KEY environment variable.

Next, have a look at prompt.json and edit promptfooconfig.yaml. The prompt uses a special built-in variable _conversation that has the following signature:

type Completion = {
  prompt: string | object;
  input: string;
  output: string;
};

type Conversation = Completion[];

When looping through _conversation, use completion.prompt in the Nunjucks prompt template to use the previous outputs. For example, completion.prompt[completion.prompt.length - 1].content is the last user message sent in a chat-formatted prompt.

completion.input is the last part of the prompt. In a chat-formatted conversation, it will be equal to completion.prompt[completion.prompt.length - 1].content. In other conversations, it will be equal to completion.prompt[completion.prompt.length - 1].

Use completion.output to get the assistant's response to that message.

Then run:

promptfoo eval

Afterwards, you can view the results by running promptfoo view

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.