NeoGraph 0.10.0
A C++17 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <string>
12#include <vector>
13#include <neograph/json.h>
14
15namespace neograph {
16
24struct ToolCall {
25 std::string id;
26 std::string name;
27 std::string arguments;
28};
29
37 std::string role;
38 std::string content;
39 std::vector<ToolCall> tool_calls;
40 std::string tool_call_id;
41 std::string tool_name;
42 std::vector<std::string> image_urls;
43};
44
51struct ChatTool {
52 std::string name;
53 std::string description;
55};
56
62
64 struct Usage {
65 int prompt_tokens = 0;
67 int total_tokens = 0;
68 } usage;
69};
70
71// --- ADL serialization: ChatMessage/ToolCall <-> json ---
72// These live in the same namespace as the types for ADL lookup.
73
77inline void to_json(json& j, const ToolCall& tc) {
78 j = json{{"id", tc.id}, {"name", tc.name}, {"arguments", tc.arguments}};
79}
80
84inline void from_json(const json& j, ToolCall& tc) {
85 tc.id = j.value("id", "");
86 tc.name = j.value("name", "");
87 tc.arguments = j.value("arguments", "");
88}
89
93inline void to_json(json& j, const ChatMessage& msg) {
94 j["role"] = msg.role;
95 j["content"] = msg.content;
96 if (!msg.tool_calls.empty()) {
97 j["tool_calls"] = json::array();
98 for (const auto& tc : msg.tool_calls) {
99 json tc_j;
100 to_json(tc_j, tc);
101 j["tool_calls"].push_back(tc_j);
102 }
103 }
104 if (!msg.tool_call_id.empty()) j["tool_call_id"] = msg.tool_call_id;
105 if (!msg.tool_name.empty()) j["tool_name"] = msg.tool_name;
106 if (!msg.image_urls.empty()) j["image_urls"] = msg.image_urls;
107}
108
112inline void from_json(const json& j, ChatMessage& msg) {
113 msg.role = j.value("role", "");
114 msg.content = j.value("content", "");
115 if (j.contains("tool_calls") && j["tool_calls"].is_array()) {
116 for (const auto& tc_j : j["tool_calls"]) {
117 ToolCall tc;
118 from_json(tc_j, tc);
119 msg.tool_calls.push_back(tc);
120 }
121 }
122 msg.tool_call_id = j.value("tool_call_id", "");
123 msg.tool_name = j.value("tool_name", "");
124 if (j.contains("image_urls") && j["image_urls"].is_array()) {
125 msg.image_urls = j["image_urls"].get<std::vector<std::string>>();
126 }
127}
128
129// --- JSON serialization helpers ---
130
140inline json messages_to_json(const std::vector<ChatMessage>& messages) {
141 json arr = json::array();
142 for (const auto& msg : messages) {
143 json j;
144 j["role"] = msg.role;
145
146 if (msg.role == "tool") {
147 j["content"] = msg.content;
148 j["tool_call_id"] = msg.tool_call_id;
149 } else if (!msg.tool_calls.empty()) {
150 j["content"] = msg.content.empty() ? json(nullptr) : json(msg.content);
151 json tc_arr = json::array();
152 for (const auto& tc : msg.tool_calls) {
153 tc_arr.push_back({
154 {"id", tc.id},
155 {"type", "function"},
156 {"function", {{"name", tc.name}, {"arguments", tc.arguments}}}
157 });
158 }
159 j["tool_calls"] = tc_arr;
160 } else if (!msg.image_urls.empty()) {
161 // Multi-modal: text + images (OpenAI Vision format)
162 json parts = json::array();
163 if (!msg.content.empty()) {
164 parts.push_back({{"type", "text"}, {"text", msg.content}});
165 }
166 for (auto& url : msg.image_urls) {
167 parts.push_back({{"type", "image_url"}, {"image_url", {{"url", url}}}});
168 }
169 j["content"] = parts;
170 } else {
171 j["content"] = msg.content;
172 }
173
174 arr.push_back(j);
175 }
176 return arr;
177}
178
185inline json tools_to_json(const std::vector<ChatTool>& tools) {
186 json arr = json::array();
187 for (const auto& tool : tools) {
188 arr.push_back({
189 {"type", "function"},
190 {"function", {
191 {"name", tool.name},
192 {"description", tool.description},
193 {"parameters", tool.parameters}
194 }}
195 });
196 }
197 return arr;
198}
199
210inline ChatMessage parse_response_message(const json& choice) {
211 ChatMessage msg;
212 auto m = choice.at("message");
213 msg.role = m.value("role", "assistant");
214 msg.content = (m.contains("content") && !m["content"].is_null())
215 ? m["content"].get<std::string>() : "";
216
217 if (m.contains("tool_calls") && m["tool_calls"].is_array()) {
218 for (const auto& tc : m["tool_calls"]) {
219 ToolCall call;
220 call.id = tc.value("id", "");
221 auto fn = tc.at("function");
222 call.name = fn.value("name", "");
223 call.arguments = fn.value("arguments", "");
224 msg.tool_calls.push_back(std::move(call));
225 }
226 }
227
228 return msg;
229}
230
231} // namespace neograph
Thin C++ RAII wrapper around yyjson with nlohmann-compatible API.
Token usage statistics for the completion.
Definition types.h:64
int completion_tokens
Number of tokens in the completion.
Definition types.h:66
int prompt_tokens
Number of tokens in the prompt.
Definition types.h:65
int total_tokens
Total tokens used (prompt + completion).
Definition types.h:67
LLM completion response including the message and token usage.
Definition types.h:60
ChatMessage message
The response message from the LLM.
Definition types.h:61
A message in the conversation history.
Definition types.h:36
std::string tool_call_id
ID of the tool call being responded to (role == "tool").
Definition types.h:40
std::string tool_name
Name of the tool being called.
Definition types.h:41
std::string content
Text content of the message.
Definition types.h:38
std::vector< std::string > image_urls
Base64 data URLs or HTTP URLs for vision support.
Definition types.h:42
std::string role
Message role: "user", "assistant", "tool", or "system".
Definition types.h:37
std::vector< ToolCall > tool_calls
Tool calls made by the assistant (if any).
Definition types.h:39
Tool definition metadata sent to the LLM.
Definition types.h:51
json parameters
JSON Schema object describing the tool's parameters.
Definition types.h:54
std::string description
Human-readable description of what the tool does.
Definition types.h:53
std::string name
Tool name (must be unique within a session).
Definition types.h:52
Represents a single tool invocation requested by the LLM.
Definition types.h:24
std::string name
Name of the tool to invoke.
Definition types.h:26
std::string id
Unique identifier for this tool call.
Definition types.h:25
std::string arguments
JSON-encoded string of tool arguments.
Definition types.h:27
json tools_to_json(const std::vector< ChatTool > &tools)
Convert a vector of ChatTools to OpenAI-compatible JSON format.
Definition types.h:185
json messages_to_json(const std::vector< ChatMessage > &messages)
Convert a vector of ChatMessages to OpenAI-compatible JSON format.
Definition types.h:140
ChatMessage parse_response_message(const json &choice)
Parse an OpenAI API response choice into a ChatMessage.
Definition types.h:210