Skip to content

Allow tool functions to return a ContentToolResult object #398

Description

@gadenbuie

In order to support custom rendering of tool results in shinychat's UI, I'd like to follow ellmer's pattern of using content_*() functions to render ContentToolResult in different contexts.

In order to provide a custom ContentToolResult renderer, tool functions need to be able to return objects that are stored in the chat turns as a custom class that inherits from ContentToolResult.

The solution I propose is that ellmer allow tool functions to return ContentToolResult objects directly. When provided in this way, ellmer would return the tool result, after updating the @id property. (id would need to be optional for users but would be filled in by the tool invocation function.)

In addition to making id an optional property when constructing the tool result, I'd also like to add a detail property that could be used by tool authors to store additional data or context that is stored alongside the tool result and that could be used by custom content rendering functions.

WeatherToolResult <- new_class(
  "WeatherToolResult",
  parent = ellmer::ContentToolResult
)

method(contents_html, WeatherToolResult) <- function(content, ...) {
  render_weather_card(content@detail$data)
}

tool(
  function(lat, lng) {
    data <- call_weather_api(lat, lng)
    
    current_conditions <- data$current_conditions

    WeatherToolResult(
      value = current_conditions,
      detail = list(data = data)
    )
  },
  .name = "get_current_weather_conditions",
  .description = "Get the current weather conditions for a location.",
  lat = type_number("Latitude"),
  lng = type_number("Longitude")
)

Activity

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

Metadata

Metadata

Assignees

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