Create an R function endpoint to 'return' a result from a credit risk calculator

Options
alexandrelusto
alexandrelusto Registered Posts: 2

I am trying to create an R function endpoint to 'return' a result from a credit risk calculator. The issue is that this function runs perfectly in an R recipe (I'm using an R environment with specific packages). However, in the API endpoint, even though I select the R environment and load the libraries at the beginning of the code, they are not loaded. For example, jsonlite, tidyr, given that tidyverse would load everything, but it also doesn't work. Is there something specific that doesn't execute in the same way?

Tks

Note: I am using a 'Test queries' example:

{
"json_data_teste": {
"chave_unica": "60_2",
"prazo_maximo": "Baixo",
"prazo_minimo_nao_atingido": "Baixo",
"novo_cedente": "Baixo",
"sacado_novo_banco": "Medio",
"vencidos": "Alto",
"risco_a_vencer": "Alto",
"recompra_30d_sacado": "Baixo",
"recompra_30d_cedente": "Baixo",
"limite_excedido": "Medio",
"ilm_30d_cedente": "Baixo",
"concentracao_de_grupo_de_risco": "Baixo",
"concentracao_de_cep": "Alto",
"concentracao_cedente": "Alto",
"concentracao_banco": "Alto",
"cep_notorio": "Baixo",
"ticket_medio": "Invalida",
"limite_definido_para_o_sacado": "Medio"
}
}

library(dplyr)
library(jsonlite)

calc_probabilidade_ajustada_2 <- function(json_input, valor_maximo_possivel = 77) {

dados <- fromJSON(json_input, flatten = TRUE)

criticidade_pesos_customizados <- setNames(
c(0, 1, 2, 3),
c("Invalida", "Baixo", "Medio", "Alto")
)

pesos_customizado <- setNames(
c(2, 5, 2, 2, 5, 1, 2, 2, 3, 1, 1, 2, 0.5, 1, 2, 2, 1),
c("prazo_maximo", "prazo_minimo_nao_atingido", "novo_cedente",
"sacado_novo_banco", "vencidos", "risco_a_vencer", "recompra_30d_sacado",
"recompra_30d_cedente", "limite_excedido", "ilm_30d_cedente",
"concentracao_de_grupo_de_risco", "concentracao_de_cep", "concentracao_cedente",
"concentracao_banco", "cep_notorio", "ticket_medio", "limite_definido_para_o_sacado")
)

# Calcula os valores ponderados usando os pesos personalizados
df_ponderado <- dados %>%
mutate(across(-chave_unica, ~criticidade_pesos_customizados[.x] * pesos_customizado[cur_column()], .names = "ponderado_{.col}"))

# # Calcula a soma dos valores ponderados para cada linha
df_soma_ponderada <- df_ponderado %>%
rowwise() %>%
mutate(SomaPonderada = sum(c_across(starts_with("ponderado_")), na.rm = TRUE)) %>%
ungroup()

# # Calcula o valor máximo possível se não fornecido
if (is.null(valor_maximo_possivel)) {
criticidades_maximas <- sapply(dados[-1], function(coluna) max(criticidade_pesos_customizados[coluna], na.rm = TRUE))
valor_maximo_possivel <- sum(pesos_customizado * criticidades_maximas)
}

# # Calcula a probabilidade normalizando pela soma máxima ponderada possível
df_final <- df_soma_ponderada %>%
mutate(Probabilidade = SomaPonderada / valor_maximo_possivel) %>%
select(chave_unica, everything())

return(as.character(df_final$Probabilidade))


}

Answers

  • Sarina
    Sarina Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer Posts: 315 Dataiker
    Options

    Hi @alexandrelusto
    ,

    When you say it doesn't work, can you clarify what happens? Do you encounter an error? Can you please attach the full "Logs" snippet that you see when testing your endpoint?

    GQLeK9lLE27BslPSrfdSf9XuDhntwK5Tfw.png

    Thank you,
    Sarina 

  • alexandrelusto
    alexandrelusto Registered Posts: 2
    Options

    The following error follows as requested

  • Sarina
    Sarina Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer Posts: 315 Dataiker
    Options

    Hi @alexandrelusto
    ,

    I'm not clear about the error you are receiving based on the error log snippet. It seems like the service is able to start up successfully:

    [2024/06/13-11:12:09.651] [qtp445202766-37] [INFO] [dku.lambda.dispatcher] - [ct: 2655] Asking service ale_api_teste to SWITCH to new mapping {"auditMetadata":{},"strategy":"RANDOM","mode":"SINGLE_GENERATION","generation":"ale_api_teste4728559048320064004","entries":[{"generation":"ale_api_teste4728559048320064004","proba":1.0}],"openAPIGeneration":"ale_api_teste4728559048320064004"}[2024/06/13-11:12:09.651] [qtp445202766-37] [INFO] [dku.lambda.service.manager] - [ct: 2655] Starting switch to mapping: {"auditMetadata":{},"strategy":"RANDOM","mode":"SINGLE_GENERATION","generation":"ale_api_teste4728559048320064004","entries":[{"generation":"ale_api_teste4728559048320064004","proba":1.0}],"openAPIGeneration":"ale_api_teste4728559048320064004"}[2024/06/13-11:12:09.660] [qtp445202766-37] [INFO] [dku.lambda.service.manager] - [ct: 2664] Done switch to new mapping[2024/06/13-11:12:09.662] [KNL-r-single-command-kernel-out-48] [INFO] [dku.utils] - [1] "Ready for service"


    Can you clarify with a screenshot of the error what error you encounter?

    Thank you,
    Sarina

Setup Info
    Tags
      Help me…