add sendprompt

This commit is contained in:
2023-11-03 02:22:27 +00:00
parent b08e45bb62
commit 9352657da7

View File

@@ -14,7 +14,7 @@ using CommUtils
# by setting the following variables, PythonCall will use system python or conda python and
# packages installed by system or conda
# if these setting are not set (comment out), PythonCall will use its own python and package that
# installed by CondaPkg
# installed by CondaPkg (from env_preparation.jl)
# ENV["JULIA_CONDAPKG_BACKEND"] = "Null"
# systemPython = split(read(`which python`, String), "\n")[1]
# ENV["JULIA_PYTHONCALL_EXE"] = systemPython # find python location with $> which python ex. raw"/root/conda/bin/python"
@@ -32,8 +32,6 @@ using CommUtils
#------------------------------------------------------------------------------------------------100
#
@kwdef mutable struct agent
availableRole = ["system", "user", "assistant"]
agentName::String = "assistant"
@@ -42,8 +40,6 @@ using CommUtils
mqttClient::Union{mqttClient, Nothing} = nothing
msgMeta::Union{Dict, Nothing} = nothing
context::String = "" # internal thinking area
""" Dict(Role=> Content) ; Role can be system, user, assistant
Example:
messages=[
@@ -52,15 +48,17 @@ using CommUtils
Dict(:role=>"user", :content=> "Hello, how are you"),
]
"""
# # Ref: Chat prompt format https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/discussions/3
messages= [Dict(:role=>"system", :content=> "You are a helpful assistant.", :timestamp=> Dates.now()),]
# Ref: Chat prompt format https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/discussions/3
# messages= [Dict(:role=>"system", :content=> "", :timestamp=> Dates.now()),]
messages = []
thougt::String = "" # internal thinking area
info::String = "" # additional info
end
function agent(
agentName::String,
systemMessage::String, # system message of an agent
mqttClientSpec::NamedTuple;
systemMessage::String="You are a helpful assistant.", # system message of an agent
msgMeta::Dict=Dict(
:msgPurpose=> "updateStatus",
:from=> "chatbothub",
@@ -76,36 +74,36 @@ function agent(
availableRole::AbstractArray=["system", "user", "assistant"],
maxUserMsg::Int=10,)
newAgent= agent()
newAgent.availableRole= availableRole
newAgent.maxUserMsg= maxUserMsg
systemMessage= "Your name is $agentName. " * systemMessage
newAgent.messages= [Dict(:role=>"system", :content=> systemMessage, :timestamp=> Dates.now()),]
newAgent.mqttClient= CommUtils.mqttClient(mqttClientSpec)
newAgent = agent()
newAgent.availableRole = availableRole
newAgent.maxUserMsg = maxUserMsg
systemMessage_ = "Your name is $agentName. " * systemMessage
push!(newAgent.messages, Dict(:role=>"system", :content=> systemMessage_, :timestamp=> Dates.now()))[,]
newAgent.mqttClient = CommUtils.mqttClient(mqttClientSpec)
newAgent.msgMeta = msgMeta
return newAgent
end
@kwdef mutable struct agentLangchain
availableRole=["system", "user", "assistant"]
maxUserMsg::Int= 10
llmAIRequestTopic_openblas = "llm/openblas/request"
llmAIRequestTopic_gpu = "llm/api/v0.0.1/gpu/request"
self_llmReceiveTopic = "chatbothub/llm/respond"
# @kwdef mutable struct agentLangchain
# availableRole=["system", "user", "assistant"]
# maxUserMsg::Int= 10
# llmAIRequestTopic_openblas = "llm/openblas/request"
# llmAIRequestTopic_gpu = "llm/api/v0.0.1/gpu/request"
# self_llmReceiveTopic = "chatbothub/llm/respond"
""" Dict(Role=> Content) ; Role can be system, user, assistant
Example:
messages=[
Dict(:role=>"system", :content=> "You are a helpful assistant."),
Dict(:role=>"assistant", :content=> "How may I help you"),
Dict(:role=>"user", :content=> "Hello, how are you"),
]
"""
# Ref: https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/discussions/3
#
messages=[Dict(:role=>"system", :content=> "You are a helpful assistant.", :timestamp=> Dates.now()),]
end
# """ Dict(Role=> Content) ; Role can be system, user, assistant
# Example:
# messages=[
# Dict(:role=>"system", :content=> "You are a helpful assistant."),
# Dict(:role=>"assistant", :content=> "How may I help you"),
# Dict(:role=>"user", :content=> "Hello, how are you"),
# ]
# """
# # Ref: https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/discussions/3
# #
# messages=[Dict(:role=>"system", :content=> "You are a helpful assistant.", :timestamp=> Dates.now()),]
# end
"""
add new message to agent
@@ -204,11 +202,19 @@ end
function conversation(a::agent, usermsg::String)
addNewMessage(a, "user", usermsg)
userIntent = identifyUserIntention(a, usermsg)
@show userIntent
#WORKING 1) add if-else user intention logic. 2) add recursive thinking
if userIntent == "chat"
generatePrompt_tokenPrefix(a, userToken="Q:", assistantToken="A:")
elseif userIntent == "task"
else
error("user intent $userIntent not define $(@__LINE__)")
end
end
#TESTING
@@ -270,22 +276,60 @@ function identifyUserIntention(a::agent, usermsg::String)
identify_usermsg = replace(identify_usermsg, "{input}" => usermsg)
result = sendPrompt(a, identify_usermsg, )
# msg = Dict(
# :msgMeta=> a.msgMeta,
# :txt=> identify_usermsg,
# )
# payloadChannel = Channel(1)
# # send prompt
# CommUtils.request(a.mqttClient, msg, pubtopic=a.mqttClient.pubtopic.llmAI)
# starttime = Dates.now()
# timeout = 10
# result = nothing
# while true
# timepass = (Dates.now() - starttime).value / 1000.0
# CommUtils.mqttRun(a.mqttClient, payloadChannel)
# if isready(payloadChannel)
# topic, payload = take!(payloadChannel)
# if payload[:msgMeta][:repondToMsgId] == msg[:msgMeta][:msgId]
# result = payload[:txt]
# break
# end
# elseif timepass <= timeout
# # skip, within waiting period
# elseif timepass > timeout
# result = nothing
# break
# else
# error("undefined condition $(@__LINE__)")
# end
# end
answer = result === nothing ? nothing : GeneralUtils.getStringBetweenCharacters(result, "{", "}")
return answer
end
function sendPrompt(a::agent, prompt::String; timeout::Int=10)
a.msgMeta[:msgId] = "$(uuid4())"
msg = Dict(
:msgMeta=> a.msgMeta,
:txt=> identify_usermsg,
:txt=> prompt,
)
payloadChannel = Channel(1)
# send prompt
CommUtils.request(a.mqttClient, msg, pubtopic=a.mqttClient.pubtopic.llmAI)
#WORKING send prompt
CommUtils.request(a.mqttClient, msg)
starttime = Dates.now()
timeout = 10
result = nothing
while true
timepass = (Dates.now() - starttime).value / 1000.0
CommUtils.mqttRun(a.mqttClient, payloadChannel)#BUG no msg received
CommUtils.mqttRun(a.mqttClient, payloadChannel)
if isready(payloadChannel)
topic, payload = take!(payloadChannel)
if payload[:msgMeta][:repondToMsgId] == msg[:msgMeta][:msgId]
@@ -300,14 +344,8 @@ function identifyUserIntention(a::agent, usermsg::String)
else
error("undefined condition $(@__LINE__)")
end
end
answer = result === nothing ? nothing : GeneralUtils.getStringBetweenCharacters(result, "{", "}")
return answer
end
# function getStringBetweenCurlyBraces(s::AbstractString)
# m = match(r"\{(.+?)\}", s)
# m = m == "" ? "" : m.captures[1]