Skip to content

Using parallel_chat_structured with custom_solver #153

@Seviks

Description

@Seviks

We use parallel_chat_structured to enable the LLM to code certain text data fields. Ideally, we could evaluate the codings using the exact same configuration.

However, when attempting to integrate parallel_chat_structured into a custom solver, we encounter an error. Is there an alternative approach to achieve this, or is this functionality not currently supported?

I am currently using the latest development version (0.1.0.9000).

Error

✖ Solving [10.5s]                                                  
Error in `purrr::map_chr()`:
ℹ In index: 1.
ℹ With name: code.
Caused by error in `c$last_turn`:
! $ operator is invalid for atomic vectors
Hide Traceback
     ▆
  1. ├─are_claude$eval(...)
  2. │ └─self$solve(..., epochs = epochs)
  3. │   └─private$solver(self$get_samples()$input, ...)
  4. │     └─global fn(...)
  5. │       └─purrr::map_chr(res, function(c) c$last_turn()@text)
  6. │         └─purrr:::map_("character", .x, .f, ..., .progress = .progress)
  7. │           ├─purrr:::with_indexed_errors(...)
  8. │           │ └─base::withCallingHandlers(...)
  9. │           ├─purrr:::call_with_cleanup(...)
 10. │           └─.f(.x[[i]], ...)
 11. └─base::.handleSimpleError(...)
 12.   └─purrr (local) h(simpleError(msg, call))
 13.     └─cli::cli_abort(...)
 14.       └─rlang::abort(...)

Reprex (gives error above, works when replacing parallel_chat_structured with parallel_chat)

library(vitals)
library(ellmer)
library(tidyverse)

openai_api_key <- Sys.getenv("key_openai")
anthropic_api_key <- Sys.getenv("key_anthropic")

type_codes <- type_object(
  code = type_string(required = FALSE)
)

# Not working with structured chat for some reason
custom_solver <- function(inputs, ..., solver_chat) {
  ch <- solver_chat$clone()
  res <- ellmer::parallel_chat_structured(ch, as.list(inputs), type_codes)
  list(
    result = purrr::map_chr(res, function(c) c$last_turn()@text), 
    solver_chat = res
  )
}

are_task <-
  Task$new(
  dataset = are[1:2,],
  solver = custom_solver,
  scorer = model_graded_qa(
    scorer_chat = chat_anthropic(api_key=anthropic_api_key, model = "claude-3-7-sonnet-latest"), 
    partial_credit = TRUE
  ),
  name = "An R Eval",
  epochs = 1
)

are_claude <- are_task$clone()
are_claude$eval(solver_chat = chat_anthropic(api_key=anthropic_api_key, model = "claude-sonnet-4-20250514"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions