diff --git a/core b/core index fd9e5f5..2092e8d 100644 Binary files a/core and b/core differ diff --git a/previousVersion/0.0.2/CondaPkg.toml b/previousVersion/0.0.2/CondaPkg.toml new file mode 100644 index 0000000..f61a833 --- /dev/null +++ b/previousVersion/0.0.2/CondaPkg.toml @@ -0,0 +1,7 @@ +channels = ["anaconda", "conda-forge", "pytorch"] + +[deps] +python = ">=3.8,<3.11" + +[pip.deps] +langchain = "" diff --git a/previousVersion/0.0.2/Manifest.toml b/previousVersion/0.0.2/Manifest.toml new file mode 100644 index 0000000..bd46251 --- /dev/null +++ b/previousVersion/0.0.2/Manifest.toml @@ -0,0 +1,299 @@ +# This file is machine-generated - editing it directly is not advised + +julia_version = "1.9.3" +manifest_format = "2.0" +project_hash = "1af8dd5b4b143461cea0c3d78c642fd87183b648" + +[[deps.ArgTools]] +uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" +version = "1.1.1" + +[[deps.Artifacts]] +uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" + +[[deps.Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[deps.Compat]] +deps = ["UUIDs"] +git-tree-sha1 = "8a62af3e248a8c4bad6b32cbbe663ae02275e32c" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "4.10.0" +weakdeps = ["Dates", "LinearAlgebra"] + + [deps.Compat.extensions] + CompatLinearAlgebraExt = "LinearAlgebra" + +[[deps.CompilerSupportLibraries_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" +version = "1.0.5+0" + +[[deps.CondaPkg]] +deps = ["JSON3", "Markdown", "MicroMamba", "Pidfile", "Pkg", "Preferences", "TOML"] +git-tree-sha1 = "bbd0c518cb11acc6707190199025dbc34b6c7ca7" +uuid = "992eb4ea-22a4-4c89-a5bb-47a3300528ab" +version = "0.2.21" + +[[deps.DataAPI]] +git-tree-sha1 = "8da84edb865b0b5b0100c0666a9bc9a0b71c553c" +uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" +version = "1.15.0" + +[[deps.DataStructures]] +deps = ["Compat", "InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "3dbd312d370723b6bb43ba9d02fc36abade4518d" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.18.15" + +[[deps.DataValueInterfaces]] +git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6" +uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464" +version = "1.0.0" + +[[deps.Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[deps.Downloads]] +deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] +uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" +version = "1.6.0" + +[[deps.FileWatching]] +uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" + +[[deps.InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[deps.IteratorInterfaceExtensions]] +git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856" +uuid = "82899510-4779-5014-852e-03e436cf321d" +version = "1.0.0" + +[[deps.JLLWrappers]] +deps = ["Artifacts", "Preferences"] +git-tree-sha1 = "7e5d6779a1e09a36db2a7b6cff50942a0a7d0fca" +uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" +version = "1.5.0" + +[[deps.JSON3]] +deps = ["Dates", "Mmap", "Parsers", "PrecompileTools", "StructTypes", "UUIDs"] +git-tree-sha1 = "95220473901735a0f4df9d1ca5b171b568b2daa3" +uuid = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" +version = "1.13.2" + +[[deps.LazyArtifacts]] +deps = ["Artifacts", "Pkg"] +uuid = "4af54fe1-eca0-43a8-85a7-787d91b784e3" + +[[deps.LibCURL]] +deps = ["LibCURL_jll", "MozillaCACerts_jll"] +uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" +version = "0.6.3" + +[[deps.LibCURL_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] +uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" +version = "7.84.0+0" + +[[deps.LibGit2]] +deps = ["Base64", "NetworkOptions", "Printf", "SHA"] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[deps.LibSSH2_jll]] +deps = ["Artifacts", "Libdl", "MbedTLS_jll"] +uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" +version = "1.10.2+0" + +[[deps.Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[deps.LinearAlgebra]] +deps = ["Libdl", "OpenBLAS_jll", "libblastrampoline_jll"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[deps.Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[deps.MacroTools]] +deps = ["Markdown", "Random"] +git-tree-sha1 = "9ee1618cbf5240e6d4e0371d6f24065083f60c48" +uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" +version = "0.5.11" + +[[deps.Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[deps.MbedTLS_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" +version = "2.28.2+0" + +[[deps.MicroMamba]] +deps = ["Pkg", "Scratch", "micromamba_jll"] +git-tree-sha1 = "011cab361eae7bcd7d278f0a7a00ff9c69000c51" +uuid = "0b3b1443-0f03-428d-bdfb-f27f9c1191ea" +version = "0.1.14" + +[[deps.Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + +[[deps.MozillaCACerts_jll]] +uuid = "14a3606d-f60d-562e-9121-12d972cd8159" +version = "2022.10.11" + +[[deps.NetworkOptions]] +uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" +version = "1.2.0" + +[[deps.OpenBLAS_jll]] +deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] +uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" +version = "0.3.21+4" + +[[deps.OrderedCollections]] +git-tree-sha1 = "2e73fe17cac3c62ad1aebe70d44c963c3cfdc3e3" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.6.2" + +[[deps.Parsers]] +deps = ["Dates", "PrecompileTools", "UUIDs"] +git-tree-sha1 = "716e24b21538abc91f6205fd1d8363f39b442851" +uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" +version = "2.7.2" + +[[deps.Pidfile]] +deps = ["FileWatching", "Test"] +git-tree-sha1 = "2d8aaf8ee10df53d0dfb9b8ee44ae7c04ced2b03" +uuid = "fa939f87-e72e-5be4-a000-7fc836dbe307" +version = "1.3.0" + +[[deps.Pkg]] +deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" +version = "1.9.2" + +[[deps.PrecompileTools]] +deps = ["Preferences"] +git-tree-sha1 = "03b4c25b43cb84cee5c90aa9b5ea0a78fd848d2f" +uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a" +version = "1.2.0" + +[[deps.Preferences]] +deps = ["TOML"] +git-tree-sha1 = "00805cd429dcb4870060ff49ef443486c262e38e" +uuid = "21216c6a-2e73-6563-6e65-726566657250" +version = "1.4.1" + +[[deps.Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[deps.PythonCall]] +deps = ["CondaPkg", "Dates", "Libdl", "MacroTools", "Markdown", "Pkg", "REPL", "Requires", "Serialization", "Tables", "UnsafePointers"] +git-tree-sha1 = "70af6bdbde63d7d0a4ea99f3e890ebdb55e9d464" +uuid = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" +version = "0.9.14" + +[[deps.REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[deps.Random]] +deps = ["SHA", "Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[deps.Requires]] +deps = ["UUIDs"] +git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" +uuid = "ae029012-a4dd-5104-9daa-d747884805df" +version = "1.3.0" + +[[deps.SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" +version = "0.7.0" + +[[deps.Scratch]] +deps = ["Dates"] +git-tree-sha1 = "30449ee12237627992a99d5e30ae63e4d78cd24a" +uuid = "6c6a2e73-6563-6170-7368-637461726353" +version = "1.2.0" + +[[deps.Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[deps.Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[deps.StructTypes]] +deps = ["Dates", "UUIDs"] +git-tree-sha1 = "ca4bccb03acf9faaf4137a9abc1881ed1841aa70" +uuid = "856f2bd8-1eba-4b0a-8007-ebc267875bd4" +version = "1.10.0" + +[[deps.TOML]] +deps = ["Dates"] +uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" +version = "1.0.3" + +[[deps.TableTraits]] +deps = ["IteratorInterfaceExtensions"] +git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39" +uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" +version = "1.0.1" + +[[deps.Tables]] +deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "OrderedCollections", "TableTraits"] +git-tree-sha1 = "a1f34829d5ac0ef499f6d84428bd6b4c71f02ead" +uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" +version = "1.11.0" + +[[deps.Tar]] +deps = ["ArgTools", "SHA"] +uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" +version = "1.10.0" + +[[deps.Test]] +deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[deps.UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[deps.Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[deps.UnsafePointers]] +git-tree-sha1 = "c81331b3b2e60a982be57c046ec91f599ede674a" +uuid = "e17b2a0c-0bdf-430a-bd0c-3a23cae4ff39" +version = "1.0.0" + +[[deps.Zlib_jll]] +deps = ["Libdl"] +uuid = "83775a58-1f1d-513f-b197-d71354ab007a" +version = "1.2.13+0" + +[[deps.libblastrampoline_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" +version = "5.8.0+0" + +[[deps.micromamba_jll]] +deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl"] +git-tree-sha1 = "66d07957bcf7e4930d933195aed484078dd8cbb5" +uuid = "f8abcde7-e9b7-5caa-b8af-a437887ae8e4" +version = "1.4.9+0" + +[[deps.nghttp2_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" +version = "1.48.0+0" + +[[deps.p7zip_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" +version = "17.4.0+0" diff --git a/previousVersion/0.0.2/Project.toml b/previousVersion/0.0.2/Project.toml new file mode 100644 index 0000000..71d8129 --- /dev/null +++ b/previousVersion/0.0.2/Project.toml @@ -0,0 +1,11 @@ +name = "ChatAgent" +uuid = "cff63402-b71f-455f-804d-24489fc61e5e" +authors = ["narawat "] +version = "0.1.0" + +[deps] +CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab" +DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" +PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" diff --git a/previousVersion/0.0.2/src/ChatAgent.jl b/previousVersion/0.0.2/src/ChatAgent.jl new file mode 100644 index 0000000..bcc89f3 --- /dev/null +++ b/previousVersion/0.0.2/src/ChatAgent.jl @@ -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 diff --git a/previousVersion/0.0.2/src/interface.jl b/previousVersion/0.0.2/src/interface.jl new file mode 100644 index 0000000..fd1ab2f --- /dev/null +++ b/previousVersion/0.0.2/src/interface.jl @@ -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]<> content <>") + 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]<> content <>") + 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 \ No newline at end of file diff --git a/src/interface.jl b/src/interface.jl index fd1ab2f..01eebe5 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -4,8 +4,13 @@ module interface export agent, addNewMessage, clearMessage, removeLatestMsg, generatePrompt_tokenPrefix, generatePrompt_tokenSuffix -using JSON3, DataStructures, Dates, PythonCall +using JSON3, DataStructures, Dates +# Ref: https://github.com/JuliaPy/PythonCall.jl/issues/252 +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" +using PythonCall const py_agents = PythonCall.pynew() const py_llms = PythonCall.pynew() function __init__()