updatet
This commit is contained in:
+27
-19
@@ -68,7 +68,7 @@ julia> result = decisionMaker(agent)
|
||||
|
||||
OrderedDict{String, Any} with 4 entries:
|
||||
"plan" => "The user provided an image of a sparkling white wine (Asolo Prosecco Bella Principessa from Italy) and requested a search for similar wines in the inventory. According to store guidelines, I must st…
|
||||
"action_name" => "CHECK_WINE"
|
||||
"action_name" => "SEARCH_WINE_DATABASE"
|
||||
"action_input" => "Sparkling white wine from Italy"
|
||||
"action_result" => "1) winery: Terrazze dell Etna, wine_name: Rose Brut.
|
||||
```
|
||||
@@ -177,7 +177,7 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10
|
||||
continue
|
||||
end
|
||||
|
||||
# if responsedict["action_name"] ∉ ["CHAT_BOX", "CHECK_WINE", "PRESENT_WINE_GUIDELINE", "END_CONVER_GUIDELINE"]
|
||||
# if responsedict["action_name"] ∉ ["CHAT_BOX", "SEARCH_WINE_DATABASE", "PRESENT_WINE_GUIDELINE", "END_CONVER_GUIDELINE"]
|
||||
# errornote = "Your previous attempt didn't use the given functions"
|
||||
# println("\nERROR YiemAgent decisionMaker() $errornote --(not qualify response)--> $(responsedict["action_name"])", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||
# continue
|
||||
@@ -392,7 +392,7 @@ function conversation(a::sommelier; userinput::Union{Dict{String, Any}, JSON.Obj
|
||||
end
|
||||
|
||||
@info "YiemAgent conversation() 2-3 think count $loopcount " @__LINE__
|
||||
thoughtdict, _ = think(a)
|
||||
thoughtdict, result_raw = think(a)
|
||||
if thoughtdict["action_name"] ∈ ["CHAT_BOX"]
|
||||
@info "YiemAgent conversation() 2-4 think count $loopcount " @__LINE__
|
||||
assistant_response = Dict{String, Any}(
|
||||
@@ -401,7 +401,25 @@ function conversation(a::sommelier; userinput::Union{Dict{String, Any}, JSON.Obj
|
||||
)
|
||||
addNewMessage(a, "assistant", assistant_response; maximumMsg=maximumMsg)
|
||||
return thoughtdict["action_input"]
|
||||
# elseif thoughtdict["action_name"] ∈ ["CHAT_BOX"]
|
||||
# @info "YiemAgent conversation() 2-4 think count $loopcount " @__LINE__
|
||||
# assistant_response = Dict{String, Any}(
|
||||
# "role" => "assistant",
|
||||
# "content" => [
|
||||
# Dict("type" => "text", "text" => thoughtdict["action_input"]),
|
||||
# Dict( #WORKING put 1st image here
|
||||
# "type" => "image_url",
|
||||
# "image_url" => Dict("url" => image1_data_uri)
|
||||
# ),
|
||||
# Dict( #WORKING put 2nd image here
|
||||
# "type" => "image_url",
|
||||
# "image_url" => Dict("url" => image2_data_uri)
|
||||
# ),
|
||||
# ]
|
||||
# )
|
||||
# addNewMessage(a, "assistant", assistant_response; maximumMsg=maximumMsg)
|
||||
|
||||
return thoughtdict["action_input"] #XXX change output from string to dict
|
||||
else
|
||||
action_name = thoughtdict["action_name"]
|
||||
action_input = thoughtdict["action_input"]
|
||||
@@ -452,27 +470,17 @@ function think(a::T)::NamedTuple{(:thoughtdict, :result_raw), Tuple{OrderedDict,
|
||||
elseif thoughtdict["action_name"] ∈ ["WINE_PRESENTATION_GUIDELINE"]
|
||||
@info "YiemAgent think() 4 " @__LINE__
|
||||
thoughtdict, result_raw = wine_presentation_guideline!(a, thoughtdict)
|
||||
|
||||
|
||||
elseif thoughtdict["action_name"] == "CHECK_WINE"
|
||||
elseif thoughtdict["action_name"] == "SEARCH_WINE_DATABASE"
|
||||
@info "YiemAgent think() 5 " @__LINE__
|
||||
thoughtdict, result_raw = checkwine!(a, thoughtdict; useSQLLLM=false)
|
||||
|
||||
thoughtdict, result_raw = search_wine_database!(a, thoughtdict; useSQLLLM=true)
|
||||
#WORKING result_raw will be a df. i need to get images so i can send to frontend
|
||||
else
|
||||
@info "YiemAgent think() 6 " @__LINE__
|
||||
error("condition is not defined ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||
end
|
||||
|
||||
# max_ind =
|
||||
# if length(a.memory["shortmem"]) == 0
|
||||
# 0
|
||||
# else
|
||||
# k = keys(a.memory["shortmem"])
|
||||
# maximum(parse.(Int, k))
|
||||
# end
|
||||
# a.memory["shortmem"]["$(max_ind + 1)"] = thoughtdict
|
||||
|
||||
pprintln(thoughtdict)
|
||||
println("\n")
|
||||
@show thoughtdict
|
||||
@info "YiemAgent think() 7 " @__LINE__
|
||||
return (thoughtdict=thoughtdict, result_raw=result_raw)
|
||||
end
|
||||
@@ -700,7 +708,7 @@ function generatechat!(a::T; maxattempt::Integer=10
|
||||
continue
|
||||
end
|
||||
|
||||
# if responsedict["action_name"] ∉ ["CHAT_BOX", "CHECK_WINE", "PRESENT_WINE_GUIDELINE", "END_CONVER_GUIDELINE"]
|
||||
# if responsedict["action_name"] ∉ ["CHAT_BOX", "SEARCH_WINE_DATABASE", "PRESENT_WINE_GUIDELINE", "END_CONVER_GUIDELINE"]
|
||||
# errornote = "Your previous attempt didn't use the given functions"
|
||||
# println("\nERROR YiemAgent decisionMaker() $errornote --(not qualify response)--> $(responsedict["action_name"])", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||
# continue
|
||||
|
||||
+66
-11
@@ -1,6 +1,6 @@
|
||||
module llmfunction
|
||||
|
||||
export virtualWineUserChatbox, jsoncorrection, checkwine!, # recommendbox,
|
||||
export virtualWineUserChatbox, jsoncorrection, search_wine_database!, # recommendbox,
|
||||
virtualWineUserRecommendbox, userChatbox, userRecommendbox, extractWineAttributes_1,
|
||||
extractWineAttributes_2, paraphrase
|
||||
|
||||
@@ -282,15 +282,15 @@ 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\"}, }"
|
||||
```
|
||||
"""
|
||||
function checkwine!(a::T, thoughtdict::AbstractDict; useSQLLLM::Bool=false
|
||||
function search_wine_database!(a::T, thoughtdict::AbstractDict; useSQLLLM::Bool=false
|
||||
)::NamedTuple{(:thoughtdict, :result_raw), Tuple{OrderedDict, Any}} where {T<:agent}
|
||||
|
||||
println("\ncheckinventory order: $(thoughtdict["action_input"]) ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||
wineattributes_1 = extractWineAttributes_1(a, thoughtdict["action_input"])
|
||||
wineattributes_2 = extractWineAttributes_2(a, thoughtdict["action_input"])
|
||||
|
||||
retrieve_attributes = ["winery", "wine_name", "wine_id", "vintage", "region", "country", "wine_type", "grape", "serving_temperature", "sweetness", "intensity", "tannin", "acidity", "tasting_notes", "price", "currency"]
|
||||
_inventoryquery = "$wineattributes_1, $wineattributes_2"
|
||||
retrieve_attributes = ["winery", "wine_name", "wine_id", "vintage", "region", "country", "wine_type", "grape", "serving_temperature", "sweetness", "intensity", "tannin", "acidity", "tasting_notes", "price", "currency", "image_url", "retailer_name", "retailer_id"]
|
||||
_inventoryquery = "$wineattributes_1, $wineattributes_2, retailer_name: $(a.retailername), retailerid: $(a.retailerid)"
|
||||
inventoryquery = "Retrieves $retrieve_attributes of wines that match the following criteria - {$_inventoryquery}"
|
||||
println("\ncheckinventory input: $inventoryquery ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
||||
|
||||
@@ -308,7 +308,44 @@ function checkwine!(a::T, thoughtdict::AbstractDict; useSQLLLM::Bool=false
|
||||
|
||||
# direct query with possible sql instead of SQLLLM.
|
||||
sql = generatesql(a, inventoryquery)
|
||||
println("\nSQL: $sql ", @__FILE__, ":", @__LINE__, " $(Dates.now()) \n")
|
||||
textresult, result_raw, _, _ = SQLexecution(a.context.executeSQL, sql)
|
||||
#WORKING if result_raw != nothing, get image from image_url column of a df.
|
||||
# then store in a.memory["shortmem]["image"] = OrderedDict(
|
||||
# Dict(
|
||||
# "wine_id"=> "wine_id,
|
||||
# "name"=> "wine name",
|
||||
# "image_url" => Dict("url" => data1_uri)
|
||||
# )
|
||||
|
||||
# )
|
||||
|
||||
|
||||
# # 1. Read local file and encode to base64 string
|
||||
# image2_path = "test/small_image.png"
|
||||
# image2_bytes = read(image2_path)
|
||||
# image2_base64_string = base64encode(image2_bytes)
|
||||
# mime_type = "image/png"
|
||||
# data2_uri = "data:$(mime_type);base64,$(image2_base64_string)"
|
||||
|
||||
# # 3. Construct payload with the Data URI
|
||||
# message = Dict(
|
||||
# "role" => "user",
|
||||
# "content" => [
|
||||
# Dict("type" => "text", "text" => "Do you know type of wine in the image?"),
|
||||
# Dict(
|
||||
# "type" => "image_url",
|
||||
# "image_url" => Dict("url" => data1_uri)
|
||||
# )
|
||||
# ]
|
||||
# )
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
thoughtdict["action_result"] = textresult
|
||||
end
|
||||
|
||||
@@ -324,7 +361,8 @@ function generatesql(a::T, searchterm::String,
|
||||
"""
|
||||
# database_search_guidelines
|
||||
- Keep SQL queries focused only on the provided information.
|
||||
- Do not create any table in the database
|
||||
- Use wildcard character (%) to search more effectively.
|
||||
- Do not create any table in the database.
|
||||
- A junction table can be used to link tables together. Another use case is for filtering data.
|
||||
- If you can't find a single table that can be used to answer the user's search term, try joining multiple tables to see if you can obtain the answer.
|
||||
- Text information in the database usually stored in lower case. If your search returns empty, try using lower case to search.
|
||||
@@ -603,7 +641,7 @@ function SQLexecution(executeSQL::Function, sql::T
|
||||
tablesize = size(df)
|
||||
row, column = tablesize
|
||||
if row == 0
|
||||
return (result_str="No records found.", result_raw=df, success=true, errormsg=nothing)
|
||||
return (result_str="No records found. Try loosening your search criteria.", result_raw=nothing, success=true, errormsg=nothing)
|
||||
elseif column > 30
|
||||
return (result_str="There are more than 30 columns. Please be more specific.", result_raw=df, success=true, errormsg=nothing)
|
||||
else
|
||||
@@ -678,6 +716,7 @@ function extractWineAttributes_1(a::T1, input::T2; maxattempt=10
|
||||
wine_price_max: maximum price range of wine. Example: For wine price 20, wine_price_max will be 20. For wine price 10 to 100, wine_price_max will be 100.
|
||||
occasion: the occasion the user is having the wine for
|
||||
food_to_be_paired_with_wine: food that the user will be served with the wine such as poultry, fish, steak, etc
|
||||
_keyword suffice is the related keyword that appears in user's query. each keyword can not be used twice.
|
||||
</you should then respond to the user with>
|
||||
<you should only respond in JSON format as described below>
|
||||
"wine_name": "...",
|
||||
@@ -756,9 +795,6 @@ function extractWineAttributes_1(a::T1, input::T2; maxattempt=10
|
||||
for attempt in 1:maxattempt
|
||||
response = a.context.text2textInstructLLM(a.id, msg)
|
||||
response = GeneralUtils.clean_json_response(response)
|
||||
println("\n--- extractWineAttributes_1-1()")
|
||||
println(response)
|
||||
println("--- \n")
|
||||
|
||||
response = GeneralUtils.remove_french_accents(response)
|
||||
think, response = GeneralUtils.extractthink(response)
|
||||
@@ -786,6 +822,18 @@ function extractWineAttributes_1(a::T1, input::T2; maxattempt=10
|
||||
_v = replace(v, r"\(.*?\)" => "")
|
||||
responsedict[k] = _v
|
||||
end
|
||||
|
||||
println("\n--- extractWineAttributes_1-1()")
|
||||
@show responsedict
|
||||
@info "---\n" @__LINE__
|
||||
|
||||
#WORKING check each attributes against database with BM25
|
||||
for (k, v) in responsedict
|
||||
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
|
||||
|
||||
result = ""
|
||||
for (k, v) in responsedict
|
||||
# some time LLM generate text with "(some comment)". this line removes it
|
||||
@@ -793,10 +841,17 @@ function extractWineAttributes_1(a::T1, input::T2; maxattempt=10
|
||||
result *= "$k: $v, "
|
||||
end
|
||||
end
|
||||
|
||||
result = result[1:end-2] # remove the ending ", "
|
||||
println("\n--- extractWineAttributes_1-2()")
|
||||
println(result)
|
||||
println("--- \n")
|
||||
@show responsedict
|
||||
@show result
|
||||
@info "---\n" @__LINE__
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return result
|
||||
end
|
||||
error("extractWineAttributes_1() failed to get a response")
|
||||
|
||||
+7
-3
@@ -16,6 +16,7 @@ mutable struct agentcontext
|
||||
insertSQLVectorDB::Function
|
||||
similarSommelierDecision::Function
|
||||
insertSommelierDecision::Function
|
||||
pg_conn_str::String
|
||||
end
|
||||
|
||||
abstract type agent end
|
||||
@@ -93,6 +94,7 @@ mutable struct sommelier <: agent
|
||||
name::String # agent name
|
||||
id::String # agent id
|
||||
retailername::String
|
||||
retailerid::String
|
||||
tools::Dict
|
||||
maxHistoryMsg::Integer # e.g. 21th and earlier messages will get summarized
|
||||
chathistory::Vector{Dict{String, Any}}
|
||||
@@ -140,11 +142,12 @@ julia> agent = sommelier(context, name="WineExpert", id="123", retailername="MyW
|
||||
```
|
||||
"""
|
||||
function sommelier(
|
||||
context::agentcontext, # app context
|
||||
context::agentcontext, # agent functions, db connect and other context
|
||||
;
|
||||
name::String= "Assistant",
|
||||
id::String= string(uuid4()),
|
||||
retailername::String= "retailer_name",
|
||||
retailername::String= "not specified",
|
||||
retailerid::String= "not specified",
|
||||
maxHistoryMsg::Integer= 20,
|
||||
chathistory::Vector{Dict{String, Any}} = Vector{Dict{String, Any}}(),
|
||||
llmFormatName::String= "granite3"
|
||||
@@ -216,6 +219,7 @@ function sommelier(
|
||||
name,
|
||||
id,
|
||||
retailername,
|
||||
retailerid,
|
||||
tools,
|
||||
maxHistoryMsg,
|
||||
chathistory,
|
||||
@@ -279,7 +283,7 @@ function sommelier(
|
||||
|
||||
# available actions
|
||||
"CHAT_BOX", which you can use to talk with the user. The input is dialogue you want to chat with the user according to your plan.
|
||||
"CHECK_WINE", allows you to check information about wines you want in your inventory's database. The input is text that specify supported search criteria includeing: retailer_name, wine price, winery, name, vintage, region, country, type, grape varietal, tasting notes, occasion, food pairing, intensity, tannin, sweetness, and acidity.
|
||||
"SEARCH_WINE_DATABASE", allows you to search information about wines you want in your inventory's database. The input is supported search criteria including: retailer_name, wine price, winery, name, vintage, region, country, type, grape varietal, tasting notes, occasion, food pairing, intensity, tannin, sweetness, and acidity.
|
||||
Example query 1: "Dry, full-bodied red wine from 1) region: Burgundy, country: France or 2) region: Tuscany, country: Italy. Grape varietal: Merlot or Syrah. price 100 to 1000 USD."
|
||||
Example query 2: "Red or white wine, medium tannin, price under 700 USD"
|
||||
Example query 3: "white wine, region: Tuscany or Bordeaux, country: Italy or France
|
||||
|
||||
+1
-1
@@ -297,7 +297,7 @@ function createTimeline(events::T1; eventindex::Union{UnitRange, Nothing}=nothin
|
||||
# timeline *= "Event_$i $(event["subject"])> action_name: $(event["action_name"]), action_input: $(event["action_input"])\n"
|
||||
# elseif event["action_name"] == "CHECKINVENTORY" && event["observation"] === nothing
|
||||
# timeline *= "Event_$i $(event["subject"])> action_name: $(event["action_name"]), action_input: $(event["action_input"]), observation: Not done yet.\n"
|
||||
if event["action_name"] == "CHECK_WINE"
|
||||
if event["action_name"] == "SEARCH_WINE_DATABASE"
|
||||
timeline *= "Event_$i $(event["subject"])> action_name: $(event["action_name"]), action_input: $(event["action_input"]), observation: $(event["observation"])\\n"
|
||||
else
|
||||
timeline *= "Event_$i $(event["subject"])> action_name: $(event["action_name"]), action_input: $(event["action_input"])\\n"
|
||||
|
||||
Reference in New Issue
Block a user