Commit 45915ece authored by wuzekai's avatar wuzekai

update:替换为外网部署的模型

parent 75d22b2a
# === 配置 === # === 配置 ===
# 内网环境 API_URL <- "https://oam0321.cixincloud.com/v1/chat/completions"
OLLAMA_URL <- "http://172.29.2.110:8137/api/generate" API_KEY <- "1affeg87354asdgds9sgsdffgr87623"
# 外网环境
#OLLAMA_URL <- "http://180.169.131.147:8137/api/generate"
MODEL_ID <- "qwen3-coder:30b" MODEL_ID <- "qwen3-coder:30b"
# === 单次对话 === # === 单次对话 ===
...@@ -16,27 +13,59 @@ chart_completion <- function(user_prompt) { ...@@ -16,27 +13,59 @@ chart_completion <- function(user_prompt) {
# 构建请求体 # 构建请求体
req_body <- list( req_body <- list(
model = MODEL_ID, model = MODEL_ID,
prompt = user_prompt, messages = list(list(
role = "user",
content = user_prompt
)),
stream = FALSE stream = FALSE
) )
# 转换为JSON
json_body <- jsonlite::toJSON(req_body, auto_unbox = TRUE, pretty = TRUE)
# 使用 curl 包发送请求 # 使用 curl 包发送请求
h <- curl::new_handle() h <- curl::new_handle()
curl::handle_setheaders(h, "Content-Type" = "application/json") curl::handle_setheaders(h,
curl::handle_setopt(h, postfields = jsonlite::toJSON(req_body, auto_unbox = TRUE)) "Content-Type" = "application/json",
"Authorization" = paste("Bearer", API_KEY)
)
curl::handle_setopt(h, postfields = json_body)
curl::handle_setopt(h, timeout = 60) 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) result <- readLines(con, warn = FALSE)
close(con) 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 === # === 构造发给模型的 Prompt ===
......
# === 配置 === # === 配置 ===
# 内网环境 API_URL <- "https://oam0321.cixincloud.com/v1/chat/completions"
OLLAMA_URL <- "http://172.29.2.110:8137/api/generate" API_KEY <- "1affeg87354asdgds9sgsdffgr87623"
# 外网环境
#OLLAMA_URL <- "http://180.169.131.147:8137/api/generate"
MODEL_ID <- "qwen3-coder:30b" MODEL_ID <- "qwen3-coder:30b"
# === 单次对话 === # === 单次对话 ===
...@@ -16,27 +13,62 @@ metrics_completion <- function(user_prompt) { ...@@ -16,27 +13,62 @@ metrics_completion <- function(user_prompt) {
# 构建请求体 # 构建请求体
req_body <- list( req_body <- list(
model = MODEL_ID, model = MODEL_ID,
prompt = user_prompt, messages = list(list(
role = "user",
content = user_prompt
)),
stream = FALSE stream = FALSE
) )
# 转换为JSON
json_body <- jsonlite::toJSON(req_body, auto_unbox = TRUE, pretty = TRUE)
# 使用 curl 包发送请求 # 使用 curl 包发送请求
h <- curl::new_handle() h <- curl::new_handle()
curl::handle_setheaders(h, "Content-Type" = "application/json") curl::handle_setheaders(h,
curl::handle_setopt(h, postfields = jsonlite::toJSON(req_body, auto_unbox = TRUE)) "Content-Type" = "application/json",
"Authorization" = paste("Bearer", API_KEY)
)
curl::handle_setopt(h, postfields = json_body)
curl::handle_setopt(h, timeout = 60) 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) result <- readLines(con, warn = FALSE)
close(con) 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
} }
......
...@@ -49,12 +49,7 @@ output$chat_history_area <- renderUI({ ...@@ -49,12 +49,7 @@ output$chat_history_area <- renderUI({
return(create_no_data_ui()) return(create_no_data_ui())
} }
# 内网环境 dify_base_url <- "http://122.112.232.121:8079/chat/pmo9kEMGQVAdv8YR"
dify_base_url <- "http://172.31.2.2:8078/chat/tfjTZpJDgjQpBeTl"
# 外网环境
# dify_base_url <- "http://180.169.131.147:8078/chat/tfjTZpJDgjQpBeTl"
dify_url <- paste0( dify_url <- paste0(
dify_base_url, dify_base_url,
"?showSidebar=true", "?showSidebar=true",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment