using Revise
using LibPQ, JSON3, PrettyPrinting, UUIDs, DataFrames, DataStructures, Dates, MQTTClient, Random
using SQLLLM, GeneralUtils


function executeSQL(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


sql = 
"""
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
"""

result = executeSQL(sql)



sql = 
"""
INSERT INTO items (embedding) VALUES ('[[1,2,3], [1,2,3], [1,2,3]]'), ('[4,5,6]');
"""
result = executeSQL(sql)


sql = 
"""
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 1;
"""
result = executeSQL(sql)
df = DataFrame(result)






config = copy(JSON3.read("config.json"))

msgMeta = GeneralUtils.generate_msgMeta(
    config[:externalservice][:text2textinstruct][:mqtttopic];
    msgPurpose= "embedding",
    senderName= "yiemagent",
    senderId= string(uuid4()),
    receiverName= "text2textinstruct",
    mqttBrokerAddress= "mqtt.yiem.cc",
    mqttBrokerPort= 1883,
  )

text = ["hello world"]

outgoingMsg = Dict(
  :msgMeta=> msgMeta,
  :payload=> Dict(
    :text=> text,
    :kwargs=> Dict(
      :max_tokens=> 2048,
      :stop=> ["<|eot_id|>"],
      :temperature=> 0.2,
      )
    )
  )



response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)


















