BG
This commit is contained in:
@@ -19,13 +19,13 @@ M.ask = function(key,content,model)
|
|||||||
headers = {
|
headers = {
|
||||||
["Authorization"] = "Bearer " .. key,
|
["Authorization"] = "Bearer " .. key,
|
||||||
["Content-Type"] = "application/json",
|
["Content-Type"] = "application/json",
|
||||||
},
|
},
|
||||||
source = ltn12.source.string(cjson.encode(payload)),
|
source = ltn12.source.string(cjson.encode(payload)),
|
||||||
sink = ltn12.sink.table(response_body)
|
sink = ltn12.sink.table(response_body)
|
||||||
}
|
}
|
||||||
if code == 200 then
|
if code == 200 then
|
||||||
local data = cjson.decode(table.concat(response_body))
|
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
|
else
|
||||||
return false, "请求失败,错误描述:" .. table.concat(response_body)
|
return false, "请求失败,错误描述:" .. table.concat(response_body)
|
||||||
end
|
end
|
||||||
@@ -49,9 +49,8 @@ M.ask_local = function(url,content,model)
|
|||||||
sink = ltn12.sink.table(response_body)
|
sink = ltn12.sink.table(response_body)
|
||||||
}
|
}
|
||||||
if code == 200 then
|
if code == 200 then
|
||||||
print(table.concat(response_body))
|
|
||||||
local data = cjson.decode(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
|
else
|
||||||
return false, "请求失败,错误描述:" .. table.concat(response_body)
|
return false, "请求失败,错误描述:" .. table.concat(response_body)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,14 +18,14 @@ M.ask = function(key,content,model)
|
|||||||
method = "POST",
|
method = "POST",
|
||||||
headers = {
|
headers = {
|
||||||
["Authorization"] = "Bearer " .. key,
|
["Authorization"] = "Bearer " .. key,
|
||||||
["Content-Type"] = "application/json",
|
["Content-Type"] = "application/json",
|
||||||
},
|
},
|
||||||
source = ltn12.source.string(cjson.encode(payload)),
|
source = ltn12.source.string(cjson.encode(payload)),
|
||||||
sink = ltn12.sink.table(response_body)
|
sink = ltn12.sink.table(response_body)
|
||||||
}
|
}
|
||||||
if code == 200 then
|
if code == 200 then
|
||||||
local data = cjson.decode(table.concat(response_body))
|
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
|
else
|
||||||
return false, "请求失败,错误描述:" .. table.concat(response_body)
|
return false, "请求失败,错误描述:" .. table.concat(response_body)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
require("fwutils.webapi")
|
require("fwutils.webapi")
|
||||||
local oss = require("aliyun.oss")
|
local oss = require("aliyun.oss")
|
||||||
|
local cos = require("tencent.cos")
|
||||||
local fw_files = require("fwutils.fwutils.function.files")
|
local fw_files = require("fwutils.fwutils.function.files")
|
||||||
local M = {}
|
local M = {}
|
||||||
|
M.use_type = "cos"
|
||||||
M.up_file = function(config,remote_dir,local_filepath,desc,conn)
|
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
|
if result == false then
|
||||||
return false,filepath
|
return false,filepath
|
||||||
end
|
end
|
||||||
@@ -20,7 +26,12 @@ M.up_file = function(config,remote_dir,local_filepath,desc,conn)
|
|||||||
return true,filepath
|
return true,filepath
|
||||||
end
|
end
|
||||||
M.up_data = function(config,remote_dir,data,ext,desc,conn)
|
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
|
if result == false then
|
||||||
return false,filepath
|
return false,filepath
|
||||||
end
|
end
|
||||||
@@ -35,4 +46,26 @@ M.up_data = function(config,remote_dir,data,ext,desc,conn)
|
|||||||
end
|
end
|
||||||
return true,filepath
|
return true,filepath
|
||||||
end
|
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
|
return M
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
local tencent_cos = require("tencent_cos")
|
local tencent_cos = require("tencent_cos")
|
||||||
local fw = require("fastweb")
|
local fw = require("fastweb")
|
||||||
local utils = require("app.utils")
|
local utils = require("utils")
|
||||||
local config = require("app.config")
|
|
||||||
local M = {}
|
local M = {}
|
||||||
-- 文件扩展名与目录的映射
|
-- 文件扩展名与目录的映射
|
||||||
M.ext_dirpath = {
|
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 filename = fw.make_software_guid().."."..ext
|
||||||
local dirpath = ""
|
local dirpath = ""
|
||||||
@@ -72,27 +71,45 @@ function M.upload_data(config,object_name,remote_dir,data,ext)
|
|||||||
|
|
||||||
|
|
||||||
local cos = tencent_cos.new()
|
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 cos_filepath = remote_dir..dirpath.."/"..filename
|
||||||
local result = cos:upfile(
|
|
||||||
config.appid,
|
|
||||||
config.endpoint,
|
local result = nil
|
||||||
config.secret_id,
|
for _, cfg in ipairs(config) do
|
||||||
config.secret_key,
|
result = cos:updata(
|
||||||
object_name,
|
cfg.appid,
|
||||||
cos_filepath,
|
cfg.endpoint,
|
||||||
local_file
|
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
|
if result == "" then
|
||||||
return true,cos_filepath
|
return true,cos_filepath
|
||||||
end
|
end
|
||||||
return false,result
|
return false,result
|
||||||
end
|
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
|
if not utils.exists_file(local_filepath) then
|
||||||
return false,"文件不存在,local_filepath: "..local_filepath
|
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 cos = tencent_cos.new()
|
||||||
|
|
||||||
|
|
||||||
local result = cos:upfile(
|
local result = nil
|
||||||
config.appid,
|
|
||||||
config.endpoint,
|
|
||||||
config.secret_id,
|
for _, cfg in ipairs(config) do
|
||||||
config.secret_key,
|
result = cos:upfile(
|
||||||
object_name,
|
cfg.appid,
|
||||||
cos_filepath,
|
cfg.endpoint,
|
||||||
local_filepath
|
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 result == "" then
|
||||||
if auto_remove then
|
if auto_remove then
|
||||||
utils.delete_file(local_filepath)
|
utils.delete_file(local_filepath)
|
||||||
@@ -144,7 +185,4 @@ function M.upload_file(config,object_name,remote_dir,local_filepath,auto_remove)
|
|||||||
end
|
end
|
||||||
return false,result
|
return false,result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
Reference in New Issue
Block a user