update
This commit is contained in:
@@ -365,58 +365,86 @@ julia> result = winestock(agent, input)
|
||||
function winestock(a::T1, input::T2
|
||||
)::Union{Tuple{String, Number, Number, Bool}, Tuple{String, Nothing, Number, Bool}} where {T1<:agent, T2<:AbstractString}
|
||||
|
||||
# SELECT *
|
||||
# FROM food
|
||||
# WHERE 'China' = ANY(food_name)
|
||||
# OR 'India' = ANY(food_name);
|
||||
|
||||
|
||||
wineattributes = wineattributes_wordToNumber(a, input)
|
||||
|
||||
systemmsg =
|
||||
"""
|
||||
As an attentive sommelier, your mission is to determine the user's preferred levels of sweetness, intensity, tannin, and acidity for a wine based on their input.
|
||||
You'll achieve this by referring to the provided conversion table.
|
||||
|
||||
Conversion Table:
|
||||
Intensity level:
|
||||
Level 1: May correspond to "light-bodied" or a similar description.
|
||||
Level 2: May correspond to "med-light" or a similar description.
|
||||
Level 3: May correspond to "medium" or a similar description.
|
||||
Level 4: May correspond to "med-full" or a similar description.
|
||||
Level 5: May correspond to "full" or a similar description.
|
||||
Sweetness level:
|
||||
Level 1: May correspond to "dry", "no-sweet" or a similar description.
|
||||
Level 2: May correspond to "off-dry", "less-sweet" or a similar description.
|
||||
Level 3: May correspond to "semi-sweet" or a similar description.
|
||||
Level 4: May correspond to "sweet" or a similar description.
|
||||
Level 5: May correspond to "very sweet" or a similar description.
|
||||
Tannin level:
|
||||
Level 1: May correspond to "low tannin" or a similar description.
|
||||
Level 2: May correspond to "semi-low tannin" or a similar description.
|
||||
Level 3: May correspond to "medium tannin" or a similar description.
|
||||
Level 4: May correspond to "semi-high tannin" or a similar description.
|
||||
Level 5: May correspond to "high tannin" or a similar description.
|
||||
Acidity level:
|
||||
Level 1: May correspond to "low acidity" or a similar description.
|
||||
Level 2: May correspond to "semi-low acidity" or a similar description.
|
||||
Level 3: May correspond to "medium acidity" or a similar description.
|
||||
Level 4: May correspond to "semi-high acidity" or a similar description.
|
||||
Level 5: May correspond to "high acidity" or a similar description.
|
||||
As a helpful sommelier, your mission is to write SQL queries that search the PostgreSQL database for wines based on user input.
|
||||
|
||||
You should only respond in JSON format as describe below:
|
||||
{
|
||||
The database has the following tables (schema):
|
||||
1. Table wine (
|
||||
wine_id uuid primary key,
|
||||
wine_name varchar,
|
||||
brand varchar,
|
||||
manufacturer varchar,
|
||||
region varchar,
|
||||
country varchar,
|
||||
wine_type varchar,
|
||||
grape_variety varchar,
|
||||
serving_temperature varchar,
|
||||
intensity integer,
|
||||
sweetness integer,
|
||||
tannin integer,
|
||||
acidity integer,
|
||||
fizziness integer,
|
||||
|
||||
other_attributes jsonb,
|
||||
|
||||
created_at timestamptz,
|
||||
updated_at timestamptz,
|
||||
description text
|
||||
)
|
||||
|
||||
2. Table food (
|
||||
food_id uuid primary key,
|
||||
food_name varchar,
|
||||
country varchar,
|
||||
spicy integer,
|
||||
sweet integer,
|
||||
sour integer,
|
||||
umami integer,
|
||||
bitter integer,
|
||||
serving_temperature integer,
|
||||
other_attributes jsonb,
|
||||
|
||||
created_at timestamptz,
|
||||
updated_at timestamptz,
|
||||
description text
|
||||
)
|
||||
|
||||
3. wine_food (
|
||||
wine_id uuid references wine(wine_id),
|
||||
food_id uuid references food(food_id),
|
||||
constraint wine_food_id primary key (wine_id, food_id),
|
||||
|
||||
created_at timestamptz,
|
||||
updated_at timestamptz
|
||||
)
|
||||
|
||||
You should only respond in JSON format as describe below:
|
||||
{
|
||||
"SQL":
|
||||
{
|
||||
"sweetness": "sweetness level",
|
||||
"acidity": "acidity level",
|
||||
"tannin": "tannin level",
|
||||
"intensity": "intensity level"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Here are some examples:
|
||||
Here are some examples:
|
||||
|
||||
user: red wines, price < 50, body=full-bodied, tannins=1, off dry, acidity=medium, intensity=intense, Thai dishes
|
||||
assistant:
|
||||
{
|
||||
"wine_attributes":
|
||||
{
|
||||
"sweetness": 2,
|
||||
"acidity": 3,
|
||||
"tannin": 1,
|
||||
"intensity": 5
|
||||
}
|
||||
}
|
||||
user: {"sweetness": 2,"acidity": 3,"tannin": 1,"intensity": 5, "food": "Thai"}
|
||||
assistant:
|
||||
{
|
||||
"SQL":
|
||||
}
|
||||
|
||||
Let's begin!
|
||||
"""
|
||||
@@ -440,107 +468,246 @@ function winestock(a::T1, input::T2
|
||||
{
|
||||
"""
|
||||
|
||||
pprint(prompt)
|
||||
externalService = a.config[:externalservice][:text2textinstruct]
|
||||
|
||||
# send formatted input to user using GeneralUtils.sendReceiveMqttMsg
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
externalService[:mqtttopic],
|
||||
senderName= "virtualWineUserChatbox",
|
||||
senderId= a.id,
|
||||
receiverName= "text2textinstruct",
|
||||
mqttBroker= a.config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort= a.config[:mqttServerInfo][:port],
|
||||
msgId = "dummyid" #CHANGE remove after testing finished
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
)
|
||||
)
|
||||
|
||||
attempt = 0
|
||||
for attempt in 1:5
|
||||
try
|
||||
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)
|
||||
_responseJsonStr = response[:response][:text]
|
||||
expectedJsonExample =
|
||||
"""
|
||||
Here is an expected JSON format:
|
||||
{
|
||||
"wine_attributes":
|
||||
{
|
||||
"...": "...",
|
||||
"...": "...",
|
||||
}
|
||||
}
|
||||
"""
|
||||
responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample)
|
||||
responseDict = copy(JSON3.read(responseJsonStr))
|
||||
|
||||
return (text, select, reward, isterminal)
|
||||
catch e
|
||||
io = IOBuffer()
|
||||
showerror(io, e)
|
||||
errorMsg = String(take!(io))
|
||||
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
|
||||
println("")
|
||||
@warn "Error occurred: $errorMsg\n$st"
|
||||
println("")
|
||||
end
|
||||
end
|
||||
error("virtualWineUserChatbox failed to get a response")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
winesStr =
|
||||
"""
|
||||
1: El Enemigo Cabernet Franc 2019
|
||||
2: Tantara Chardonnay 2017
|
||||
"""
|
||||
result =
|
||||
"""
|
||||
I found the following wines in our stock:
|
||||
{
|
||||
$winesStr
|
||||
}
|
||||
"""
|
||||
return result, nothing, 0, false
|
||||
end
|
||||
# function winestock(a::T1, input::T2
|
||||
# )::Union{Tuple{String, Number, Number, Bool}, Tuple{String, Nothing, Number, Bool}} where {T1<:agent, T2<:AbstractString}
|
||||
|
||||
# winesStr =
|
||||
# """
|
||||
# 1: El Enemigo Cabernet Franc 2019
|
||||
# 2: Tantara Chardonnay 2017
|
||||
# """
|
||||
# result =
|
||||
# """
|
||||
# I found the following wines in our stock:
|
||||
# {
|
||||
# $winesStr
|
||||
# }
|
||||
# """
|
||||
# return result, nothing, 0, false
|
||||
# end
|
||||
pprint(prompt)
|
||||
externalService = a.config[:externalservice][:text2textinstruct]
|
||||
|
||||
# send formatted input to user using GeneralUtils.sendReceiveMqttMsg
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
externalService[:mqtttopic],
|
||||
senderName= "virtualWineUserChatbox",
|
||||
senderId= a.id,
|
||||
receiverName= "text2textinstruct",
|
||||
mqttBroker= a.config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort= a.config[:mqttServerInfo][:port],
|
||||
msgId = "dummyid" #CHANGE remove after testing finished
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
)
|
||||
)
|
||||
|
||||
attempt = 0
|
||||
for attempt in 1:5
|
||||
try
|
||||
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)
|
||||
_responseJsonStr = response[:response][:text]
|
||||
expectedJsonExample =
|
||||
"""
|
||||
Here is an expected JSON format:
|
||||
{
|
||||
"attributes":
|
||||
{
|
||||
"...": "...",
|
||||
"...": "...",
|
||||
}
|
||||
}
|
||||
"""
|
||||
responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample)
|
||||
_responseDict = copy(JSON3.read(responseJsonStr))
|
||||
responseDict = _responseDict[:attributes]
|
||||
|
||||
return responseDict
|
||||
catch e
|
||||
io = IOBuffer()
|
||||
showerror(io, e)
|
||||
errorMsg = String(take!(io))
|
||||
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
|
||||
println("")
|
||||
@warn "Error occurred: $errorMsg\n$st"
|
||||
println("")
|
||||
end
|
||||
end
|
||||
error("wineattributes_wordToNumber() failed to get a response")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# winesStr =
|
||||
# """
|
||||
# 1: El Enemigo Cabernet Franc 2019
|
||||
# 2: Tantara Chardonnay 2017
|
||||
# """
|
||||
# result =
|
||||
# """
|
||||
# I found the following wines in our stock:
|
||||
# {
|
||||
# $winesStr
|
||||
# }
|
||||
# """
|
||||
# return result, nothing, 0, false
|
||||
end
|
||||
|
||||
function wineattributes_wordToNumber(a::T1, input::T2
|
||||
)::Dict where {T1<:agent, T2<:AbstractString}
|
||||
|
||||
systemmsg =
|
||||
"""
|
||||
As an attentive sommelier, your mission is to determine the user's preferred levels of sweetness, intensity, tannin, acidity and other criteria for a wine based on their input.
|
||||
You'll achieve this by referring to the provided conversion table.
|
||||
|
||||
Conversion Table:
|
||||
Intensity level:
|
||||
Level 1: May correspond to "light-bodied" or a similar description.
|
||||
Level 2: May correspond to "med light", "medium light" or a similar description.
|
||||
Level 3: May correspond to "medium" or a similar description.
|
||||
Level 4: May correspond to "med full", "medium full" or a similar description.
|
||||
Level 5: May correspond to "full" or a similar description.
|
||||
Sweetness level:
|
||||
Level 1: May correspond to "dry", "no sweet" or a similar description.
|
||||
Level 2: May correspond to "off dry", "less sweet" or a similar description.
|
||||
Level 3: May correspond to "semi sweet" or a similar description.
|
||||
Level 4: May correspond to "sweet" or a similar description.
|
||||
Level 5: May correspond to "very sweet" or a similar description.
|
||||
Tannin level:
|
||||
Level 1: May correspond to "low tannin" or a similar description.
|
||||
Level 2: May correspond to "semi low tannin" or a similar description.
|
||||
Level 3: May correspond to "medium tannin" or a similar description.
|
||||
Level 4: May correspond to "semi high tannin" or a similar description.
|
||||
Level 5: May correspond to "high tannin" or a similar description.
|
||||
Acidity level:
|
||||
Level 1: May correspond to "low acidity" or a similar description.
|
||||
Level 2: May correspond to "semi low acidity" or a similar description.
|
||||
Level 3: May correspond to "medium acidity" or a similar description.
|
||||
Level 4: May correspond to "semi high acidity" or a similar description.
|
||||
Level 5: May correspond to "high acidity" or a similar description.
|
||||
|
||||
You should only respond in JSON format as describe below:
|
||||
{
|
||||
"attributes":
|
||||
{
|
||||
"sweetness": "sweetness level",
|
||||
"acidity": "acidity level",
|
||||
"tannin": "tannin level",
|
||||
"intensity": "intensity level"
|
||||
}
|
||||
}
|
||||
|
||||
Here are some examples:
|
||||
|
||||
user: "price < 25, full-bodied white wine with sweetness level 2, low tannin level and medium acidity level, Pizza"
|
||||
assistant:
|
||||
{
|
||||
"attributes":
|
||||
{
|
||||
"wine_type": "white"
|
||||
"budget": less than 25",
|
||||
"food_pairing": "Pizza",
|
||||
"sweetness": 2,
|
||||
"acidity": 3,
|
||||
"tannin": 1,
|
||||
"intensity": 5
|
||||
}
|
||||
}
|
||||
|
||||
user: body=full-bodied, off dry, acidity=medium, intensity=intense
|
||||
assistant:
|
||||
{
|
||||
"attributes":
|
||||
{
|
||||
"sweetness": 2,
|
||||
"acidity": 3,
|
||||
"tannin": "not specified",
|
||||
"intensity": 5
|
||||
}
|
||||
}
|
||||
|
||||
Let's begin!
|
||||
"""
|
||||
|
||||
usermsg =
|
||||
"""
|
||||
$input
|
||||
"""
|
||||
|
||||
chathistory =
|
||||
[
|
||||
Dict(:name=> "system", :text=> systemmsg),
|
||||
Dict(:name=> "user", :text=> usermsg)
|
||||
]
|
||||
|
||||
# put in model format
|
||||
prompt = formatLLMtext(chathistory, "llama3instruct")
|
||||
prompt *=
|
||||
"""
|
||||
<|start_header_id|>assistant<|end_header_id|>
|
||||
{
|
||||
"""
|
||||
|
||||
pprint(prompt)
|
||||
externalService = a.config[:externalservice][:text2textinstruct]
|
||||
|
||||
# send formatted input to user using GeneralUtils.sendReceiveMqttMsg
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
externalService[:mqtttopic],
|
||||
senderName= "wineattributes_wordToNumber",
|
||||
senderId= a.id,
|
||||
receiverName= "text2textinstruct",
|
||||
mqttBroker= a.config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort= a.config[:mqttServerInfo][:port],
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
)
|
||||
)
|
||||
|
||||
attempt = 0
|
||||
for attempt in 1:5
|
||||
try
|
||||
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)
|
||||
_responseJsonStr = response[:response][:text]
|
||||
expectedJsonExample =
|
||||
"""
|
||||
Here is an expected JSON format:
|
||||
{
|
||||
"attributes":
|
||||
{
|
||||
"...": "...",
|
||||
"...": "...",
|
||||
}
|
||||
}
|
||||
"""
|
||||
responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample)
|
||||
_responseDict = copy(JSON3.read(responseJsonStr))
|
||||
responseDict = _responseDict[:attributes]
|
||||
|
||||
return responseDict
|
||||
catch e
|
||||
io = IOBuffer()
|
||||
showerror(io, e)
|
||||
errorMsg = String(take!(io))
|
||||
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
|
||||
println("")
|
||||
@warn "Error occurred: $errorMsg\n$st"
|
||||
println("")
|
||||
end
|
||||
end
|
||||
error("wineattributes_wordToNumber() failed to get a response")
|
||||
end
|
||||
|
||||
""" Attemp to correct LLM response's incorrect JSON response.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user