chatGPT in R

x handZone psicostat

Example of use of chatGPT using OPENAI API via R

## https://www.business-science.io/code-tools/2024/05/11/chattr-chatgpt-in-r.html

## install.packages("openai)
library(openai)

## set your api key
# Sys.setenv( OPENAI_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" )

## create a chatbot with prompts
answer = create_chat_completion(
  model = "gpt-3.5-turbo",
  temperature = 0,
  messages = list(
    list(
      "role" = "system",
      "content" = "You are a professional translator."
    ),
    list(
      "role" = "user",
      "content" = "Please translate this text in to English: 
              La ricerca sulla dislessia sta mettendo sempre più in luce che il disturbo può essere prodotto
      dalla concomitanza di una serie di fattori e che un assessment completo può includere, oltre alle prove
      classiche di apprendimento e di intelligenza, anche prove che esaminano il ruolo di questi fattori e, in
      primo luogo, quello della Memoria di Lavoro Fonologica. Il presente lavoro presenta una nuova prova (span
      di MLFA) per esaminare la Memoria di Lavoro Fonologica, che include la considerazione dei meccanismi
      associativi che sembrano particolarmente importanti nell’apprendimento della lettura e offre dati
      normativi per le fasce scolastiche dalla quarta primaria alla seconda secondaria di I grado. L’agilità
      della prova e la sua capacità di cogliere aspetti fondamentali della lettura e della dislessia
      suggeriscono di includerla nell’assessment di routine."
    )
  )
)

## print result
print( answer )
$id
[1] "chatcmpl-9S3B2SrxEYi1DL3rlHuPV3smOGLcI"

$object
[1] "chat.completion"

$created
[1] 1716472424

$model
[1] "gpt-3.5-turbo-0125"

$choices
  index logprobs finish_reason message.role
1     0       NA          stop    assistant
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         message.content
1 Research on dyslexia is increasingly highlighting that the disorder can be caused by the combination of a series of factors and that a comprehensive assessment may include, in addition to the classic tests of learning and intelligence, also tests that examine the role of these factors and, first and foremost, that of Phonological Working Memory. This paper presents a new test (span of MLFA) to examine Phonological Working Memory, which includes the consideration of associative mechanisms that appear to be particularly important in reading acquisition and provides normative data for school age groups from fourth grade to second grade of lower secondary school. The agility of the test and its ability to capture fundamental aspects of reading and dyslexia suggest including it in routine assessment.

$usage
$usage$prompt_tokens
[1] 263

$usage$completion_tokens
[1] 143

$usage$total_tokens
[1] 406


$system_fingerprint
NULL

Print only the output text

print( answer$choices$message.content )
[1] "Research on dyslexia is increasingly highlighting that the disorder can be caused by the combination of a series of factors and that a comprehensive assessment may include, in addition to the classic tests of learning and intelligence, also tests that examine the role of these factors and, first and foremost, that of Phonological Working Memory. This paper presents a new test (span of MLFA) to examine Phonological Working Memory, which includes the consideration of associative mechanisms that appear to be particularly important in reading acquisition and provides normative data for school age groups from fourth grade to second grade of lower secondary school. The agility of the test and its ability to capture fundamental aspects of reading and dyslexia suggest including it in routine assessment."