Repository navigation
Expand file tree
/
Copy pathpalm.connect.R
More file actions
76 lines (72 loc) · 2.76 KB
/
Copy pathpalm.connect.R
File metadata and controls
76 lines (72 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#' Set up and connect to the Google PaLM 2 text model.
#'
#' This function establishes a connection to the Google PaLM text model by specifying the API key, and model version.
#'
#' @param api A character string representing the API key for accessing the Google PaLM 2 text model.
#' The API key should be 39 characters long and must be of the "character" class.
#' @param version A character string representing the model version to use, either \code{"v1beta2"} or \code{"v1beta3"}.
#' @param proxy A boolean value indicating whether to use a proxy for accessing the API
#' URL (default is \code{FALSE}). If your local internet cannot access the APIs, set this
#' parameter to \code{TRUE}.
#'
#' @return If successful, the function returns a character vector containing the API key, model version, and proxy status.
#' If the API response indicates an error, the function stops execution and provides an error message.
#'
#' @details
#' This function performs the necessary setup to connect to the Google PaLM 2 text model. It validates the provided API key and
#' checks the correctness of the model version. If the input is valid, it constructs the API request and sends
#' it to the PaLM 2 API endpoint.
#'
#' If an error occurs during the API request, such as an invalid API key or input parameters, an error message is displayed.
#' If the API request is successful, the function prints the model details to the console and returns a character vector
#' with the API key, model version, and model type.
#'
#' @examples
#' \dontrun{
#' # Connect to the model, replace API_KEY with your api key
#' palm.model = palm.connect("v1beta2",
#' "API_KEY",
#' FALSE)
#' palm.model
#' }
#'
#' @seealso
#' \href{https://palmr.ly.gd.edu.kg/documentation/}{PaLMr - Documentation}
#'
#' @export
#'
#' @importFrom jsonlite toJSON
#' @importFrom httr GET POST add_headers content
palm.connect = function(version,
api,
proxy = FALSE) {
version = match.arg(version, c("v1beta2", "v1beta3"), several.ok = FALSE)
apiURL = ifelse(
proxy,
paste0(
"https://api.genai.gd.edu.kg/google/",
version,
"/models/text-bison-001",
"?key=",
api
),
paste0(
"https://generativelanguage.googleapis.com/",
version,
"/models/text-bison-001",
"?key=",
api
)
)
response = httr::GET(url = apiURL,
httr::add_headers("Content-Type" = "application/json"))
responseJSON = httr::content(response, "parsed")
if (!is.null(responseJSON$error)) {
stop(responseJSON$error$message)
}
return(c(
"version" = version,
"api" = api,
"proxy" = proxy
))
}