From ca0898d1ed1ef0753df78bf4b8d3e8371917199b Mon Sep 17 00:00:00 2001 From: Akalin Date: Mon, 18 Dec 2023 00:25:05 +0100 Subject: [PATCH] new setupAgent option "generic" is added, extractCode() is updated --- NEWS.md | 5 ++++- R/clean_code_blocks.R | 1 + R/parseBotResponse.R | 2 ++ R/sendPrompt.R | 29 +++++++++++++++++++++++++++++ R/setupAgent.R | 18 ++++++++++++++++-- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index bd407e9..a52d94c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,7 @@ # mergen Version 0.1.1 _2023-12-29_ - * URLs added + * URLs in DESCRIPTION added + * new feature: now users can specify openai-like APIs by providing url and setting + agent name as "generic" in setupAgent() function + * new feature: clean_code_blocks() now automatically runs when extractCode() is called * fix: typos in setupAgent fixed * fix: sendPrompt fixed which was broken due to openai API changes diff --git a/R/clean_code_blocks.R b/R/clean_code_blocks.R index 133fd4c..7fcad7a 100644 --- a/R/clean_code_blocks.R +++ b/R/clean_code_blocks.R @@ -30,6 +30,7 @@ clean_code_blocks<-function(response){ #clear response of weird characters, otherwise this will return as error response<-gsub("```r", "```", response) response<-gsub("```R", "```", response) + response<-gsub("```bash", "```", response) response<-gsub("```\\{r\\}", "```", response) response<-gsub("```\\{R\\}", "```", response) diff --git a/R/parseBotResponse.R b/R/parseBotResponse.R index 4a3e7bd..db53fbe 100644 --- a/R/parseBotResponse.R +++ b/R/parseBotResponse.R @@ -32,6 +32,8 @@ extractCode<-function(text,delimiter="```"){ ) # ----------------------------------------------------------------------------- + text<-clean_code_blocks(text) # cleans code blocks automatically + # Split the text by the delimiter parts <- strsplit(text, delimiter, fixed = TRUE)[[1]] diff --git a/R/sendPrompt.R b/R/sendPrompt.R index 44551d7..8ad518d 100644 --- a/R/sendPrompt.R +++ b/R/sendPrompt.R @@ -68,6 +68,8 @@ sendPrompt<-function(agent,prompt,context=rbionfoExp, } }else if (agent$API == "replicate"){ promptFunc = .replicate_chat + }else if (agent$API == "generic"){ + promptFunc = genericChat }else{ stop("The specified API ",agent$API," is not compatible with the current setup") } @@ -230,3 +232,30 @@ testPrompter<-function(agent,prompt, ...){ return (parsed_get) } } + +# send chat prompt to a generic API that is similar to openai API +genericChat<-function(agent, prompt,...){ + + response <- POST( + url =agent$url, + + add_headers(Authorization = paste("Bearer", agent$ai_api_key)), + content_type("application/json"), + encode = "json", + body = list( + model = agent$model, + messages = list( + list(role = "user", content = prompt) + ) + ) + ) + + if(status_code(response)>200) { + result <- trimws(content(response)$error$message) + } else { + result <- trimws(content(response)$choices[[1]]$message$content) + } + + return(result) + +} diff --git a/R/setupAgent.R b/R/setupAgent.R index d6b2a8c..5a1f6e3 100644 --- a/R/setupAgent.R +++ b/R/setupAgent.R @@ -35,7 +35,11 @@ setupopenaiAgent<-function(model,type=c("chat","completion"), #' set up an online LLM API for subsequent tasks #' #' This function sets up an large language model API for tasks. -#' @param name Name of the API you want to use. Currently supported APIs are "openai" and "replicate" +#' @param name A string for the name of the API, one of "openai", "replicate" or "generic". +#' Currently supported APIs are "openai" and "replicate". If the user wishes to use +#' another API that has similar syntax to openai API this is also supported via the +#' the "generic" option. In this case, the user should also provide a url for the API +#' using the #' @param type Specify type of model (chat or completion). This parameter only needs to be specified when using 'openai #' @param model LLM model you wish to use. #' For openAI chat model examples are: @@ -52,6 +56,7 @@ setupopenaiAgent<-function(model,type=c("chat","completion"), #' For a full list of openAI models #' \href{https://platform.openai.com/docs/models/overview}{click here}. For a full list of Replicate models, #' \href{https://replicate.com/collections/language-models}{click here}. +#' @param url the url for the API in case the API "generic" is selected. (Default: NULL) #' @param ai_api_key personal API key for accessing LLM #' @return A list holding agent information. #' @examples @@ -65,7 +70,9 @@ setupopenaiAgent<-function(model,type=c("chat","completion"), #' @export -setupAgent<-function(name=c("openai","replicate"), type=NULL, model=NULL, ai_api_key=Sys.getenv("AI_API_KEY")){ +setupAgent<-function(name=c("openai","replicate","generic"), + type=NULL, model=NULL,url=NULL, ai_api_key=Sys.getenv("AI_API_KEY")){ + if (ai_api_key==""){ stop("Invalid API key provided. Please set this as a string or load AI_API_KEY into your system environment.") } @@ -111,6 +118,13 @@ setupAgent<-function(name=c("openai","replicate"), type=NULL, model=NULL, ai_api headers <- c( "Authorization" = paste("Bearer", ai_api_key), "Content-Type" = "application/json") + } + else if(name=="generic"){ + + base_url=url + headers <- c( + "Authorization" = paste("Bearer", ai_api_key), + "Content-Type" = "application/json") }else { stop("Chosen API ",name, " not supported.") }