-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson.cpp
More file actions
26 lines (22 loc) · 933 Bytes
/
Copy pathjson.cpp
File metadata and controls
26 lines (22 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define LLM_JSON_IMPLEMENTATION
#include "llm_json.hpp"
#include <cstdio>
int main() {
namespace json = llm::json;
auto body = json::object(); // build a request body
body["model"] = "gpt-4o-mini";
body["temperature"] = 0.5;
auto msg = json::object();
msg["role"] = "user";
msg["content"] = "Say \"hi\"";
body["messages"].push_back(msg);
std::printf("%s\n\n", body.dump_pretty().c_str());
auto resp = json::parse(R"({"choices":[{"message":{"content":"hi!"}}],
"usage":{"total_tokens":17}})");
auto& text = resp["choices"][0]["message"]["content"];
std::printf("content: %s\ntokens: %lld\n", text.as_string().c_str(),
resp["usage"]["total_tokens"].as_int());
auto bad = json::try_parse(R"({"choices": [}")");
std::printf("\nbad input -> ok=%s, %s\n",
bad.ok ? "true" : "false", bad.error.c_str());
}