Learn R Programming

chatAI4R (version 0.3.6)

textFileInput4ai: Send Text File Content to OpenAI API and Retrieve Response

Description

This function reads the content of a specified text file, sends it to the OpenAI API using the provided API key, and retrieves the generated response from the GPT model.

Usage

textFileInput4ai(
  file_path,
  model = "gpt-4o-mini",
  system_prompt = "You are a helpful assistant to analyze your input.",
  max_tokens = 50,
  api_key = Sys.getenv("OPENAI_API_KEY")
)

Value

A character string containing the response from the OpenAI API.

Arguments

file_path

A string representing the path to the text or csv file to be read and sent to the API.

model

A string specifying the OpenAI model to be used (default is "gpt-4o-mini").

system_prompt

Optional. A system-level instruction that can be used to guide the model's behavior (default is "You are a helpful assistant to analyze your input.").

max_tokens

A numeric value specifying the maximum number of tokens to generate (default is 50).

api_key

A string containing the OpenAI API key. Defaults to the "OPENAI_API_KEY" environment variable.

Author

Satoshi Kume

Examples

Run this code
if (FALSE) {
  # Example usage of the function
  api_key <- "YOUR_OPENAI_API_KEY"
  file_path <- "path/to/your/text_file.txt"
  response <- textFileInput4ai(file_path, api_key = api_key, max_tokens = 50)
  cat(response)
}

Run the code above in your browser using DataLab