Skip to content
Open

S3 #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
^docs$
^pkgdown$
^\.github$
^README\.qmd$
^tidychatmodels\.png$
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ Type: Package
Package: tidychatmodels
Title: Chat With All Kinds of AI Models Through a Common Interface
Version: 0.1.0
Authors@R:
person("Albert", "Rapp", , "info@albert-rapp.de", role = c("aut", "cre"))
Authors@R: c(
person("Albert", "Rapp", , "info@albert-rapp.de", role = c("aut", "cre")),
person("John", "Coene", , "jcoenep@gmail.com", role = c("ctb"))
)
Description: This packages lets you chat with models from openAI and
mistral.ai really easily. This package is set up in a modular fashion
(similar to {tidymodels}) so that it is easy to switch between using
Expand All @@ -16,7 +18,6 @@ Depends:
R (>= 4.1.0)
Imports:
cli,
glue,
httr2,
knitr,
purrr,
Expand Down
34 changes: 33 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
# Generated by roxygen2: do not edit by hand

S3method(print,chat)
S3method(add_message,tidychat)
S3method(add_params,tidychat)
S3method(append_message,antropic)
S3method(append_message,ollama)
S3method(append_message,tidychat)
S3method(extract_chat,tidychat)
S3method(get_engine,tidychat)
S3method(get_messages,tidychat)
S3method(get_model,tidychat)
S3method(get_params,tidychat)
S3method(get_uses,tidychat)
S3method(inc_uses,tidychat)
S3method(perform_chat,tidychat)
S3method(perform_query,tidychat)
S3method(prepare_engine,anthropic)
S3method(prepare_engine,tidychat)
S3method(print,tidychat)
S3method(set_uses,tidychat)
export(add_message)
export(add_model)
export(add_params)
export(append_message)
export(create_chat)
export(extract_chat)
export(get_engine)
export(get_messages)
export(get_model)
export(get_params)
export(get_uses)
export(inc_uses)
export(new_chat)
export(new_chat_anthropic)
export(new_chat_mistral)
export(new_chat_ollama)
export(new_chat_openai)
export(perform_chat)
export(perform_query)
export(prepare_engine)
export(set_uses)
68 changes: 63 additions & 5 deletions R/add_message.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#' Add messages to a chat object.
#'
#' @param chat_obj A chat object created from `create_chat()`
#' @param chat A chat object of class `tidychat`.
#' @param message A character vector with one element. The message to add to the chat.
#' @param role A character vector with one element. The role of the message. Typically 'user' or 'system'.
#'
#' @return A chat object with the messages added
#' @export
#'
#' @examples
#' \dontrun{dotenv::load_dot_env()
Expand All @@ -31,13 +30,72 @@
#' ) |>
#' add_message('2 + 2 is 4, minus 1 that\'s 3, ')
#' }
add_message <- function(chat_obj, message, role = 'user') {
#' @export
#' @name add_message
add_message <- function(chat, message, role = "user") UseMethod("add_message")

#' @describeIn add_message Add a message to a `tidychat` object.
#' @export
add_message.tidychat <- function(chat, message, role = "user") {
messages <- get_messages(chat)

chat_obj$messages[[length(chat_obj$messages) + 1]] <- list(
messages[[length(messages) + 1]] <- list(
role = role,
content = message
)
chat_obj

attr(chat, "messages") <- messages
chat
}

#' Get messages from a chat object.
#' @param chat An object of class `tidychat`.
#' @export
#' @name get_messages
get_messages <- function(chat) UseMethod("get_messages")

#' @describeIn get_messages Get messages from a `tidychat` object.
#' @export
get_messages.tidychat <- function(chat) {
attr(chat, "messages")
}

#' Append a message to a `tidychat` object.
#' @param chat An object of class `tidychat`.
#' @param response The response as returned by `perform_query`.
#' @param ... Ignored for future compatibility.
#' @export
#' @name append_message
append_message <- function(chat, response, ...) UseMethod("append_message")

#' @describeIn append_message Appends a message to a `tidychat` object.
#' @export
append_message.tidychat <- function(chat, response, ...) {
messages <- get_messages(chat)

messages[[length(messages) + 1]] <- response$choices[[1]]$message

attr(chat, "messages") <- messages
invisible(chat)
}

#' @describeIn append_message Appends a message to a `ollama` object.
#' @export
append_message.ollama <- function(chat, response, ...){
messages <- get_messages(chat)
messages[[length(messages) + 1]] <- response$message
attr(chat, "messages") <- messages
invisible(chat)
}

#' @describeIn append_message Appends a message to a `anthropic` object.
#' @export
append_message.antropic <- function(chat, response, ...){
messages <- get_messages(chat)
messages[[length(messages) + 1]] <- list(
role = "assistant",
content = response$content[[1]]$text
)
attr(chat, "messages") <- messages
invisible(chat)
}
27 changes: 21 additions & 6 deletions R/add_model.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#' Add a model to a chat object.
#'
#' @param chat_obj A chat object created from `create_chat()`
#' @param model A character vector with one element. The model to use for the chat. You can use any chat completion model from openAI and mistral.ai here. Refer to their API docs for specific names.
#' @param chat An object of class `tidychat`.
#' @param model A character vector with one element.
#' The model to use for the chat.
#' You can use any chat completion model from openAI and mistral.ai here.
#' Refer to their API docs for specific names.
#'
#' @return A chat object with the model added
#' @export
#'
#' @examples
#' \dontrun{
Expand All @@ -15,8 +17,21 @@
#' chat_mistral <- create_chat('mistral', Sys.getenv('MISTRAL_DEV_KEY')) |>
#' add_model('mistral-large-latest')
#' }
add_model <- function(chat_obj, model) {
chat_obj$model <- model
chat_obj
#' @export
#' @name add_model
add_model <- function(chat, model) {
attr(chat, "model") <- model
chat
}

#' Get model from a chat object.
#' @param chat An object of class `tidychat`.
#' @export
#' @name get_model
get_model <- function(chat) UseMethod("get_model")

#' @describeIn get_model Gets a model from a `tidychat` object.
#' @export
get_model.tidychat <- function(chat) {
attr(chat, "model")
}
31 changes: 20 additions & 11 deletions R/add_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#' @param ... A named list of parameters to add to the chat object. Must be valid parameters from the API documentation.
#'
#' @return A chat object with the parameters added
#' @export
#'
#' @examples
#' \dontrun{dotenv::load_dot_env()
Expand All @@ -16,16 +15,26 @@
#' add_model('mistral-large-latest') |>
#' add_params('temperature' = 0.5, 'max_tokens' = 100)
#' }
add_params <- function(chat_obj, ...) {
if (is.null(chat_obj$params)) {
chat_obj$params <- utils::modifyList(list(), list(...))
} else {
chat_obj$params <- utils::modifyList(chat_obj$params, list(...))
}
chat_obj
}


#' @export
#' @name add_params
add_params <- function(chat, ...) UseMethod("add_params")

#' @describeIn add_params Add parameters to a `tidychat` object.
#' @export
add_params.tidychat <- function(chat, ...) {
params <- modifyList(get_params(chat), list(...))
attr(chat, "params") <- params
chat
}

#' Get parameters from a chat object.
#' @param chat An object of class `tidychat`.
#' @export
#' @name get_params
get_params <- function(chat) UseMethod("get_params")

#' @describeIn get_params Add parameters to a `tidychat` object.
#' @export
get_params.tidychat <- function(chat) {
attr(chat, "params")
}
Loading