This commit is contained in:
narawat lamaiin
2024-05-04 17:41:31 +07:00
parent 0286bc13c7
commit ae9d80df5e
4 changed files with 154 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
module llmfunction
export virtualWineCustomerChatbox
export virtualWineCustomerChatbox, jsoncorrection
using HTTP, JSON3, URIs, Random
using GeneralUtils
@@ -362,6 +362,115 @@ end
# return result
# end
""" Attemp to correct LLM response's incorrect JSON response.
# Arguments
- `a::T1`
one of Yiem's agent
- `input::T2`
text to be send to virtual wine customer
# Return
- `response::String`
response of virtual wine customer
# Example
```jldoctest
julia>
```
# TODO
- [] update docstring
- [TESTING] implement the function
# Signature
"""
function jsoncorrection(a::T1, input::T2,
correctJsonExample::T3) where {T1<:agent, T2<:AbstractString, T3<:AbstractString}
attemptround = 0
incorrectjson = input
correctjson = nothing
while true
attemptround += 1
if attemptround <= 5
try
JSON3.read(incorrectjson)
correctjson = incorrectjson
break
catch
println("Attempting correct JSON string. $attempting")
_prompt =
"""
Your goal is to correct a given incorrect JSON string.
$correctJsonExample
Incorrect JSON:
$incorrectjson
Corrention:
"""
externalService = a.config[:externalservice][:text2textinstruct]
llminfo = externalService[:llminfo]
prompt =
if llminfo[:name] == "llama3instruct"
formatLLMtext_llama3instruct("system", _prompt)
else
error("llm model name is not defied yet $(@__LINE__)")
end
# send formatted input to user using GeneralUtils.sendReceiveMqttMsg
msgMeta = GeneralUtils.generate_msgMeta(
externalService[:mqtttopic],
senderName= "jsoncorrection",
senderId= a.id,
receiverName= "text2textinstruct",
mqttBroker= a.config[:mqttServerInfo][:broker],
mqttBrokerPort= a.config[:mqttServerInfo][:port],
)
outgoingMsg = Dict(
:msgMeta=> msgMeta,
:payload=> Dict(
:text=> prompt,
:kwargs=> Dict(
:max_tokens=> 512,
:stop=> ["<|eot_id|>"],
)
)
)
result = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
incorrectjson = result[:response][:text]
end
else
error("Can't fix JSON string")
break
end
end
@show correctjson
return correctjson
end