add ability to specify python version
This commit is contained in:
92
previousVersion/0.0.2/src/ChatAgent.jl
Normal file
92
previousVersion/0.0.2/src/ChatAgent.jl
Normal file
@@ -0,0 +1,92 @@
|
||||
module ChatAgent
|
||||
|
||||
# export agent, addNewMessage, clearMessage
|
||||
|
||||
|
||||
""" Order by dependencies of each file. The 1st included file must not depend on any other
|
||||
files and each file can only depend on the file included before it.
|
||||
"""
|
||||
|
||||
include("interface.jl")
|
||||
using .interface
|
||||
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
""" version 0.0.1
|
||||
Todo:
|
||||
[] add chat mechanism
|
||||
|
||||
Change from version: 0.0.0
|
||||
-
|
||||
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module ChatAgent
|
||||
214
previousVersion/0.0.2/src/interface.jl
Normal file
214
previousVersion/0.0.2/src/interface.jl
Normal file
@@ -0,0 +1,214 @@
|
||||
module interface
|
||||
|
||||
|
||||
export agent, addNewMessage, clearMessage, removeLatestMsg, generatePrompt_tokenPrefix,
|
||||
generatePrompt_tokenSuffix
|
||||
|
||||
using JSON3, DataStructures, Dates, PythonCall
|
||||
|
||||
const py_agents = PythonCall.pynew()
|
||||
const py_llms = PythonCall.pynew()
|
||||
function __init__()
|
||||
# PythonCall.pycopy!(py_cv2, pyimport("cv2"))
|
||||
|
||||
# equivalent to from urllib.request import urlopen in python
|
||||
PythonCall.pycopy!(py_agents, pyimport("langchain.agents"))
|
||||
PythonCall.pycopy!(py_llms, pyimport("langchain.llms"))
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------------------------100
|
||||
|
||||
|
||||
|
||||
@kwdef mutable struct agent
|
||||
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
|
||||
|
||||
@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
|
||||
|
||||
"""
|
||||
add new message to agent
|
||||
# Example
|
||||
```jldoctest
|
||||
julia> addNewMessage(agent1, "user", "Where should I go to buy snacks")
|
||||
````
|
||||
"""
|
||||
function addNewMessage(a::agent, role::String, content::String)
|
||||
if role ∉ a.availableRole # guard against typo
|
||||
error("role is not in agent.availableRole")
|
||||
end
|
||||
|
||||
# check whether user messages exceed limit
|
||||
userMsg = 0
|
||||
for i in a.messages
|
||||
if i[:role] == "user"
|
||||
userMsg += 1
|
||||
end
|
||||
end
|
||||
messageleft = 0
|
||||
|
||||
if userMsg > a.maxUserMsg # delete all conversation
|
||||
clearMessage(a)
|
||||
messageleft = a.maxUserMsg
|
||||
else
|
||||
userMsg += 1
|
||||
d = Dict(:role=> role, :content=> content, :timestamp=> Dates.now())
|
||||
push!(a.messages, d)
|
||||
messageleft = a.maxUserMsg - userMsg
|
||||
end
|
||||
|
||||
return messageleft
|
||||
end
|
||||
|
||||
|
||||
function clearMessage(a::agent)
|
||||
for i in eachindex(a.messages)
|
||||
if length(a.messages) > 1 # system instruction will NOT be deleted
|
||||
pop!(a.messages)
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function removeLatestMsg(a::agent)
|
||||
if length(a.messages) > 1
|
||||
pop!(a.messages)
|
||||
end
|
||||
end
|
||||
|
||||
function generatePrompt_tokenSuffix(a::agent;
|
||||
userToken::String="[/INST]", assistantToken="[INST]",
|
||||
systemToken="[INST]<<SYS>> content <</SYS>>")
|
||||
prompt = nothing
|
||||
for msg in a.messages
|
||||
role = msg[:role]
|
||||
content = msg[:content]
|
||||
|
||||
if role == "system"
|
||||
prompt = replace(systemToken, "content" => content) * " "
|
||||
elseif role == "user"
|
||||
prompt *= " " * content * " " * userToken
|
||||
elseif role == "assistant"
|
||||
prompt *= " " * content * " " * assistantToken
|
||||
else
|
||||
error("undefied condition role = $role")
|
||||
end
|
||||
end
|
||||
|
||||
return prompt
|
||||
end
|
||||
|
||||
function generatePrompt_tokenPrefix(a::agent;
|
||||
userToken::String="Q:", assistantToken="A:",
|
||||
systemToken="[INST]<<SYS>> content <</SYS>>")
|
||||
prompt = nothing
|
||||
for msg in a.messages
|
||||
role = msg[:role]
|
||||
content = msg[:content]
|
||||
|
||||
if role == "system"
|
||||
prompt = replace(systemToken, "content" => content) * " "
|
||||
elseif role == "user"
|
||||
prompt *= userToken * " " * content * " "
|
||||
elseif role == "assistant"
|
||||
prompt *= assistantToken * " " * content * " "
|
||||
else
|
||||
error("undefied condition role = $role")
|
||||
end
|
||||
end
|
||||
|
||||
return prompt
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module
|
||||
Reference in New Issue
Block a user