update
This commit is contained in:
+237
-8
@@ -5,7 +5,7 @@ export virtualWineUserChatbox, jsoncorrection, search_wine_database!, # recomme
|
||||
extractWineAttributes_2, paraphrase, SQLexecution
|
||||
|
||||
using HTTP, JSON, URIs, Random, PrettyPrinting, UUIDs, Dates, DataFrames, DataStructures,
|
||||
Base64
|
||||
Base64, Serde
|
||||
using GeneralUtils, SQLLLM
|
||||
using ..type, ..util
|
||||
|
||||
@@ -277,14 +277,19 @@ end
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using ChatAgent
|
||||
julia> agent = ChatAgent.agentReflex("Jene")
|
||||
julia> input = "{\"food\": \"pizza\", \"occasion\": \"anniversary\"}"
|
||||
julia> result = checkinventory(agent, input)
|
||||
"{"wine 1": {\"Winery\": \"Pichon Baron\", \"wine name\": \"Pauillac (Grand Cru Classé)\", \"grape variety\": \"Cabernet Sauvignon\", \"year\": 2010, \"price\": \"125 USD\", \"stock ID\": \"ar-17\"}, }"
|
||||
julia> agent = YiemAgent.sommelier(...)
|
||||
julia> thoughtdict =
|
||||
OrderedDict{String, Any}(
|
||||
"plan" => "The user is asking a very specific question about a wine (Brunello di Montalcino from Tenuta CastelGiocondo). Although the policy suggests gathering budget, wine type, and occasion, the user has provided enough specific information (name, region, producer) to attempt a direct search in the database. I will use the SEARCH_WINE_DATABASE action to check if this specific wine is in our inventory.",
|
||||
"action_name" => "SEARCH_WINE_DATABASE",
|
||||
"action_input" => "Brunello di Montalcino from Tenuta CastelGiocondo")
|
||||
```
|
||||
"""
|
||||
function search_wine_database!(a::T, thoughtdict::AbstractDict; useSQLLLM::Bool=false
|
||||
)::NamedTuple{(:thoughtdict, :result_raw), Tuple{OrderedDict, Any}} where {T<:agent}
|
||||
|
||||
# WORKING
|
||||
# look_for_wine_in_wine_database(a, thoughtdict["action_input"])
|
||||
|
||||
println("\ncheckinventory order: $(thoughtdict["action_input"]) ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||
wineattributes_1 = extractWineAttributes_1(a, thoughtdict["action_input"])
|
||||
@@ -366,9 +371,9 @@ function generatesql(a::T, searchterm::String,
|
||||
Fulfill the objective.
|
||||
|
||||
# you should then respond to the user with interleaving plan, action_name, action_input
|
||||
1) "plan, Based on the current situation, state a complete action plan to complete the task and rationale. Be specific.
|
||||
2) "action_name, Must be "RUNSQL"
|
||||
3) "action_input, The input to the action you are about to perform according to your plan.
|
||||
1) "plan", Based on the current situation, state a complete action plan to complete the task and rationale. Be specific.
|
||||
2) "action_name", Must be "RUNSQL"
|
||||
3) "action_input", The input to the action you are about to perform according to your plan.
|
||||
After the action is executed you gets "action_result". It is the output from the action you selected.
|
||||
|
||||
# you should only respond in JSON format as described below
|
||||
@@ -610,6 +615,230 @@ function generatesql(a::T, searchterm::String,
|
||||
error("SQLLLM DecisionMaker() failed to generate a thought \n", response)
|
||||
end
|
||||
|
||||
|
||||
"""
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> using ChatAgent
|
||||
julia> agent = YiemAgent.sommelier(...)
|
||||
julia> thoughtdict =
|
||||
OrderedDict{String, Any}(
|
||||
"plan" => "The user is asking a very specific question about a wine (Brunello di Montalcino from Tenuta CastelGiocondo). Although the policy suggests gathering budget, wine type, and occasion, the user has provided enough specific information (name, region, producer) to attempt a direct search in the database. I will use the SEARCH_WINE_DATABASE action to check if this specific wine is in our inventory.",
|
||||
"action_name" => "SEARCH_WINE_DATABASE",
|
||||
"action_input" => "Brunello di Montalcino from Tenuta CastelGiocondo")
|
||||
```
|
||||
julia> look_for_wine_in_wine_database(agent, thoughtdict["action_input"])
|
||||
"""
|
||||
function look_for_wine_in_wine_database(a::T, searchterm::String,
|
||||
; maxattempt=10
|
||||
)::String where {T<:agent}
|
||||
|
||||
systemmsg =
|
||||
"""
|
||||
|
||||
# situation
|
||||
At each round of conversation, you will be given the following:
|
||||
- user search term
|
||||
- database tables schema
|
||||
|
||||
# objective
|
||||
Consult the provided database schema (tables and columns), please map a user's natural-language search term to the appropriate database columns and tables—identify the relevant fields, operators, and values (e.g., for SQL filtering).
|
||||
|
||||
# your responsibility includes
|
||||
Fulfill the objective.
|
||||
|
||||
# you should only respond in YAML 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: "..."
|
||||
...
|
||||
|
||||
# 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"
|
||||
</assistant>
|
||||
"""
|
||||
|
||||
|
||||
# use find_related_tables_for_user_question and inject only related table schema for a given search term
|
||||
# to LLM instead of giving LLM all tables schema.
|
||||
related_tables = a.context.find_related_tables_for_user_question(searchterm)
|
||||
table_schema = ""
|
||||
for table in related_tables
|
||||
_table_schema_str = GeneralUtils.get_db_table_schema_simple(a.context.pg_conn_str, table)
|
||||
table_schema_str = sprint(show, _table_schema_str) * "\n"
|
||||
table_schema = table_schema * table_schema_str
|
||||
end
|
||||
|
||||
context =
|
||||
"""
|
||||
<internal_context_for_assistant>
|
||||
<database_table_schema>
|
||||
$table_schema
|
||||
</database_table_schema>
|
||||
</internal_context_for_assistant>
|
||||
"""
|
||||
input = context * searchterm
|
||||
|
||||
msg = Dict(
|
||||
"model" => "gemma-4-E4B-it-UD-Q4_K_XL",
|
||||
"messages" => [
|
||||
Dict(
|
||||
"role" => "system",
|
||||
"content" => [
|
||||
Dict("type" => "text", "text" => systemmsg),
|
||||
]
|
||||
),
|
||||
Dict(
|
||||
"role" => "user",
|
||||
"content" => [
|
||||
Dict("type" => "text", "text" => input),
|
||||
]
|
||||
),
|
||||
],
|
||||
"temperature" => 0.7
|
||||
)
|
||||
|
||||
for attempt in 1:maxattempt
|
||||
response = a.context.text2textInstructLLM("random_id", msg)
|
||||
responsedict = Serde.parse_yaml(response)
|
||||
|
||||
"""
|
||||
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
|
||||
|
||||
#
|
||||
do_not_resolve_BM25_list = ["tasting_notes", "seo_name", "vintage", "grape"]
|
||||
if column_name ∉ do_not_resolve_list
|
||||
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] = resolved_word
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# check each attributes against each column in a database table with BM25 and get the closest
|
||||
# word match because there is a typo sometimes.
|
||||
for (k, v) in responsedict
|
||||
if k ∉ ["tasting_notes"]
|
||||
words_catalog = GeneralUtils.harvest_entity_catalog(a.context.pg_conn_str, "wine", k)
|
||||
resolved_word = GeneralUtils.resolve_entity(v, words_catalog; threshold=0.9)
|
||||
responsedict[k] = resolved_word
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#WORKING
|
||||
println("\n", responsedict)
|
||||
@info "test done " @__LINE__
|
||||
error(9999)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
think, response = GeneralUtils.extractthink(response)
|
||||
responsedict = nothing
|
||||
try
|
||||
_responsedict = JSON.parse(response)
|
||||
responsedict = GeneralUtils.dictify(_responsedict, keytype=String)
|
||||
catch
|
||||
println("\nERROR decisionMaker() failed to parse response: $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||
continue
|
||||
end
|
||||
|
||||
# check each attributes against each column in a database table with BM25 and get the closest
|
||||
# word match because there is a typo sometimes.
|
||||
for (k, v) in responsedict
|
||||
if k ∉ ["tasting_notes"]
|
||||
words_catalog = GeneralUtils.harvest_entity_catalog(a.context.pg_conn_str, "wine", k)
|
||||
resolved_word = GeneralUtils.resolve_entity(v, words_catalog; threshold=0.9)
|
||||
responsedict[k] = resolved_word
|
||||
end
|
||||
end
|
||||
|
||||
# LLM already extract user search term against tables schema
|
||||
# Ex. responsedict = Dict(
|
||||
# "wine_type"=> "red", # hard constraint
|
||||
# "region"=> "bordeaux", # hard constraint
|
||||
# "price_max"=> "100", # hard constraint
|
||||
# "tasting_notes"=> "fruity, oak" # semantic search)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return items
|
||||
end
|
||||
error("SQLLLM DecisionMaker() failed to generate a thought \n", response)
|
||||
end
|
||||
|
||||
function SQLexecution(executeSQL::Function, sql::T
|
||||
)::NamedTuple where {T<:AbstractString}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user