Compare commits
33 Commits
bf223b64b2
...
v0.2.7
| Author | SHA1 | Date | |
|---|---|---|---|
| 243e3fe10b | |||
| c594a34e4e | |||
| 2efb016646 | |||
| 216a8bdabb | |||
| cbaa480e84 | |||
| 6cb4073e29 | |||
| 42b8f5bdb1 | |||
| 35f1482228 | |||
| b4cac4f383 | |||
| 685ee7a48f | |||
| b55ae31e5b | |||
| e8e1764bb4 | |||
| e0b3ffa8e3 | |||
| ec9f44e5a1 | |||
| 6b3f8620e1 | |||
| 9cd37317d7 | |||
| 1577d7ae25 | |||
| c4e255ec2a | |||
| 681a91a0ca | |||
| c085a445e2 | |||
| bc81033924 | |||
| f5875dcb61 | |||
| 69208b2ffc | |||
| a4227ec165 | |||
| 21416f4b13 | |||
| ff4db039ab | |||
| b3537a83e0 | |||
| 0a0e36d86a | |||
| 8c5b1b6938 | |||
| aeda7e0baf | |||
| 2541223bbb | |||
| c8f5983620 | |||
| 5112701dc2 |
+387
-211
File diff suppressed because it is too large
Load Diff
+5
-2
@@ -1,7 +1,7 @@
|
|||||||
name = "SQLLLM"
|
name = "SQLLLM"
|
||||||
uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3"
|
uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3"
|
||||||
|
version = "0.2.7"
|
||||||
authors = ["narawat lamaiin <narawat@outlook.com>"]
|
authors = ["narawat lamaiin <narawat@outlook.com>"]
|
||||||
version = "0.2.4"
|
|
||||||
|
|
||||||
[deps]
|
[deps]
|
||||||
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
|
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
|
||||||
@@ -11,7 +11,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
|
|||||||
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
|
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
|
||||||
GeneralUtils = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
|
GeneralUtils = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
|
||||||
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
|
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
|
||||||
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
|
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
|
||||||
LLMMCTS = "d76c5a4d-449e-4835-8cc4-dd86ec44f241"
|
LLMMCTS = "d76c5a4d-449e-4835-8cc4-dd86ec44f241"
|
||||||
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
|
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
|
||||||
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
|
PrettyPrinting = "54e16d92-306c-5ea0-a30b-337be88ac337"
|
||||||
@@ -24,3 +24,6 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
|
|||||||
|
|
||||||
[compat]
|
[compat]
|
||||||
Dates = "1.11.0"
|
Dates = "1.11.0"
|
||||||
|
GeneralUtils = "0.4.0 - 0.9.0"
|
||||||
|
JSON = "1.6.1"
|
||||||
|
LLMMCTS = "0.1.5 - 0.9.0"
|
||||||
|
|||||||
+429
-492
File diff suppressed because it is too large
Load Diff
+150
-164
@@ -3,7 +3,7 @@ module llmfunction
|
|||||||
export listAllTable_json, listAllTable_str, tableinfo, getdata, finalAnswerBox,
|
export listAllTable_json, listAllTable_str, tableinfo, getdata, finalAnswerBox,
|
||||||
getTableNameFromSQL, extractContent_dataframe, SQLexecution, compareState
|
getTableNameFromSQL, extractContent_dataframe, SQLexecution, compareState
|
||||||
|
|
||||||
using HTTP, JSON3, URIs, Random, PrettyPrinting, UUIDs, LibPQ, Tables, DataFrames, CSV,
|
using HTTP, JSON, URIs, Random, PrettyPrinting, UUIDs, LibPQ, Tables, DataFrames, CSV,
|
||||||
DataStructures, StatsBase, Dates
|
DataStructures, StatsBase, Dates
|
||||||
using GeneralUtils, LLMMCTS
|
using GeneralUtils, LLMMCTS
|
||||||
using ..util
|
using ..util
|
||||||
@@ -189,7 +189,7 @@ function tableinfo(executeSQL::Function, tablenames::T
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if !isempty(notExistingTable)
|
if !isempty(notExistingTable)
|
||||||
result = "Error, the following tables does not exist in the database: $(JSON3.write(notExistingTable))"
|
result = "Error, the following tables does not exist in the database: $(JSON.json(notExistingTable))"
|
||||||
return (result=result,)
|
return (result=result,)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -287,17 +287,17 @@ function getdata_transition(state::T, args::NamedTuple
|
|||||||
# decisionMaker::Function = args[:decisionMaker]
|
# decisionMaker::Function = args[:decisionMaker]
|
||||||
# evaluator::Function = args[:evaluator]
|
# evaluator::Function = args[:evaluator]
|
||||||
# reflector::Function = args[:reflector]
|
# reflector::Function = args[:reflector]
|
||||||
context = args[:context]
|
context = args["context"]
|
||||||
executeSQL::Function = args[:executeSQL]
|
executeSQL::Function = args["executeSQL"]
|
||||||
text2textInstructLLM::Function = args[:text2textInstructLLM]
|
text2textInstructLLM::Function = args["text2textInstructLLM"]
|
||||||
|
|
||||||
thought, sql =
|
thought, sql =
|
||||||
if state[:code] !== nothing
|
if state["code"] !== nothing
|
||||||
result = getdata_decisionMaker(state, context, text2textInstructLLM)
|
result = getdata_decisionMaker(state, context, text2textInstructLLM)
|
||||||
result[:thought], result[:code]
|
result["thought"], result["code"]
|
||||||
else
|
else
|
||||||
nothing, state[:question]
|
nothing, state["question"]
|
||||||
end
|
end
|
||||||
|
|
||||||
# make new state
|
# make new state
|
||||||
newNodeKey = GeneralUtils.uuid4snakecase()
|
newNodeKey = GeneralUtils.uuid4snakecase()
|
||||||
@@ -314,15 +314,15 @@ function getdata_transition(state::T, args::NamedTuple
|
|||||||
isterminal=false)
|
isterminal=false)
|
||||||
end
|
end
|
||||||
println("getdata_transition() 1 ", @__FILE__, " ", @__LINE__)
|
println("getdata_transition() 1 ", @__FILE__, " ", @__LINE__)
|
||||||
newstate[:code] = sql
|
newstate["code"] = sql
|
||||||
newstate[:response] = response
|
newstate["response"] = response
|
||||||
newstate[:errorexplain] = thought
|
newstate["errorexplain"] = thought
|
||||||
newstate[:errormsg] = errormsg
|
newstate["errormsg"] = errormsg
|
||||||
newstate[:reward] = reward
|
newstate["reward"] = reward
|
||||||
newstate[:isterminal] = isterminal
|
newstate["isterminal"] = isterminal
|
||||||
if response !== nothing
|
if response !== nothing
|
||||||
extracted = extractContent_dataframe(response, context, text2textInstructLLM)
|
extracted = extractContent_dataframe(response, context, text2textInstructLLM)
|
||||||
newstate[:response] = extracted
|
newstate["response"] = extracted
|
||||||
end
|
end
|
||||||
println("getdata_transition() 2 ", @__FILE__, " ", @__LINE__)
|
println("getdata_transition() 2 ", @__FILE__, " ", @__LINE__)
|
||||||
stateevaluation = "None"
|
stateevaluation = "None"
|
||||||
@@ -347,8 +347,9 @@ end
|
|||||||
|
|
||||||
# Signature
|
# Signature
|
||||||
"""
|
"""
|
||||||
function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM::Function
|
function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM::Function,
|
||||||
)::NamedTuple{(:thought, :code, :success, :errormsg),Tuple{Union{String,Nothing},Union{String,Nothing},Bool,Union{String,Nothing}}}
|
llmFormatName::String
|
||||||
|
)::NamedTuple{(:thought, :code, :success, :errormsg),Tuple{Union{String,Nothing},Union{String,Nothing},Bool,Union{String,Nothing}}}
|
||||||
|
|
||||||
Hints = "None"
|
Hints = "None"
|
||||||
|
|
||||||
@@ -366,17 +367,14 @@ function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM:
|
|||||||
- Text information in the database is sometimes stored in lower case. If your search returns empty, try using lower case to search.
|
- Text information in the database is sometimes stored in lower case. If your search returns empty, try using lower case to search.
|
||||||
|
|
||||||
You should then respond to the user with:
|
You should then respond to the user with:
|
||||||
1) Comprehension:
|
1) Plan: Step-by-step instructions of how to complete the task.
|
||||||
- State your comprehension about the current situation.
|
|
||||||
3) Plan: Step-by-step instructions of how to complete the task.
|
|
||||||
- Focus on improving the code from the last round.
|
- Focus on improving the code from the last round.
|
||||||
- Do not create any table in the database.
|
- Do not create any table in the database.
|
||||||
4) Code:
|
2) Code:
|
||||||
- Write new improved code.
|
- Write new improved code.
|
||||||
- Do not wrap the code and no comment as it will be executed directly without any modification against the database.
|
- Do not wrap the code and no comment as it will be executed directly without any modification against the database.
|
||||||
|
|
||||||
You should only respond in format as described below and nothing more:
|
You should only respond in format as described below and nothing more:
|
||||||
Comprehension: ...
|
|
||||||
Plan:
|
Plan:
|
||||||
1) ...
|
1) ...
|
||||||
2) ...
|
2) ...
|
||||||
@@ -391,10 +389,10 @@ function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM:
|
|||||||
for attempt in 1:10
|
for attempt in 1:10
|
||||||
usermsg = """
|
usermsg = """
|
||||||
Context:
|
Context:
|
||||||
$(context[:mentionedTableInfo])
|
$(context["mentionedTableInfo"])
|
||||||
User intention: $(context[:userintention])
|
User intention: $(context["userintention"])
|
||||||
Code executed from the last round: $(state[:code])
|
Code executed from the last round: $(state["code"])
|
||||||
Execution error: $(state[:errormsg])
|
Execution error: $(state["errormsg"])
|
||||||
$noise
|
$noise
|
||||||
$note_flag
|
$note_flag
|
||||||
"""
|
"""
|
||||||
@@ -406,22 +404,23 @@ function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM:
|
|||||||
]
|
]
|
||||||
|
|
||||||
# put in model format
|
# put in model format
|
||||||
prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
|
prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
|
||||||
try
|
try
|
||||||
response = text2textInstructLLM(prompt, modelsize="medium")
|
response = text2textInstructLLM(prompt, modelsize="medium")
|
||||||
response = GeneralUtils.deFormatLLMtext(response, "granite3")
|
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
||||||
|
think, response = GeneralUtils.extractthink(response)
|
||||||
header = ["Comprehension:", "Plan:", "Code:"]
|
|
||||||
dictkey = ["comprehension", "plan", "code"]
|
header = ["Plan:", "Code:"]
|
||||||
|
dictkey = ["plan", "code"]
|
||||||
|
|
||||||
responsedict = GeneralUtils.textToDict(response, header;
|
responsedict = GeneralUtils.textToDict(response, header;
|
||||||
dictKey=dictkey, symbolkey=true)
|
dictKey=dictkey, symbolkey=false)
|
||||||
_code = responsedict[:code]
|
_code = responsedict["code"]
|
||||||
code = strip(_code)
|
code = strip(_code)
|
||||||
|
|
||||||
if length(code) < 2
|
if length(code) < 2
|
||||||
error("No code available.")
|
error("No code available.")
|
||||||
elseif code == state[:code]
|
elseif code == state["code"]
|
||||||
error("generated code is the same as earlier.")
|
error("generated code is the same as earlier.")
|
||||||
else
|
else
|
||||||
end
|
end
|
||||||
@@ -441,7 +440,7 @@ function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM:
|
|||||||
|
|
||||||
println("\n~~~ getdata_decisionMaker() ", @__FILE__, " ", @__LINE__)
|
println("\n~~~ getdata_decisionMaker() ", @__FILE__, " ", @__LINE__)
|
||||||
pprintln(Dict(responsedict))
|
pprintln(Dict(responsedict))
|
||||||
return (thought=responsedict[:comprehension], code=code, success=true, errormsg=nothing)
|
return (thought=responsedict["comprehension"], code=code, success=true, errormsg=nothing)
|
||||||
catch e
|
catch e
|
||||||
io = IOBuffer()
|
io = IOBuffer()
|
||||||
showerror(io, e)
|
showerror(io, e)
|
||||||
@@ -482,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)
|
||||||
|
|
||||||
@@ -509,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("The resulting table has 0 row. Possible causes: 1) Your search criteria might be too specific. Relaxing some conditions could yield better results. Remember, you can always refine your search later. 2) There could be a typo in your search query. 3) You might be searching in the wrong place.")
|
return (result_str="No records found.", result_raw=df, success=true, errormsg=nothing)
|
||||||
elseif column > 30
|
elseif column > 30
|
||||||
error("SQL 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
|
||||||
|
|
||||||
@@ -560,9 +545,10 @@ end
|
|||||||
- `result::String`
|
- `result::String`
|
||||||
|
|
||||||
# Signature
|
# Signature
|
||||||
"""
|
""" #PENDING
|
||||||
function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function, action::String
|
function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function, action::String,
|
||||||
)::String
|
llmFormatName::String
|
||||||
|
)::String
|
||||||
tablesize = size(df)
|
tablesize = size(df)
|
||||||
row = tablesize[1]
|
row = tablesize[1]
|
||||||
column = tablesize[2]
|
column = tablesize[2]
|
||||||
@@ -628,32 +614,34 @@ function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function,
|
|||||||
]
|
]
|
||||||
|
|
||||||
# put in model format
|
# put in model format
|
||||||
prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
|
prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
|
||||||
header = ["About_resulting_table:", "Search_summary:"]
|
header = ["About_resulting_table:", "Search_summary:"]
|
||||||
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, "granite3")
|
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
||||||
|
think, response = GeneralUtils.extractthink(response)
|
||||||
|
|
||||||
kw = []
|
# check whether response has all header
|
||||||
# use for loop and detect_keyword function to get the exact variation of each keyword in the text then push to kw list
|
detected_kw = GeneralUtils.detectKeywordVariation(header, response)
|
||||||
for keyword in header
|
missingkeys = [k for (k, v) in detected_kw if v === nothing]
|
||||||
detected = GeneralUtils.detect_keyword(keyword, response)
|
if !isempty(missingkeys)
|
||||||
push!(kw, detected)
|
errornote = "$missingkeys are missing from your previous response"
|
||||||
end
|
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||||
if nothing ∈ kw
|
continue
|
||||||
println("Some keywords are missing, Required keywords=$header, Response keywords=$kw ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
elseif sum([length(i) for i in values(detected_kw)]) > length(header)
|
||||||
continue # try again next loop
|
errornote = "\nYour previous attempt has duplicated points according to the required response format"
|
||||||
|
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||||
|
continue
|
||||||
end
|
end
|
||||||
|
|
||||||
responsedict = GeneralUtils.textToDict(response, header;
|
responsedict = GeneralUtils.textToDict(response, header;
|
||||||
dictKey=dictkey, symbolkey=true)
|
dictKey=dictkey, symbolkey=false)
|
||||||
|
|
||||||
# result = dfstr
|
|
||||||
result =
|
result =
|
||||||
"""
|
"""
|
||||||
Summary: $(responsedict[:search_summary])
|
Summary: $(responsedict["search_summary"])
|
||||||
More details: $dfstr
|
More details: $dfstr
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -736,7 +724,9 @@ julia> result = SQLLLM.getTableNameFromSQL(sql, text2textInstructLLM)
|
|||||||
|
|
||||||
# Signature
|
# Signature
|
||||||
"""
|
"""
|
||||||
function getTableNameFromSQL(sql::T, text2textInstructLLM::Function)::Vector{String} where {T<:AbstractString}
|
function getTableNameFromSQL(sql::T, text2textInstructLLM::Function,
|
||||||
|
llmFormatName::String
|
||||||
|
)::Vector{String} where {T<:AbstractString}
|
||||||
systemmsg = """
|
systemmsg = """
|
||||||
Extract table name out of the user query.
|
Extract table name out of the user query.
|
||||||
|
|
||||||
@@ -764,17 +754,17 @@ function getTableNameFromSQL(sql::T, text2textInstructLLM::Function)::Vector{Str
|
|||||||
]
|
]
|
||||||
|
|
||||||
# put in model format
|
# put in model format
|
||||||
prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
|
prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
|
||||||
header = ["Table_name:"]
|
header = ["Table_name:"]
|
||||||
dictkey = ["table_name"]
|
dictkey = ["table_name"]
|
||||||
|
|
||||||
for attempt in 1:5
|
for attempt in 1:5
|
||||||
try
|
try
|
||||||
response = text2textInstructLLM(prompt, modelsize="medium")
|
response = text2textInstructLLM(prompt, modelsize="medium")
|
||||||
response = GeneralUtils.deFormatLLMtext(response, "granite3")
|
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
||||||
responsedict = GeneralUtils.textToDict(response, header;
|
responsedict = GeneralUtils.textToDict(response, header;
|
||||||
dictKey=dictkey, symbolkey=true)
|
dictKey=dictkey, symbolkey=false)
|
||||||
response = copy(JSON3.read(responsedict[:table_name]))
|
response = copy(JSON.parse(responsedict["table_name"]))
|
||||||
|
|
||||||
return response
|
return response
|
||||||
catch e
|
catch e
|
||||||
@@ -816,90 +806,85 @@ 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},
|
||||||
text2textInstructLLM::Function)::Integer where {T<:AbstractDict}
|
text2textInstructLLM::Function, llmFormatName::String
|
||||||
|
)::Integer where {T<:AbstractDict}
|
||||||
|
|
||||||
systemmsg =
|
systemmsg =
|
||||||
"""
|
"""
|
||||||
<Your profile>
|
Your profile:
|
||||||
- You are a helpful assistant
|
- You are a helpful assistant
|
||||||
</Your profile>
|
Situation:
|
||||||
<Situation>
|
- The user has made multiple attempts to solve the question, resulting in various answers
|
||||||
The user has made multiple attempts to solve the question, resulting in various answers
|
Your mission:
|
||||||
<Your mission>
|
|
||||||
- Identify and select the most accurate and relevant response from these multiple results for the user
|
- Identify and select the most accurate and relevant response from these multiple results for the user
|
||||||
</Your mission>
|
At each round of conversation, you will be given the following:
|
||||||
<At each round of conversation, you will be given the following>
|
|
||||||
Question: the question the user is trying to answer
|
Question: the question the user is trying to answer
|
||||||
Attempt: the user's attempted actions and their corresponding results
|
Attempt: the user's attempted actions and their corresponding results
|
||||||
</At each round of conversation, you will be given the following>
|
You should then respond to the user with the following:
|
||||||
<You should then respond to the user with the following>
|
Comparison: detailed comparison of all results from all attempts from various aspects.
|
||||||
Comparison: a comparison of all results from all attempts
|
|
||||||
Rationale: a brief explanation of why the selected response is the most accurate and relevant
|
Rationale: a brief explanation of why the selected response is the most accurate and relevant
|
||||||
Selected_response_number: the number the selected response in the list of results (e.g., 1, 2, 3, ...)
|
Selected_response_number: the number the selected response in the list of results (e.g., 1, 2, 3, ...)
|
||||||
</You should then respond to the user with the following>
|
You should only respond in format as described below:
|
||||||
<You should only respond in format as described below>
|
|
||||||
Comparison: ...
|
Comparison: ...
|
||||||
Rationale: ...
|
Rationale: ...
|
||||||
Selected_response_number: ...
|
Selected_response_number: ...
|
||||||
</You should only respond in format as described below>
|
Here are some examples:
|
||||||
<Here are some examples>
|
User's question: "How many German wines do you have?"
|
||||||
User's question: "How many German wines do you have?"
|
Attempt 1)
|
||||||
Attempt 1:
|
Action: SELECT COUNT(*) FROM wines WHERE country = 'Germany'
|
||||||
Action: SELECT COUNT(*) FROM wines WHERE country = 'Germany'
|
Result: 100 wines
|
||||||
Result: 100 wines
|
Attempt 2)
|
||||||
Attempt 2:
|
Action: SELECT COUNT(*) FROM wines WHERE country = 'Germany' AND type = 'Red'
|
||||||
Action: SELECT COUNT(*) FROM wines WHERE country = 'Germany' AND type = 'Red'
|
Result: 50 red wines
|
||||||
Result: 50 red wines
|
Comparison: The second attempt counts only German red wines while the first attempt includes all German wines.
|
||||||
Comparison: The second attempt counts only German red wines while the first attempt includes all German wines.
|
Rationale: The user is asking for the number of German wines without specifying a type, so the most accurate response is the first attempt because it includes all German wines.
|
||||||
Rationale: The user is asking for the number of German wines without specifying a type, so the most accurate response is the first attempt because it includes all German wines.
|
Selected_response_number:1
|
||||||
Selected_response_number:1
|
|
||||||
</Here are some examples>
|
|
||||||
|
|
||||||
Let's begin!
|
Let's begin!
|
||||||
"""
|
"""
|
||||||
|
|
||||||
potentialSolution = []
|
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
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# put potential solutions from potentialSolution into the following form
|
# put potential solutions from potentialSolution into the following form
|
||||||
Attempt 1
|
Attempt 1)
|
||||||
action_name:
|
action_name:
|
||||||
action_input:
|
action_input:
|
||||||
observation:
|
observation:
|
||||||
Attempt 2
|
Attempt 2)
|
||||||
action_name:
|
action_name:`
|
||||||
action_input:
|
action_input:
|
||||||
observation:
|
observation:`
|
||||||
...
|
...
|
||||||
"""
|
"""
|
||||||
potentialSolutionStr = ""
|
potentialSolutionStr = ""
|
||||||
for (i, state) in enumerate(potentialSolution)
|
for (i, state) in enumerate(potentialSolution)
|
||||||
potentialSolutionStr *= "Attempt $i\n"
|
potentialSolutionStr *= "Attempt $i)\n"
|
||||||
for k in keys
|
for k in keys
|
||||||
potentialSolutionStr *= "$k: $(state[k])\n"
|
potentialSolutionStr *= "$k: $(state[k])\n"
|
||||||
println("")
|
println("")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
errornote = ""
|
errornote = "N/A"
|
||||||
|
|
||||||
for attempt in 1:10
|
for attempt in 1:10
|
||||||
errorFlag = false
|
errorFlag = false
|
||||||
@@ -918,7 +903,7 @@ function compareState(question::String, highValueStateList::Vector{T},
|
|||||||
]
|
]
|
||||||
|
|
||||||
# put in model format
|
# put in model format
|
||||||
prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
|
prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
|
||||||
|
|
||||||
header = ["Comparison:", "Rationale:", "Selected_response_number:"]
|
header = ["Comparison:", "Rationale:", "Selected_response_number:"]
|
||||||
dictkey = ["comparison", "rationale", "selected_response_number"]
|
dictkey = ["comparison", "rationale", "selected_response_number"]
|
||||||
@@ -928,26 +913,27 @@ function compareState(question::String, highValueStateList::Vector{T},
|
|||||||
# sometime LLM output something like **Comprehension**: which is not expected
|
# sometime LLM output something like **Comprehension**: which is not expected
|
||||||
response = replace(response, "**"=>"")
|
response = replace(response, "**"=>"")
|
||||||
response = replace(response, "***"=>"")
|
response = replace(response, "***"=>"")
|
||||||
response = GeneralUtils.deFormatLLMtext(response, "granite3")
|
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
||||||
|
think, response = GeneralUtils.extractthink(response)
|
||||||
|
|
||||||
# make sure every header is in the response
|
# check whether response has all header
|
||||||
for i in header
|
detected_kw = GeneralUtils.detectKeywordVariation(header, response)
|
||||||
detected = GeneralUtils.detect_keyword(i, response)
|
missingkeys = [k for (k, v) in detected_kw if v === nothing]
|
||||||
if detected === nothing
|
if !isempty(missingkeys)
|
||||||
errornote = "Your previous attempt didn't provide $i"
|
errornote = "$missingkeys are missing from your previous response"
|
||||||
errorFlag = true
|
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||||
end
|
continue
|
||||||
end
|
elseif sum([length(i) for i in values(detected_kw)]) > length(header)
|
||||||
if errorFlag
|
errornote = "\nYour previous attempt has duplicated points according to the required response format"
|
||||||
println("\nERROR SQLLLM compareState() Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||||
continue # skip to the next iteration
|
continue
|
||||||
end
|
end
|
||||||
|
|
||||||
responsedict = GeneralUtils.textToDict(response, header; dictKey=dictkey, symbolkey=true)
|
responsedict = GeneralUtils.textToDict(response, header; dictKey=dictkey, symbolkey=false)
|
||||||
|
|
||||||
responsedict[:selected_response_number] = responsedict[:selected_response_number][1] # some time "6\nThe trajectories are incomplete" is generated but I only need the number.
|
responsedict["selected_response_number"] = responsedict["selected_response_number"][1] # some time "6\nThe trajectories are incomplete" is generated but I only need the number.
|
||||||
try
|
try
|
||||||
responsedict[:selected_response_number] = parse(Int, responsedict[:selected_response_number]) # convert string "5" into integer 5
|
responsedict["selected_response_number"] = parse(Int, responsedict["selected_response_number"]) # convert string "5" into integer 5
|
||||||
catch
|
catch
|
||||||
errornote = "In your previous attempt, Selected_response_number was not a number. It must be a number."
|
errornote = "In your previous attempt, Selected_response_number was not a number. It must be a number."
|
||||||
println("\nERROR SQLLLM compareState() Attempt $attempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
println("\nERROR SQLLLM compareState() Attempt $attempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||||
@@ -957,7 +943,7 @@ function compareState(question::String, highValueStateList::Vector{T},
|
|||||||
println("\n~~~ compareState() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
println("\n~~~ compareState() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||||
pprintln(Dict(responsedict))
|
pprintln(Dict(responsedict))
|
||||||
|
|
||||||
return responsedict[:selected_response_number]
|
return responsedict["selected_response_number"]
|
||||||
end
|
end
|
||||||
error("compareState() failed to generate an evaluation, Response: \n$response\n<|End of error|>", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
error("compareState() failed to generate an evaluation, Response: \n$response\n<|End of error|>", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||||
end
|
end
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ module util
|
|||||||
|
|
||||||
export makekey
|
export makekey
|
||||||
|
|
||||||
makekey(key, indice) = Symbol("$(key)_$indice")
|
makekey(key, indice) = "$(key)_$indice"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+53
-46
@@ -1,53 +1,60 @@
|
|||||||
"""
|
"""
|
||||||
Default system message template:
|
# -------------------------------- Default system message template ------------------------------- #
|
||||||
|
|
||||||
<Your role>
|
<Your role>
|
||||||
- You are a helpful assistant
|
- You are a helpful assistant
|
||||||
</Your role>
|
</Your role>
|
||||||
<Situation>
|
<Situation>
|
||||||
- Describe the current situation
|
- Describe the current situation
|
||||||
</Situation>
|
Ex. The world use enormous energy from non-sustainable sources. This leads to climate change.
|
||||||
<Your vision>
|
</Situation>
|
||||||
- state your vision of how the situation will evolve, what would you want the situation to evolve into
|
<Your vision>
|
||||||
</Your vision>
|
- state your vision of how the situation will evolve, what would you want the situation to evolve into
|
||||||
<Your mission>
|
Ex. To be the leading innovator in sustainable technology by 2030, transforming global energy systems.
|
||||||
- state the goal
|
</Your vision>
|
||||||
</Your mission>
|
<Your mission>
|
||||||
<Your mission's objective includes>
|
- state the goal
|
||||||
- Break the goal into smaller steps
|
Ex. Empowering communities through clean energy solutions to create a sustainable future.
|
||||||
</Your mission's objective includes>
|
</Your mission>
|
||||||
<Your responsibility includes>
|
<Your mission's objective includes>
|
||||||
- state the mini goals that fall under your responsibility
|
- Specific, measurable, and time-bound goals that directly support the mission.
|
||||||
</Your responsibility includes>
|
Ex. Launch 50 solar-powered water purification systems in 3 regions by 2025.
|
||||||
<Your responsibility does NOT includes>
|
</Your mission's objective includes>
|
||||||
|
<Your responsibility includes>
|
||||||
|
- state the mini goals that fall under your responsibility
|
||||||
|
</Your responsibility includes>
|
||||||
|
<Your responsibility does NOT includes>
|
||||||
-
|
-
|
||||||
</Your responsibility does NOT includes>
|
</Your responsibility does NOT includes>
|
||||||
<At each round of conversation, you will be given the following information>
|
<At each round of conversation, you will be given the following information>
|
||||||
|
-
|
||||||
|
</At each round of conversation, you will be given the following information>
|
||||||
|
<You must follow the following policy>
|
||||||
|
-
|
||||||
|
</You must follow the following policy>
|
||||||
|
<You should follow the following guidelines>
|
||||||
|
-
|
||||||
|
</You should follow the following guidelines>
|
||||||
|
<You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
|
||||||
|
Comprehension: State your comprehension about the current situation.
|
||||||
|
Plan: Given the current circumstances, outline a detailed, step-by-step plan to accomplish the task. Be specific.
|
||||||
|
Action_name: (Typically corresponds to the execution of the first step in your plan) Can be one of the following function names:
|
||||||
|
- CHATBOX which you can use to talk with the user. The input is your intentions for the dialogue. Be specific.
|
||||||
|
- CHECKRESOURCES which you can use to check resources
|
||||||
|
- IMPLEMENT which you can use to implement the solution
|
||||||
|
Action_input: Detail the input for the action.
|
||||||
|
</You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
|
||||||
|
<You should only respond in format as described below>
|
||||||
|
Comprehension: ...
|
||||||
|
Plan: ...
|
||||||
|
Action_name: ...
|
||||||
|
Action_input: ...
|
||||||
|
</You should only respond in format as described below>
|
||||||
|
<Here are some examples>
|
||||||
|
|
||||||
</At each round of conversation, you will be given the following information>
|
</Here are some examples>
|
||||||
<You must follow the following guidelines>
|
|
||||||
-
|
|
||||||
</You must follow the following guidelines>
|
|
||||||
<You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
|
|
||||||
Comprehension: State your comprehension about the current situation.
|
|
||||||
Plan: Given the current circumstances, outline a detailed, step-by-step plan to accomplish the task. Be specific.
|
|
||||||
Action_name: (Typically corresponds to the execution of the first step in your plan) Can be one of the following function names:
|
|
||||||
- CHATBOX which you can use to talk with the user. The input is your intentions for the dialogue. Be specific.
|
|
||||||
- CHECKRESOURCES which you can use to check resources
|
|
||||||
- IMPLEMENT which you can use to implement the solution
|
|
||||||
Action_input: Detail the input for the action.
|
|
||||||
</You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
|
|
||||||
<You should only respond in format as described below>
|
|
||||||
Comprehension: ...
|
|
||||||
Plan: ...
|
|
||||||
Action_name: ...
|
|
||||||
Action_input: ...
|
|
||||||
</You should only respond in format as described below>
|
|
||||||
<Here are some examples>
|
|
||||||
|
|
||||||
</Here are some examples>
|
Let's begin!
|
||||||
|
|
||||||
Let's begin!
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -57,7 +64,7 @@ Default system message template:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
Example:
|
# ------------------------------------------- Example: ------------------------------------------- #
|
||||||
|
|
||||||
<Your profile>
|
<Your profile>
|
||||||
- You are a founder of a tech startup
|
- You are a founder of a tech startup
|
||||||
|
|||||||
+81
-20
@@ -1,9 +1,9 @@
|
|||||||
using Revise
|
using Revise
|
||||||
using LibPQ, Dates, JSON3, PrettyPrinting, UUIDs, DataFrames, DataStructures, Base64
|
using LibPQ, Dates, JSON, PrettyPrinting, UUIDs, DataFrames, DataStructures, Base64
|
||||||
using GeneralUtils, SQLLLM
|
using GeneralUtils, SQLLLM
|
||||||
|
|
||||||
|
|
||||||
config = copy(JSON3.read("/appfolder/mountvolume/appdata/config.json"))
|
config = JSON.parse("/appfolder/app/dev/YiemAgent/test/config.json")
|
||||||
|
|
||||||
function executeSQL(sql::T) where {T<:AbstractString}
|
function executeSQL(sql::T) where {T<:AbstractString}
|
||||||
host = config[:externalservice][:wineDB][:host]
|
host = config[:externalservice][:wineDB][:host]
|
||||||
@@ -29,13 +29,19 @@ function executeSQLVectorDB(sql)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
function text2textInstructLLM(prompt::String; maxattempt=3)
|
function text2textInstructLLM(prompt::String; maxattempt::Integer=3, modelsize::String="medium",
|
||||||
|
senderId=GeneralUtils.uuid4snakecase(), timeout=180,
|
||||||
|
llmkwargs=Dict(
|
||||||
|
:num_ctx => 32768,
|
||||||
|
:temperature => 0.5,
|
||||||
|
)
|
||||||
|
)
|
||||||
msgMeta = GeneralUtils.generate_msgMeta(
|
msgMeta = GeneralUtils.generate_msgMeta(
|
||||||
config[:externalservice][:loadbalancer][:mqtttopic];
|
config[:externalservice][:loadbalancer][:mqtttopic];
|
||||||
msgPurpose="inference",
|
msgPurpose="inference",
|
||||||
senderName="yiemagent",
|
senderName="yiemagent",
|
||||||
senderId=sessionId,
|
senderId=senderId,
|
||||||
receiverName="text2textinstruct_small",
|
receiverName="text2textinstruct_$modelsize",
|
||||||
mqttBrokerAddress=config[:mqttServerInfo][:broker],
|
mqttBrokerAddress=config[:mqttServerInfo][:broker],
|
||||||
mqttBrokerPort=config[:mqttServerInfo][:port],
|
mqttBrokerPort=config[:mqttServerInfo][:port],
|
||||||
)
|
)
|
||||||
@@ -44,16 +50,13 @@ function text2textInstructLLM(prompt::String; maxattempt=3)
|
|||||||
:msgMeta => msgMeta,
|
:msgMeta => msgMeta,
|
||||||
:payload => Dict(
|
:payload => Dict(
|
||||||
:text => prompt,
|
:text => prompt,
|
||||||
:kwargs => Dict(
|
:kwargs => llmkwargs
|
||||||
:num_ctx => 16384,
|
|
||||||
:temperature => 0.2,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
response = nothing
|
response = nothing
|
||||||
for attempts in 1:maxattempt
|
for attempts in 1:maxattempt
|
||||||
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=300, maxattempt=2)
|
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=timeout, maxattempt=maxattempt)
|
||||||
payload = _response[:response]
|
payload = _response[:response]
|
||||||
if _response[:success] && payload[:text] !== nothing
|
if _response[:success] && payload[:text] !== nothing
|
||||||
response = _response[:response][:text]
|
response = _response[:response][:text]
|
||||||
@@ -76,7 +79,7 @@ function getEmbedding(text::T) where {T<:AbstractString}
|
|||||||
msgPurpose="embedding",
|
msgPurpose="embedding",
|
||||||
senderName="yiemagent",
|
senderName="yiemagent",
|
||||||
senderId=sessionId,
|
senderId=sessionId,
|
||||||
receiverName="text2textinstruct_small",
|
receiverName="textembedding",
|
||||||
mqttBrokerAddress=config[:mqttServerInfo][:broker],
|
mqttBrokerAddress=config[:mqttServerInfo][:broker],
|
||||||
mqttBrokerPort=config[:mqttServerInfo][:port],
|
mqttBrokerPort=config[:mqttServerInfo][:port],
|
||||||
)
|
)
|
||||||
@@ -87,7 +90,8 @@ function getEmbedding(text::T) where {T<:AbstractString}
|
|||||||
:text => [text] # must be a vector of string
|
:text => [text] # must be a vector of string
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)
|
|
||||||
|
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120, maxattempt=3)
|
||||||
embedding = response[:response][:embeddings]
|
embedding = response[:response][:embeddings]
|
||||||
return embedding
|
return embedding
|
||||||
end
|
end
|
||||||
@@ -108,7 +112,6 @@ function findSimilarTextFromVectorDB(text::T1, tablename::T2, embeddingColumnNam
|
|||||||
return df
|
return df
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function similarSQLVectorDB(query; maxdistance::Integer=100)
|
function similarSQLVectorDB(query; maxdistance::Integer=100)
|
||||||
tablename = "sqlllm_decision_repository"
|
tablename = "sqlllm_decision_repository"
|
||||||
# get embedding of the query
|
# get embedding of the query
|
||||||
@@ -131,11 +134,10 @@ function similarSQLVectorDB(query; maxdistance::Integer=100)
|
|||||||
end
|
end
|
||||||
end
|
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)
|
||||||
@@ -155,18 +157,77 @@ function insertSQLVectorDB(query::T1, SQL::T2; maxdistance::Integer=3) where {T1
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
sessionId = "555"
|
|
||||||
|
function similarSommelierDecision(recentevents::T1; maxdistance::Integer=3
|
||||||
|
)::Union{AbstractDict, Nothing} where {T1<:AbstractString}
|
||||||
|
tablename = "sommelier_decision_repository"
|
||||||
|
# find similar
|
||||||
|
println("\n~~~ search vectorDB for this: $recentevents ", @__FILE__, " ", @__LINE__)
|
||||||
|
df = findSimilarTextFromVectorDB(recentevents, tablename,
|
||||||
|
"function_input_embedding", executeSQLVectorDB)
|
||||||
|
row, col = size(df)
|
||||||
|
distance = row == 0 ? Inf : df[1, :distance]
|
||||||
|
if row != 0 && distance < maxdistance
|
||||||
|
# if there is usable decision, return it.
|
||||||
|
rowid = df[1, :id]
|
||||||
|
println("\n~~~ found similar decision. row id $rowid, distance $distance ", @__FILE__, " ", @__LINE__)
|
||||||
|
output_b64 = df[1, :function_output_base64] # pick the closest match
|
||||||
|
_output_str = String(base64decode(output_b64))
|
||||||
|
output = copy(JSON.parse(_output_str))
|
||||||
|
return output
|
||||||
|
else
|
||||||
|
println("\n~~~ similar decision not found, max distance $maxdistance ", @__FILE__, " ", @__LINE__)
|
||||||
|
return nothing
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function insertSommelierDecision(recentevents::T1, decision::T2; maxdistance::Integer=5
|
||||||
|
) where {T1<:AbstractString, T2<:AbstractDict}
|
||||||
|
tablename = "sommelier_decision_repository"
|
||||||
|
# find similar
|
||||||
|
df = findSimilarTextFromVectorDB(recentevents, tablename,
|
||||||
|
"function_input_embedding", executeSQLVectorDB)
|
||||||
|
row, col = size(df)
|
||||||
|
distance = row == 0 ? Inf : df[1, :distance]
|
||||||
|
if row == 0 || distance > maxdistance # no close enough SQL stored in the database
|
||||||
|
recentevents_embedding = getEmbedding(recentevents)[1]
|
||||||
|
recentevents = replace(recentevents, "'" => "")
|
||||||
|
decision_json = JSON.json(decision)
|
||||||
|
decision_base64 = base64encode(decision_json)
|
||||||
|
decision = replace(decision_json, "'" => "")
|
||||||
|
|
||||||
|
sql = """
|
||||||
|
INSERT INTO $tablename (function_input, function_output, function_output_base64, function_input_embedding) VALUES ('$recentevents', '$decision', '$decision_base64', '$recentevents_embedding');
|
||||||
|
"""
|
||||||
|
println("\n~~~ added new decision to vectorDB ", @__FILE__, " ", @__LINE__)
|
||||||
|
println(sql)
|
||||||
|
_ = executeSQLVectorDB(sql)
|
||||||
|
else
|
||||||
|
println("~~~ similar decision previously cached, distance $distance ", @__FILE__, " ", @__LINE__)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
sessionId = GeneralUtils.uuid4snakecase()
|
||||||
|
d = Dict(:id => sessionId)
|
||||||
|
filepath = "/appfolder/app/sessionid.json"
|
||||||
|
open(filepath, "w") do io
|
||||||
|
JSON.pretty(io, d)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# query = "How many German wines do you have?"
|
# query = "How many German wines do you have?"
|
||||||
# highValueStateList = copy(JSON3.read("/appfolder/app/highValueState_1.json"))
|
# highValueStateList = copy(JSON.parse("/appfolder/app/highValueState_1.json"))
|
||||||
# selectedState = SQLLLM.compareState(query, highValueStateList, text2textInstructLLM)
|
# selectedState = SQLLLM.compareState(query, highValueStateList, text2textInstructLLM)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# query = Dict(:text=> "How many wines from France do you have that can be paired with lamb?")
|
# query = Dict(:text=> "How many wines from France do you have that can be paired with lamb?")
|
||||||
query = "How many French wines from Yiem store under 100 dollars do you have?"
|
# query = "How many French wines from Yiem store under 100 dollars do you have?"
|
||||||
# query = "retailer: Yiem, wine_type: red, sweetness: 1-2, intensity: 4-5, wine price: 20-40"
|
# query = "retailer: Yiem, wine_type: red, sweetness: 1-2, intensity: 4-5, wine price: 20-40"
|
||||||
|
query = "from Yiem retailer, red wine from France. price 100 to 1000 USD. sweetness: 1-2, intensity: 4-5"
|
||||||
# query = "wine_type: white, country: United States, sweetness: 1-2, tannin: 3, food to be served with wine: pizza"
|
# query = "wine_type: white, country: United States, sweetness: 1-2, tannin: 3, food to be served with wine: pizza"
|
||||||
# query = "wine_type: white, country: Austria, food to be served with wine: pork"
|
# query = "wine_type: white, country: Austria, food to be served with wine: pork"
|
||||||
# query = "wine price: less than 25, wine_type: rose, country: France, sweetness: 2, tannin: 3, food to be served with wine: pizza"
|
# query = "wine price: less than 25, wine_type: rose, country: France, sweetness: 2, tannin: 3, food to be served with wine: pizza"
|
||||||
@@ -291,8 +352,8 @@ 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{Symbol, 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,
|
||||||
# :suggestion => "None")
|
# :suggestion => "None")
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
using Revise
|
using Revise
|
||||||
using LibPQ, JSON3, PrettyPrinting, UUIDs, DataFrames, DataStructures, Dates, MQTTClient, Random
|
using LibPQ, JSON, PrettyPrinting, UUIDs, DataFrames, DataStructures, Dates, MQTTClient, Random
|
||||||
using SQLLLM, GeneralUtils
|
using SQLLLM, GeneralUtils
|
||||||
|
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ df = DataFrame(result)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
config = copy(JSON3.read("config.json"))
|
config = copy(JSON.parse("config.json"))
|
||||||
|
|
||||||
msgMeta = GeneralUtils.generate_msgMeta(
|
msgMeta = GeneralUtils.generate_msgMeta(
|
||||||
config[:externalservice][:text2textinstruct][:mqtttopic];
|
config[:externalservice][:text2textinstruct][:mqtttopic];
|
||||||
|
|||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
# using Revise
|
# using Revise
|
||||||
# using SQLLLM, LLMMCTS, DataStructures, JSON3
|
# using SQLLLM, LLMMCTS, DataStructures, JSON
|
||||||
|
|
||||||
# query = "How many German wines do you have?"
|
# query = "How many German wines do you have?"
|
||||||
# highValueStateList = copy(JSON3.read("/appfolder/app/highValueState_1.json"))
|
# highValueStateList = copy(JSON.parse("/appfolder/app/highValueState_1.json"))
|
||||||
# selectedState = SQLLLM.compareState(query, highValueStateList)
|
# selectedState = SQLLLM.compareState(query, highValueStateList)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user