This commit is contained in:
2026-08-06 18:59:39 +07:00
parent dde9b019dd
commit ebd79aa686
+50 -37
View File
@@ -116,46 +116,59 @@ end
""" convert preparedcontext into openai message format ready to be used by LLM """ convert preparedcontext into openai message format ready to be used by LLM
""" """
function formatMsgForLLM(messages::Vector{agentMessage})::Vector{Dict{String, Any}} function formatMsgForLLM(messages::Vector{agentMessage})::Vector{Dict{String, Any}}
""" openai message format example
msg = Dict( openai_messages = Vector{Dict{String, Any}}()
"model" => "gemma-4-E4B-it-UD-Q4_K_XL",
"messages" => [ for msg in messages
Dict( if msg isa userMessage
"role" => "system", push!(openai_messages, _toOpenAIUserMsg(msg))
"content" => [ elseif msg isa assistantMessage
Dict("type" => "text", "text" => systemmsg), push!(openai_messages, _toOpenAIAssistantMsg(msg))
] elseif msg isa toolResultMessage
), push!(openai_messages, _toOpenAIToolResultMsg(msg))
Dict( end
"role" => "user", end
"content" => [
Dict("type" => "text", "text" => "Do you have something similar to the one in the image?"), return openai_messages
Dict( end
"type" => "image_url",
"image_url" => Dict("url" => data1_uri) function _toOpenAIUserMsg(msg::userMessage)::Dict{String, Any}
) return Dict{String, Any}(
] "role" => "user",
), "content" => _toOpenAIBlocks(msg.content),
Dict(
"role" => "assistant",
"content" => [
Dict("type" => "text", "text" => "let me check."),
]
),
Dict(
"role" => "toolResult",
"content" => [
Dict("type" => "text", "text" => "name: Chateau Montelena ..."),
]
),
],
"temperature" => 0.7
) )
""" end
# convert various messages to openai message format function _toOpenAIAssistantMsg(msg::assistantMessage)::Dict{String, Any}
return Dict{String, Any}(
"role" => "assistant",
"content" => _toOpenAIBlocks(msg.content),
)
end
return openai_message function _toOpenAIToolResultMsg(msg::toolResultMessage)::Dict{String, Any}
return Dict{String, Any}(
"role" => "tool",
"tool_call_id" => msg.toolCallId,
"name" => msg.toolName,
"content" => _toOpenAIBlocks(msg.content),
)
end
function _toOpenAIBlocks(contents::Vector{messageContent})::Vector{Dict{String, Any}}
blocks = Vector{Dict{String, Any}}()
for c in contents
if c isa textContent
push!(blocks, Dict("type" => "text", "text" => c.text))
elseif c isa imageContent
data_uri = "data:$(c.mimeType);base64,$(c.data)"
push!(blocks, Dict(
"type" => "image_url",
"image_url" => Dict("url" => data_uri),
))
end
end
return blocks
end end