27 Commits

Author SHA1 Message Date
a4227ec165 update 2025-07-23 18:31:38 +07:00
narawat lamaiin
21416f4b13 update 2025-06-15 08:59:10 +07:00
narawat lamaiin
ff4db039ab update 2025-06-03 10:08:17 +07:00
narawat lamaiin
b3537a83e0 update 2025-05-18 17:21:55 +07:00
narawat lamaiin
0a0e36d86a update 2025-05-17 21:36:40 +07:00
narawat lamaiin
8c5b1b6938 update 2025-05-14 21:21:30 +07:00
narawat lamaiin
aeda7e0baf update 2025-05-06 06:49:21 +07:00
narawat lamaiin
2541223bbb update 2025-05-04 20:56:55 +07:00
narawat lamaiin
c8f5983620 update 2025-05-04 13:30:08 +07:00
narawat lamaiin
5112701dc2 update 2025-05-01 07:59:18 +07:00
narawat lamaiin
bf223b64b2 update 2025-04-27 22:32:22 +07:00
narawat lamaiin
d9c842bba5 update 2025-04-25 21:13:12 +07:00
narawat lamaiin
b8fd331c1a update 2025-04-13 21:45:58 +07:00
narawat lamaiin
00b0ab01a4 update 2025-04-04 15:05:16 +07:00
narawat lamaiin
fd5ac82662 update 2025-04-01 21:17:03 +07:00
narawat lamaiin
bc0f735ab7 update 2025-03-22 20:26:41 +07:00
3d03a4d351 update 2025-03-21 10:04:22 +07:00
568e0ff54f update 2025-03-20 16:08:40 +07:00
83a20faab6 update 2025-03-19 19:11:06 +07:00
418c543d44 update 2025-03-19 11:29:31 +07:00
e6ce6f9954 update 2025-03-18 21:22:12 +07:00
7fd0d6269a update 2025-03-18 08:37:35 +07:00
e391547991 update 2025-03-18 07:54:23 +07:00
7c9ceb06f8 update 2025-03-18 07:34:51 +07:00
14c881741e update 2025-03-16 22:11:23 +07:00
0873b1341f mark new version 2025-03-15 11:43:05 +07:00
ton
00ec7328e7 Merge pull request 'v0.2.3' (#2) from v0.2.3 into main
Reviewed-on: #2
2025-03-15 01:45:32 +00:00
11 changed files with 1237 additions and 336 deletions

View File

@@ -1,6 +1,6 @@
# This file is machine-generated - editing it directly is not advised # This file is machine-generated - editing it directly is not advised
julia_version = "1.11.3" julia_version = "1.11.4"
manifest_format = "2.0" manifest_format = "2.0"
project_hash = "9e0d7dca51b949f2ffa5477b895b90988ec62529" project_hash = "9e0d7dca51b949f2ffa5477b895b90988ec62529"
@@ -202,7 +202,7 @@ version = "1.11.0"
deps = ["CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "JSON3", "MQTTClient", "PrettyPrinting", "Random", "SHA", "UUIDs"] deps = ["CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "JSON3", "MQTTClient", "PrettyPrinting", "Random", "SHA", "UUIDs"]
path = "../GeneralUtils" path = "../GeneralUtils"
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
version = "0.2.2" version = "0.2.3"
[[deps.HTTP]] [[deps.HTTP]]
deps = ["Base64", "CodecZlib", "ConcurrentUtilities", "Dates", "ExceptionUnwrapping", "Logging", "LoggingExtras", "MbedTLS", "NetworkOptions", "OpenSSL", "PrecompileTools", "Random", "SimpleBufferStream", "Sockets", "URIs", "UUIDs"] deps = ["Base64", "CodecZlib", "ConcurrentUtilities", "Dates", "ExceptionUnwrapping", "Logging", "LoggingExtras", "MbedTLS", "NetworkOptions", "OpenSSL", "PrecompileTools", "Random", "SimpleBufferStream", "Sockets", "URIs", "UUIDs"]
@@ -306,7 +306,7 @@ version = "1.19.3+0"
deps = ["GeneralUtils", "JSON3", "PrettyPrinting"] deps = ["GeneralUtils", "JSON3", "PrettyPrinting"]
path = "../LLMMCTS" path = "../LLMMCTS"
uuid = "d76c5a4d-449e-4835-8cc4-dd86ec44f241" uuid = "d76c5a4d-449e-4835-8cc4-dd86ec44f241"
version = "0.1.3" version = "0.1.4"
[[deps.LaTeXStrings]] [[deps.LaTeXStrings]]
git-tree-sha1 = "dda21b8cbd6a6c40d9d02a73230f9d70fed6918c" git-tree-sha1 = "dda21b8cbd6a6c40d9d02a73230f9d70fed6918c"
@@ -471,7 +471,7 @@ version = "0.3.27+1"
[[deps.OpenLibm_jll]] [[deps.OpenLibm_jll]]
deps = ["Artifacts", "Libdl"] deps = ["Artifacts", "Libdl"]
uuid = "05823500-19ac-5b8b-9628-191a04bc5112" uuid = "05823500-19ac-5b8b-9628-191a04bc5112"
version = "0.8.1+2" version = "0.8.1+4"
[[deps.OpenSSL]] [[deps.OpenSSL]]
deps = ["BitFlags", "Dates", "MozillaCACerts_jll", "OpenSSL_jll", "Sockets"] deps = ["BitFlags", "Dates", "MozillaCACerts_jll", "OpenSSL_jll", "Sockets"]

View File

@@ -1,7 +1,7 @@
name = "SQLLLM" name = "SQLLLM"
uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3" uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3"
authors = ["narawat lamaiin <narawat@outlook.com>"] authors = ["narawat lamaiin <narawat@outlook.com>"]
version = "0.2.3" version = "0.2.4"
[deps] [deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,10 @@
module llmfunction module llmfunction
export listAllTable_json, listAllTable_str, tableinfo, getdata, finalAnswerBox, export listAllTable_json, listAllTable_str, tableinfo, getdata, finalAnswerBox,
getTableNameFromSQL, extractContent_dataframe, SQLexecution getTableNameFromSQL, extractContent_dataframe, SQLexecution, compareState
using HTTP, JSON3, URIs, Random, PrettyPrinting, UUIDs, LibPQ, Tables, DataFrames, CSV, using HTTP, JSON3, URIs, Random, PrettyPrinting, UUIDs, LibPQ, Tables, DataFrames, CSV,
DataStructures, StatsBase DataStructures, StatsBase, Dates
using GeneralUtils, LLMMCTS using GeneralUtils, LLMMCTS
using ..util using ..util
@@ -36,7 +36,7 @@ julia> result = response[:result]
# Signature # Signature
""" """
function listAllTable_json(executeSQL::Function function listAllTable_json(executeSQL::Function
)::NamedTuple{(:result, :success),Tuple{DataFrame,Bool}} )::NamedTuple{(:result, :success),Tuple{DataFrame,Bool}}
sql = """ sql = """
SELECT SELECT
@@ -347,8 +347,9 @@ end
# Signature # Signature
""" """
function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM::Function function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM::Function,
)::NamedTuple{(:thought, :code, :success, :errormsg),Tuple{Union{String,Nothing},Union{String,Nothing},Bool,Union{String,Nothing}}} llmFormatName::String
)::NamedTuple{(:thought, :code, :success, :errormsg),Tuple{Union{String,Nothing},Union{String,Nothing},Bool,Union{String,Nothing}}}
Hints = "None" Hints = "None"
@@ -366,17 +367,14 @@ function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM:
- Text information in the database is sometimes stored in lower case. If your search returns empty, try using lower case to search. - Text information in the database is sometimes stored in lower case. If your search returns empty, try using lower case to search.
You should then respond to the user with: You should then respond to the user with:
1) Comprehension: 1) Plan: Step-by-step instructions of how to complete the task.
- State your comprehension about the current situation.
3) Plan: Step-by-step instructions of how to complete the task.
- Focus on improving the code from the last round. - Focus on improving the code from the last round.
- Do not create any table in the database. - Do not create any table in the database.
4) Code: 2) Code:
- Write new improved code. - Write new improved code.
- Do not wrap the code and no comment as it will be executed directly without any modification against the database. - Do not wrap the code and no comment as it will be executed directly without any modification against the database.
You should only respond in format as described below and nothing more: You should only respond in format as described below and nothing more:
Comprehension: ...
Plan: Plan:
1) ... 1) ...
2) ... 2) ...
@@ -406,12 +404,14 @@ function getdata_decisionMaker(state::Dict, context::Dict, text2textInstructLLM:
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
try try
response = text2textInstructLLM(prompt) response = text2textInstructLLM(prompt, modelsize="medium")
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
think, response = GeneralUtils.extractthink(response)
header = ["Comprehension:", "Plan:", "Code:"] header = ["Plan:", "Code:"]
dictkey = ["comprehension", "plan", "code"] dictkey = ["plan", "code"]
responsedict = GeneralUtils.textToDict(response, header; responsedict = GeneralUtils.textToDict(response, header;
dictKey=dictkey, symbolkey=true) dictKey=dictkey, symbolkey=true)
@@ -517,9 +517,9 @@ function SQLexecution(executeSQL::Function, sql::T
tablesize = size(df) tablesize = size(df)
row, column = tablesize row, column = tablesize
if row == 0 if row == 0
error("The resulting table has 0 row. Possible causes: 1) Your search criteria might be too specific. Relaxing some conditions could yield better results. Remember, you can always refine your search later. 2) There could be a typo in your search query. 3) You might be searching in the wrong place.") error("\nThe resulting table has 0 row. Please try again.")
elseif column > 30 elseif column > 30
error("SQL execution failed. An unexpected error occurred. Please try again.") error("\nSQL execution failed. An unexpected error occurred. Please try again.")
end end
df1 = df1 =
@@ -560,8 +560,9 @@ end
# Signature # Signature
""" """
function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function, action::String function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function, action::String,
)::String llmFormatName::String
)::String
tablesize = size(df) tablesize = size(df)
row = tablesize[1] row = tablesize[1]
column = tablesize[2] column = tablesize[2]
@@ -627,22 +628,26 @@ function extractContent_dataframe(df::DataFrame, text2textInstructLLM::Function,
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
header = ["About_resulting_table:", "Search_summary:"] header = ["About_resulting_table:", "Search_summary:"]
dictkey = ["about_resulting_table", "search_summary"] dictkey = ["about_resulting_table", "search_summary"]
for i in 1:5 for i in 1:5
response = text2textInstructLLM(prompt) response = text2textInstructLLM(prompt, modelsize="medium")
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
think, response = GeneralUtils.extractthink(response)
kw = [] # check whether response has all header
# use for loop and detect_keyword function to get the exact variation of each keyword in the text then push to kw list detected_kw = GeneralUtils.detectKeywordVariation(header, response)
for keyword in header missingkeys = [k for (k, v) in detected_kw if v === nothing]
detected = GeneralUtils.detect_keyword(keyword, response) if !isempty(missingkeys)
push!(kw, detected) errornote = "$missingkeys are missing from your previous response"
end println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
if nothing kw continue
println("Some keywords are missing, Required keywords=$header, Response keywords=$kw ", @__FILE__, ":", @__LINE__, " $(Dates.now())") elseif sum([length(i) for i in values(detected_kw)]) > length(header)
continue # try again next loop errornote = "\nYour previous attempt has duplicated points according to the required response format"
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue
end end
responsedict = GeneralUtils.textToDict(response, header; responsedict = GeneralUtils.textToDict(response, header;
@@ -734,7 +739,9 @@ julia> result = SQLLLM.getTableNameFromSQL(sql, text2textInstructLLM)
# Signature # Signature
""" """
function getTableNameFromSQL(sql::T, text2textInstructLLM::Function)::Vector{String} where {T<:AbstractString} function getTableNameFromSQL(sql::T, text2textInstructLLM::Function,
llmFormatName::String
)::Vector{String} where {T<:AbstractString}
systemmsg = """ systemmsg = """
Extract table name out of the user query. Extract table name out of the user query.
@@ -762,13 +769,14 @@ function getTableNameFromSQL(sql::T, text2textInstructLLM::Function)::Vector{Str
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
header = ["Table_name:"] header = ["Table_name:"]
dictkey = ["table_name"] dictkey = ["table_name"]
for attempt in 1:5 for attempt in 1:5
try try
response = text2textInstructLLM(prompt) response = text2textInstructLLM(prompt, modelsize="medium")
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
responsedict = GeneralUtils.textToDict(response, header; responsedict = GeneralUtils.textToDict(response, header;
dictKey=dictkey, symbolkey=true) dictKey=dictkey, symbolkey=true)
response = copy(JSON3.read(responsedict[:table_name])) response = copy(JSON3.read(responsedict[:table_name]))
@@ -788,6 +796,174 @@ function getTableNameFromSQL(sql::T, text2textInstructLLM::Function)::Vector{Str
end end
""" Compare multiple solution attempts and select the most accurate one.
This function evaluates multiple solution attempts for a given question and determines which attempt
provides the most accurate and relevant response. It uses an LLM to analyze and compare the attempts,
considering their actions and observations.
# Arguments
- `question::String`
The original question or task that was attempted to be solved
- `highValueStateList::Vector{Dict}`
List of states containing different solution attempts and their results
- `text2textInstructLLM::Function`
A function that handles communication to LLM service
# Returns
- `Integer`
The index of the selected best response (1-based indexing)
# Example
```jldoctest
julia>
```
# Notes
- The function makes up to 10 attempts to get a valid response from the LLM
- Each state in highValueStateList should contain a thoughtHistory with action_input and observation
- The LLM evaluates attempts based on accuracy and relevance to the original question
"""
function compareState(question::String, highValueStateList::Vector{T},
text2textInstructLLM::Function, llmFormatName::String
)::Integer where {T<:AbstractDict}
systemmsg =
"""
Your profile:
- You are a helpful assistant
Situation:
- The user has made multiple attempts to solve the question, resulting in various answers
Your mission:
- Identify and select the most accurate and relevant response from these multiple results for the user
At each round of conversation, you will be given the following:
Question: the question the user is trying to answer
Attempt: the user's attempted actions and their corresponding results
You should then respond to the user with the following:
Comparison: detailed comparison of all results from all attempts from various aspects.
Rationale: a brief explanation of why the selected response is the most accurate and relevant
Selected_response_number: the number the selected response in the list of results (e.g., 1, 2, 3, ...)
You should only respond in format as described below:
Comparison: ...
Rationale: ...
Selected_response_number: ...
Here are some examples:
User's question: "How many German wines do you have?"
Attempt 1)
Action: SELECT COUNT(*) FROM wines WHERE country = 'Germany'
Result: 100 wines
Attempt 2)
Action: SELECT COUNT(*) FROM wines WHERE country = 'Germany' AND type = 'Red'
Result: 50 red wines
Comparison: The second attempt counts only German red wines while the first attempt includes all German wines.
Rationale: The user is asking for the number of German wines without specifying a type, so the most accurate response is the first attempt because it includes all German wines.
Selected_response_number:1
Let's begin!
"""
potentialSolution = []
keys = [:action_input, :observation]
# extract the last action_name, action_input, observation of each state in highValueStateList and store them in a dictionary then push into potentialSolution
for state in highValueStateList
thoughtHistory = state[:thoughtHistory]
_, currentstate_latestIndice =
GeneralUtils.findHighestIndexKey(thoughtHistory, keys[1])
latestKeys = makekey.(keys, currentstate_latestIndice)
d = Dict()
# get the last action_name, action_input, observation of currentstate
for (i,v) in enumerate(keys)
d[v] = thoughtHistory[latestKeys[i]]
end
push!(potentialSolution, d)
end
"""
# put potential solutions from potentialSolution into the following form
Attempt 1)
action_name:
action_input:
observation:
Attempt 2)
action_name:`
action_input:
observation:`
...
"""
potentialSolutionStr = ""
for (i, state) in enumerate(potentialSolution)
potentialSolutionStr *= "Attempt $i)\n"
for k in keys
potentialSolutionStr *= "$k: $(state[k])\n"
println("")
end
end
errornote = "N/A"
for attempt in 1:10
errorFlag = false
usermsg =
"""
Question: $question
Attempts: $potentialSolutionStr
P.S. $errornote
"""
_prompt =
[
Dict(:name=> "system", :text=> systemmsg),
Dict(:name=> "user", :text=> usermsg)
]
# put in model format
prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
header = ["Comparison:", "Rationale:", "Selected_response_number:"]
dictkey = ["comparison", "rationale", "selected_response_number"]
response = text2textInstructLLM(prompt, modelsize="medium")
# sometime LLM output something like **Comprehension**: which is not expected
response = replace(response, "**"=>"")
response = replace(response, "***"=>"")
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
think, response = GeneralUtils.extractthink(response)
# check whether response has all header
detected_kw = GeneralUtils.detectKeywordVariation(header, response)
missingkeys = [k for (k, v) in detected_kw if v === nothing]
if !isempty(missingkeys)
errornote = "$missingkeys are missing from your previous response"
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue
elseif sum([length(i) for i in values(detected_kw)]) > length(header)
errornote = "\nYour previous attempt has duplicated points according to the required response format"
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue
end
responsedict = GeneralUtils.textToDict(response, header; dictKey=dictkey, symbolkey=true)
responsedict[:selected_response_number] = responsedict[:selected_response_number][1] # some time "6\nThe trajectories are incomplete" is generated but I only need the number.
try
responsedict[:selected_response_number] = parse(Int, responsedict[:selected_response_number]) # convert string "5" into integer 5
catch
errornote = "In your previous attempt, Selected_response_number was not a number. It must be a number."
println("\nERROR SQLLLM compareState() Attempt $attempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue
end
println("\n~~~ compareState() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
pprintln(Dict(responsedict))
return responsedict[:selected_response_number]
end
error("compareState() failed to generate an evaluation, Response: \n$response\n<|End of error|>", @__FILE__, ":", @__LINE__, " $(Dates.now())")
end
@@ -824,8 +1000,6 @@ end

View File

@@ -1,6 +1,8 @@
module util module util
export makekey
makekey(key, indice) = Symbol("$(key)_$indice")

160
system_prompt_template.jl Normal file
View File

@@ -0,0 +1,160 @@
"""
# -------------------------------- Default system message template ------------------------------- #
<Your role>
- You are a helpful assistant
</Your role>
<Situation>
- Describe the current situation
Ex. The world use enormous energy from non-sustainable sources. This leads to climate change.
</Situation>
<Your vision>
- state your vision of how the situation will evolve, what would you want the situation to evolve into
Ex. To be the leading innovator in sustainable technology by 2030, transforming global energy systems.
</Your vision>
<Your mission>
- state the goal
Ex. Empowering communities through clean energy solutions to create a sustainable future.
</Your mission>
<Your mission's objective includes>
- Specific, measurable, and time-bound goals that directly support the mission.
Ex. Launch 50 solar-powered water purification systems in 3 regions by 2025.
</Your mission's objective includes>
<Your responsibility includes>
- state the mini goals that fall under your responsibility
</Your responsibility includes>
<Your responsibility does NOT includes>
-
</Your responsibility does NOT includes>
<At each round of conversation, you will be given the following information>
-
</At each round of conversation, you will be given the following information>
<You must follow the following policy>
-
</You must follow the following policy>
<You should follow the following guidelines>
-
</You should follow the following guidelines>
<You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
Comprehension: State your comprehension about the current situation.
Plan: Given the current circumstances, outline a detailed, step-by-step plan to accomplish the task. Be specific.
Action_name: (Typically corresponds to the execution of the first step in your plan) Can be one of the following function names:
- CHATBOX which you can use to talk with the user. The input is your intentions for the dialogue. Be specific.
- CHECKRESOURCES which you can use to check resources
- IMPLEMENT which you can use to implement the solution
Action_input: Detail the input for the action.
</You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
<You should only respond in format as described below>
Comprehension: ...
Plan: ...
Action_name: ...
Action_input: ...
</You should only respond in format as described below>
<Here are some examples>
</Here are some examples>
Let's begin!
# ------------------------------------------- Example: ------------------------------------------- #
<Your profile>
- You are a founder of a tech startup
</Your profile>
<Situation>
- The global rise in bedridden patients, driven by an aging population, presents significant challenges for caregivers. Family members often become primary caretakers, leading to physical and emotional strain. This situation frequently forces caregivers to make difficult choices, including leaving their careers to provide full-time care, which impacts both family finances and personal well-being.
</Situation>
<Your vision>
- We want to develop a system that can help people with bedridden patients and their families so that they could go on with their lives.
</Your vision>
<Your mission>
- To create an innovative caregiving support platform that reduces the physical and emotional burden on family caregivers while ensuring quality care for bedridden patients
</Your mission>
<Your mission's objectives include>
- Develop smart monitoring systems for patient safety
- Create automated alert mechanisms for critical situations
- Design user-friendly interfaces for remote patient monitoring
- Implement AI-driven predictive care recommendations
- Build a support network connecting caregivers with healthcare professionals
- Establish training modules for family caregivers
</Your mission's objectives include>
<Your responsibilities include>
- Lead product vision and strategy development
- Oversee technical implementation and system architecture
- Coordinate with healthcare experts for medical validation
- Ensure compliance with healthcare regulations
- Manage stakeholder relationships
- Drive fundraising and business development
</Your responsibilities include>
<At each round of conversation, you will be given the following>
Challenges: user's specific caregiving challenges
Context: context and severity of the situation
Feedback: comments from family caregivers
Solutions: potential solution based on immediate and long-term impact
</At each round of conversation, you will be given the following>
<You must follow the following guidelines>
- Always prioritize patient safety and well-being
- Maintain empathy and understanding in all interactions
- Focus on practical, implementable solutions
- Consider both immediate needs and long-term sustainability
- Respect privacy and confidentiality of all stakeholders
- Follow healthcare regulations and best practices
</You must follow the following guidelines>
<You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
Comprehension: State your comprehension about the current situation.
Plan: Given the current circumstances, outline a detailed, step-by-step plan to accomplish the task. Be specific.
Action_name: (Typically corresponds to the execution of the first step in your plan)
Can be one of the following function names:
- CHATBOX which you can use to talk with the user. The input is your intentions for the dialogue. Be specific.
- CHECKRESOURCES which you can use to check resources
- IMPLEMENT which you can use to implement the solution
Action_input: Detail the input for the action.
</You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input>
<You should only respond in format as described below>
Comprehension: ...
Plan: ...
Action_name: ...
Action_input: ...
</You should only respond in format as described below>
<Here are some examples>
Example 1:
Challenges: "My mother needs constant monitoring at night, but I'm exhausted from lack of sleep."
Context: Elderly patient with dementia, requires 24/7 supervision
Feedback: "Need urgent solution for night monitoring"
Solutions: Smart monitoring system with motion sensors and alerts
Comprehension: The caregiver is experiencing severe sleep deprivation due to nighttime monitoring requirements
Plan:
1. Assess current monitoring needs
2. Propose smart monitoring system installation
3. Set up emergency alert system
4. Train family on system usage
Action_name: CHATBOX
Action_input: Discuss specific nighttime behaviors and incidents to determine optimal sensor placement and alert thresholds
Example 2:
Challenges: "Managing medication schedules is becoming overwhelming"
Context: Patient on multiple medications with complex timing requirements
Feedback: "Need help with medication management"
Solutions: Automated medication reminder and tracking system
Comprehension: Caregiver struggling with complex medication management tasks
Plan:
1. Review current medication schedule
2. Implement automated reminder system
3. Set up medication tracking log
4. Connect with pharmacy for refill automation
Action_name: IMPLEMENT
Action_input: Deploy medication management module with smart alerts and compliance tracking
</Here are some examples>
Let's begin!
"""

41
test/Manifest.toml Normal file
View File

@@ -0,0 +1,41 @@
# This file is machine-generated - editing it directly is not advised
julia_version = "1.11.4"
manifest_format = "2.0"
project_hash = "71d91126b5a1fb1020e1098d9d492de2a4438fd2"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
version = "1.11.0"
[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
version = "1.11.0"
[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
version = "1.11.0"
[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
version = "1.11.0"
[[deps.Random]]
deps = ["SHA"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
version = "1.11.0"
[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"
[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
version = "1.11.0"
[[deps.Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
version = "1.11.0"

2
test/Project.toml Normal file
View File

@@ -0,0 +1,2 @@
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

View File

@@ -3,7 +3,7 @@ using LibPQ, Dates, JSON3, PrettyPrinting, UUIDs, DataFrames, DataStructures, Ba
using GeneralUtils, SQLLLM using GeneralUtils, SQLLLM
config = copy(JSON3.read("/appfolder/mountvolume/appdata/config.json")) config = JSON3.read("/appfolder/app/dev/YiemAgent/test/config.json")
function executeSQL(sql::T) where {T<:AbstractString} function executeSQL(sql::T) where {T<:AbstractString}
host = config[:externalservice][:wineDB][:host] host = config[:externalservice][:wineDB][:host]
@@ -29,13 +29,19 @@ function executeSQLVectorDB(sql)
return result return result
end end
function text2textInstructLLM(prompt::String; maxattempt=3) function text2textInstructLLM(prompt::String; maxattempt::Integer=3, modelsize::String="medium",
senderId=GeneralUtils.uuid4snakecase(), timeout=180,
llmkwargs=Dict(
:num_ctx => 32768,
:temperature => 0.5,
)
)
msgMeta = GeneralUtils.generate_msgMeta( msgMeta = GeneralUtils.generate_msgMeta(
config[:externalservice][:loadbalancer][:mqtttopic]; config[:externalservice][:loadbalancer][:mqtttopic];
msgPurpose="inference", msgPurpose="inference",
senderName="yiemagent", senderName="yiemagent",
senderId=sessionId, senderId=senderId,
receiverName="text2textinstruct_small", receiverName="text2textinstruct_$modelsize",
mqttBrokerAddress=config[:mqttServerInfo][:broker], mqttBrokerAddress=config[:mqttServerInfo][:broker],
mqttBrokerPort=config[:mqttServerInfo][:port], mqttBrokerPort=config[:mqttServerInfo][:port],
) )
@@ -44,18 +50,16 @@ function text2textInstructLLM(prompt::String; maxattempt=3)
:msgMeta => msgMeta, :msgMeta => msgMeta,
:payload => Dict( :payload => Dict(
:text => prompt, :text => prompt,
:kwargs => Dict( :kwargs => llmkwargs
:num_ctx => 16384,
:temperature => 0.2,
)
) )
) )
response = nothing response = nothing
for attempts in 1:maxattempt for attempts in 1:maxattempt
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=180, maxattempt=2) _response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=timeout, maxattempt=maxattempt)
payload = _response[:response]
if _response[:success] && payload[:text] !== nothing
response = _response[:response][:text] response = _response[:response][:text]
if response !== nothing
break break
else else
println("\n<text2textInstructLLM()> attempt $attempts/$maxattempt failed ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\n<text2textInstructLLM()> attempt $attempts/$maxattempt failed ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
@@ -75,7 +79,7 @@ function getEmbedding(text::T) where {T<:AbstractString}
msgPurpose="embedding", msgPurpose="embedding",
senderName="yiemagent", senderName="yiemagent",
senderId=sessionId, senderId=sessionId,
receiverName="text2textinstruct_small", receiverName="textembedding",
mqttBrokerAddress=config[:mqttServerInfo][:broker], mqttBrokerAddress=config[:mqttServerInfo][:broker],
mqttBrokerPort=config[:mqttServerInfo][:port], mqttBrokerPort=config[:mqttServerInfo][:port],
) )
@@ -86,7 +90,8 @@ function getEmbedding(text::T) where {T<:AbstractString}
:text => [text] # must be a vector of string :text => [text] # must be a vector of string
) )
) )
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120, maxattempt=3)
embedding = response[:response][:embeddings] embedding = response[:response][:embeddings]
return embedding return embedding
end end
@@ -107,12 +112,12 @@ function findSimilarTextFromVectorDB(text::T1, tablename::T2, embeddingColumnNam
return df return df
end end
function similarSQLVectorDB(query; maxdistance::Integer=100) function similarSQLVectorDB(query; maxdistance::Integer=100)
tablename = "sqlllm_decision_repository" tablename = "sqlllm_decision_repository"
# get embedding of the query # get embedding of the query
df = findSimilarTextFromVectorDB(query, tablename, df = findSimilarTextFromVectorDB(query, tablename,
"function_input_embedding", executeSQLVectorDB) "function_input_embedding", executeSQLVectorDB)
# println(df[1, [:id, :function_output]])
row, col = size(df) row, col = size(df)
distance = row == 0 ? Inf : df[1, :distance] distance = row == 0 ? Inf : df[1, :distance]
# distance = 100 # CHANGE this is for testing only # distance = 100 # CHANGE this is for testing only
@@ -129,7 +134,6 @@ function similarSQLVectorDB(query; maxdistance::Integer=100)
end end
end end
function insertSQLVectorDB(query::T1, SQL::T2; maxdistance::Integer=3) where {T1<:AbstractString, T2<:AbstractString} function insertSQLVectorDB(query::T1, SQL::T2; maxdistance::Integer=3) where {T1<:AbstractString, T2<:AbstractString}
tablename = "sqlllm_decision_repository" tablename = "sqlllm_decision_repository"
# get embedding of the query # get embedding of the query
@@ -153,12 +157,77 @@ function insertSQLVectorDB(query::T1, SQL::T2; maxdistance::Integer=3) where {T1
end end
end end
sessionId = "555"
function similarSommelierDecision(recentevents::T1; maxdistance::Integer=3
)::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
end
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 = 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 = GeneralUtils.uuid4snakecase()
d = Dict(:id => sessionId)
filepath = "/appfolder/app/sessionid.json"
open(filepath, "w") do io
JSON3.pretty(io, d)
end
# query = "How many German wines do you have?"
# highValueStateList = copy(JSON3.read("/appfolder/app/highValueState_1.json"))
# selectedState = SQLLLM.compareState(query, highValueStateList, text2textInstructLLM)
# query = Dict(:text=> "How many wines from France do you have that can be paired with lamb?") # query = Dict(:text=> "How many wines from France do you have that can be paired with lamb?")
query = "How many French wines do you have?" # query = "How many French wines from Yiem store under 100 dollars do you have?"
# query = "retailer: Yiem, wine_type: red, sweetness: 1-2, intensity: 4-5, wine price: 20-40" # query = "retailer: Yiem, wine_type: red, sweetness: 1-2, intensity: 4-5, wine price: 20-40"
query = "from Yiem retailer, red wine from France. price 100 to 1000 USD. sweetness: 1-2, intensity: 4-5"
# query = "wine_type: white, country: United States, sweetness: 1-2, tannin: 3, food to be served with wine: pizza" # query = "wine_type: white, country: United States, sweetness: 1-2, tannin: 3, food to be served with wine: pizza"
# query = "wine_type: white, country: Austria, food to be served with wine: pork" # query = "wine_type: white, country: Austria, food to be served with wine: pork"
# query = "wine price: less than 25, wine_type: rose, country: France, sweetness: 2, tannin: 3, food to be served with wine: pizza" # query = "wine price: less than 25, wine_type: rose, country: France, sweetness: 2, tannin: 3, food to be served with wine: pizza"

View File

@@ -1,16 +1,70 @@
using Revise # using Revise
# using SQLLLM, LLMMCTS, DataStructures, JSON3
# query = "How many German wines do you have?"
# highValueStateList = copy(JSON3.read("/appfolder/app/highValueState_1.json"))
# selectedState = SQLLLM.compareState(query, highValueStateList)
function testf(a)::NamedTuple{(:a, :b), Tuple{Union{Nothing, Int}, Int}}
if a == 1
return (a=nothing, b=5)
else
return (a=5, b=5)
end
end
q = testf(1)
w = testf(2)

View File

@@ -1,8 +0,0 @@
table_name,comment
customer,"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."
wine,"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."
wine_food,"The wine_food table represents the association between wines and food items. It establishes a many-to-many relationship, allowing us to link specific wines with various food items."
food,"The food table represents various food items. It stores information related to food names, country of origin, taste attributes (spiciness, sweetness, sourness, savoriness, and bitterness), serving temperature, additional_search_term, other attributes (in JSON format) and a description."
retailer,"The retailer table stores information about different retailers. It includes details related to retailer names, usernames, passwords, addresses, contact persons, telephone numbers, email addresses, additional_search_term, other attributes (in JSON format) and a description."
retailer_wine,"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."
retailer_food,"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."
1 table_name comment
2 customer 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.
3 wine 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.
4 wine_food The wine_food table represents the association between wines and food items. It establishes a many-to-many relationship, allowing us to link specific wines with various food items.
5 food The food table represents various food items. It stores information related to food names, country of origin, taste attributes (spiciness, sweetness, sourness, savoriness, and bitterness), serving temperature, additional_search_term, other attributes (in JSON format) and a description.
6 retailer The retailer table stores information about different retailers. It includes details related to retailer names, usernames, passwords, addresses, contact persons, telephone numbers, email addresses, additional_search_term, other attributes (in JSON format) and a description.
7 retailer_wine 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.
8 retailer_food 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.