chatGPT API call in R code returns an error

Hello

When I try to run the following R-code to call chatGPT:

library(httr)
library(stringr)

api_key ← “…”

response ← POST(
url = “https://api.openai.com/v1/chat/completions”,
add_headers(Authorization = paste(“Bearer”, api_key)),
content_type_json(),
encode = “json”,
body = list(
model = “gpt-3.5-turbo”,
messages = list(
list(
role = “user”,
content = “What is ChatGPT?”
)
)
)
)

content(response)

I get an error:
Error in UseMethod(“content”, x) :
no applicable method for ‘content’ applied to an object of class “response”

Any ideas what’s wrong?

Hi Jake,

Thank you very much for your find!
I have the ‘tm’ library initiated and this caused the issue. An explicit reference to the ‘content’ function in httr helped.

Sorry for the code formatting - it was a copy-paste issue. The code is formatted in my R-file and also wrapped in the function.

1 Like