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]






















