This commit is contained in:
a158
2026-03-03 02:03:50 +08:00
parent 9dd5a02c88
commit f8503964b0
4 changed files with 109 additions and 39 deletions

View File

@@ -19,13 +19,13 @@ M.ask = function(key,content,model)
headers = {
["Authorization"] = "Bearer " .. key,
["Content-Type"] = "application/json",
},
},
source = ltn12.source.string(cjson.encode(payload)),
sink = ltn12.sink.table(response_body)
}
if code == 200 then
local data = cjson.decode(table.concat(response_body))
return true,{content = data.choices[1].message.content,reasoning_content = data.choices[1].reasoning_content}
return true,{content = data.choices[1].message.content,data=data}
else
return false, "请求失败,错误描述:" .. table.concat(response_body)
end
@@ -49,9 +49,8 @@ M.ask_local = function(url,content,model)
sink = ltn12.sink.table(response_body)
}
if code == 200 then
print(table.concat(response_body))
local data = cjson.decode(table.concat(response_body))
return true,{content = data.message.content}
return true,{content = data.message.content,data=data}
else
return false, "请求失败,错误描述:" .. table.concat(response_body)
end

View File

@@ -18,14 +18,14 @@ M.ask = function(key,content,model)
method = "POST",
headers = {
["Authorization"] = "Bearer " .. key,
["Content-Type"] = "application/json",
["Content-Type"] = "application/json",
},
source = ltn12.source.string(cjson.encode(payload)),
sink = ltn12.sink.table(response_body)
}
if code == 200 then
local data = cjson.decode(table.concat(response_body))
return true,{content = data.choices[1].message.content}
return true,{content = data.choices[1].message.content,data=data}
else
return false, "请求失败,错误描述:" .. table.concat(response_body)
end

View File

@@ -1,10 +1,16 @@
require("fwutils.webapi")
local oss = require("aliyun.oss")
local cos = require("tencent.cos")
local fw_files = require("fwutils.fwutils.function.files")
local M = {}
M.use_type = "cos"
M.up_file = function(config,remote_dir,local_filepath,desc,conn)
local result,filepath = oss.upload_file(config,remote_dir,local_filepath,false)
local result,filepath = nil,nil
if M.use_type == "oss" then
result,filepath = oss.upload_file(config,remote_dir,local_filepath,false)
elseif M.use_type == "cos" then
result,filepath = cos.upload_file(config,remote_dir,local_filepath,false)
end
if result == false then
return false,filepath
end
@@ -20,7 +26,12 @@ M.up_file = function(config,remote_dir,local_filepath,desc,conn)
return true,filepath
end
M.up_data = function(config,remote_dir,data,ext,desc,conn)
local result,filepath = oss.upload_data(config,remote_dir,data,ext)
local result,filepath = nil,nil
if M.use_type == "oss" then
result,filepath = oss.upload_data(config,remote_dir,data,ext)
elseif M.use_type == "cos" then
result,filepath = cos.upload_data(config,remote_dir,data,ext)
end
if result == false then
return false,filepath
end
@@ -35,4 +46,26 @@ M.up_data = function(config,remote_dir,data,ext,desc,conn)
end
return true,filepath
end
M.up_file_ex = function(config,remote_dir,local_filepath,desc,conn)
local result,filepath = nil,nil
if M.use_type == "oss" then
fw.throw_string("不支持oss上传")
elseif M.use_type == "cos" then
result,filepath = cos.upload_file(config,remote_dir,local_filepath,false)
end
if result == false then
return false,filepath
end
result = fw_files.add(
utils.file_size(local_filepath),
filepath,
desc,
conn
)
if result == false then
return false,"添加文件失败"
end
return true,filepath
end
return M

View File

@@ -1,7 +1,6 @@
local tencent_cos = require("tencent_cos")
local fw = require("fastweb")
local utils = require("app.utils")
local config = require("app.config")
local utils = require("utils")
local M = {}
-- 文件扩展名与目录的映射
M.ext_dirpath = {
@@ -43,7 +42,7 @@ M.ext_dirpath = {
}
}
function M.upload_data(config,object_name,remote_dir,data,ext)
function M.upload_data(config,remote_dir,data,ext)
local filename = fw.make_software_guid().."."..ext
local dirpath = ""
@@ -72,27 +71,45 @@ function M.upload_data(config,object_name,remote_dir,data,ext)
local cos = tencent_cos.new()
local local_file = fw.website_dir()..config.path.temp.."/"..filename
utils.save_file(local_file,data)
local cos_filepath = remote_dir..dirpath.."/"..filename
local result = cos:upfile(
config.appid,
config.endpoint,
config.secret_id,
config.secret_key,
object_name,
cos_filepath,
local_file
)
local result = nil
for _, cfg in ipairs(config) do
result = cos:updata(
cfg.appid,
cfg.endpoint,
cfg.secret_id,
cfg.secret_key,
cfg.bucket_name,
cos_filepath,
data
)
if result ~= "" then
-- 如果不是第一个失败,那么就循环删除之前已上传的文件
if _ > 1 then
for i = 1, _ - 1 do
local prev_cfg = config[i]
cos:del(
prev_cfg.appid,
prev_cfg.endpoint,
prev_cfg.secret_id,
prev_cfg.secret_key,
prev_cfg.bucket_name,
cos_filepath
)
end
end
break
end
end
if result == "" then
return true,cos_filepath
end
return false,result
end
function M.upload_file(config,object_name,remote_dir,local_filepath,auto_remove)
function M.upload_file(config,remote_dir,local_filepath,auto_remove)
if not utils.exists_file(local_filepath) then
return false,"文件不存在,local_filepath: "..local_filepath
@@ -127,15 +144,39 @@ function M.upload_file(config,object_name,remote_dir,local_filepath,auto_remove)
local cos = tencent_cos.new()
local result = cos:upfile(
config.appid,
config.endpoint,
config.secret_id,
config.secret_key,
object_name,
cos_filepath,
local_filepath
)
local result = nil
for _, cfg in ipairs(config) do
result = cos:upfile(
cfg.appid,
cfg.endpoint,
cfg.secret_id,
cfg.secret_key,
cfg.bucket_name,
cos_filepath,
local_filepath
)
if result ~= "" then
-- 如果不是第一个失败,那么就循环删除之前已上传的文件
if _ > 1 then
for i = 1, _ - 1 do
local prev_cfg = config[i]
-- 删除之前上传的文件
cos:del(
prev_cfg.appid,
prev_cfg.endpoint,
prev_cfg.secret_id,
prev_cfg.secret_key,
prev_cfg.bucket_name,
cos_filepath
)
end
end
break
end
end
if result == "" then
if auto_remove then
utils.delete_file(local_filepath)
@@ -144,7 +185,4 @@ function M.upload_file(config,object_name,remote_dir,local_filepath,auto_remove)
end
return false,result
end
return M