update
This commit is contained in:
18
test/etc.jl
18
test/etc.jl
@@ -1,9 +1,9 @@
|
||||
using GeneralUtils
|
||||
|
||||
response = "trajectory_evaluation:\nThe trajectory is correct so far. The thought accurately reflects the user's question, and the action taken is a valid attempt to retrieve data from the database that matches the specified criteria.\n\nanswer_evaluation:\nThe observation provides information about two red wines from Bordeaux rive droite in France, which partially answers the question. However, it does not provide a complete answer as it only lists the wine names and characteristics, but does not explicitly state whether there are any other wines that match the criteria.\n\naccepted_as_answer: No\n\nscore: 6\nThe trajectory is mostly correct, but the observation does not fully address the question.\n\nsuggestion: Consider adding more filters or parameters to the database query to retrieve a complete list of wines that match the specified criteria."
|
||||
|
||||
responsedict = GeneralUtils.textToDict(response,
|
||||
["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"],
|
||||
rightmarker=":", symbolkey=true)
|
||||
|
||||
|
||||
using GeneralUtils
|
||||
|
||||
response = "trajectory_evaluation:\nThe trajectory is correct so far. The thought accurately reflects the user's question, and the action taken is a valid attempt to retrieve data from the database that matches the specified criteria.\n\nanswer_evaluation:\nThe observation provides information about two red wines from Bordeaux rive droite in France, which partially answers the question. However, it does not provide a complete answer as it only lists the wine names and characteristics, but does not explicitly state whether there are any other wines that match the criteria.\n\naccepted_as_answer: No\n\nscore: 6\nThe trajectory is mostly correct, but the observation does not fully address the question.\n\nsuggestion: Consider adding more filters or parameters to the database query to retrieve a complete list of wines that match the specified criteria."
|
||||
|
||||
responsedict = GeneralUtils.textToDict(response,
|
||||
["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"],
|
||||
rightmarker=":", symbolkey=true)
|
||||
|
||||
|
||||
|
||||
@@ -1,159 +1,159 @@
|
||||
using Revise
|
||||
using YiemAgent, GeneralUtils, JSON3, DataStructures
|
||||
|
||||
thoughtDict = OrderedDict(
|
||||
:Question=> "Hello, I would like a get a bottle of wine",
|
||||
:Thought_1=> "The customer wants to buy a bottle of wine, but we need more information about their preferences.",
|
||||
:Action_1=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What occasion are you buying the wine for?",
|
||||
),
|
||||
:Observation_1=> "We are having a wedding pary this weekend.",
|
||||
|
||||
:Thought_2=> "A wedding party is a great occasion to have a good bottle of wine.",
|
||||
:Action_2=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What type of food will you be serving with the wine?",
|
||||
),
|
||||
:Observation_2=> "I think it is Thai dishes",
|
||||
|
||||
:Thought_3=> "Now that I know the occasion and food, I need to ask about the budget.",
|
||||
:Action_3=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What is your budget for this wine?",
|
||||
),
|
||||
:Observation_3=> "50 bucks",
|
||||
|
||||
:Thought_4=> "With a budget of \$50, we have a wide range of options. Now that I know it's a wedding party and Thai dishes, I need to ask about the type of wine they prefer.",
|
||||
:Action_4=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What type of wine are you looking for? (Red, White, Sparkling, Rose, Dessert, Fortified)",
|
||||
),
|
||||
:Observation_4=> "Sparkling please.",
|
||||
|
||||
:Thought_5=> "Now that I know the occasion, food, budget and preferred type of wine, it's time to check our inventory for the best matching wine.",
|
||||
:Action_5=> Dict(
|
||||
:name=> "winestock",
|
||||
:input=> "wine with budget \$50, Thai dishes, sparkling, wedding party",
|
||||
),
|
||||
:Observation_5=> "I found the following wine in stock {1 : Zena Crown Vista, 2 : Schrader Cabernet Sauvignon}",
|
||||
|
||||
:Thought_6=> "Now that I have all the information, it's time to recommend a wine that fits their preferences.",
|
||||
:Action_6=> Dict(
|
||||
:name=> "recommendation",
|
||||
:input=> "I recommend Zena Crown Vista for its sparkling and affordable price.",
|
||||
),
|
||||
:Observation_6=> "I don't like it. Do you have another option?",
|
||||
)
|
||||
|
||||
_thoughtJsonStr = JSON3.write(thoughtDict)
|
||||
thoughtJsonStr = _thoughtJsonStr[1:end-1] # remove } at the end
|
||||
# @show thoughtJsonStr
|
||||
|
||||
_, latestThoughtIndice = GeneralUtils.findHighestIndexKey(thoughtDict, "Thought")
|
||||
nextThoughtIndice = latestThoughtIndice + 1
|
||||
|
||||
_prompt =
|
||||
"""
|
||||
You are a helpful sommelier working for a wine store.
|
||||
Your goal is to reccommend the best wine from your inventory that match the user preferences.
|
||||
|
||||
You must follow the following criteria:
|
||||
1) Get to know what occasion the user is buying wine for
|
||||
2) Get to know what food the user will have with wine
|
||||
3) Get to know how much the user willing to spend
|
||||
4) Get to know type of wine the user is looking for e.g. Red, White, Sparkling, Rose, Dessert, Fortified
|
||||
5) Get to know what characteristics of wine the user is looking for
|
||||
e.g. tannin, sweetness, intensity, acidity
|
||||
6) Check your inventory for the best wine that match the user preference
|
||||
7) Recommend wine to the user
|
||||
|
||||
You should only respond with interleaving Thought, Action, Observation steps.
|
||||
Thought can reason about the current situation, and Action can be three types:
|
||||
1) winestock[query], which you can use to find wine in your inventory. The more input data the better.
|
||||
2) chatbox[text], which you can use to interact with the user.
|
||||
3) recommendation[answer], which returns your wine reccommendation to the user.
|
||||
|
||||
You should only respond in JSON format as describe below:
|
||||
{
|
||||
"Thought": "your reasoning",
|
||||
"Action": {"name": "action to take", "input": "Action input"},
|
||||
"Observation": "result of the action"
|
||||
}
|
||||
|
||||
Here are some examples:
|
||||
{
|
||||
"Question": "I would like to buy a sedan with 8 seats.",
|
||||
"Thought_1": "Our showroom carries various vehicle model. But I'm not sure whether we have a models that fits the user demand, I need to check our inventory.",
|
||||
"Action_1": {"name": "inventory", "input": "sedan with 8 seats."},
|
||||
"Observation_1": "Several model has 8 seats. Available color are black, red green"
|
||||
}
|
||||
{
|
||||
"Thought_2": "I have to ask the user what color he likes.",
|
||||
"Action_2": {"name": "chatbox", "input": "Which color do you like?"}
|
||||
"Observation_2": "I'll take black."
|
||||
}
|
||||
{
|
||||
"Thought_3": "There is only one model that fits the user preference. It's Yiem model A",
|
||||
"Action_3": {"name": "recommendation", "input": "I recommend a Yiem model A"}
|
||||
}
|
||||
|
||||
Let's begin!
|
||||
|
||||
$(JSON3.write(thoughtDict))
|
||||
{Thought_$nextThoughtIndice
|
||||
"""
|
||||
|
||||
prompt = YiemAgent.formatLLMtext_llama3instruct("system", _prompt)
|
||||
@show prompt
|
||||
msgMeta = Dict(:requestResponse => nothing,
|
||||
:msgPurpose => nothing,
|
||||
:receiverId => nothing,
|
||||
:getPost => nothing,
|
||||
:msgId => "4c7111e0-c30e-44c3-8f85-1c8b3f03a8be",
|
||||
:acknowledgestatus => nothing,
|
||||
:replyToMsgId => nothing,
|
||||
:msgFormatVersion => nothing,
|
||||
:mqttServerInfo => Dict(:port => 1883, :broker => "mqtt.yiem.cc"),
|
||||
:sendTopic => "/loadbalancer/requestingservice",
|
||||
:receiverName => "text2textinstruct",
|
||||
:replyTopic => nothing,
|
||||
:senderName => "decisionMaker",
|
||||
:senderSelfnote => nothing,
|
||||
:senderId => "testingSessionID",
|
||||
:timeStamp => "2024-05-04T08:06:23.561"
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
thoughtJsonStr = _response[:response][:text]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using Revise
|
||||
using YiemAgent, GeneralUtils, JSON3, DataStructures
|
||||
|
||||
thoughtDict = OrderedDict(
|
||||
:Question=> "Hello, I would like a get a bottle of wine",
|
||||
:Thought_1=> "The customer wants to buy a bottle of wine, but we need more information about their preferences.",
|
||||
:Action_1=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What occasion are you buying the wine for?",
|
||||
),
|
||||
:Observation_1=> "We are having a wedding pary this weekend.",
|
||||
|
||||
:Thought_2=> "A wedding party is a great occasion to have a good bottle of wine.",
|
||||
:Action_2=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What type of food will you be serving with the wine?",
|
||||
),
|
||||
:Observation_2=> "I think it is Thai dishes",
|
||||
|
||||
:Thought_3=> "Now that I know the occasion and food, I need to ask about the budget.",
|
||||
:Action_3=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What is your budget for this wine?",
|
||||
),
|
||||
:Observation_3=> "50 bucks",
|
||||
|
||||
:Thought_4=> "With a budget of \$50, we have a wide range of options. Now that I know it's a wedding party and Thai dishes, I need to ask about the type of wine they prefer.",
|
||||
:Action_4=> Dict(
|
||||
:name=> "chatbox",
|
||||
:input=> "What type of wine are you looking for? (Red, White, Sparkling, Rose, Dessert, Fortified)",
|
||||
),
|
||||
:Observation_4=> "Sparkling please.",
|
||||
|
||||
:Thought_5=> "Now that I know the occasion, food, budget and preferred type of wine, it's time to check our inventory for the best matching wine.",
|
||||
:Action_5=> Dict(
|
||||
:name=> "winestock",
|
||||
:input=> "wine with budget \$50, Thai dishes, sparkling, wedding party",
|
||||
),
|
||||
:Observation_5=> "I found the following wine in stock {1 : Zena Crown Vista, 2 : Schrader Cabernet Sauvignon}",
|
||||
|
||||
:Thought_6=> "Now that I have all the information, it's time to recommend a wine that fits their preferences.",
|
||||
:Action_6=> Dict(
|
||||
:name=> "recommendation",
|
||||
:input=> "I recommend Zena Crown Vista for its sparkling and affordable price.",
|
||||
),
|
||||
:Observation_6=> "I don't like it. Do you have another option?",
|
||||
)
|
||||
|
||||
_thoughtJsonStr = JSON3.write(thoughtDict)
|
||||
thoughtJsonStr = _thoughtJsonStr[1:end-1] # remove } at the end
|
||||
# @show thoughtJsonStr
|
||||
|
||||
_, latestThoughtIndice = GeneralUtils.findHighestIndexKey(thoughtDict, "Thought")
|
||||
nextThoughtIndice = latestThoughtIndice + 1
|
||||
|
||||
_prompt =
|
||||
"""
|
||||
You are a helpful sommelier working for a wine store.
|
||||
Your goal is to reccommend the best wine from your inventory that match the user preferences.
|
||||
|
||||
You must follow the following criteria:
|
||||
1) Get to know what occasion the user is buying wine for
|
||||
2) Get to know what food the user will have with wine
|
||||
3) Get to know how much the user willing to spend
|
||||
4) Get to know type of wine the user is looking for e.g. Red, White, Sparkling, Rose, Dessert, Fortified
|
||||
5) Get to know what characteristics of wine the user is looking for
|
||||
e.g. tannin, sweetness, intensity, acidity
|
||||
6) Check your inventory for the best wine that match the user preference
|
||||
7) Recommend wine to the user
|
||||
|
||||
You should only respond with interleaving Thought, Action, Observation steps.
|
||||
Thought can reason about the current situation, and Action can be three types:
|
||||
1) winestock[query], which you can use to find wine in your inventory. The more input data the better.
|
||||
2) chatbox[text], which you can use to interact with the user.
|
||||
3) recommendation[answer], which returns your wine reccommendation to the user.
|
||||
|
||||
You should only respond in JSON format as describe below:
|
||||
{
|
||||
"Thought": "your reasoning",
|
||||
"Action": {"name": "action to take", "input": "Action input"},
|
||||
"Observation": "result of the action"
|
||||
}
|
||||
|
||||
Here are some examples:
|
||||
{
|
||||
"Question": "I would like to buy a sedan with 8 seats.",
|
||||
"Thought_1": "Our showroom carries various vehicle model. But I'm not sure whether we have a models that fits the user demand, I need to check our inventory.",
|
||||
"Action_1": {"name": "inventory", "input": "sedan with 8 seats."},
|
||||
"Observation_1": "Several model has 8 seats. Available color are black, red green"
|
||||
}
|
||||
{
|
||||
"Thought_2": "I have to ask the user what color he likes.",
|
||||
"Action_2": {"name": "chatbox", "input": "Which color do you like?"}
|
||||
"Observation_2": "I'll take black."
|
||||
}
|
||||
{
|
||||
"Thought_3": "There is only one model that fits the user preference. It's Yiem model A",
|
||||
"Action_3": {"name": "recommendation", "input": "I recommend a Yiem model A"}
|
||||
}
|
||||
|
||||
Let's begin!
|
||||
|
||||
$(JSON3.write(thoughtDict))
|
||||
{Thought_$nextThoughtIndice
|
||||
"""
|
||||
|
||||
prompt = YiemAgent.formatLLMtext_llama3instruct("system", _prompt)
|
||||
@show prompt
|
||||
msgMeta = Dict(:requestResponse => nothing,
|
||||
:msgPurpose => nothing,
|
||||
:receiverId => nothing,
|
||||
:getPost => nothing,
|
||||
:msgId => "4c7111e0-c30e-44c3-8f85-1c8b3f03a8be",
|
||||
:acknowledgestatus => nothing,
|
||||
:replyToMsgId => nothing,
|
||||
:msgFormatVersion => nothing,
|
||||
:mqttServerInfo => Dict(:port => 1883, :broker => "mqtt.yiem.cc"),
|
||||
:sendTopic => "/loadbalancer/requestingservice",
|
||||
:receiverName => "text2textinstruct",
|
||||
:replyTopic => nothing,
|
||||
:senderName => "decisionMaker",
|
||||
:senderSelfnote => nothing,
|
||||
:senderId => "testingSessionID",
|
||||
:timeStamp => "2024-05-04T08:06:23.561"
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
thoughtJsonStr = _response[:response][:text]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,87 +1,87 @@
|
||||
using Revise # remove when this package is completed
|
||||
using YiemAgent, GeneralUtils, JSON3, MQTTClient, Dates, UUIDs, DataStructures
|
||||
using Base.Threads
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
config = copy(JSON3.read("config.json"))
|
||||
|
||||
instanceInternalTopic = config[:serviceInternalTopic][:mqtttopic] * "/1"
|
||||
|
||||
client, connection = MakeConnection(config[:mqttServerInfo][:broker],
|
||||
config[:mqttServerInfo][:port])
|
||||
|
||||
receiveUserMsgChannel = Channel{Dict}(4)
|
||||
receiveInternalMsgChannel = Channel{Dict}(4)
|
||||
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
"N/A",
|
||||
replyTopic = config[:servicetopic][:mqtttopic] # ask frontend reply to this instance_chat_topic
|
||||
)
|
||||
|
||||
agentConfig = Dict(
|
||||
:mqttServerInfo=> config[:mqttServerInfo],
|
||||
:receivemsg=> Dict(
|
||||
:prompt=> config[:servicetopic][:mqtttopic], # topic to receive prompt i.e. frontend send msg to this topic
|
||||
:internal=> instanceInternalTopic,
|
||||
),
|
||||
:externalservice=> config[:externalservice],
|
||||
)
|
||||
|
||||
# Instantiate an agent
|
||||
tools=Dict( # update input format
|
||||
"askbox"=> Dict(
|
||||
:description => "<askbox tool description>Useful for when you need to ask the user for more context. Do not ask the user their own question.</askbox tool description>",
|
||||
:input => """<input>Input is a text in JSON format.</input><input example>{\"Q1\": \"How are you doing?\", \"Q2\": \"How may I help you?\"}</input example>""",
|
||||
:output => "" ,
|
||||
:func => nothing,
|
||||
),
|
||||
# "winestock"=> Dict(
|
||||
# :description => "<winestock tool description>A handy tool for searching wine in your inventory that match the user preferences.</winestock tool description>",
|
||||
# :input => """<input>Input is a JSON-formatted string that contains a detailed and precise search query.</input><input example>{\"wine type\": \"rose\", \"price\": \"max 35\", \"sweetness level\": \"sweet\", \"intensity level\": \"light bodied\", \"Tannin level\": \"low\", \"Acidity level\": \"low\"}</input example>""",
|
||||
# :output => """<output>Output are wines that match the search query in JSON format.""",
|
||||
# :func => ChatAgent.winestock,
|
||||
# ),
|
||||
"finalanswer"=> Dict(
|
||||
:description => "<tool description>Useful for when you are ready to recommend wines to the user.</tool description>",
|
||||
:input => """<input format>{\"finalanswer\": \"some text\"}.</input format><input example>{\"finalanswer\": \"I recommend Zena Crown Vista\"}</input example>""",
|
||||
:output => "" ,
|
||||
:func => nothing,
|
||||
),
|
||||
)
|
||||
|
||||
a = YiemAgent.sommelier(
|
||||
receiveUserMsgChannel,
|
||||
receiveInternalMsgChannel,
|
||||
agentConfig,
|
||||
name= "assistant",
|
||||
id= "testingSessionID", # agent instance id
|
||||
tools=tools,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
input =
|
||||
OrderedDict{Symbol, Any}(:question => "Hello, I would like a get a bottle of wine", :thought_1 => "It's great that the user is looking for a bottle of wine. To give them a personalized recommendation, I need to know more about their preferences.", :action_1 => Dict{Symbol, Any}(:name => "chatbox", :input => "What occasion are you planning to use this wine for?"), :observation_1 => "We are holding a wedding party", :thought_2 => "A wedding party is a great occasion for a special bottle of wine. I need to know what type of food will be served, and how much the user is willing to spend.", :action_2 => Dict{Symbol, Any}(:name => "chatbox", :input => "What type of food will you be serving at the wedding?"), :observation_2 => "It will be Thai dishes.", :thought_3 => "The type of wine that pairs well with Thai dishes is usually a crisp and refreshing white wine, but I also need to consider the budget and personal preferences.", :action_3 => Dict{Symbol, Any}(:name => "chatbox", :input => "How much are you willing to spend on this bottle of wine?"), :observation_3 => "I would spend up to 50 bucks.", :thought_4 => "I have a good idea of the occasion, food, and budget. Now I need to know what type of wine the user is looking for.", :action_4 => Dict{Symbol, Any}(:name => "chatbox", :input => "What type of wine are you usually looking for? Red, White, Sparkling, Rose, Dessert or Fortified?"), :observation_4 => "I like full-bodied Red wine with low tannin.", :thought_5 => "Now that I have all the necessary information, I can start searching for a suitable wine in our inventory.", :action_5 => Dict{Symbol, Any}(:name => "winestock", :input => "red wine with low tannins"), :observation_5 => "I found the following wines in our stock: \n{\n 1: El Enemigo Cabernet Franc 2019\n2: Tantara Chardonnay 2017\n\n}\n", :thought_6 => "Now that I have the information about the wine, it's time to make a recommendation.", :action_6 => Dict{Symbol, Any}(:name => "recommendbox", :input => "El Enemigo Cabernet Franc 2019"), :observation_6 => "I don't like the one you recommend. I want dry wine.")
|
||||
|
||||
|
||||
result = YiemAgent.jsoncorrection(a, input)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using Revise # remove when this package is completed
|
||||
using YiemAgent, GeneralUtils, JSON3, MQTTClient, Dates, UUIDs, DataStructures
|
||||
using Base.Threads
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
config = copy(JSON3.read("config.json"))
|
||||
|
||||
instanceInternalTopic = config[:serviceInternalTopic][:mqtttopic] * "/1"
|
||||
|
||||
client, connection = MakeConnection(config[:mqttServerInfo][:broker],
|
||||
config[:mqttServerInfo][:port])
|
||||
|
||||
receiveUserMsgChannel = Channel{Dict}(4)
|
||||
receiveInternalMsgChannel = Channel{Dict}(4)
|
||||
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
"N/A",
|
||||
replyTopic = config[:servicetopic][:mqtttopic] # ask frontend reply to this instance_chat_topic
|
||||
)
|
||||
|
||||
agentConfig = Dict(
|
||||
:mqttServerInfo=> config[:mqttServerInfo],
|
||||
:receivemsg=> Dict(
|
||||
:prompt=> config[:servicetopic][:mqtttopic], # topic to receive prompt i.e. frontend send msg to this topic
|
||||
:internal=> instanceInternalTopic,
|
||||
),
|
||||
:externalservice=> config[:externalservice],
|
||||
)
|
||||
|
||||
# Instantiate an agent
|
||||
tools=Dict( # update input format
|
||||
"askbox"=> Dict(
|
||||
:description => "<askbox tool description>Useful for when you need to ask the user for more context. Do not ask the user their own question.</askbox tool description>",
|
||||
:input => """<input>Input is a text in JSON format.</input><input example>{\"Q1\": \"How are you doing?\", \"Q2\": \"How may I help you?\"}</input example>""",
|
||||
:output => "" ,
|
||||
:func => nothing,
|
||||
),
|
||||
# "winestock"=> Dict(
|
||||
# :description => "<winestock tool description>A handy tool for searching wine in your inventory that match the user preferences.</winestock tool description>",
|
||||
# :input => """<input>Input is a JSON-formatted string that contains a detailed and precise search query.</input><input example>{\"wine type\": \"rose\", \"price\": \"max 35\", \"sweetness level\": \"sweet\", \"intensity level\": \"light bodied\", \"Tannin level\": \"low\", \"Acidity level\": \"low\"}</input example>""",
|
||||
# :output => """<output>Output are wines that match the search query in JSON format.""",
|
||||
# :func => ChatAgent.winestock,
|
||||
# ),
|
||||
"finalanswer"=> Dict(
|
||||
:description => "<tool description>Useful for when you are ready to recommend wines to the user.</tool description>",
|
||||
:input => """<input format>{\"finalanswer\": \"some text\"}.</input format><input example>{\"finalanswer\": \"I recommend Zena Crown Vista\"}</input example>""",
|
||||
:output => "" ,
|
||||
:func => nothing,
|
||||
),
|
||||
)
|
||||
|
||||
a = YiemAgent.sommelier(
|
||||
receiveUserMsgChannel,
|
||||
receiveInternalMsgChannel,
|
||||
agentConfig,
|
||||
name= "assistant",
|
||||
id= "testingSessionID", # agent instance id
|
||||
tools=tools,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
input =
|
||||
OrderedDict{Symbol, Any}(:question => "Hello, I would like a get a bottle of wine", :thought_1 => "It's great that the user is looking for a bottle of wine. To give them a personalized recommendation, I need to know more about their preferences.", :action_1 => Dict{Symbol, Any}(:name => "chatbox", :input => "What occasion are you planning to use this wine for?"), :observation_1 => "We are holding a wedding party", :thought_2 => "A wedding party is a great occasion for a special bottle of wine. I need to know what type of food will be served, and how much the user is willing to spend.", :action_2 => Dict{Symbol, Any}(:name => "chatbox", :input => "What type of food will you be serving at the wedding?"), :observation_2 => "It will be Thai dishes.", :thought_3 => "The type of wine that pairs well with Thai dishes is usually a crisp and refreshing white wine, but I also need to consider the budget and personal preferences.", :action_3 => Dict{Symbol, Any}(:name => "chatbox", :input => "How much are you willing to spend on this bottle of wine?"), :observation_3 => "I would spend up to 50 bucks.", :thought_4 => "I have a good idea of the occasion, food, and budget. Now I need to know what type of wine the user is looking for.", :action_4 => Dict{Symbol, Any}(:name => "chatbox", :input => "What type of wine are you usually looking for? Red, White, Sparkling, Rose, Dessert or Fortified?"), :observation_4 => "I like full-bodied Red wine with low tannin.", :thought_5 => "Now that I have all the necessary information, I can start searching for a suitable wine in our inventory.", :action_5 => Dict{Symbol, Any}(:name => "winestock", :input => "red wine with low tannins"), :observation_5 => "I found the following wines in our stock: \n{\n 1: El Enemigo Cabernet Franc 2019\n2: Tantara Chardonnay 2017\n\n}\n", :thought_6 => "Now that I have the information about the wine, it's time to make a recommendation.", :action_6 => Dict{Symbol, Any}(:name => "recommendbox", :input => "El Enemigo Cabernet Franc 2019"), :observation_6 => "I don't like the one you recommend. I want dry wine.")
|
||||
|
||||
|
||||
result = YiemAgent.jsoncorrection(a, input)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,119 +1,119 @@
|
||||
using Revise
|
||||
using YiemAgent, GeneralUtils, JSON3, DataStructures, LibPQ
|
||||
using SQLLLM
|
||||
|
||||
|
||||
# _prompt =
|
||||
# """
|
||||
# You are a helpful assistant.
|
||||
# answer the following question:
|
||||
# From the following CSV text:
|
||||
# "{\"tabledescription\":[\"The customer table stores information about customers. It includes details such as first name, last name, display name, username, password, gender, country, telephone number, email, birthdate, additional_search_term, other attributes (in JSON format) and a description.\",\"The wine table stores information about different wines. It includes details namely id, name, brand, manufacturer, region, country, wine_type, grape_variety, serving_temperature, intensity, sweetness, tannin, acidity, fizziness, additional_search_term, other attributes (in JSON format) and a description.\",\"The wine_food table represents the association between wines and food items. It estab" ⋯ 477 bytes ⋯ "ed to retailer names, usernames, passwords, addresses, contact persons, telephone numbers, email addresses, additional_search_term, other attributes (in JSON format) and a description.\",\"The retailer_wine table represents the relationship between retailers and wines. It stores information about the wines available from which retailers, including vintage, their price, and the currency.\",\"The retailer_food table represents the relationship between retailers and food items. It stores information about the food items available from which retailers, including their price and the currency.\"],\"tablename\":[\"customer\",\"wine\",\"wine_food\",\"food\",\"retailer\",\"retailer_wine\",\"retailer_food\"]}"
|
||||
# What is the description of table wine?
|
||||
# """
|
||||
|
||||
# prompt = YiemAgent.formatLLMtext_llama3instruct("system", _prompt)
|
||||
# @show prompt
|
||||
# msgMeta = Dict(:requestResponse => nothing,
|
||||
# :msgPurpose => nothing,
|
||||
# :receiverId => nothing,
|
||||
# :getPost => nothing,
|
||||
# :msgId => "4c7111e0-c30e-44c3-8f85-1c8b3f03a8be",
|
||||
# :acknowledgestatus => nothing,
|
||||
# :replyToMsgId => nothing,
|
||||
# :msgFormatVersion => nothing,
|
||||
# :mqttServerInfo => Dict(:port => 1883, :broker => "mqtt.yiem.cc"),
|
||||
# :sendTopic => "/loadbalancer/requestingservice",
|
||||
# :receiverName => "text2textinstruct",
|
||||
# :replyTopic => nothing,
|
||||
# :senderName => "decisionMaker",
|
||||
# :senderSelfnote => nothing,
|
||||
# :senderId => "testingSessionID",
|
||||
# :timeStamp => "2024-05-04T08:06:23.561"
|
||||
# )
|
||||
|
||||
# outgoingMsg = Dict(
|
||||
# :msgMeta=> msgMeta,
|
||||
# :payload=> Dict(
|
||||
# :text=> prompt,
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
|
||||
# _response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
# result = _response[:response][:text]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
DBconnection = LibPQ.Connection("host=192.168.88.12 port=5432 dbname=yiem_wine_assistant user=yiem password=yiem@Postgres_0.0")
|
||||
|
||||
tableinfo, df1, df2, df3 = SQLLLM.tableinfo(DBconnection, "wine")
|
||||
|
||||
|
||||
_prompt =
|
||||
"""
|
||||
You are a helpful assistant helping to answer user question from a database table.
|
||||
|
||||
$tableinfo
|
||||
|
||||
Are there any chardonnay?
|
||||
"""
|
||||
|
||||
prompt = YiemAgent.formatLLMtext_llama3instruct("system", _prompt)
|
||||
@show prompt
|
||||
msgMeta = Dict(:requestResponse => nothing,
|
||||
:msgPurpose => nothing,
|
||||
:receiverId => nothing,
|
||||
:getPost => nothing,
|
||||
:msgId => "4c7111e0-c30e-44c3-8f85-1c8b3f03a8be",
|
||||
:acknowledgestatus => nothing,
|
||||
:replyToMsgId => nothing,
|
||||
:msgFormatVersion => nothing,
|
||||
:mqttServerInfo => Dict(:port => 1883, :broker => "mqtt.yiem.cc"),
|
||||
:sendTopic => "/loadbalancer/requestingservice",
|
||||
:receiverName => "text2textinstruct",
|
||||
:replyTopic => nothing,
|
||||
:senderName => "decisionMaker",
|
||||
:senderSelfnote => nothing,
|
||||
:senderId => "testingSessionID",
|
||||
:timeStamp => "2024-05-04T08:06:23.561"
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
result2 = _response[:response][:text]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using Revise
|
||||
using YiemAgent, GeneralUtils, JSON3, DataStructures, LibPQ
|
||||
using SQLLLM
|
||||
|
||||
|
||||
# _prompt =
|
||||
# """
|
||||
# You are a helpful assistant.
|
||||
# answer the following question:
|
||||
# From the following CSV text:
|
||||
# "{\"tabledescription\":[\"The customer table stores information about customers. It includes details such as first name, last name, display name, username, password, gender, country, telephone number, email, birthdate, additional_search_term, other attributes (in JSON format) and a description.\",\"The wine table stores information about different wines. It includes details namely id, name, brand, manufacturer, region, country, wine_type, grape_variety, serving_temperature, intensity, sweetness, tannin, acidity, fizziness, additional_search_term, other attributes (in JSON format) and a description.\",\"The wine_food table represents the association between wines and food items. It estab" ⋯ 477 bytes ⋯ "ed to retailer names, usernames, passwords, addresses, contact persons, telephone numbers, email addresses, additional_search_term, other attributes (in JSON format) and a description.\",\"The retailer_wine table represents the relationship between retailers and wines. It stores information about the wines available from which retailers, including vintage, their price, and the currency.\",\"The retailer_food table represents the relationship between retailers and food items. It stores information about the food items available from which retailers, including their price and the currency.\"],\"tablename\":[\"customer\",\"wine\",\"wine_food\",\"food\",\"retailer\",\"retailer_wine\",\"retailer_food\"]}"
|
||||
# What is the description of table wine?
|
||||
# """
|
||||
|
||||
# prompt = YiemAgent.formatLLMtext_llama3instruct("system", _prompt)
|
||||
# @show prompt
|
||||
# msgMeta = Dict(:requestResponse => nothing,
|
||||
# :msgPurpose => nothing,
|
||||
# :receiverId => nothing,
|
||||
# :getPost => nothing,
|
||||
# :msgId => "4c7111e0-c30e-44c3-8f85-1c8b3f03a8be",
|
||||
# :acknowledgestatus => nothing,
|
||||
# :replyToMsgId => nothing,
|
||||
# :msgFormatVersion => nothing,
|
||||
# :mqttServerInfo => Dict(:port => 1883, :broker => "mqtt.yiem.cc"),
|
||||
# :sendTopic => "/loadbalancer/requestingservice",
|
||||
# :receiverName => "text2textinstruct",
|
||||
# :replyTopic => nothing,
|
||||
# :senderName => "decisionMaker",
|
||||
# :senderSelfnote => nothing,
|
||||
# :senderId => "testingSessionID",
|
||||
# :timeStamp => "2024-05-04T08:06:23.561"
|
||||
# )
|
||||
|
||||
# outgoingMsg = Dict(
|
||||
# :msgMeta=> msgMeta,
|
||||
# :payload=> Dict(
|
||||
# :text=> prompt,
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
|
||||
# _response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
# result = _response[:response][:text]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
DBconnection = LibPQ.Connection("host=192.168.88.12 port=5432 dbname=yiem_wine_assistant user=yiem password=yiem@Postgres_0.0")
|
||||
|
||||
tableinfo, df1, df2, df3 = SQLLLM.tableinfo(DBconnection, "wine")
|
||||
|
||||
|
||||
_prompt =
|
||||
"""
|
||||
You are a helpful assistant helping to answer user question from a database table.
|
||||
|
||||
$tableinfo
|
||||
|
||||
Are there any chardonnay?
|
||||
"""
|
||||
|
||||
prompt = YiemAgent.formatLLMtext_llama3instruct("system", _prompt)
|
||||
@show prompt
|
||||
msgMeta = Dict(:requestResponse => nothing,
|
||||
:msgPurpose => nothing,
|
||||
:receiverId => nothing,
|
||||
:getPost => nothing,
|
||||
:msgId => "4c7111e0-c30e-44c3-8f85-1c8b3f03a8be",
|
||||
:acknowledgestatus => nothing,
|
||||
:replyToMsgId => nothing,
|
||||
:msgFormatVersion => nothing,
|
||||
:mqttServerInfo => Dict(:port => 1883, :broker => "mqtt.yiem.cc"),
|
||||
:sendTopic => "/loadbalancer/requestingservice",
|
||||
:receiverName => "text2textinstruct",
|
||||
:replyTopic => nothing,
|
||||
:senderName => "decisionMaker",
|
||||
:senderSelfnote => nothing,
|
||||
:senderId => "testingSessionID",
|
||||
:timeStamp => "2024-05-04T08:06:23.561"
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
result2 = _response[:response][:text]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
412
test/runtest.jl
412
test/runtest.jl
@@ -1,227 +1,255 @@
|
||||
using Revise # remove when this package is completed
|
||||
using YiemAgent, GeneralUtils, JSON3, MQTTClient, Dates, UUIDs, LibPQ, Base64, DataFrames
|
||||
using Revise
|
||||
using JSON, JSON3, MQTTClient, Dates, UUIDs, PrettyPrinting, LibPQ, Base64, DataFrames
|
||||
using YiemAgent, GeneralUtils
|
||||
using Base.Threads
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
config = copy(JSON3.read("config.json"))
|
||||
|
||||
# instanceInternalTopic = config[:serviceInternalTopic][:mqtttopic] * "/1"
|
||||
|
||||
# client, connection = MakeConnection(config[:mqttServerInfo][:broker],
|
||||
# config[:mqttServerInfo][:port])
|
||||
|
||||
receiveUserMsgChannel = Channel{Dict}(4)
|
||||
# receiveInternalMsgChannel = Channel{Dict}(4)
|
||||
# println(typeof(connection))
|
||||
# msgMeta = GeneralUtils.generate_msgMeta(
|
||||
# "N/A",
|
||||
# replyTopic = config[:servicetopic][:mqtttopic] # ask frontend reply to this instance_chat_topic
|
||||
# )
|
||||
# load config
|
||||
config = copy(JSON3.read("../mountvolume/config.json"))
|
||||
|
||||
function executeSQL(sql::T) where {T<:AbstractString}
|
||||
DBconnection = LibPQ.Connection("host=192.168.88.12 port=5432 dbname=yiem_wine_assistant user=yiem password=yiem@Postgres_0.0")
|
||||
DBconnection = LibPQ.Connection("host=192.168.88.12 port=10201 dbname=wineDB user=yiemtechnologies password=yiemtechnologies@Postgres_0.0")
|
||||
result = LibPQ.execute(DBconnection, sql)
|
||||
close(DBconnection)
|
||||
return result
|
||||
end
|
||||
|
||||
function executeSQLVectorDB(sql)
|
||||
DBconnection = LibPQ.Connection("host=192.168.88.12 port=10203 dbname=SQLVectorDB user=yiemtechnologies password=yiemtechnologies@Postgres_0.0")
|
||||
result = LibPQ.execute(DBconnection, sql)
|
||||
close(DBconnection)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
function text2textInstructLLM(prompt::String)
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
config[:externalservice][:text2textinstruct][:mqtttopic];
|
||||
msgPurpose= "inference",
|
||||
senderName= "yiemagent",
|
||||
senderId= string(uuid4()),
|
||||
receiverName= "text2textinstruct",
|
||||
mqttBrokerAddress= config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort= config[:mqttServerInfo][:port],
|
||||
msgPurpose="inference",
|
||||
senderName="yiemagent",
|
||||
senderId=string(uuid4()),
|
||||
receiverName="text2textinstruct",
|
||||
mqttBrokerAddress=config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort=config[:mqttServerInfo][:port],
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> prompt,
|
||||
:kwargs=> Dict(
|
||||
:max_tokens=> 2048,
|
||||
:stop=> ["<|eot_id|>"],
|
||||
:temperature=> 0.2,
|
||||
)
|
||||
:msgMeta => msgMeta,
|
||||
:payload => Dict(
|
||||
:text => prompt,
|
||||
:kwargs => Dict(
|
||||
:num_ctx => 16384,
|
||||
:temperature => 0.2,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)
|
||||
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=6000)
|
||||
response = _response[:response][:text]
|
||||
|
||||
return response
|
||||
end
|
||||
|
||||
function executeSQLVectorDB(sql)
|
||||
DBconnection = LibPQ.Connection("host=192.168.88.12 port=5433 dbname=SQLVectorDB user=yiemtechnologies@gmail.com password=yiem@Postgres_0.0")
|
||||
result = LibPQ.execute(DBconnection, sql)
|
||||
close(DBconnection)
|
||||
return result
|
||||
end
|
||||
|
||||
function addSQLVectorDB(state)
|
||||
# get embedding of the query
|
||||
query = [state[:thoughtHistory][:question]]
|
||||
# get text embedding from a LLM service
|
||||
function getEmbedding(text::T) where {T<:AbstractString}
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
config[:externalservice][:text2textinstruct][:mqtttopic];
|
||||
msgPurpose= "embedding",
|
||||
senderName= "yiemagent",
|
||||
senderId= string(uuid4()),
|
||||
receiverName= "text2textinstruct",
|
||||
mqttBrokerAddress= config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort= config[:mqttServerInfo][:port],
|
||||
msgPurpose="embedding",
|
||||
senderName="yiemagent",
|
||||
senderId=string(uuid4()),
|
||||
receiverName="text2textinstruct",
|
||||
mqttBrokerAddress=config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort=config[:mqttServerInfo][:port],
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> query
|
||||
)
|
||||
:msgMeta => msgMeta,
|
||||
:payload => Dict(
|
||||
:text => [text] # must be a vector of string
|
||||
)
|
||||
)
|
||||
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
embedding = response[:response][:embeddings][1]
|
||||
embedding = response[:response][:embeddings]
|
||||
return embedding
|
||||
end
|
||||
|
||||
function findSimilarTextFromVectorDB(text::T1, tablename::T2, embeddingColumnName::T3,
|
||||
vectorDB::Function; limit::Integer=1
|
||||
)::DataFrame where {T1<:AbstractString, T2<:AbstractString, T3<:AbstractString}
|
||||
|
||||
# get embedding from LLM service
|
||||
embedding = getEmbedding(text)[1]
|
||||
|
||||
# check whether there is close enough vector already store in vectorDB. if no, add, else skip
|
||||
sql =
|
||||
"""
|
||||
SELECT *, embedding <-> '$embedding' as distance
|
||||
FROM sql_statement_repository
|
||||
ORDER BY distance LIMIT 1;
|
||||
"""
|
||||
response = executeSQLVectorDB(sql)
|
||||
sql = """
|
||||
SELECT *, $embeddingColumnName <-> '$embedding' as distance
|
||||
FROM $tablename
|
||||
ORDER BY distance LIMIT $limit;
|
||||
"""
|
||||
response = vectorDB(sql)
|
||||
df = DataFrame(response)
|
||||
return df
|
||||
end
|
||||
|
||||
|
||||
function similarSQLVectorDB(query; maxdistance::Integer=100)
|
||||
tablename = "sqlllm_decision_repository"
|
||||
# get embedding of the query
|
||||
df = findSimilarTextFromVectorDB(query, 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 SQL, return it.
|
||||
output_b64 = df[1, :function_output_base64] # pick the closest match
|
||||
output_str = String(base64decode(output_b64))
|
||||
rowid = df[1, :id]
|
||||
println("\n~~~ found similar sql. row id $rowid, distance $distance ", @__FILE__, " ", @__LINE__)
|
||||
return (dict=output_str, distance=distance)
|
||||
else
|
||||
println("\n~~~ similar sql not found, max distance $maxdistance ", @__FILE__, " ", @__LINE__)
|
||||
return (dict=nothing, distance=nothing)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function insertSQLVectorDB(query::T1, SQL::T2; maxdistance::Integer=1) where {T1<:AbstractString, T2<:AbstractString}
|
||||
tablename = "sqlllm_decision_repository"
|
||||
# get embedding of the query
|
||||
# query = state[:thoughtHistory][:question]
|
||||
df = findSimilarTextFromVectorDB(query, tablename,
|
||||
"function_input_embedding", executeSQLVectorDB)
|
||||
row, col = size(df)
|
||||
distance = row == 0 ? Inf : df[1, :distance]
|
||||
if row == 0 || distance > 10 # no close enough SQL stored in the database
|
||||
latestKey, _ = GeneralUtils.findHighestIndexKey(state[:thoughtHistory], :action_input)
|
||||
_sqlStatement = state[:thoughtHistory][latestKey]
|
||||
if occursin("SELECT", _sqlStatement) # make sure it is an SQL statement before adding into DB
|
||||
sqlStatementBase64 = base64encode(_sqlStatement)
|
||||
sqlStatement = replace(_sqlStatement, "'"=>"")
|
||||
sql =
|
||||
"""
|
||||
INSERT INTO sql_statement_repository (question, sql_statement, sql_statement_base64, embedding) VALUES ('$query', '$sqlStatement', '$sqlStatementBase64', '$embedding');
|
||||
"""
|
||||
_ = executeSQLVectorDB(sql)
|
||||
println("--> added new SQL statement to vectorDB ", @__FILE__, " ", @__LINE__)
|
||||
println(sqlStatement)
|
||||
end
|
||||
if row == 0 || distance > maxdistance # no close enough SQL stored in the database
|
||||
query_embedding = getEmbedding(query)[1]
|
||||
query = replace(query, "'" => "")
|
||||
sql_base64 = base64encode(SQL)
|
||||
sql_ = replace(SQL, "'" => "")
|
||||
|
||||
sql = """
|
||||
INSERT INTO $tablename (function_input, function_output, function_output_base64, function_input_embedding) VALUES ('$query', '$sql_', '$sql_base64', '$query_embedding');
|
||||
"""
|
||||
println("\n~~~ added new decision to vectorDB ", @__FILE__, " ", @__LINE__)
|
||||
println(sql)
|
||||
_ = executeSQLVectorDB(sql)
|
||||
end
|
||||
end
|
||||
|
||||
function querySQLVectorDB(state)
|
||||
|
||||
# provide similarSQL at the first time thinking only
|
||||
latestKey, _ = GeneralUtils.findHighestIndexKey(state[:thoughtHistory], :action_input)
|
||||
if latestKey === nothing
|
||||
# get embedding of the query
|
||||
query = [state[:thoughtHistory][:question]]
|
||||
msgMeta = GeneralUtils.generate_msgMeta(
|
||||
config[:externalservice][:text2textinstruct][:mqtttopic];
|
||||
msgPurpose= "embedding",
|
||||
senderName= "yiemagent",
|
||||
senderId= string(uuid4()),
|
||||
receiverName= "text2textinstruct",
|
||||
mqttBrokerAddress= config[:mqttServerInfo][:broker],
|
||||
mqttBrokerPort= config[:mqttServerInfo][:port],
|
||||
)
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> query
|
||||
)
|
||||
)
|
||||
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
|
||||
embedding = response[:response][:embeddings][1]
|
||||
|
||||
# check whether there is close enough vector already store in vectorDB. if no, add, else skip
|
||||
sql =
|
||||
"""
|
||||
SELECT *, embedding <-> '$embedding' as distance
|
||||
FROM sql_statement_repository
|
||||
ORDER BY distance LIMIT 1;
|
||||
"""
|
||||
response = executeSQLVectorDB(sql)
|
||||
df = DataFrame(response)
|
||||
row, col = size(df)
|
||||
distance = row == 0 ? Inf : df[1, :distance]
|
||||
if row != 0 && distance < 100
|
||||
# if there is usable SQL, return it.
|
||||
sqlStatementBase64 = df[1, :sql_statement_base64]
|
||||
sqlStatement = String(base64decode(sqlStatementBase64))
|
||||
println("--> getting SQL statement from vectorDB ", @__FILE__, " ", @__LINE__)
|
||||
println(sqlStatement)
|
||||
return sqlStatement
|
||||
else
|
||||
return nothing
|
||||
end
|
||||
function similarSommelierDecision(recentevents::T1; maxdistance::Integer=5
|
||||
)::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(JSON3.read(_output_str))
|
||||
return output
|
||||
else
|
||||
println("\n~~~ similar decision not found, max distance $maxdistance ", @__FILE__, " ", @__LINE__)
|
||||
return nothing
|
||||
end
|
||||
return nothing
|
||||
end
|
||||
|
||||
|
||||
# Instantiate an agent
|
||||
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 = a.func[:getEmbedding](recentevents)[1]
|
||||
recentevents = replace(recentevents, "'" => "")
|
||||
decision_json = JSON3.write(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 = "12345"
|
||||
|
||||
externalFunction = (
|
||||
getEmbedding=getEmbedding,
|
||||
text2textInstructLLM=text2textInstructLLM,
|
||||
executeSQL=executeSQL,
|
||||
similarSQLVectorDB=similarSQLVectorDB,
|
||||
insertSQLVectorDB=insertSQLVectorDB,
|
||||
similarSommelierDecision=similarSommelierDecision,
|
||||
insertSommelierDecision=insertSommelierDecision,
|
||||
)
|
||||
|
||||
|
||||
|
||||
a = YiemAgent.sommelier(
|
||||
text2textInstructLLM,
|
||||
executeSQL,
|
||||
querySQLVectorDB,
|
||||
addSQLVectorDB;
|
||||
name= "Jene",
|
||||
id= "tempId", # agent instance id
|
||||
externalFunction;
|
||||
name="Ton",
|
||||
id=sessionId, # agent instance id
|
||||
retailername="Yiem",
|
||||
)
|
||||
|
||||
function main()
|
||||
for i in 1:10
|
||||
userinput = ""
|
||||
for i in 1:3
|
||||
if userinput == ""
|
||||
println("")
|
||||
println("--> user input:")
|
||||
userinput = readline()
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
response = YiemAgent.conversation(a, Dict(:text=> userinput))
|
||||
println("")
|
||||
println("--> assistant response: \n", response)
|
||||
end
|
||||
while true
|
||||
println("your respond: ")
|
||||
user_answer = readline()
|
||||
response = YiemAgent.conversation(a, Dict(:text=> user_answer))
|
||||
println("\n$response")
|
||||
end
|
||||
|
||||
main()
|
||||
|
||||
"""
|
||||
I'm joining a graduation party this evening. I want to get a bottle of white wine from the US to celebrate. I'm ok with any price range.
|
||||
Well, the party is small casual with close friends and no food serving.
|
||||
I'm open to suggestion since I have no specific idea.
|
||||
I'm ok with any region.
|
||||
|
||||
|
||||
|
||||
|
||||
The input is instructions on how you want the presentation to be conducted.
|
||||
"""
|
||||
|
||||
|
||||
wines =
|
||||
"""
|
||||
Summary: This table contains two wine records, both from the United States, with white wine types, moderate sweetness (2), and high intensity (5).
|
||||
More details: 1) wine_id: add9824f-81b0-47da-a08a-ee20498bc6c8, wine_name: Belle Cote Chardonnay, brand: Peter Michael, manufacturer: Peter Michael, region: Californian, country: United States, wine_type: white, grape_variety: Chardonnay, serving_temperature: 11 to 13 Celsius, intensity: 5, sweetness: 2, tannin: missing, acidity: 3, fizziness: missing, tasting_notes: oak, butter, vanilla, cream, oil, lemon curd, pear, peach, apple
|
||||
2) wine_id: ff9a494c-e916-44c4-9385-1c18b23aa825, wine_name: Ma Belle-Fille Chardonnay, brand: Peter Michael, manufacturer: Peter Michael, region: Californian, country: United States, wine_type: white, grape_variety: Chardonnay, serving_temperature: 11 to 13 Celsius, intensity: 5, sweetness: 2, tannin: missing, acidity: 3, fizziness: missing, tasting_notes: oak, butter, vanilla, cream, banana, cheese, apricot, peach, apple
|
||||
"""
|
||||
|
||||
|
||||
|
||||
# response = YiemAgent.conversation(a, Dict(:text=> "newtopic",) )
|
||||
|
||||
# response = YiemAgent.conversation(a, Dict(:text=> "Hello, I would like a get a bottle of wine."))
|
||||
# println("---> YiemAgent: ", response)
|
||||
|
||||
# response = YiemAgent.conversation(a, Dict(:text=> "I'm having a graduation party this evening. I'll pay at most 30 bucks."))
|
||||
# response = YiemAgent.conversation(a, Dict(:text=> "I want to get a French red wine under 100."))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -229,60 +257,16 @@ More details: 1) wine_id: add9824f-81b0-47da-a08a-ee20498bc6c8, wine_name: Belle
|
||||
|
||||
|
||||
|
||||
# # input = "query=\"off dry, medium tannin, French Rosé\""
|
||||
# input = "Search the database for wine type: white, country: France, sweetness level: 1"
|
||||
# YiemAgent.winestock(a, input)
|
||||
|
||||
|
||||
|
||||
|
||||
# input = "French dry white wines with medium body"
|
||||
# input = "query=\"medium-bodied dry white wine\""
|
||||
# # input = "the customer is looking for a medium-bodied, dry white wine."
|
||||
# result = YiemAgent.checkinventory(a, input)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
error("test done")
|
||||
|
||||
|
||||
|
||||
|
||||
function run_with_timeout(f, args...; timeout=5)
|
||||
result = Ref{Any}()
|
||||
task = Threads.@spawn try
|
||||
result[] = f(args...)
|
||||
catch e
|
||||
println("Task interrupted: ", e)
|
||||
end
|
||||
|
||||
Timer(timeout) do _
|
||||
if !istaskdone(task)
|
||||
schedule(task, InterruptException())
|
||||
println("Task did not complete in time. Aborting.")
|
||||
else
|
||||
println("Task completed within the timeout.")
|
||||
end
|
||||
end
|
||||
|
||||
return result[]
|
||||
end
|
||||
|
||||
# Example function that takes arguments and returns a value
|
||||
function example_function(x, y)
|
||||
sleep(10) # Simulate a long-running task
|
||||
return x + y
|
||||
end
|
||||
|
||||
# Example usage
|
||||
result = run_with_timeout(example_function, 3, 4; timeout=5)
|
||||
println("Result: ", result)
|
||||
|
||||
|
||||
a = `$"hello\nworld"`
|
||||
a = $"hello\nworld"
|
||||
|
||||
a = """$("hello\nworld")"""
|
||||
|
||||
|
||||
192
test/test_1.jl
192
test/test_1.jl
@@ -1,192 +0,0 @@
|
||||
using Revise
|
||||
using YiemAgent, GeneralUtils, JSON3, DataStructures
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
msgMeta = Dict(:requestResponse => nothing,
|
||||
:msgPurpose => nothing,
|
||||
:receiverId => nothing,
|
||||
:getPost => nothing,
|
||||
:msgId => "4c7111e0-c30e-44c3-8f85-1c8b3f03a8be",
|
||||
:acknowledgestatus => nothing,
|
||||
:replyToMsgId => "dummyid",
|
||||
:msgFormatVersion => nothing,
|
||||
:mqttServerInfo => Dict(:port => 1883, :broker => "mqtt.yiem.cc"),
|
||||
:sendTopic => "/testingSessionID",
|
||||
:receiverName => "wineassistant",
|
||||
:replyTopic => nothing,
|
||||
:senderName => "test_1",
|
||||
:senderSelfnote => nothing,
|
||||
:senderId => nothing,
|
||||
:timeStamp => "2024-05-04T08:06:23.561"
|
||||
)
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "We are holding a wedding party",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "It will be Thai dishes.",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "I would spend up to 50 bucks.",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "I like dry, full-bodied red wine with low tannin.",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "What do you have?",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "Dry wine please.",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "You did not gave me any choice.",
|
||||
:select=> nothing,
|
||||
:reward=> -1,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "You don't need to ask me. Just tell me already!",
|
||||
:select=> nothing,
|
||||
:reward=> -1,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "I like dry wine with fruity flavors.",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "OK, I'll take it.",
|
||||
:select=> 1,
|
||||
:reward=> 1,
|
||||
:isterminal=> true,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "Yes.",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
outgoingMsg = Dict(
|
||||
:msgMeta=> msgMeta,
|
||||
:payload=> Dict(
|
||||
:text=> "I like dry wine with medium acidity.",
|
||||
:select=> nothing,
|
||||
:reward=> 0,
|
||||
:isterminal=> false,
|
||||
)
|
||||
)
|
||||
result = GeneralUtils.sendMqttMsg(outgoingMsg)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import requests
|
||||
|
||||
# URL of the API endpoint
|
||||
url = 'https://api.yiem.cc/wine/agent/sommelier/prompt/apiv1'
|
||||
|
||||
# Data to be sent in the JSON request
|
||||
data = {
|
||||
'sid': 'dummySID',
|
||||
'txt': 'hello'
|
||||
}
|
||||
|
||||
# Sending the POST request
|
||||
response = requests.post(url, json=data)
|
||||
|
||||
# Displaying the response
|
||||
print('Status Code:', response.status_code)
|
||||
print('Response JSON:', response.json())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user