aisetup: Configure AI services for R4VN

View source: R/ai.R

aisetupR Documentation

Configure AI services for R4VN

Description

Adds, updates, lists, selects, or removes named AI configurations. Only the configuration is saved by R4VN. A token supplied through tokenenv remains in an environment variable. A token supplied directly is stored in the system keyring when package keyring is available; otherwise it is kept only for the current R session.

Usage

aisetup(
  name = NULL,
  provider = "openai",
  model = NULL,
  url = NULL,
  token = NULL,
  tokenenv = NULL,
  api = NULL,
  language = "vi",
  prompt = NULL,
  default = FALSE,
  use = NULL,
  list = FALSE,
  clear = FALSE,
  save = TRUE,
  timeout = 120,
  temperature = NULL,
  maxtokens = 1200,
  inputtokens = 3000,
  overwrite = TRUE
)

Arguments

name

Configuration name, for example "gpt" or "local".

provider

"openai" or "compatible". The latter is for services implementing an OpenAI-compatible endpoint.

model

Model identifier required by the selected service.

url

Full API endpoint or API base ending in ⁠/v1⁠. When omitted for provider = "openai", the official OpenAI endpoint is used.

token

Optional API token. It is never written into the R4VN configuration file.

tokenenv

Optional environment-variable name containing the token.

api

"responses" or "chat". It is inferred from provider and url when omitted.

language

Default language for AI comments. Common values are "vi" and "en".

prompt

Optional permanent instruction added to every request using this configuration.

default

Logical; make this configuration the default.

use

Name of an existing configuration to make default.

list

Logical; list current configurations.

clear

FALSE, TRUE, or a configuration name. TRUE removes all configurations.

save

Logical; save the configuration for future R sessions.

timeout

Request timeout in seconds.

temperature

Optional model temperature. Leave NULL for provider defaults and maximum compatibility.

maxtokens

Maximum generated tokens.

inputtokens

Approximate target maximum tokens for statistical results sent to the AI service. aiask() automatically reduces this budget and retries when the provider reports that a request is too large.

overwrite

Logical; permit replacing an existing configuration.

Value

Invisibly returns the saved configuration, the configuration table, or TRUE after a management action.

Examples

## Not run: 
# Recommended: keep the token in .Renviron
# OPENAI_API_KEY=your-token
aisetup(
  name = "gpt",
  provider = "openai",
  model = "gpt-5.6",
  tokenenv = "OPENAI_API_KEY",
  default = TRUE
)

# Direct token: saved securely when keyring is installed
aisetup(
  name = "gpt",
  provider = "openai",
  model = "gpt-5.6",
  token = "your-token",
  default = TRUE
)

# OpenAI-compatible local or third-party service
aisetup(
  name = "local",
  provider = "compatible",
  model = "local-model",
  url = "http://localhost:11434/v1",
  default = TRUE
)

aisetup(list = TRUE)
aisetup(use = "gpt")
aisetup(clear = "local")

## End(Not run)


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.