Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lygitdata authored Jan 22, 2024
1 parent 4315fa6 commit d4b1284
Show file tree
Hide file tree
Showing 15 changed files with 1,934 additions and 48 deletions.
45 changes: 24 additions & 21 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
Package: PaLMr
Type: Package
Title: Interface for 'Google Pathways Language Model (PaLM)'
Version: 0.1.4
Authors@R: c(
person(
given = "Li", family = "Yuan", role = c("aut", "cre"),
email = "lyuan@gd.edu.kg"
)
)
Maintainer: Li Yuan <lyuan@gd.edu.kg>
Description: 'Google's 'PaLM' <https://developers.generativeai.google/> as a coding and writing assistant designed for 'R' and 'RStudio.' With a range of functions, including natural language processing and coding optimization, to assist R developers in simplifying tedious coding tasks and content searching.
License: CC BY 4.0
URL: https://palmr.r.ly.gd.edu.kg/
BugReports: https://github.com/lygitdata/PaLMr/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
Imports:
httr,
jsonlite
Package: PaLMr
Type: Package
Title: Interface for 'Google Pathways Language Model 2 (PaLM 2)'
Version: 0.2.0
Authors@R: c(
person(
given = "Li",
family = "Yuan",
role = c("aut", "cre"),
email = "lyuan@gd.edu.kg",
comment = c(ORCID = "0009-0008-1075-9922")
)
)
Maintainer: Li Yuan <lyuan@gd.edu.kg>
Description: 'Google's 'PaLM 2' <https://developers.generativeai.google/> as a coding and writing assistant designed for 'R' and 'RStudio.' With a range of functions, including natural language processing and coding optimization, to assist R developers in simplifying tedious coding tasks and content searching.
License: CC BY 4.0
URL: https://palmr.ly.gd.edu.kg/
BugReports: https://github.com/lygitdata/PaLMr/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.0
Imports:
httr,
jsonlite
22 changes: 13 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export(setupPALM)
export(getTextPALM)
export(getReferencePALM)
export(fixGrammarPALM)
export(explainCodePALM)
export(optimizeCodePALM)

import(httr)
import(jsonlite)
# Generated by roxygen2: do not edit by hand

export(palm.connect)
export(palm.txt)
export(palm.txt.explain.code)
export(palm.txt.fix.grammar)
export(palm.txt.get.reference)
export(palm.txt.optimize.code)
importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(jsonlite,toJSON)
19 changes: 1 addition & 18 deletions R/PaLMr_utils.R
Original file line number Diff line number Diff line change
@@ -1,57 +1,41 @@
#' @noRd
checkModelSelection = function(modelVersion, modelType) {
# Check whether parameters are vaild apiKey vaildilty will be checked by PaLM API
modelVersion = match.arg(modelVersion, c("v1beta2", "v1beta3"), several.ok = FALSE)
modelType = match.arg(modelType, "text-bison-001", several.ok = FALSE)
}

#' @noRd
generateOutput = function(responseJSON) {
# Check if a valid output is present in the response
if (!is.null(responseJSON$candidates[[1]]$output)) {
# Output the generated text
return(as.character(responseJSON$candidates[[1]]$output))
}
# Check if there's an error in the response
else if (!is.null(responseJSON$error)) {
# Display a warning with the error message
warning(responseJSON$error$message)
}
# Check if there's safety feedback in the response
else if (!is.null(responseJSON$safetyFeedback)) {
# Filter safety feedback with "MEDIUM" or "HIGH" probability
riskyFeedback = responseJSON$safetyFeedback[sapply(responseJSON$safetyFeedback, function(f) {
f$rating$probability %in% c("MEDIUM", "HIGH")
})]

# If there is risky feedback, display a warning
if (length(riskyFeedback) > 0) {
# Extract and display the categories with risky feedback
categories = sapply(riskyFeedback, function(f) f$rating$category)
warning('Unsafe inquery. Try to change harm thresholds. Your inquery violates safety setting(s):\n', paste(categories, collapse = ", "))
warning('Your prompt violates safety setting(s):\n', paste(categories, collapse = ", "))
}
# If there's safety feedback but not risky, inform the user
else {
warning('Safe inquery, but there is safety feedback. Consider reviewing them.')
}
}
# If there are no candidates, error, or safety feedback, provide general guidance
else {
warning('Unknown error. Cannot generate output. Please refer to:\n1) https://developers.generativeai.google/api\n2) https://palmr.r.ly.gd.edu.kg/')
}
}

#' @noRd
generateSafetySettings = function(htUnspecified, htDerogatory, htToxicity,
htViolence, htSexual, htMedical, htDangerous) {
# Map the threshold values to category names
thresholdMap = c("unsp" = "HARM_BLOCK_THRESHOLD_UNSPECIFIED",
"lowa" = "BLOCK_LOW_AND_ABOVE",
"meda" = "BLOCK_MEDIUM_AND_ABOVE",
"high" = "BLOCK_ONLY_HIGH",
"none" = "BLOCK_NONE")

# Check validity of the threshold values
stopifnot(all(htUnspecified %in% names(thresholdMap)),
all(htDerogatory %in% names(thresholdMap)),
all(htToxicity %in% names(thresholdMap)),
Expand All @@ -60,7 +44,6 @@ generateSafetySettings = function(htUnspecified, htDerogatory, htToxicity,
all(htMedical %in% names(thresholdMap)),
all(htDangerous %in% names(thresholdMap)))

# Create safety settings list in one go
safetySettings = list(
list(category = "HARM_CATEGORY_UNSPECIFIED", threshold = thresholdMap[htUnspecified]),
list(category = "HARM_CATEGORY_DEROGATORY", threshold = thresholdMap[htDerogatory]),
Expand Down
76 changes: 76 additions & 0 deletions R/palm.connect.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
))
}
Loading

0 comments on commit d4b1284

Please # to comment.