Skip to content

Commit 0cb3091

Browse files
Merge pull request #18 from cafferychen777/dev
Resolved the issue of inconsistency between the international version…
2 parents 30c8a0a + c9ce30f commit 0cb3091

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

R/R/process_qwen.R

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,75 @@
33
process_qwen <- function(prompt, model, api_key) {
44
write_log(sprintf("Starting QWEN API request with model: %s", model))
55

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))
875
write_log(sprintf("Using model: %s", model))
976

1077
# Process all input at once

0 commit comments

Comments
 (0)