AlbertRapp / tidychatmodels

Chat With All Kinds of AI Models Through a Common Interface
https://tidychatmodels.albert-rapp.de/
Other
118 stars 10 forks source link

S3 #18

Open JohnCoene opened 6 months ago

JohnCoene commented 6 months ago

Hi Albert,

Thanks for the package. I thought I would change the API to using R's S3 interface namely so that it is extensible; this PR enables anyone to add new vendors.

I know this is quite a large PR which changes a lot of the source code so I'd totally understand if you do not want to merge it.

The idea is that change create_chat to a generic that returns an object of class tidychat on which we can then call various methods.

I could for instance bring support for a new vendor via:

new_chat_xyz <- function(key){
  req <- httr2::request(
    base_url = "https://api.xyz.com/"
  ) |>
    httr2::req_headers(
      "Authorization" = paste("Bearer", key),
      "Content-Type" = "application/json"
    )
  new_chat(req, engine = "xyz")
}

Either the existing methods to add_message, add_param, etc. for the object tidychat work fine with my API in which case I can leave it as is. Otherwise I can create methods.

add_message.xyz <- function(chat, message) {
  messages <- get_messages(chat)

  messages[[length(messages) + 1]] <- list(
    content = as.character(message)
  )

  attr(chat, "messages") <- messages
  chat
}
AlbertRapp commented 6 months ago

Hi @JohnCoene, love the idea. Haven't yet had time to take a look at the code. But I do want to do that within the next weeks. Promise!