Using OpenAI API with R
Introduction
This tutorial demonstrates how to integrate and use the OpenAI API with R to leverage advanced AI capabilities in your scripts.
1. Setting Up Your OpenAI API Key
Before starting, make sure you have your OpenAI API key ready. You can obtain it from the OpenAI website after signing up for an account.
2. Installing Necessary Packages
To use the OpenAI API in your R scripts, you will need to have the `httr` and `jsonlite` packages installed. You can install them using the following commands:
install.packages("httr") install.packages("jsonlite")
3. Making API Requests
To make requests to the OpenAI API using R, you'll use the `httr` package to send HTTP requests and the `jsonlite` package to handle JSON data.
Text Completion
Here’s an example of making a request for text completion:
# R Script for Text Completion using OpenAI API library(httr) library(jsonlite) api_key <- "YOUR_API_KEY_HERE" prompt <- "Translate English to French: Hello, how are you?" model <- "text-davinci-003" max_tokens <- 50 response <- POST( url = "https://api.openai.com/v1/completions", add_headers(Authorization = paste("Bearer", api_key)), body = toJSON(list(model = model, prompt = prompt, max_tokens = max_tokens)), encode = "json" ) content <- content(response, "text", encoding = "UTF-8") result <- fromJSON(content)$choices[[1]]$text cat("Output:", result)
Output: Bonjour, comment ça va?
This R script sends a POST request to the OpenAI API for text completion and prints the API response.
Code Generation
Here’s an example of making a request for code generation:
# R Script for Code Generation using OpenAI API library(httr) library(jsonlite) api_key <- "YOUR_API_KEY_HERE" prompt <- "Generate Python code to sort an array using bubble sort" model <- "davinci-codex" max_tokens <- 150 response <- POST( url = paste0("https://api.openai.com/v1/engines/", model, "/completions"), add_headers(Authorization = paste("Bearer", api_key)), body = toJSON(list(prompt = prompt, max_tokens = max_tokens, stop = "\n")), encode = "json" ) content <- content(response, "text", encoding = "UTF-8") result <- fromJSON(content)$choices[[1]]$text cat("Output:\n", result)
Output:
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr
This R script sends a POST request to the OpenAI Codex engine for code generation and prints the generated code.
4. Handling Responses
Once you receive a response from the API, you can handle it in your R script.
Text Completion
Here’s how you might handle the completion response:
# R Script to Handle Text Completion Response response <- POST( url = "https://api.openai.com/v1/completions", add_headers(Authorization = paste("Bearer", api_key)), body = toJSON(list(model = model, prompt = prompt, max_tokens = max_tokens)), encode = "json" ) content <- content(response, "text", encoding = "UTF-8") result <- fromJSON(content)$choices[[1]]$text cat("Translated Text:", result)
In this example, `content` contains the JSON response from the OpenAI API, and `cat` is used to print the translated text.
Code Generation
Here’s how you might handle the code generation response:
# R Script to Handle Code Generation Response response <- POST( url = paste0("https://api.openai.com/v1/engines/", model, "/completions"), add_headers(Authorization = paste("Bearer", api_key)), body = toJSON(list(prompt = prompt, max_tokens = max_tokens, stop = "\n")), encode = "json" ) content <- content(response, "text", encoding = "UTF-8") result <- fromJSON(content)$choices[[1]]$text cat("Generated Code:\n", result)
In this example, `content` contains the JSON response from the OpenAI API with the generated code, and `cat` is used to print the code.
Conclusion
Integrating the OpenAI API with R allows you to enhance your scripts with powerful AI capabilities. Explore more API endpoints and functionalities to innovate further.