From 0f59725ac4eff2fa2743b26d3418607650e3f598 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Mon, 15 Jan 2024 15:00:13 +0000 Subject: [PATCH] update --- src/interface.jl | 323 ++++++++++++++++++++++++++++++++++++++++++--- src/llmfunction.jl | 10 +- 2 files changed, 308 insertions(+), 25 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index e771822..f2e4eda 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -278,6 +278,58 @@ function updatePlan(a::agentReflex) end +# function selfAwareness(a::agentReflex) +# getonlykeys = ["Actinput", "Obs"] +# worknoplan = similar(a.memory[:shortterm]) +# for (k, v) in a.memory[:shortterm] +# count = 0 +# for i in getonlykeys +# if occursin(i, k) +# count += 1 +# end +# end +# if count != 0 +# worknoplan[k] = v +# end +# end + +# work = dictToString(worknoplan) + +# aboutYourself = +# """ +# Your name is $(a.agentName) +# $(a.roles[a.role]) +# """ + +# prompt = +# """ +# <|system|> +# +# $aboutYourself +# +# +# $work +# +# $(a.memory[:shortterm]["Plan 1:"]) +# +# +# Use the following format: +# What I know: based on observed results, repeat all the details of what you have been gathered. +# Current progress: focus on checking your progress against the plan. +# What I am missing: describe in detail what you are missing. +# +# +# <|assistant|> +# What I know: +# """ +# response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=180, +# stopword=["/n/n", "END", "End", "Obs", "<|", " - Use the following format: - What I know: based on observed results, repeat all the details of what you have been gathered. - Current progress: check your progress against the plan. - What I am missing: describe in detail what you are missing. + Use the following format strictly: + What do I know: based on observed results, repeat all the details of what you have been gathered. + What am I missing: based on observed results, describe in detail what you are still missing compared to your plan. <|assistant|> What I know: """ - response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=180, + response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.2, timeout=180, stopword=["/n/n", "END", "End", "Obs", "<|", " Use the following format: $thought - Act: an action should aligned with your thought, must be one of [{toolnames}]. + Act: based on your thought what action to choose?, must be one of [{toolnames}]. Actinput: your input to the action (pay attention to the tool's input) Obs: observed result of the action @@ -557,6 +608,233 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing) return toolname, toolinput, chunkedtext end +# function actor_mistral_openorca(a::agentReflex, selfaware=nothing) +# getonlykeys = ["Actinput", "Obs"] +# worknoplan = similar(a.memory[:shortterm]) +# for (k, v) in a.memory[:shortterm] +# count = 0 +# for i in getonlykeys +# if occursin(i, k) +# count += 1 +# end +# end +# if count != 0 +# worknoplan[k] = v +# end +# end + +# work = dictToString(worknoplan) + + + +# """ +# general prompt format: + +# " +# <|im_start|>system +# {role} +# {tools} +# {thinkingFormat} +# <|im_end|> +# {context} +# <|im_start|>user +# {usermsg} +# <|im_end|> +# <|im_start|>assistant + +# " + +# Note: +# {context} = +# " +# {earlierConversation} +# {env state} +# {shortterm memory} +# {longterm memory} +# " +# """ + +# toolslist = [] +# toolnames = "" +# toollines = "" +# for (toolname, v) in a.tools +# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" +# toollines *= toolline +# toolnames *= "$toolname, " +# push!(toolslist, toolname) +# end + +# thought = "Thought: you should always think about what to do according to the plan (pay attention to correct numeral calculation and commonsense and do one thing at a time.)" +# if selfaware !== nothing + +# # aware = "Self-awareness: based on the recap, assess your progress against the plan then identify areas where you need to address." +# # aware = "Self-awareness: based on the recap and the plan, state your current understanding of the matter in details then identify areas where you need to address." +# # aware = "Self-awareness: Based on Obs, review your progress against the plan. Then, describe in detail the results you have achieved so far. Finally, describe in detail what you are missing. (focus on your actions and their results)" +# # aware = "Self-awareness: Based on action's input and observed results, check your progress against the plan. Then, repeat all the details of what you have been gathered. Finally, describe in detail what you are missing." +# thought = +# "Self-awareness: $selfaware +# Thought: you should always think about what to do based on what are you missing. (P.S. 1) let's think a single step. 2) pay attention to correct numeral calculation and commonsense.) +# " +# end + +# aboutYourself = +# """ +# Your name is $(a.agentName) +# $(a.roles[a.role]) +# """ + +# prompt = +# """ +# <|system|> +# +# $aboutYourself +# +# +# $toollines +# +# +# $(a.memory[:shortterm]["Plan 1:"]) +# +# +# Use the following format: +# $thought +# Act: based on your thought what action to choose?, must be one of [{toolnames}]. +# Actinput: your input to the action (pay attention to the tool's input) +# Obs: observed result of the action +# +# +# Thought: Greet user and begin the conversation. +# Act: askbox +# Actinput: {\"askbox\": \"Hello! Welcome to our wine store.\"} +# + +# +# <|assistant|> +# $work +# "Thought:" +# """ +# prompt = replace(prompt, "{toolnames}" => toolnames) + +# println("") +# @show actor_prompt = prompt + +# response = nothing +# chunkedtext = nothing +# latestTask = nothing + +# tempcounter = 0.2 +# seed = nothing +# while true # while Thought or Act is empty, run actor again +# # tempcounter += 0.2 +# @show tempcounter +# response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=180, +# stopword=["Thought:", "Obs:", "<|system|>", "", "<|end|>"], +# seed=seed) +# response = splittext(response, ["/n/n", "END", "End", "Obs", "<|im_end|>"]) + +# latestTask = shortMemLatestTask(a.memory[:shortterm]) +1 + +# response = "Thought:" * response + +# headerToDetect = ["Question:", "Plan:", "Self-awareness:", "Thought:", +# "Act:", "Actinput:", "Obs:", "...", +# "Answer:", "Conclusion:", "Summary:"] + +# # replace headers with headers with correct attempt and task number +# response = replaceHeaders(response, headerToDetect, latestTask) +# response = split(response, "<|")[1] +# response = split(response, " 5 +# # check for empty Actinput +# check_6 = length(chunkedtext["Actinput $latestTask:"]) > 5 + +# # print all check_1 to check_6 +# println("check_1: $check_1, check_2: $check_2, check_3: $check_3, check_4: $check_4, check_5: $check_5, check_6: $check_6") + +# if check_1 && check_2 && check_3 && check_4 && check_5 && check_6 +# #WORKING paraphrase selfaware +# break +# end +# end + +# toolname = toolNameBeingCalled(chunkedtext["Act $latestTask:"], a.tools) + +# # change trailing number to continue a.memory[:shortterm] +# headerToDetect = ["Question:", "Plan:", "Self-awareness:", "Thought:", +# "Act:", "Actinput:", "Obs:", "...", +# "Answer:", "Conclusion:", "Summary:"] +# response = replaceHeaders(response, headerToDetect, latestTask) +# println("") +# @show actor_response_1 = response +# headerToDetect = ["Plan $(a.attempt):", +# "Thought $latestTask:", +# "Act $latestTask:", +# "Actinput $latestTask:", +# "Obs $latestTask:", +# "Check $latestTask:",] +# headers = detectCharacters(response, headerToDetect) +# chunkedtext = chunktext(response, headers) +# chunkedtext = delete!(chunkedtext, "Self-awareness $latestTask") + +# println("") +# @show chunkedtext + +# toolinput = chunkedtext["Actinput $latestTask:"] + +# # because tools has JSON input but sometime LLM output is not JSON, we need to check. +# if occursin("{", toolinput) +# act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end") +# act = copy(JSON3.read(act)) +# chunkedtext["Actinput $latestTask:"] = JSON3.write(act[Symbol(toolname)]) +# toolinput = act[Symbol(toolname)] +# end + + +# chunkedtext["Act $latestTask:"] = toolname + +# return toolname, toolinput, chunkedtext +# end + """ @@ -1074,25 +1352,30 @@ function formulateUserresponse(a) prompt = """ <|system|> - Symbol: - Plan: a plan - Thought: your thought - Act: the action you took - Actinput: the input to the action - Obs: the result of the action + + Plan: a plan + Thought: your thought + Act: the action you took + Actinput: the input to the action + Obs: the result of the action + - Your talk with the user: - $conversation + + $conversation + - Your work: - $work + + $work + - From your talk with the user and your work, formulate a response for the user by comparing and explaining your rational behind each choice in details. + + Based on your talk with the user and your work, present a response that compares and justifies each option in great detail. + <|assistant|> - Response: + Recommendation: """ - response = sendReceivePrompt(a, prompt) + response = sendReceivePrompt(a, prompt, max_tokens=1024, timeout=300) return response end diff --git a/src/llmfunction.jl b/src/llmfunction.jl index b4af4f5..8f7e03a 100644 --- a/src/llmfunction.jl +++ b/src/llmfunction.jl @@ -151,16 +151,16 @@ function winestock(a::agentReflex, query::Dict) acidity = 5, high acidity - Consult the conversion table then write a specific SQL command from a JSON-format query. - List of keywords not allowed in SQL: ["BETWEEN", "--"] + Consult the conversion table then write a specific SQL command using only available info from a JSON-format query. + List of keywords not allowed in SQL: ["BETWEEN", "--", "WHEN] query: {\"wine type\": \"white\", \"wine characteristics\": \"full-bodied | off-dry | low acidity | medium tannin\", \"price\": {\"max\": \"50\"}} assistant: SELECT * FROM White WHERE intensity = 5 AND sweetness = 2 AND acidity = 1 AND tannin = 3 AND price <= 50; - query: {\"wine characteristics\":\"low-bodied | semi-sweet | low tannin\",\"price\":\"22 USD\",\"occasion\":\"anniversary\",\"wine type\":\"Rose\",\"food\":\"American dishes\"} - assistant: SELECT * FROM Rose WHERE intensity = 1 AND sweetness = 3 AND tannin = 1 AND price <= 22; + query: {\"wine characteristics\":\"low-bodied | semi-sweet | low-medium tannin\",\"price\":\"22 USD\",\"occasion\":\"anniversary\",\"wine type\":\"Rose\",\"food\":\"American dishes\"} + assistant: SELECT * FROM Rose WHERE intensity = 1 AND sweetness = 3 AND (tannin = 2 OR tannin = 3) AND price <= 22; <|query|> @@ -183,7 +183,7 @@ function winestock(a::agentReflex, query::Dict) end end println("") - @show db_sql = _sql + @show db_sql = replace(_sql, '\n'=>'') # remove any blank character in front of a string newsql = nothing