update
This commit is contained in:
+186
-190
@@ -637,45 +637,47 @@ function predefined_wine_search_sql(a::T, searchterm::String,
|
||||
# your responsibility includes
|
||||
Fulfill the objective.
|
||||
|
||||
# you should only respond in JSON format as described below
|
||||
{
|
||||
table_name_1:
|
||||
column_name_1:
|
||||
operator: "="
|
||||
value: "..."
|
||||
column_name_2:
|
||||
operator: "="
|
||||
value: "..."
|
||||
...
|
||||
table_name_2:
|
||||
column_name_1:
|
||||
operator: "="
|
||||
value: "..."
|
||||
column_name_2:
|
||||
operator: "="
|
||||
value: "..."
|
||||
...
|
||||
}
|
||||
# You must output your response as a JSON object containing a single key: "extracted_info".
|
||||
The "extracted_info" key must contain an array of objects. Each object must contain:
|
||||
1) "table_name": The name of the table.
|
||||
2) "column_name": The specific column being filtered.
|
||||
3) "operator": The comparison operator (e.g., "=", ">", "LIKE").
|
||||
4) "value": The value to compare against.
|
||||
|
||||
If the user does not specify any filters, return an empty array for "extracted_info": {"extracted_info": []}.
|
||||
|
||||
# here are some example
|
||||
<user>
|
||||
4-wheel drive car with red color that will give me fast and furious emotion. No more than 7000 USD
|
||||
</user>
|
||||
<assistant>
|
||||
car_info: # table_name
|
||||
drive_type: # column_name
|
||||
operator: "=" # operator is not "N/A" because drive_type column store quantitative value
|
||||
value: "4-wheel" # column_value
|
||||
color:
|
||||
operator: "=" # operator is not "N/A" because color column store quantitative value
|
||||
value: "red"
|
||||
drive_feeling:
|
||||
operator: "N/A" # operator is "N/A" because drive_feeling column store qualitative value
|
||||
value: "fast and furious"
|
||||
price_list:
|
||||
price:
|
||||
operator: "<" # operator is not "N/A" because drive_type column store quantitative value
|
||||
value: "7000"
|
||||
{
|
||||
"extracted_info": [
|
||||
{
|
||||
"table_name": "car_info",
|
||||
"column_name": "drive_type",
|
||||
"operator": "=",
|
||||
"value": "4-wheel"
|
||||
},
|
||||
{
|
||||
"table_name": "car_info",
|
||||
"column_name": "color",
|
||||
"operator": "=",
|
||||
"value": "red"
|
||||
},
|
||||
{
|
||||
"table_name": "car_info",
|
||||
"column_name": "drive_feeling",
|
||||
"operator": "ILIKE",
|
||||
"value": "fast and furious"
|
||||
},
|
||||
{
|
||||
"table_name": "price_list",
|
||||
"column_name": "price",
|
||||
"operator": "<",
|
||||
"value": "7000"
|
||||
}
|
||||
}
|
||||
</assistant>
|
||||
"""
|
||||
|
||||
@@ -702,23 +704,47 @@ function predefined_wine_search_sql(a::T, searchterm::String,
|
||||
"""
|
||||
input = context * searchterm
|
||||
|
||||
json_schema = Dict(
|
||||
"type"=> "json_schema",
|
||||
"json_schema"=> Dict(
|
||||
"name"=> "user_profile",
|
||||
"strict"=> true,
|
||||
"schema"=> Dict(
|
||||
"type"=> "object",
|
||||
"properties"=> Dict(
|
||||
"plan"=> Dict("type"=> "string"),
|
||||
"action_name"=> Dict("type"=> "string"),
|
||||
"action_input"=> Dict("type"=> "string"),
|
||||
),
|
||||
"required"=> ["plan", "action_name", "action_input"],
|
||||
"additionalProperties"=> false
|
||||
response_format = Dict(
|
||||
"type" => "json_schema",
|
||||
"json_schema" => Dict(
|
||||
"name" => "extracted_conditions",
|
||||
"strict" => true,
|
||||
"schema" => Dict(
|
||||
"type" => "object",
|
||||
"properties" => Dict(
|
||||
"extracted_info" => Dict(
|
||||
"type" => "array",
|
||||
"items" => Dict(
|
||||
"type" => "object",
|
||||
"properties" => Dict(
|
||||
"table_name" => Dict(
|
||||
"type" => "string",
|
||||
"description" => "The name of the database table."
|
||||
),
|
||||
"column_name" => Dict(
|
||||
"type" => "string",
|
||||
"description" => "The name of the column to filter on."
|
||||
),
|
||||
"operator" => Dict(
|
||||
"type" => "string",
|
||||
"enum" => ["=", "!=", ">", "<", ">=", "<=", "LIKE", "IN", "IS NULL", "IS NOT NULL"],
|
||||
"description" => "The SQL comparison operator."
|
||||
),
|
||||
"value" => Dict(
|
||||
"type" => ["string", "null"],
|
||||
"description" => "The value to compare against. Use null for IS NULL/IS NOT NULL."
|
||||
)
|
||||
),
|
||||
"required" => ["table_name", "column_name", "operator", "value"],
|
||||
"additionalProperties" => false
|
||||
)
|
||||
)
|
||||
),
|
||||
"required" => ["extracted_info"],
|
||||
"additionalProperties" => false
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
msg = Dict(
|
||||
"model" => "gemma-4-E4B-it-UD-Q4_K_XL",
|
||||
@@ -736,85 +762,63 @@ function predefined_wine_search_sql(a::T, searchterm::String,
|
||||
]
|
||||
),
|
||||
],
|
||||
"temperature" => 0.7
|
||||
"temperature" => 0.7,
|
||||
"response_format"=> response_format,
|
||||
)
|
||||
|
||||
for attempt in 1:maxattempt
|
||||
response = a.context.text2textInstructLLM("random_id", msg)
|
||||
|
||||
responsedict = nothing
|
||||
try
|
||||
responsedict = Serde.parse_yaml(response)
|
||||
catch e
|
||||
println("\nERROR YiemAgent predefined_wine_search_sql() Error: $e --(not qualify response)-> $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())\n")
|
||||
continue
|
||||
end
|
||||
responsedict = JSON.parse(response)
|
||||
# responsedict = nothing
|
||||
# try
|
||||
# responsedict = Serde.parse_yaml(response)
|
||||
# catch e
|
||||
# println("\nERROR YiemAgent predefined_wine_search_sql() Error: $e --(not qualify response)-> $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())\n")
|
||||
# continue
|
||||
# end
|
||||
|
||||
# println("\n ", table_schema)
|
||||
println("\n ", responsedict)
|
||||
@info "before BM25 " @__LINE__
|
||||
@info "before BM25 " @__LINE__
|
||||
|
||||
"""
|
||||
responsedict = Dict(
|
||||
"wine" => Dict(
|
||||
"tasting_notes" => Dict(
|
||||
"operator" => "N/A",
|
||||
"value" => "casual dinner"
|
||||
),
|
||||
"wine_type" => Dict(
|
||||
"operator" => "=",
|
||||
"value" => "red"
|
||||
)
|
||||
),
|
||||
"retailer_wine" => Dict(
|
||||
"currency" => Dict(
|
||||
"operator" => "=", "value" => "USD"
|
||||
),
|
||||
"price" => Dict(
|
||||
"operator" => "<", "value" => "1000"
|
||||
)
|
||||
)
|
||||
)
|
||||
"""
|
||||
|
||||
for (table_name, table_info_dict) in responsedict
|
||||
for (column_name, v) in table_info_dict
|
||||
bucket = classify_column(a.context.pg_conn_str, table_name, column_name)
|
||||
|
||||
if bucket == "fuzzy_correction"
|
||||
words_catalog = GeneralUtils.harvest_entity_catalog(a.context.pg_conn_str, table_name, column_name)
|
||||
resolved_word = GeneralUtils.resolve_entity(v["value"], words_catalog; threshold=0.9)
|
||||
table_info_dict[column_name]["value"] = resolved_word
|
||||
end
|
||||
#WORKING to ensure user input is correct
|
||||
for entry in responsedict["extracted_info"]
|
||||
table_name = entry["table_name"]::String
|
||||
column_name = entry["column_name"]::String
|
||||
|
||||
bucket = classify_column(a.context.pg_conn_str, table_name, column_name)
|
||||
|
||||
if bucket == "fuzzy_correction"
|
||||
words_catalog = GeneralUtils.harvest_entity_catalog(a.context.pg_conn_str, table_name, column_name)
|
||||
resolved_word = GeneralUtils.resolve_entity(entry["value"], words_catalog; threshold=0.9)
|
||||
entry["value"] = resolved_word
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# filter for column that will be used for hard condition (SQL where clause)
|
||||
# column with "N/A" operator will be used in vector search
|
||||
# column with non-standard operator will be used in vector search
|
||||
vector_search_words = ""
|
||||
for (table_name, table_dict) in responsedict
|
||||
for (column_name, column_dict) in table_dict
|
||||
if column_dict["operator"] ∉ ["=","<>","!=",">","<",">=","<=","!<","!>","<=>"]
|
||||
vector_search_words = vector_search_words * column_dict["value"] * ", "
|
||||
delete!(table_dict, column_name)
|
||||
hard_operators = ["=","<>","!=",">","<",">=","<=","!<","!>","<=>"]
|
||||
|
||||
# remove table from responsedict if there is no column to used
|
||||
if length(responsedict[table_name]) == 0
|
||||
delete!(responsedict, table_name)
|
||||
end
|
||||
end
|
||||
# Build new list of hard condition entries
|
||||
hard_conditions = JSON.Object{String, Any}[]
|
||||
for entry in responsedict["extracted_info"]
|
||||
if entry["operator"] ∈ hard_operators
|
||||
push!(hard_conditions, entry)
|
||||
else
|
||||
vector_search_words = vector_search_words * entry["value"] * ", "
|
||||
end
|
||||
end
|
||||
responsedict = hard_conditions
|
||||
|
||||
println("")
|
||||
pprintln(responsedict)
|
||||
@show responsedict
|
||||
@info "predefined_wine_search_sql() " @__LINE__
|
||||
|
||||
#WORKING do vector searched
|
||||
#WORKING do vector search
|
||||
println("")
|
||||
@show vector_search_words
|
||||
|
||||
# error(9999)
|
||||
sql = predefined_wine_search_sql(responsedict)
|
||||
|
||||
return sql
|
||||
@@ -822,6 +826,83 @@ function predefined_wine_search_sql(a::T, searchterm::String,
|
||||
error("SQLLLM DecisionMaker() failed to generate a thought \n", response)
|
||||
end
|
||||
|
||||
function predefined_wine_search_sql(conditions::Vector{JSON.Object{String, Any}})::String
|
||||
# 1. Base SQL structure
|
||||
base_query =
|
||||
"""
|
||||
SELECT
|
||||
w.winery,
|
||||
w.wine_name,
|
||||
w.wine_id,
|
||||
w.vintage,
|
||||
w.region,
|
||||
w.country,
|
||||
w.wine_type,
|
||||
w.grape,
|
||||
w.serving_temperature,
|
||||
w.sweetness,
|
||||
w.intensity,
|
||||
w.tannin,
|
||||
w.acidity,
|
||||
w.tasting_notes,
|
||||
rw.price,
|
||||
rw.currency,
|
||||
w.image_url,
|
||||
r.retailer_name,
|
||||
rw.retailer_id
|
||||
FROM wine AS w
|
||||
JOIN retailer_wine AS rw ON w.wine_id = rw.wine_id
|
||||
JOIN retailer AS r ON rw.retailer_id = r.retailer_id
|
||||
"""
|
||||
|
||||
# 2. Dynamic WHERE Clause Builder
|
||||
where_clauses = String[]
|
||||
|
||||
# Iterate over each condition object in the array
|
||||
for cond in conditions
|
||||
table_name = String(cond["table_name"])
|
||||
column_name = String(cond["column_name"])
|
||||
op = String(cond["operator"])
|
||||
raw_val = cond["value"]
|
||||
|
||||
# Determine table alias
|
||||
alias = if table_name == "wine"
|
||||
"w"
|
||||
elseif table_name == "retailer_wine"
|
||||
"rw"
|
||||
else
|
||||
continue
|
||||
end
|
||||
|
||||
# --- Value Type Handling ---
|
||||
final_val = raw_val
|
||||
|
||||
if op in ("=", "<", ">", "<=", ">=")
|
||||
str_val = string(raw_val)
|
||||
num_val = tryparse(Float64, str_val)
|
||||
|
||||
if !isnothing(num_val)
|
||||
final_val = isinteger(num_val) ? round(Int, num_val) : num_val
|
||||
end
|
||||
end
|
||||
|
||||
# --- SQL Formatting ---
|
||||
if isa(final_val, Number)
|
||||
clause = "$(alias).$(column_name) $(op) $(final_val)"
|
||||
else
|
||||
escaped_val = replace(string(final_val), "'" => "''")
|
||||
clause = "$(alias).$(column_name) $(op) '$(escaped_val)'"
|
||||
end
|
||||
|
||||
push!(where_clauses, clause)
|
||||
end
|
||||
|
||||
# 3. Assemble Final Query
|
||||
where_sql = isempty(where_clauses) ? "" : "WHERE " * join(where_clauses, " AND ")
|
||||
|
||||
return string(base_query, where_sql, ";")
|
||||
end
|
||||
|
||||
function SQLexecution(executeSQL::Function, sql::T
|
||||
)::NamedTuple where {T<:AbstractString}
|
||||
|
||||
@@ -1306,92 +1387,7 @@ function extractWineAttributes_2(a::T1, input::T2)::String where {T1<:agent, T2<
|
||||
error("extractWineAttributes_2() failed to get a response")
|
||||
end
|
||||
|
||||
function predefined_wine_search_sql(conditions::Dict{String, Any})::String
|
||||
# 1. Base SQL structure
|
||||
base_query =
|
||||
"""
|
||||
SELECT
|
||||
w.winery,
|
||||
w.wine_name,
|
||||
w.wine_id,
|
||||
w.vintage,
|
||||
w.region,
|
||||
w.country,
|
||||
w.wine_type,
|
||||
w.grape,
|
||||
w.serving_temperature,
|
||||
w.sweetness,
|
||||
w.intensity,
|
||||
w.tannin,
|
||||
w.acidity,
|
||||
w.tasting_notes,
|
||||
rw.price,
|
||||
rw.currency,
|
||||
w.image_url,
|
||||
r.retailer_name,
|
||||
rw.retailer_id
|
||||
FROM wine AS w
|
||||
JOIN retailer_wine AS rw ON w.wine_id = rw.wine_id
|
||||
JOIN retailer AS r ON rw.retailer_id = r.retailer_id
|
||||
"""
|
||||
|
||||
# 2. Dynamic WHERE Clause Builder
|
||||
where_clauses = String[]
|
||||
|
||||
# Iterate over each table condition provided
|
||||
for (table_name, table_conditions) in conditions
|
||||
|
||||
# Determine table alias
|
||||
alias = if table_name == "wine"
|
||||
"w"
|
||||
elseif table_name == "retailer_wine"
|
||||
"rw"
|
||||
else
|
||||
continue # Skip unsupported tables
|
||||
end
|
||||
|
||||
# Process condition dictionaries
|
||||
if isa(table_conditions, Dict) && !isempty(table_conditions)
|
||||
|
||||
for (column_name, filter_details) in table_conditions
|
||||
|
||||
if isa(filter_details, Dict) && haskey(filter_details, "operator")
|
||||
op = filter_details["operator"]
|
||||
raw_val = filter_details["value"]
|
||||
|
||||
# --- Value Type Handling ---
|
||||
# Use tryparse instead of try/catch for cleaner, faster parsing
|
||||
final_val = raw_val
|
||||
|
||||
if op in ("=", "<", ">", "<=", ">=")
|
||||
str_val = string(raw_val)
|
||||
num_val = tryparse(Float64, str_val)
|
||||
|
||||
if !isnothing(num_val)
|
||||
final_val = isinteger(num_val) ? round(Int, num_val) : num_val
|
||||
end
|
||||
end
|
||||
|
||||
# --- SQL Formatting ---
|
||||
if isa(final_val, Number)
|
||||
clause = "$(alias).$(column_name) $(op) $(final_val)"
|
||||
else
|
||||
# Escape single quotes within string values
|
||||
escaped_val = replace(string(final_val), "'" => "''")
|
||||
clause = "$(alias).$(column_name) $(op) '$(escaped_val)'"
|
||||
end
|
||||
|
||||
push!(where_clauses, clause)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 3. Assemble Final Query
|
||||
where_sql = isempty(where_clauses) ? "" : "WHERE " * join(where_clauses, " AND ")
|
||||
|
||||
return string(base_query, where_sql, ";")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user