Skip to content

Commit

Permalink
new setupAgent option "generic" is added, extractCode() is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalin committed Dec 17, 2023
1 parent b46229d commit ca0898d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions R/clean_code_blocks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions R/parseBotResponse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down
29 changes: 29 additions & 0 deletions R/sendPrompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)

}
18 changes: 16 additions & 2 deletions R/setupAgent.R
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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.")
}
Expand Down Expand Up @@ -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.")
}
Expand Down

0 comments on commit ca0898d

Please sign in to comment.