This commit is contained in:
narawat lamaiin
2024-08-28 10:40:17 +07:00
parent 83315a747f
commit 9f80e8359f
2 changed files with 30 additions and 10 deletions

View File

@@ -874,7 +874,7 @@ julia> response = ChatAgent.conversation(newAgent, "Hi! how are you?")
# Signature # Signature
""" """
function conversation(a::sommelier, userinput::Dict) function conversation(a::sommelier, userinput::Dict)
println("--> conver 1 ", @__FILE__, " ", @__LINE__)
# place holder # place holder
actionname = nothing actionname = nothing
result = nothing result = nothing
@@ -886,7 +886,7 @@ function conversation(a::sommelier, userinput::Dict)
else else
# add usermsg to a.chathistory # add usermsg to a.chathistory
addNewMessage(a, "user", userinput[:text]) addNewMessage(a, "user", userinput[:text])
println("--> conver 2 ", @__FILE__, " ", @__LINE__)
# add user activity to events memory # add user activity to events memory
push!(a.memory[:events], push!(a.memory[:events],
eventdict(; eventdict(;
@@ -896,7 +896,7 @@ function conversation(a::sommelier, userinput::Dict)
action_or_dialogue= userinput[:text], action_or_dialogue= userinput[:text],
) )
) )
println("--> conver 3 ", @__FILE__, " ", @__LINE__)
# use dummy memory to check generatechat() for halucination (checking inventory) # use dummy memory to check generatechat() for halucination (checking inventory)
for i in 1:3 for i in 1:3
actionname, result = think(a) actionname, result = think(a)
@@ -904,7 +904,7 @@ function conversation(a::sommelier, userinput::Dict)
break break
end end
end end
println("--> conver 4 ", @__FILE__, " ", @__LINE__)
# thought will be added to chat model via context # thought will be added to chat model via context
chatresponse = generatechat(a) chatresponse = generatechat(a)
@@ -931,7 +931,7 @@ function conversation(a::companion, userinput::Dict)
else else
# add usermsg to a.chathistory # add usermsg to a.chathistory
addNewMessage(a, "user", userinput[:text]) addNewMessage(a, "user", userinput[:text])
println("--> conver 2 ", @__FILE__, " ", @__LINE__)
# add user activity to events memory # add user activity to events memory
push!(a.memory[:events], push!(a.memory[:events],
eventdict(; eventdict(;
@@ -1200,16 +1200,24 @@ function generatechat(a::companion)
Your ongoing conversation with the user: ... Your ongoing conversation with the user: ...
Context: ... Context: ...
You should then respond to the user with:
1) Chat: Given the situation, what would you say to the user?
You should only respond in format as described below:
Chat: ...
Let's begin! Let's begin!
""" """
chathistory = vectorOfDictToText(a.chathistory) chathistory = vectorOfDictToText(a.chathistory)
response = nothing # placeholder for show when error msg show up response = nothing # placeholder for show when error msg show up
noise = ""
for attempt in 1:10 for attempt in 1:10
usermsg = usermsg =
""" """
Your ongoing conversation with the user: $chathistory Your ongoing conversation with the user: $chathistory
$noise
""" """
_prompt = _prompt =
@@ -1227,12 +1235,14 @@ function generatechat(a::companion)
try try
response = a.text2textInstructLLM(prompt) response = a.text2textInstructLLM(prompt)
println("") println("")
println("--> generatechat() ", @__FILE__, " ", @__LINE__) println("--> generatechat() ", @__FILE__, " ", @__LINE__)
pprintln(response) pprintln(response)
result = response responsedict = GeneralUtils.textToDict(response,["Chat"],
rightmarker=":", symbolkey=true, lowercasekey=true)
result = responsedict[:chat]
return result return result
catch e catch e
@@ -1240,6 +1250,7 @@ function generatechat(a::companion)
showerror(io, e) showerror(io, e)
errorMsg = String(take!(io)) errorMsg = String(take!(io))
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
noise = noises(3, 5)
println("") println("")
println("Attempt $attempt. Error occurred: $errorMsg\n$st") println("Attempt $attempt. Error occurred: $errorMsg\n$st")
println("") println("")
@@ -1248,6 +1259,7 @@ function generatechat(a::companion)
error("generatechat failed to generate an evaluation") error("generatechat failed to generate an evaluation")
end end
function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::String function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::String
# systemmsg = # systemmsg =

View File

@@ -1,6 +1,6 @@
module util module util
export clearhistory, addNewMessage, vectorOfDictToText, eventdict export clearhistory, addNewMessage, vectorOfDictToText, eventdict, noises
using UUIDs, Dates, DataStructures, HTTP, MQTTClient, JSON3 using UUIDs, Dates, DataStructures, HTTP, MQTTClient, JSON3
using GeneralUtils using GeneralUtils
@@ -181,7 +181,15 @@ function eventdict(;
end end
noise(n::Integer) = String(rand('a':'z', n))
function noises(totalword::Integer, wordlength::Integer)
noises = ""
for i in 1:totalword
noises *= noise(wordlength) * " "
end
noises = strip(noises)
return noises
end
# """ Convert a single chat dictionary into LLM model instruct format. # """ Convert a single chat dictionary into LLM model instruct format.