5#include "absl/status/status.h"
6#include "absl/strings/str_cat.h"
7#include "nlohmann/json.hpp"
15 nlohmann::json function;
16 function[
"name"] = spec.
name;
22 nlohmann::json parameters;
23 parameters[
"type"] =
"object";
25 nlohmann::json properties = nlohmann::json::object();
26 nlohmann::json required = nlohmann::json::array();
29 nlohmann::json arg_schema;
30 arg_schema[
"type"] =
"string";
31 arg_schema[
"description"] = arg.description;
32 if (!arg.example.empty()) {
33 arg_schema[
"example"] = arg.example;
35 properties[arg.name] = std::move(arg_schema);
38 required.push_back(arg.name);
42 parameters[
"properties"] = std::move(properties);
43 if (!required.empty()) {
44 parameters[
"required"] = std::move(required);
47 function[
"parameters"] = std::move(parameters);
52 const nlohmann::json& schema_json) {
53 if (!schema_json.is_object()) {
54 return absl::InvalidArgumentError(
55 "Tool schema entries must be JSON objects");
58 if (schema_json.contains(
"function")) {
59 const auto& function = schema_json[
"function"];
60 if (!function.is_object()) {
61 return absl::InvalidArgumentError(
62 "OpenAI tool schema must contain an object-valued function field");
73 const std::vector<ToolSpecification>& tool_specs) {
74 nlohmann::json functions = nlohmann::json::array();
75 for (
const auto& spec : tool_specs) {
76 functions.push_back(BuildFunctionDeclaration(spec));
83 nlohmann::json function_declarations =
85 if (!function_declarations.empty()) {
86 return function_declarations;
93 const nlohmann::json& schema_json) {
94 if (schema_json.is_object() &&
95 schema_json.contains(
"function_declarations")) {
96 const auto& functions = schema_json[
"function_declarations"];
97 if (!functions.is_array()) {
98 return absl::InvalidArgumentError(
99 "function_declarations must be a JSON array");
104 if (schema_json.is_array()) {
105 nlohmann::json functions = nlohmann::json::array();
106 for (
const auto& entry : schema_json) {
107 auto function_or = ExtractFunctionDeclaration(entry);
108 if (!function_or.ok()) {
109 return function_or.status();
111 functions.push_back(std::move(function_or).value());
116 auto function_or = ExtractFunctionDeclaration(schema_json);
117 if (!function_or.ok()) {
118 return function_or.status();
121 nlohmann::json functions = nlohmann::json::array();
122 functions.push_back(std::move(function_or).value());
127 std::string_view schema_json) {
128 if (schema_json.empty()) {
129 return nlohmann::json::array();
134 }
catch (
const nlohmann::json::exception& e) {
135 return absl::InvalidArgumentError(
136 absl::StrCat(
"Failed to parse tool schema JSON: ", e.what()));
140absl::StatusOr<nlohmann::json>
142 const std::string& relative_path) {
144 if (!schema_path_or.ok()) {
145 return schema_path_or.status();
148 std::ifstream file(schema_path_or->string());
149 if (!file.is_open()) {
150 return absl::NotFoundError(absl::StrCat(
151 "Failed to open tool schema asset: ", schema_path_or->string()));
155 nlohmann::json schema_json;
158 }
catch (
const nlohmann::json::exception& e) {
159 return absl::InvalidArgumentError(
160 absl::StrCat(
"Failed to parse tool schema asset: ", e.what()));
165 const nlohmann::json& function_declarations) {
166 nlohmann::json tools = nlohmann::json::array();
167 for (
const auto& function : function_declarations) {
168 tools.push_back({{
"type",
"function"}, {
"function", function}});
174 const nlohmann::json& function_declarations) {
175 if (function_declarations.empty()) {
176 return nlohmann::json::array();
179 return nlohmann::json::array(
180 {{{
"function_declarations", function_declarations}}});
184 const nlohmann::json& function_declarations) {
185 nlohmann::json tools = nlohmann::json::array();
186 for (
const auto& function : function_declarations) {
187 tools.push_back({{
"name", function.value(
"name",
"")},
188 {
"description", function.value(
"description",
"")},
190 function.value(
"parameters", nlohmann::json::object())}});
const std::vector< ToolSpecification > & tool_specs() const
Namespace for the command line interface.