Current section

Files

Jump to
gopenai src gopenai@tool.erl
Raw

src/gopenai@tool.erl

-module(gopenai@tool).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([tool_type_from_json/1, validate_property/1, tool_to_json/1, tools_to_json/1, function_call_from_json/1]).
-export_type([tool_type/0, data_type/0, function_property/0, function_parameters/0, function_/0, tool/0, value/0, function_call/0, tool_call/0]).
-type tool_type() :: function_type.
-type data_type() :: string_type |
number_type |
array_type |
boolean_type |
null_type |
object_type.
-type function_property() :: {function_property,
data_type(),
binary(),
gleam@option:option(list(binary()))}.
-type function_parameters() :: {function_parameters,
data_type(),
gleam@dict:dict(binary(), function_property()),
list(binary())}.
-type function_() :: {function,
binary(),
binary(),
gleam@option:option(function_parameters())}.
-type tool() :: {tool, tool_type(), function_()}.
-type value() :: int | float | string.
-type function_call() :: {function_call, binary(), binary()}.
-type tool_call() :: {tool_call, binary(), tool_type(), function_call()}.
-spec tool_type_to_json(tool_type()) -> gleam@json:json().
tool_type_to_json(Tool_type) ->
_pipe = case Tool_type of
function_type ->
<<"function"/utf8>>
end,
gleam@json:string(_pipe).
-spec tool_type_from_json(gleam@dynamic:dynamic_()) -> {ok, tool_type()} |
{error, list(gleam@dynamic:decode_error())}.
tool_type_from_json(Data) ->
case gleam@dynamic:string(Data) of
{ok, Str} ->
case Str of
<<"function"/utf8>> ->
{ok, function_type};
Default ->
{error,
[{decode_error, <<"valid tool type"/utf8>>, Default, []}]}
end;
{error, E} ->
{error, E}
end.
-spec data_type_to_json(data_type()) -> gleam@json:json().
data_type_to_json(Data_type) ->
_pipe = case Data_type of
string_type ->
<<"string"/utf8>>;
number_type ->
<<"number"/utf8>>;
array_type ->
<<"array"/utf8>>;
boolean_type ->
<<"boolean"/utf8>>;
null_type ->
<<"null"/utf8>>;
object_type ->
<<"object"/utf8>>
end,
gleam@json:string(_pipe).
-spec function_property_to_json(function_property()) -> gleam@json:json().
function_property_to_json(Prop) ->
gleam@json:object(
[{<<"type"/utf8>>, data_type_to_json(erlang:element(2, Prop))},
{<<"description"/utf8>>, gleam@json:string(erlang:element(3, Prop))}]
).
-spec validate_property(function_property()) -> boolean().
validate_property(Prop) ->
case erlang:element(2, Prop) of
string_type ->
true;
_ ->
false
end.
-spec function_paramteres_to_json(gleam@option:option(function_parameters())) -> gleam@json:json().
function_paramteres_to_json(Parameters) ->
case Parameters of
{some, Params} ->
gleam@json:object(
[{<<"type"/utf8>>, data_type_to_json(erlang:element(2, Params))},
{<<"properties"/utf8>>,
gleam@json:object(
begin
_pipe = gleam@dict:map_values(
erlang:element(3, Params),
fun(_, Value) ->
function_property_to_json(Value)
end
),
maps:to_list(_pipe)
end
)},
{<<"required"/utf8>>,
gleam@json:array(
erlang:element(4, Params),
fun gleam@json:string/1
)}]
);
none ->
gleam@json:null()
end.
-spec function_to_json(function_()) -> gleam@json:json().
function_to_json(Function) ->
gleam@json:object(
[{<<"description"/utf8>>,
gleam@json:string(erlang:element(2, Function))},
{<<"name"/utf8>>, gleam@json:string(erlang:element(3, Function))},
{<<"parameters"/utf8>>,
function_paramteres_to_json(erlang:element(4, Function))}]
).
-spec tool_to_json(tool()) -> gleam@json:json().
tool_to_json(Tool) ->
gleam@json:object(
[{<<"type"/utf8>>, tool_type_to_json(erlang:element(2, Tool))},
{<<"function"/utf8>>, function_to_json(erlang:element(3, Tool))}]
).
-spec tools_to_json(gleam@option:option(list(tool()))) -> gleam@json:json().
tools_to_json(Tools) ->
case Tools of
{some, Ts} ->
gleam@json:array(Ts, fun tool_to_json/1);
none ->
gleam@json:null()
end.
-spec function_call_from_json(gleam@dynamic:dynamic_()) -> {ok, function_call()} |
{error, list(gleam@dynamic:decode_error())}.
function_call_from_json(Data) ->
_pipe = Data,
(gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {function_call, Field@0, Field@1} end,
gleam@dynamic:field(<<"name"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"arguments"/utf8>>, fun gleam@dynamic:string/1)
))(_pipe).