This commit is contained in:
2026-07-02 18:25:56 +07:00
parent c4e255ec2a
commit 1577d7ae25
2 changed files with 114 additions and 145 deletions
+21 -36
View File
@@ -481,20 +481,9 @@ julia> response = SQLLLM.SQLexecution(executeSQL, sql)
# Signature
"""
function SQLexecution(executeSQL::Function, sql::T
) where {T<:AbstractString}
)::NamedTuple where {T<:AbstractString}
try
#XXX dummy SQL. use for testing
# sql = "SELECT w.wine_name FROM wine w JOIN wine_food wf ON w.wine_id = wf.wine_id JOIN food f ON wf.food_id = f.food_id WHERE f.\"food_name\" = 'lamb';"
# sql = " SELECT w.wine_name FROM wine w JOIN food f ON f.food_name = 'lamb' JOIN wine_food wf ON w.wine_id = wf.wine_id AND f.food_id = wf.food_id GROUP BY w.wine_name ORDER BY COUNT(DISTINCT w.wine_id) DESC;"
# sql = " SELECT COUNT(DISTINCT wf.wine_id) FROM wine w JOIN wine_food wf ON w.wine_id = wf.wine_id JOIN food f ON wf.food_id = f.food_id WHERE f.food_name ILIKE '%lamb%'"
#XXX use for package testing, remove when done
# ans = "1.schilfwein zweigelt 2.cabernet sauvignon reserve limited edition"
# ans = "There are 1500 wines that can be paired with lamb."
# ans = "1500"
# return (response=ans, errormsg=nothing, reward=1, isterminal=true)
# add LIMIT to the SQL to prevent loading large data
sql = strip(sql)
@@ -508,39 +497,36 @@ function SQLexecution(executeSQL::Function, sql::T
else
sql = sql * ";"
end
println("\n~~~ SQLexecution() SQL: ", @__FILE__, " ", @__LINE__)
println(sql)
result = executeSQL(sql)
df = DataFrame(result)
tablesize = size(df)
row, column = tablesize
if row == 0
error("\nThe resulting table has 0 row. Please try again.")
elseif column > 50
error("\nSQL execution success but there are more than 50 rows Please be more specific.")
return (result_str="The resulting table has 0 row.", result_raw=df, success=true, errormsg=nothing)
elseif column > 30
return (result_str="There are more than 30 columns. Please be more specific.", result_raw=df, success=true, errormsg=nothing)
else
df1 =
if row > 2
# ramdom row to pick
df[sample(1:nrow(df), 2, replace=false), :] # random select 2 rows from df
else
df
end
result = GeneralUtils.dfToString(df1)
println("\n~~~ SQLexecution() result: ", @__FILE__, " ", @__LINE__)
println(sql)
println(df1)
println("\n")
return (result_str=result, result_raw=df1, success=true, errormsg=nothing)
end
df1 =
if row > 2
# ramdom row to pick
df[sample(1:nrow(df), 2, replace=false), :] # random select 2 rows from df
else
df
end
println("\n~~~ SQLexecution() result: ", @__FILE__, " ", @__LINE__)
println(df1)
return (result=df1, success=true, errormsg=nothing)
catch e
io = IOBuffer()
showerror(io, e)
errorMsg = String(take!(io))
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
println(errorMsg)
response = (result=nothing, success=false, errormsg=errorMsg)
return response
return (result_str=nothing, result_raw=nothing, success=false, errormsg=errorMsg)
end
end
@@ -559,7 +545,7 @@ end
- `result::String`
# Signature
"""
""" #WORKING
function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function, action::String,
llmFormatName::String
)::String
@@ -633,7 +619,7 @@ function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function,
dictkey = ["about_resulting_table", "search_summary"]
for i in 1:5
response = text2textInstructLLM(prompt, modelsize="medium")
response = text2textInstructLLM("ramdom_id", prompt)
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
think, response = GeneralUtils.extractthink(response)
@@ -653,7 +639,6 @@ function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function,
responsedict = GeneralUtils.textToDict(response, header;
dictKey=dictkey, symbolkey=false)
# result = dfstr
result =
"""
Summary: $(responsedict["search_summary"])