tidychatmodels icon indicating copy to clipboard operation
tidychatmodels copied to clipboard

Add timeout support

Open AlbertRapp opened this issue 1 year ago • 1 comments

Similar to req_timeout()

AlbertRapp avatar May 09 '24 09:05 AlbertRapp

Here some example where it could be useful:

library(tidychatmodels)

generate_reply <- \(prompt, model = "llama3.3:70b") {
  ollama_model <-
    create_chat('ollama') |>
    add_model(model)
  chat <- 
    ollama_model |>
    add_message(prompt) |> 
    perform_chat()
  msgs <- chat |> extract_chat(silent = TRUE)
  msgs$message[[2]]
}
generate_reply("Explain to me why 1+1 = 2", model = "llama3.2:3b")
#> [1] "The equation 1 + 1 = 2 is a fundamental concept in mathematics, and it's based on the way we understand addition. Here's a breakdown of why this equation holds true:\n\n**What is addition?**\n\nAddition is a mathematical operation that combines two or more numbers to find their total sum. In other words, when you add numbers together, you're counting how many units or objects you have in total.\n\n**The concept of unity**\n\nIn the case of 1 + 1, we're dealing with one unit or object. When we say \"one,\" we mean that there is only one single unit or object present.\n\n**The process of addition**\n\nWhen we add 1 and 1 together, we're essentially combining two identical units. We start with one unit, and then we take another unit and combine it with the first one.\n\nThink of it like this: Imagine you have one apple (representing the first unit) in your hand. Your friend gives you another apple (representing the second unit). Now, you have two apples in total!\n\n**Why 1 + 1 = 2**\n\nSo, when we count the number of units or objects we have after combining the two single units, we get... 2! That's because each unit is counted separately, and there are now two units present.\n\nTherefore, based on this understanding of addition and unity, we can confidently say that 1 + 1 = 2.\n\nDoes this explanation help clarify things for you?"
tryCatch(
  {
    generate_reply("Explain to me why 1+1 = 2", model = "llama3.3:70b")
  },
  error = \(cnd) message(cnd)
)
#> Error in `httr2::req_perform()`:
#> ! Failed to perform HTTP request.
#> Caused by error in `curl::curl_fetch_memory()`:
#> ! Timeout was reached [localhost]: Operation too slow. Less than 1 bytes/sec transferred the last 600 seconds

Created on 2024-12-10 with reprex v2.1.1

jrosell avatar Dec 10 '24 11:12 jrosell