-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_custom_model.cpp
More file actions
74 lines (60 loc) · 1.84 KB
/
Copy path03_custom_model.cpp
File metadata and controls
74 lines (60 loc) · 1.84 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2026 Bontal LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
/**
* @file 03_custom_model.cpp
* @brief Parsing API responses directly into custom structs via from_json.
*/
#include <typesafe/typesafe.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include "openrouter_helper.h"
namespace
{
/**
* @brief Custom application struct for ticket triage results.
*/
struct TriageSummary
{
std::string category;
double urgency = 0.0;
std::int64_t input_tokens = 0;
};
void from_json(const nlohmann::json &j, TriageSummary &summary)
{
const nlohmann::json &answers = j.at("answers");
summary.category = answers.at("category").at("choice").get<std::string>();
summary.urgency = answers.at("urgency").at("score").get<double>();
summary.input_tokens = j.at("usage").at("input_tokens").get<std::int64_t>();
}
} // namespace
int main()
{
try
{
auto client = typesafe::examples::createClient();
typesafe::SystemOneRequest request;
request.state = "I was charged twice. Please fix this ASAP.";
request.add("category",
typesafe::Choice{"What is this ticket about?",
{{"billing", std::nullopt},
{"technical", std::nullopt}}});
request.add(
"urgency",
typesafe::Score{"How urgent?", {"can wait", "this week", "today"}});
const TriageSummary summary
= client.systemOneAs<TriageSummary>(request);
std::cout << "category: " << summary.category << "\n";
std::cout << "urgency: " << summary.urgency << "\n";
std::cout << "cost " << summary.input_tokens << " input tokens\n";
return (0);
}
catch (const typesafe::TypeSafeError &error)
{
std::cerr << "TypeSafe: " << error.what() << "\n";
return (1);
}
}