v0.3.0-use_openai_format #1

Merged
ton merged 6 commits from v0.3.0-use_openai_format into v0.3.0 2026-07-04 06:10:43 +00:00
7 changed files with 469 additions and 707 deletions
+20 -13
View File
@@ -2,7 +2,7 @@
julia_version = "1.12.6" julia_version = "1.12.6"
manifest_format = "2.0" manifest_format = "2.0"
project_hash = "6e0efa362e5748de9ce219e0376be2f93d464376" project_hash = "f82baf5953223c6185bd47af518fd540515402d1"
[[deps.Accessors]] [[deps.Accessors]]
deps = ["CompositionsBase", "ConstructionBase", "Dates", "InverseFunctions", "MacroTools"] deps = ["CompositionsBase", "ConstructionBase", "Dates", "InverseFunctions", "MacroTools"]
@@ -268,19 +268,24 @@ deps = ["Random"]
uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820" uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820"
version = "1.11.0" version = "1.11.0"
[[deps.Gamma]]
git-tree-sha1 = "86f86b6168a016ed88e4ae4e64577b98c3b59e8e"
uuid = "a0844989-3bd2-4988-8bea-c9407ab0941b"
version = "1.1.0"
[[deps.GeneralUtils]] [[deps.GeneralUtils]]
deps = ["CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "JSON", "NATS", "PrettyPrinting", "Random", "Revise", "SHA", "UUIDs"] deps = ["CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "HTTP", "JSON", "NATS", "PrettyPrinting", "Random", "Revise", "SHA", "UUIDs"]
git-tree-sha1 = "f1bad7621e6ac2d235adc8c593b0aff87bd1d93e" git-tree-sha1 = "7c0600c166a5deb2c607018a491c04eb25969c2e"
repo-rev = "main" repo-rev = "main"
repo-url = "https://git.yiem.cc/ton/GeneralUtils" repo-url = "https://git.yiem.cc/ton/GeneralUtils"
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
version = "0.4.2" version = "0.4.9"
[[deps.HTTP]] [[deps.HTTP]]
deps = ["Base64", "CodecZlib", "Dates", "EnumX", "PrecompileTools", "Random", "Reseau", "SHA", "URIs", "UUIDs", "Zlib_jll"] deps = ["Base64", "CodecZlib", "Dates", "EnumX", "PrecompileTools", "Random", "Reseau", "SHA", "URIs", "UUIDs", "Zlib_jll"]
git-tree-sha1 = "69343dd8afb1671b84c3aa2dda511238d0919a55" git-tree-sha1 = "eda1d37cb55d90a17d0957c75841138c88b361a1"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3" uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
version = "2.5.0" version = "2.5.4"
[[deps.HashArrayMappedTries]] [[deps.HashArrayMappedTries]]
git-tree-sha1 = "2eaa69a7cab70a52b9687c8bf950a5a93ec895ae" git-tree-sha1 = "2eaa69a7cab70a52b9687c8bf950a5a93ec895ae"
@@ -288,10 +293,10 @@ uuid = "076d061b-32b6-4027-95e0-9a2c6f6d7e74"
version = "0.2.0" version = "0.2.0"
[[deps.HypergeometricFunctions]] [[deps.HypergeometricFunctions]]
deps = ["LinearAlgebra", "OpenLibm_jll", "SpecialFunctions"] deps = ["Gamma", "LinearAlgebra"]
git-tree-sha1 = "68c173f4f449de5b438ee67ed0c9c748dc31a2ec" git-tree-sha1 = "18d7deab5fb0440dc6a7b6993c5c27b25420de10"
uuid = "34004b35-14d8-5ef3-9330-4cdb6864b03a" uuid = "34004b35-14d8-5ef3-9330-4cdb6864b03a"
version = "0.3.28" version = "0.3.29"
[[deps.ICU_jll]] [[deps.ICU_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"] deps = ["Artifacts", "JLLWrappers", "Libdl"]
@@ -401,9 +406,11 @@ version = "1.21.3+0"
[[deps.LLMMCTS]] [[deps.LLMMCTS]]
deps = ["GeneralUtils", "JSON", "PrettyPrinting"] deps = ["GeneralUtils", "JSON", "PrettyPrinting"]
path = "../LLMMCTS" git-tree-sha1 = "6b4f123b03c0fcce5b21c0dbcb947e8dd23f333a"
repo-rev = "main"
repo-url = "https://git.yiem.cc/ton/LLMMCTS"
uuid = "d76c5a4d-449e-4835-8cc4-dd86ec44f241" uuid = "d76c5a4d-449e-4835-8cc4-dd86ec44f241"
version = "0.1.4" version = "0.1.5"
[[deps.LaTeXStrings]] [[deps.LaTeXStrings]]
git-tree-sha1 = "dda21b8cbd6a6c40d9d02a73230f9d70fed6918c" git-tree-sha1 = "dda21b8cbd6a6c40d9d02a73230f9d70fed6918c"
@@ -727,9 +734,9 @@ version = "0.5.1+0"
[[deps.Roots]] [[deps.Roots]]
deps = ["Accessors", "CommonSolve", "Printf"] deps = ["Accessors", "CommonSolve", "Printf"]
git-tree-sha1 = "91cfb1cb4f6e27557cc2df798a31eff6089a41eb" git-tree-sha1 = "ed45bcc7cf3c8887595b973f2b1efbe91dcc50ec"
uuid = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" uuid = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
version = "3.0.0" version = "3.0.1"
[deps.Roots.extensions] [deps.Roots.extensions]
RootsChainRulesCoreExt = "ChainRulesCore" RootsChainRulesCoreExt = "ChainRulesCore"
+3 -2
View File
@@ -1,6 +1,6 @@
name = "SQLLLM" name = "SQLLLM"
uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3" uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3"
version = "0.2.4" version = "0.2.5"
authors = ["narawat lamaiin <narawat@outlook.com>"] authors = ["narawat lamaiin <narawat@outlook.com>"]
[deps] [deps]
@@ -24,5 +24,6 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[compat] [compat]
Dates = "1.11.0" Dates = "1.11.0"
GeneralUtils = "0.4.2" GeneralUtils = "0.4.0 - 0.9.0"
JSON = "1.6.1" JSON = "1.6.1"
LLMMCTS = "0.1.5"
+82
View File
@@ -0,0 +1,82 @@
BIN
View File
Binary file not shown.
+338 -651
View File
File diff suppressed because it is too large Load Diff
+24 -39
View File
@@ -481,20 +481,9 @@ julia> response = SQLLLM.SQLexecution(executeSQL, sql)
# Signature # Signature
""" """
function SQLexecution(executeSQL::Function, sql::T function SQLexecution(executeSQL::Function, sql::T
) where {T<:AbstractString} )::NamedTuple where {T<:AbstractString}
try 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 # add LIMIT to the SQL to prevent loading large data
sql = strip(sql) sql = strip(sql)
@@ -508,39 +497,36 @@ function SQLexecution(executeSQL::Function, sql::T
else else
sql = sql * ";" sql = sql * ";"
end end
println("\n~~~ SQLexecution() SQL: ", @__FILE__, " ", @__LINE__)
println(sql)
result = executeSQL(sql) result = executeSQL(sql)
df = DataFrame(result) df = DataFrame(result)
tablesize = size(df) tablesize = size(df)
row, column = tablesize row, column = tablesize
if row == 0 if row == 0
error("\nThe resulting table has 0 row. Please try again.") return (result_str="The resulting table has 0 row.", result_raw=df, success=true, errormsg=nothing)
elseif column > 30 elseif column > 30
error("\nSQL execution failed. An unexpected error occurred. Please try again.") 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 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 catch e
io = IOBuffer() io = IOBuffer()
showerror(io, e) showerror(io, e)
errorMsg = String(take!(io)) errorMsg = String(take!(io))
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
println(errorMsg) println(errorMsg)
response = (result=nothing, success=false, errormsg=errorMsg) return (result_str=nothing, result_raw=nothing, success=false, errormsg=errorMsg)
return response
end end
end end
@@ -559,7 +545,7 @@ end
- `result::String` - `result::String`
# Signature # Signature
""" """ #WORKING
function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function, action::String, function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function, action::String,
llmFormatName::String llmFormatName::String
)::String )::String
@@ -633,7 +619,7 @@ function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function,
dictkey = ["about_resulting_table", "search_summary"] dictkey = ["about_resulting_table", "search_summary"]
for i in 1:5 for i in 1:5
response = text2textInstructLLM(prompt, modelsize="medium") response = text2textInstructLLM("ramdom_id", prompt)
response = GeneralUtils.deFormatLLMtext(response, llmFormatName) response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
think, response = GeneralUtils.extractthink(response) think, response = GeneralUtils.extractthink(response)
@@ -653,7 +639,6 @@ function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function,
responsedict = GeneralUtils.textToDict(response, header; responsedict = GeneralUtils.textToDict(response, header;
dictKey=dictkey, symbolkey=false) dictKey=dictkey, symbolkey=false)
# result = dfstr
result = result =
""" """
Summary: $(responsedict["search_summary"]) Summary: $(responsedict["search_summary"])
@@ -821,7 +806,7 @@ julia>
# Notes # Notes
- The function makes up to 10 attempts to get a valid response from the LLM - The function makes up to 10 attempts to get a valid response from the LLM
- Each state in highValueStateList should contain a thoughtHistory with action_input and observation - Each state in highValueStateList should contain a action_history with action_input and observation
- The LLM evaluates attempts based on accuracy and relevance to the original question - The LLM evaluates attempts based on accuracy and relevance to the original question
""" """
function compareState(question::String, highValueStateList::Vector{T}, function compareState(question::String, highValueStateList::Vector{T},
@@ -866,14 +851,14 @@ potentialSolution = []
keys = ["action_input", "observation"] keys = ["action_input", "observation"]
# extract the last action_name, action_input, observation of each state in highValueStateList and store them in a dictionary then push into potentialSolution # extract the last action_name, action_input, observation of each state in highValueStateList and store them in a dictionary then push into potentialSolution
for state in highValueStateList for state in highValueStateList
thoughtHistory = state["thoughtHistory"] action_history = state["action_history"]
_, currentstate_latestIndice = _, currentstate_latestIndice =
GeneralUtils.findHighestIndexKey(thoughtHistory, keys[1]) GeneralUtils.findHighestIndexKey(action_history, keys[1])
latestKeys = makekey.(keys, currentstate_latestIndice) latestKeys = makekey.(keys, currentstate_latestIndice)
d = Dict() d = Dict()
# get the last action_name, action_input, observation of currentstate # get the last action_name, action_input, observation of currentstate
for (i,v) in enumerate(keys) for (i,v) in enumerate(keys)
d[v] = thoughtHistory[latestKeys[i]] d[v] = action_history[latestKeys[i]]
end end
push!(potentialSolution, d) push!(potentialSolution, d)
end end
+2 -2
View File
@@ -137,7 +137,7 @@ end
function insertSQLVectorDB(query::T1, SQL::T2; maxdistance::Integer=3) where {T1<:AbstractString, T2<:AbstractString} function insertSQLVectorDB(query::T1, SQL::T2; maxdistance::Integer=3) where {T1<:AbstractString, T2<:AbstractString}
tablename = "sqlllm_decision_repository" tablename = "sqlllm_decision_repository"
# get embedding of the query # get embedding of the query
# query = state[:thoughtHistory][:question] # query = state[:action_history][:question]
df = findSimilarTextFromVectorDB(query, tablename, df = findSimilarTextFromVectorDB(query, tablename,
"function_input_embedding", executeSQLVectorDB) "function_input_embedding", executeSQLVectorDB)
row, col = size(df) row, col = size(df)
@@ -352,7 +352,7 @@ SELECT * FROM wine WHERE wine_type = 'red' AND country = 'France' AND sweetness
# :evaluation => # :evaluation =>
# "The user's question is to search the database for wines that have a type of \"white\", are from \"France\", and have a sweetness level of 1. The thought is correct in identifying the conditions needed to filter the wine table. The action taken is to execute a SQL query to retrieve the desired data, which is also correct. The observation provides a search summary and two search results that match the user's question. Each result includes details about the wine such as ID, name, brand, manufacturer, region, country, type, grape variety, serving temperature, intensity, sweetness, tannin, and acidity.", # "The user's question is to search the database for wines that have a type of \"white\", are from \"France\", and have a sweetness level of 1. The thought is correct in identifying the conditions needed to filter the wine table. The action taken is to execute a SQL query to retrieve the desired data, which is also correct. The observation provides a search summary and two search results that match the user's question. Each result includes details about the wine such as ID, name, brand, manufacturer, region, country, type, grape variety, serving temperature, intensity, sweetness, tannin, and acidity.",
# :accepted_as_answer => "Yes", # :accepted_as_answer => "Yes",
# :thoughtHistory => # :action_history =>
# OrderedDict{String, Any}("question" => "Search the database for wine_type: white, country: France, sweetness: 1", "thought_1" => "The user wants to search the database for wines that have a type of \"white\", are from \"France\", and have a sweetness level of 1. To achieve this, we need to filter the wine table based on these conditions.", "action_name_1" => "GETDATA", "action_input_1" => "SELECT * FROM wine WHERE wine.wine_type = 'white' AND wine.country = 'France' AND wine.sweetness = 1;", "observation_1" => "\"Search summary: The resulting table represents wines.\\nSearch result: 1) wine_id: 5b6b6df9-d87c-4f33-8995-7249c2ecc917, wine_name: corton-charlemagne grand cru, brand: domaine des croix, manufacturer: domaine des croix, region: bourgogne, country: France, wine_type: white, grape_variety: cote de beaune blanc, serving_temperature: 11 to 13 Celsius, intensity: 4, sweetness: 1, tannin: missing, acidity: 3, fizziness: missing\\n2) wine_id: 1ad27d16-ef64-4907-acf1-40631630c143, wine_name: puligny-montrachet 1er cru 'les demoiselles', brand: amiot guy, manufacturer: amiot guy, region: bourgogne, country: France, wine_type: white, grape_variety: cote de beaune blanc, serving_temperature: 11 to 13 Celsius, intensity: 4, sweetness: 1, tannin: missing, acidity: 3, fizziness: missing\\n\\n\""), # OrderedDict{String, Any}("question" => "Search the database for wine_type: white, country: France, sweetness: 1", "thought_1" => "The user wants to search the database for wines that have a type of \"white\", are from \"France\", and have a sweetness level of 1. To achieve this, we need to filter the wine table based on these conditions.", "action_name_1" => "GETDATA", "action_input_1" => "SELECT * FROM wine WHERE wine.wine_type = 'white' AND wine.country = 'France' AND wine.sweetness = 1;", "observation_1" => "\"Search summary: The resulting table represents wines.\\nSearch result: 1) wine_id: 5b6b6df9-d87c-4f33-8995-7249c2ecc917, wine_name: corton-charlemagne grand cru, brand: domaine des croix, manufacturer: domaine des croix, region: bourgogne, country: France, wine_type: white, grape_variety: cote de beaune blanc, serving_temperature: 11 to 13 Celsius, intensity: 4, sweetness: 1, tannin: missing, acidity: 3, fizziness: missing\\n2) wine_id: 1ad27d16-ef64-4907-acf1-40631630c143, wine_name: puligny-montrachet 1er cru 'les demoiselles', brand: amiot guy, manufacturer: amiot guy, region: bourgogne, country: France, wine_type: white, grape_variety: cote de beaune blanc, serving_temperature: 11 to 13 Celsius, intensity: 4, sweetness: 1, tannin: missing, acidity: 3, fizziness: missing\\n\\n\""),
# :evaluationscore => 9, # :evaluationscore => 9,
# :select => nothing, # :select => nothing,