Interacte with Ollama running locally

library(httr)
# Define the Ollama URL
ollama_url <- "http://localhost:11434/api/generate"
# Define the payload (data to be sent in the POST request)
# nl_query <- "Please introduce Cypher language."
# payload <- list(model = "llama2",
#                 prompt = nl_query,
#                 stream = FALSE)
# 
# library(jsonlite)
# payload_json <- toJSON(payload, auto_unbox = TRUE)
# 
# # Send the POST request to the Ollama API
# response <- POST(url = ollama_url, body = payload_json, encode = "json")
# 
# # Check if the request was successful
# if (httr::status_code(response) == 200) {
#   # Print the response from the server
#   cat("Response from Ollama model:\n")
#   print(httr::content(response, "parsed")$response)
# } else {
#   # Print error message
#   cat("Error:", httr::status_code(response), "\n")
#   cat(httr::content(response, "text"), "\n")
# }

answer_me = function(nl_query) {
  payload <- list(model = "llama2",
                  prompt = nl_query,
                  stream = FALSE)
  
  library(jsonlite)
  payload_json <- toJSON(payload, auto_unbox = TRUE)
  response <- POST(url = ollama_url,
                   body = payload_json,
                   encode = "json")
  if (httr::status_code(response) == 200) {
    # Print the response from the server
    cat("Response from Ollama model:\n")
    return(httr::content(response, "parsed")$response)
  } else {
    # Print error message
    cat("Error:", httr::status_code(response), "\n")
    cat(httr::content(response, "text"), "\n")
  }
}
answer_me("What is the capital of France?")
## Response from Ollama model:
## [1] "\nThe capital of France is Paris."
answer_me("Why the sky is blue?")
## Response from Ollama model:
## [1] "\nThe sky appears blue to us because of a phenomenon called Rayleigh scattering. When sunlight enters Earth's atmosphere, it encounters tiny molecules of gases such as nitrogen and oxygen. These molecules scatter the light in all directions, but they scatter shorter (blue) wavelengths more than longer (red) wavelengths. This is known as Rayleigh scattering.\n\nAs a result of this scattering, the blue light is dispersed throughout the atmosphere, giving the sky its characteristic blue color. The deeper into the atmosphere you go, the more blue the light appears, until it becomes a deep blue at the horizon.\n\nOther factors can also affect the appearance of the sky, such as pollution, dust, and water vapor. However, Rayleigh scattering is the primary reason why the sky appears blue to our eyes.\n\nIt's worth noting that the color of the sky can appear different under different conditions, such as at sunrise or sunset when the light has to travel through more of the atmosphere, or in areas with high levels of pollution. But in general, the blue color of the sky is due to Rayleigh scattering."