|
3 | 3 | process_qwen <- function(prompt, model, api_key) { |
4 | 4 | write_log(sprintf("Starting QWEN API request with model: %s", model)) |
5 | 5 |
|
6 | | - # QWEN API endpoint |
7 | | - url <- "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions" |
| 6 | + # Determine the appropriate API endpoint based on the key |
| 7 | + # Try international endpoint first, if it fails, try mainland endpoint |
| 8 | + international_endpoint <- "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions" |
| 9 | + mainland_endpoint <- "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions" |
| 10 | + |
| 11 | + # Function to detect API key type |
| 12 | + detect_api_endpoint <- function(api_key) { |
| 13 | + # First try with international endpoint |
| 14 | + test_url <- international_endpoint |
| 15 | + test_body <- list( |
| 16 | + model = model, |
| 17 | + max_tokens = 10, |
| 18 | + messages = list(list(role = "user", content = "test")) |
| 19 | + ) |
| 20 | + |
| 21 | + test_response <- tryCatch({ |
| 22 | + httr::POST( |
| 23 | + url = test_url, |
| 24 | + httr::add_headers( |
| 25 | + "Authorization" = paste("Bearer", api_key), |
| 26 | + "Content-Type" = "application/json" |
| 27 | + ), |
| 28 | + body = jsonlite::toJSON(test_body, auto_unbox = TRUE), |
| 29 | + encode = "json" |
| 30 | + ) |
| 31 | + }, error = function(e) { |
| 32 | + write_log(sprintf("Error testing international endpoint: %s", e$message)) |
| 33 | + return(NULL) |
| 34 | + }) |
| 35 | + |
| 36 | + # Check if international endpoint works |
| 37 | + if (!is.null(test_response) && !httr::http_error(test_response)) { |
| 38 | + write_log("International API endpoint detected and working") |
| 39 | + return(international_endpoint) |
| 40 | + } |
| 41 | + |
| 42 | + # If international fails, try mainland endpoint |
| 43 | + write_log("International endpoint failed, trying mainland endpoint") |
| 44 | + test_url <- mainland_endpoint |
| 45 | + |
| 46 | + test_response <- tryCatch({ |
| 47 | + httr::POST( |
| 48 | + url = test_url, |
| 49 | + httr::add_headers( |
| 50 | + "Authorization" = paste("Bearer", api_key), |
| 51 | + "Content-Type" = "application/json" |
| 52 | + ), |
| 53 | + body = jsonlite::toJSON(test_body, auto_unbox = TRUE), |
| 54 | + encode = "json" |
| 55 | + ) |
| 56 | + }, error = function(e) { |
| 57 | + write_log(sprintf("Error testing mainland endpoint: %s", e$message)) |
| 58 | + return(NULL) |
| 59 | + }) |
| 60 | + |
| 61 | + # Check if mainland endpoint works |
| 62 | + if (!is.null(test_response) && !httr::http_error(test_response)) { |
| 63 | + write_log("Mainland API endpoint detected and working") |
| 64 | + return(mainland_endpoint) |
| 65 | + } |
| 66 | + |
| 67 | + # If both fail, default to international endpoint and let the main function handle the error |
| 68 | + write_log("Both endpoints failed, defaulting to international endpoint") |
| 69 | + return(international_endpoint) |
| 70 | + } |
| 71 | + |
| 72 | + # Detect and use the appropriate endpoint |
| 73 | + url <- detect_api_endpoint(api_key) |
| 74 | + write_log(sprintf("Using endpoint: %s", url)) |
8 | 75 | write_log(sprintf("Using model: %s", model)) |
9 | 76 |
|
10 | 77 | # Process all input at once |
|
0 commit comments