Tool definitions don't accept valid special characters for function names or arguments, such as "ñ"
R functions that contain characters such as "ñ" (very common Spanish letter that produces a common problem in data science, as its used in the word _año_, meaning _year_) are completely valid in R, but they cause problems with `{ellmer}`
Lets create a basic function that has `ñ` in its name and in its argument:
```r
obtener_datos_por_año <- function(año = NULL) {
require(dplyr)
datos <- data.frame(
año = c(2020, 2021, 2022, 2023, 2024),
inversión = c(100, 150, 200, 250, 300),
proyectos = c(5, 8, 12, 15, 18)
)
datos <- datos |>
filter(año == .env$año)
return(datos)
}
```
The function simply filters data from a dataframe.
Try out the function and it will work fine:
```r
obtener_datos_por_año(año = 2022)
```
Now let's create a tool definition with `{ellmer}`:
```r
library(ellmer)
# Crear la herramienta ellmer
tool_obtener_datos <- ellmer::tool(
obtener_datos_por_año,
"Obtiene datos de inversión y proyectos, filtrados opcionalmente por año (2020-2024)",
arguments = list(
año = type_integer(description = "Año a filtrar, entre 2020 y 2024")
)
)
```
We get the first error: `ñ` is not supported in a function name:
```
# Error in `ellmer::tool()`:
# ! `name` must contain only letters, numbers, - and _.
```
We have to change the function name (let´s say `anio` instead of `año`) to get rid of the error.
Now let's provide the LLM with the function definition:
```r
chat <- chat_anthropic(
model = "claude-haiku-4-5"
)
chat$register_tool(tool_obtener_datos)
```
No problem so far, but let's ask a question that requires the model to use the tool, specifically the argument with `ñ` in its name:
```
chat$chat("muéstrame los proyectos de 2020") # "show me projects from 2020"
```
We get another `ñ` related error:
```
# Error in `req_perform_connection()`:
# ! HTTP 400 Bad Request.
# ℹ tools.0.custom.input_schema.properties: Property keys should match
# pattern '^[a-zA-Z0-9_.-]{1,64}$' [invalid_request_error]
# Run `rlang::last_trace()` to see where the error occurred.
```
I believe some cautions were taken to validate proper function and argument names in `{ellmer}` but those leave some users having to modify their work in order to match the english alphabet.
0 条评论