244 lines
6.7 KiB
Julia
Executable File
244 lines
6.7 KiB
Julia
Executable File
# ---------------------------------------------- 100 --------------------------------------------- #
|
|
|
|
|
|
using JSON, Dates, UUIDs, PrettyPrinting, Base64, DataFrames, NATS, HTTP
|
|
using GeneralUtils, msghandler
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
# llama.cpp chat request example (direct HTTP) #
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
|
|
# 1. Read local file and encode to base64 string
|
|
image_path = "test/large_image.png"
|
|
image_bytes = read(image_path)
|
|
base64_string = base64encode(image_bytes)
|
|
|
|
# 2. Match the MIME type according to your file extension (e.g., png, jpeg)
|
|
mime_type = "image/png"
|
|
data_uri = "data:$(mime_type);base64,$(base64_string)"
|
|
|
|
|
|
headers = [
|
|
"Content-Type" => "application/json",
|
|
"Authorization" => "Bearer no-key"
|
|
]
|
|
|
|
# 3. Construct payload with the Data URI
|
|
messages = Dict(
|
|
"model" => "gemma-4-E4B-it-UD-Q4_K_XL",
|
|
"messages" => [
|
|
Dict(
|
|
"role" => "user",
|
|
"content" => [
|
|
Dict("type" => "text", "text" => "Describe this image for me."),
|
|
Dict(
|
|
"type" => "image_url",
|
|
"image_url" => Dict("url" => data_uri)
|
|
)
|
|
]
|
|
)
|
|
],
|
|
"temperature" => 0.7
|
|
)
|
|
url = "http://192.168.1.8:9001/v1/chat/completions"
|
|
response = HTTP.post(url, headers, JSON.json(messages))
|
|
r = JSON.parse(response.body)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
# Llama.cpp Embedding multiple text example (direct HTTP) #
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
using HTTP
|
|
using JSON
|
|
|
|
url = "http://192.168.88.50:9010/v1/embeddings"
|
|
headers = [
|
|
"Content-Type" => "application/json",
|
|
"Authorization" => "Bearer no-key"
|
|
]
|
|
|
|
# Pass an array of strings instead of a single string
|
|
documents = [
|
|
"System architecture specification for heart monitor logic.",
|
|
"NATS broker cluster configuration options for low-latency.",
|
|
"Julia signal processing worker initialization sequence."
|
|
]
|
|
|
|
body_data = Dict(
|
|
"model" => "embeddinggemma-300M-BF16",
|
|
"input" => documents
|
|
)
|
|
|
|
try
|
|
response = HTTP.post(url, headers, JSON.json(body_data))
|
|
parsed = JSON.parse(String(response.body))
|
|
|
|
# Extract multiple vectors (ordered identically to your input array)
|
|
for (i, item) in enumerate(parsed["data"])
|
|
idx = item["index"] + 1 # API indexes are 0-based; Julia is 1-based
|
|
vector = item["embedding"]
|
|
println("Document $idx unified vector size: ", length(vector))
|
|
end
|
|
|
|
catch e
|
|
println("Batch embedding failed: ", e)
|
|
end
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
# test openai_message (NATS) #
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
# Llama.cpp Embedding multiple text with NATS #
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
using JSON, Dates, UUIDs, PrettyPrinting, Base64, DataFrames, NATS, HTTP
|
|
using GeneralUtils, msghandler
|
|
|
|
const config = JSON.parsefile("./appconfig.json")
|
|
|
|
documents = [
|
|
"System architecture specification for heart monitor logic.",
|
|
# "NATS broker cluster configuration options for low-latency.",
|
|
# "Julia signal processing worker initialization sequence."
|
|
]
|
|
|
|
documents_dict = Dict("documents"=> documents)
|
|
|
|
payloads = [("documents", documents_dict, "dictionary")]
|
|
message_envelope, forward_msg_envelope_json_str = msghandler.smartpack(
|
|
config["this_service_input_channel"]["nats"],
|
|
payloads;
|
|
msg_purpose="embedding",
|
|
broker_url=config["nats_server_info"]["url"],
|
|
fileserver_url=config["externalService"]["fileserver"]["url"])
|
|
|
|
connection = NATS.connect(config["nats_server_info"]["url"])
|
|
reply = NATS.request(connection,
|
|
config["this_service_input_channel"]["nats"],
|
|
forward_msg_envelope_json_str, timeout=120)
|
|
|
|
incoming_env_json_str = String(reply.payload)
|
|
incoming_env = msghandler.smartunpack(incoming_env_json_str)
|
|
embedding_response = incoming_env["payloads"][1][2]
|
|
|
|
for (i, item) in enumerate(embedding_response["data"])
|
|
vector = item["embedding"]
|
|
println("Document $i embedding vector size: ", length(vector))
|
|
end
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
# test openai_message (NATS) #
|
|
# ------------------------------------------------------------------------------------------------ #
|
|
using JSON, Dates, UUIDs, PrettyPrinting, Base64, DataFrames, NATS, HTTP
|
|
using GeneralUtils, msghandler
|
|
|
|
const config = JSON.parsefile("./appconfig.json")
|
|
|
|
# 1. Read local file and encode to base64 string
|
|
image_path = "test/large_image.png"
|
|
image_bytes = read(image_path)
|
|
base64_string = base64encode(image_bytes)
|
|
|
|
# 2. Match the MIME type according to your file extension (e.g., png, jpeg)
|
|
mime_type = "image/png"
|
|
data_uri = "data:$(mime_type);base64,$(base64_string)"
|
|
|
|
|
|
headers = [
|
|
"Content-Type" => "application/json",
|
|
"Authorization" => "Bearer no-key"
|
|
]
|
|
|
|
# 3. Construct payload with the Data URI
|
|
openai_msg = Dict(
|
|
"model" => "gemma-4-E4B-it-UD-Q4_K_XL",
|
|
"messages" => [
|
|
Dict(
|
|
"role" => "user",
|
|
"content" => [
|
|
Dict("type" => "text", "text" => "Describe this image for me in detail."),
|
|
Dict(
|
|
"type" => "image_url",
|
|
"image_url" => Dict("url" => data_uri)
|
|
)
|
|
]
|
|
)
|
|
],
|
|
"temperature" => 0.7
|
|
)
|
|
|
|
payloads = [("msg", openai_msg, "dictionary")] # List of tuples
|
|
message_envelope, forward_msg_envelope_json_str = msghandler.smartpack(
|
|
config["this_service_input_channel"]["nats"],
|
|
payloads;
|
|
msg_purpose="text2text",
|
|
broker_url=config["nats_server_info"]["url"],
|
|
fileserver_url=config["externalService"]["fileserver"]["url"])
|
|
|
|
|
|
connection = NATS.connect(config["nats_server_info"]["url"])
|
|
reply = NATS.request(connection,
|
|
config["this_service_input_channel"]["nats"],
|
|
forward_msg_envelope_json_str, timeout=120)
|
|
|
|
incoming_env_json_str = String(reply.payload)
|
|
incoming_env = msghandler.smartunpack(incoming_env_json_str)
|
|
ai_response = incoming_env["payloads"][1][2]
|
|
content = ai_response["choices"][1]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|