Current section

Files

Jump to
gloq src gloq@response.erl
Raw

src/gloq@response.erl

-module(gloq@response).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src\\gloq\\response.gleam").
-export([decode/1, decode_error/1, content/1, all_contents/1]).
-export_type([usage/0, response_message/0, choice/0, chat_completion/0, api_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type usage() :: {usage, integer(), integer(), integer()}.
-type response_message() :: {response_message, binary(), binary()}.
-type choice() :: {choice,
integer(),
response_message(),
gleam@option:option(binary())}.
-type chat_completion() :: {chat_completion,
binary(),
binary(),
list(choice()),
usage()}.
-type api_error() :: {api_error,
binary(),
binary(),
gleam@option:option(binary())}.
-file("src\\gloq\\response.gleam", 79).
-spec response_message_decoder() -> gleam@dynamic@decode:decoder(response_message()).
response_message_decoder() ->
gleam@dynamic@decode:field(
<<"role"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Role) ->
gleam@dynamic@decode:field(
<<"content"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Content) ->
gleam@dynamic@decode:success(
{response_message, Role, Content}
)
end
)
end
).
-file("src\\gloq\\response.gleam", 85).
-spec choice_decoder() -> gleam@dynamic@decode:decoder(choice()).
choice_decoder() ->
gleam@dynamic@decode:field(
<<"index"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Index) ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
response_message_decoder(),
fun(Message) ->
gleam@dynamic@decode:field(
<<"finish_reason"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Finish_reason) ->
gleam@dynamic@decode:success(
{choice, Index, Message, Finish_reason}
)
end
)
end
)
end
).
-file("src\\gloq\\response.gleam", 99).
-spec chat_completion_decoder() -> gleam@dynamic@decode:decoder(chat_completion()).
chat_completion_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"model"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Model) ->
gleam@dynamic@decode:field(
<<"choices"/utf8>>,
gleam@dynamic@decode:list(choice_decoder()),
fun(Choices) ->
gleam@dynamic@decode:field(
<<"usage"/utf8>>,
usage_decoder(),
fun(Usage) ->
gleam@dynamic@decode:success(
{chat_completion,
Id,
Model,
Choices,
Usage}
)
end
)
end
)
end
)
end
).
-file("src\\gloq\\response.gleam", 121).
?DOC(
" Decode a JSON response string into a `ChatCompletion`.\n"
" Returns an error if the JSON is malformed or missing required fields.\n"
).
-spec decode(binary()) -> {ok, chat_completion()} |
{error, gleam@json:decode_error()}.
decode(Json_string) ->
gleam@json:parse(Json_string, chat_completion_decoder()).
-file("src\\gloq\\response.gleam", 68).
-spec usage_decoder() -> gleam@dynamic@decode:decoder(usage()).
usage_decoder() ->
gleam@dynamic@decode:field(
<<"prompt_tokens"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Prompt_tokens) ->
gleam@dynamic@decode:field(
<<"completion_tokens"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Completion_tokens) ->
gleam@dynamic@decode:field(
<<"total_tokens"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Total_tokens) ->
gleam@dynamic@decode:success(
{usage,
Prompt_tokens,
Completion_tokens,
Total_tokens}
)
end
)
end
)
end
).
-file("src\\gloq\\response.gleam", 112).
-spec api_error_decoder() -> gleam@dynamic@decode:decoder(api_error()).
api_error_decoder() ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:field(
<<"type"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Error_type) ->
gleam@dynamic@decode:field(
<<"code"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Code) ->
gleam@dynamic@decode:success(
{api_error, Message, Error_type, Code}
)
end
)
end
)
end
).
-file("src\\gloq\\response.gleam", 125).
-spec outer_error_decoder() -> gleam@dynamic@decode:decoder(api_error()).
outer_error_decoder() ->
gleam@dynamic@decode:field(
<<"error"/utf8>>,
api_error_decoder(),
fun(Error) -> gleam@dynamic@decode:success(Error) end
).
-file("src\\gloq\\response.gleam", 132).
?DOC(
" Decode an API error response. Use this when `decode` fails to check\n"
" whether the server returned a structured error object.\n"
).
-spec decode_error(binary()) -> {ok, api_error()} |
{error, gleam@json:decode_error()}.
decode_error(Json_string) ->
gleam@json:parse(Json_string, outer_error_decoder()).
-file("src\\gloq\\response.gleam", 138).
?DOC(
" Extract the text content from the first choice in a completion.\n"
" Returns an empty string if there are no choices.\n"
).
-spec content(chat_completion()) -> binary().
content(Completion) ->
case erlang:element(4, Completion) of
[] ->
<<""/utf8>>;
[Choice | _] ->
erlang:element(3, erlang:element(3, Choice))
end.
-file("src\\gloq\\response.gleam", 146).
?DOC(" Extract text content from all choices in a completion.\n").
-spec all_contents(chat_completion()) -> list(binary()).
all_contents(Completion) ->
gleam@list:map(
erlang:element(4, Completion),
fun(C) -> erlang:element(3, erlang:element(3, C)) end
).