From 45915ece35e5e4a6eb098e8edb892da170a7036a Mon Sep 17 00:00:00 2001 From: wuzekai <3025054974@qq.com> Date: Sun, 25 Jan 2026 16:07:01 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=9B=BF=E6=8D=A2=E4=B8=BA=E5=A4=96?= =?UTF-8?q?=E7=BD=91=E9=83=A8=E7=BD=B2=E7=9A=84=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- radiant.quickgen/R/quickgen_chart.R | 57 +++++++++++++----- radiant.quickgen/R/quickgen_metrics.R | 58 ++++++++++++++----- .../app/tools/analysis/quickgen_chat_ui.R | 7 +-- 3 files changed, 89 insertions(+), 33 deletions(-) diff --git a/radiant.quickgen/R/quickgen_chart.R b/radiant.quickgen/R/quickgen_chart.R index 0f6506d..d4bf700 100644 --- a/radiant.quickgen/R/quickgen_chart.R +++ b/radiant.quickgen/R/quickgen_chart.R @@ -1,9 +1,6 @@ # === 配置 === -# 内网环境 -OLLAMA_URL <- "http://172.29.2.110:8137/api/generate" -# 外网环境 -#OLLAMA_URL <- "http://180.169.131.147:8137/api/generate" - +API_URL <- "https://oam0321.cixincloud.com/v1/chat/completions" +API_KEY <- "1affeg87354asdgds9sgsdffgr87623" MODEL_ID <- "qwen3-coder:30b" # === 单次对话 === @@ -16,27 +13,59 @@ chart_completion <- function(user_prompt) { # 构建请求体 req_body <- list( model = MODEL_ID, - prompt = user_prompt, + messages = list(list( + role = "user", + content = user_prompt + )), stream = FALSE ) + # 转换为JSON + json_body <- jsonlite::toJSON(req_body, auto_unbox = TRUE, pretty = TRUE) + # 使用 curl 包发送请求 h <- curl::new_handle() - curl::handle_setheaders(h, "Content-Type" = "application/json") - curl::handle_setopt(h, postfields = jsonlite::toJSON(req_body, auto_unbox = TRUE)) + curl::handle_setheaders(h, + "Content-Type" = "application/json", + "Authorization" = paste("Bearer", API_KEY) + ) + curl::handle_setopt(h, postfields = json_body) curl::handle_setopt(h, timeout = 60) - con <- curl::curl(OLLAMA_URL, handle = h) + # 捕获响应 + con <- curl::curl(API_URL, handle = h) result <- readLines(con, warn = FALSE) close(con) - # 解析响应 - body <- jsonlite::fromJSON(paste(result, collapse = "")) + # 检查响应是否为空 + if (length(result) == 0 || all(result == "")) { + stop("API 返回空响应") + } + + # 尝试解析JSON + body <- tryCatch({ + jsonlite::fromJSON(paste(result, collapse = ""), simplifyVector = FALSE) + }, error = function(e) { + stop("JSON解析失败: ", e$message, "\n原始响应: ", paste(result, collapse = "")) + }) - if (is.null(body$response) || trimws(body$response) == "") - stop("Ollama API 返回空内容:", paste(result, collapse = "")) + # 检查是否有错误字段 + if (!is.null(body$error)) { + stop("API 返回错误: ", jsonlite::toJSON(body$error, auto_unbox = TRUE)) + } + + # 提取内容 + if (!is.list(body) || !is.list(body$choices) || length(body$choices) == 0) { + stop("API 响应格式异常: ", paste(result, collapse = "")) + } + + response_content <- body$choices[[1]]$message$content + + if (is.null(response_content) || trimws(response_content) == "") { + stop("API 返回空内容: ", paste(result, collapse = "")) + } - body$response + response_content } # === 构造发给模型的 Prompt === diff --git a/radiant.quickgen/R/quickgen_metrics.R b/radiant.quickgen/R/quickgen_metrics.R index ca4b4dd..a69d063 100644 --- a/radiant.quickgen/R/quickgen_metrics.R +++ b/radiant.quickgen/R/quickgen_metrics.R @@ -1,9 +1,6 @@ # === 配置 === -# 内网环境 -OLLAMA_URL <- "http://172.29.2.110:8137/api/generate" -# 外网环境 -#OLLAMA_URL <- "http://180.169.131.147:8137/api/generate" - +API_URL <- "https://oam0321.cixincloud.com/v1/chat/completions" +API_KEY <- "1affeg87354asdgds9sgsdffgr87623" MODEL_ID <- "qwen3-coder:30b" # === 单次对话 === @@ -16,27 +13,62 @@ metrics_completion <- function(user_prompt) { # 构建请求体 req_body <- list( model = MODEL_ID, - prompt = user_prompt, + messages = list(list( + role = "user", + content = user_prompt + )), stream = FALSE ) + # 转换为JSON + json_body <- jsonlite::toJSON(req_body, auto_unbox = TRUE, pretty = TRUE) + # 使用 curl 包发送请求 h <- curl::new_handle() - curl::handle_setheaders(h, "Content-Type" = "application/json") - curl::handle_setopt(h, postfields = jsonlite::toJSON(req_body, auto_unbox = TRUE)) + curl::handle_setheaders(h, + "Content-Type" = "application/json", + "Authorization" = paste("Bearer", API_KEY) + ) + curl::handle_setopt(h, postfields = json_body) curl::handle_setopt(h, timeout = 60) - con <- curl::curl(OLLAMA_URL, handle = h) + # 捕获响应 + con <- curl::curl(API_URL, handle = h) result <- readLines(con, warn = FALSE) close(con) + # 检查响应是否为空 + if (length(result) == 0 || all(result == "")) { + stop("API 返回空响应") + } + # 解析响应 - body <- jsonlite::fromJSON(paste(result, collapse = "")) + body <- jsonlite::fromJSON(paste(result, collapse = ""), simplifyVector = FALSE) + + # 尝试解析JSON + body <- tryCatch({ + jsonlite::fromJSON(paste(result, collapse = ""), simplifyVector = FALSE) + }, error = function(e) { + stop("JSON解析失败: ", e$message, "\n原始响应: ", paste(result, collapse = "")) + }) - if (is.null(body$response) || trimws(body$response) == "") - stop("Ollama API 返回空内容:", paste(result, collapse = "")) + # 检查是否有错误字段 + if (!is.null(body$error)) { + stop("API 返回错误: ", jsonlite::toJSON(body$error, auto_unbox = TRUE)) + } + + # 提取内容 + if (!is.list(body) || !is.list(body$choices) || length(body$choices) == 0) { + stop("API 响应格式异常: ", paste(result, collapse = "")) + } + + response_content <- body$choices[[1]]$message$content + + if (is.null(response_content) || trimws(response_content) == "") { + stop("API 返回空内容: ", paste(result, collapse = "")) + } - body$response + response_content } diff --git a/radiant.quickgen/inst/app/tools/analysis/quickgen_chat_ui.R b/radiant.quickgen/inst/app/tools/analysis/quickgen_chat_ui.R index adc3680..d1c7bbe 100644 --- a/radiant.quickgen/inst/app/tools/analysis/quickgen_chat_ui.R +++ b/radiant.quickgen/inst/app/tools/analysis/quickgen_chat_ui.R @@ -49,12 +49,7 @@ output$chat_history_area <- renderUI({ return(create_no_data_ui()) } - # 内网环境 - dify_base_url <- "http://172.31.2.2:8078/chat/tfjTZpJDgjQpBeTl" - - # 外网环境 - # dify_base_url <- "http://180.169.131.147:8078/chat/tfjTZpJDgjQpBeTl" - + dify_base_url <- "http://122.112.232.121:8079/chat/pmo9kEMGQVAdv8YR" dify_url <- paste0( dify_base_url, "?showSidebar=true", -- 2.22.0