Skip to content

Add prompt_template parameter for per-sample prompt customization. #202

Closed
SokolovAnatoliy wants to merge 1 commit into
tidyverse:mainfrom
SokolovAnatoliy:string-iterpolation-for-solver
Closed

Add prompt_template parameter for per-sample prompt customization. #202
SokolovAnatoliy wants to merge 1 commit into
tidyverse:mainfrom
SokolovAnatoliy:string-iterpolation-for-solver

Conversation

@SokolovAnatoliy

Copy link
Copy Markdown
Contributor

Fixes #201.

Solution

Add a prompt_template argument to Task$new() that:

  1. Uses {{ column_name }} syntax (via ellmer::interpolate()) to reference metadata columns
  2. Automatically prepends the interpolated template to each sample's input
  3. Works with both generate() and generate_structured() solvers

Examples

With generate()

library(vitals)                                                                 
library(ellmer)                                                                 
library(tibble)                                                                 
                                                                               
shapes_data <- tibble(                                                          
 input = c(                                                                    
   "The shapes are square, circle and rhombus",                                
   "The shapes are square, circle and rhombus",                                
   "The shapes are square, circle and rhombus"                                 
 ),                                                                            
 target = c("square", "circle", "rhombus"),                                    
 shape_to_pick = c("square", "circle", "rhombus")                              
)                                                                               
                                                                               
tsk <- Task$new(                                                                
 dataset = shapes_data,                                                        
 solver = generate(chat_anthropic(model = "claude-sonnet-4-5")),               
 scorer = detect_match("any"),                                                 
 prompt_template = "Always pick {{ shape_to_pick }}. Just return the single chosen shape as 1 word.",                                                       
 dir = tempdir()                                                               
)                                                                               
                                                                               
tsk$eval()                                                                      

Desired result:

tsk$get_samples()$result
[1] "square"  "circle"  "rhombus"

With generate_structured()

type_shape <- type_object(                                                      
 shape = type_string("The name of the shape that was picked")                  
)                                                                               
                                                                               
shapes_data <- tibble(                                                          
 input = c(                                                                    
   "The shapes are square, circle and rhombus",                                
   "The shapes are square, circle and rhombus",                                
   "The shapes are square, circle and rhombus"                                 
 ),                                                                            
 target = c("square", "circle", "rhombus"),                                    
 shape_to_pick = c("square", "circle", "rhombus")                              
)                                                                               
                                                                               
tsk_str <- Task$new(                                                            
 dataset = shapes_data,                                                        
 solver = generate_structured(                                                 
   solver_chat = chat_anthropic(model = "claude-sonnet-4-5"),                  
   type = type_shape                                                           
 ),                                                                            
 scorer = detect_match("any"),                                                 
 prompt_template = "Always pick {{ shape_to_pick }}. Just return the single chosen shape as 1 word.",                                                       
 dir = tempdir()                                                               
)                                                                               
                                                                               
tsk_str$eval()                                                                  

Desired result:

tsk_str$get_samples()$result
[1] "[{\"shape\":\"square\"}]"  "[{\"shape\":\"circle\"}]"  "[{\"shape\":\"rhombus\"}]"

Implementation Notes

  • The template is interpolated at the Task level in $solve(), keeping solvers simple and unchanged
  • Uses ellmer::interpolate() which uses {{ }} syntax (avoids conflicts with JSON/R code)
  • The interpolated template is prepended to the input with \n\n separator
  • When prompt_template = NULL (default), behavior is unchanged

@simonpcouch

Copy link
Copy Markdown
Collaborator

Will close in favor of conversation on the related issue! Can reopen and revisit if need be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add prompt_template parameter for per-sample prompt customization

2 participants