Current section

Files

Jump to
gopenai src gopenai@embeddings.erl
Raw

src/gopenai@embeddings.erl

-module(gopenai@embeddings).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([request_to_json/1, embeddings_from_json/1, embeddings/2]).
-export_type([object_type/0, encoding_format/0, embeddings_request/0, embedding/0, usage/0, embeddings_response/0, embeddings_error/0]).
-type object_type() :: list_type | embedding_type.
-type encoding_format() :: float | base64.
-type embeddings_request() :: {embeddings_request,
list(binary()),
gopenai@model:model(),
encoding_format()}.
-type embedding() :: {embedding, object_type(), list(float())}.
-type usage() :: {usage, integer(), integer()}.
-type embeddings_response() :: {embeddings_response,
object_type(),
list(embedding()),
gopenai@model:model(),
usage()}.
-type embeddings_error() :: {dynamic_error, gleam@dynamic:dynamic_()} |
{json_decode_error, gleam@json:decode_error()}.
-spec object_type_from_json(gleam@dynamic:dynamic_()) -> {ok, object_type()} |
{error, list(gleam@dynamic:decode_error())}.
object_type_from_json(Data) ->
case gleam@dynamic:string(Data) of
{ok, Str} ->
case Str of
<<"list"/utf8>> ->
{ok, list_type};
<<"embedding"/utf8>> ->
{ok, embedding_type};
Default ->
{error,
[{decode_error,
<<"valid object type"/utf8>>,
Default,
[]}]}
end;
{error, E} ->
{error, E}
end.
-spec encoding_format_to_json(encoding_format()) -> gleam@json:json().
encoding_format_to_json(Ef) ->
_pipe = case Ef of
float ->
<<"float"/utf8>>;
base64 ->
<<"base64"/utf8>>
end,
gleam@json:string(_pipe).
-spec request_to_json(embeddings_request()) -> binary().
request_to_json(Req) ->
_pipe = gleam@json:object(
[{<<"model"/utf8>>, gopenai@model:model_to_json(erlang:element(3, Req))},
{<<"input"/utf8>>,
gleam@json:array(
erlang:element(2, Req),
fun gleam@json:string/1
)},
{<<"encoding_format"/utf8>>,
encoding_format_to_json(erlang:element(4, Req))}]
),
gleam@json:to_string(_pipe).
-spec embedding_from_json(gleam@dynamic:dynamic_()) -> {ok, embedding()} |
{error, list(gleam@dynamic:decode_error())}.
embedding_from_json(Data) ->
_pipe = Data,
(gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {embedding, Field@0, Field@1} end,
gleam@dynamic:field(<<"object"/utf8>>, fun object_type_from_json/1),
gleam@dynamic:field(
<<"embedding"/utf8>>,
gleam@dynamic:list(fun gleam@dynamic:float/1)
)
))(_pipe).
-spec embeddings_from_json(binary()) -> {ok, embeddings_response()} |
{error, gleam@json:decode_error()}.
embeddings_from_json(Data) ->
_pipe = Data,
gleam@json:decode(
_pipe,
gleam@dynamic:decode4(
fun(Field@0, Field@1, Field@2, Field@3) -> {embeddings_response, Field@0, Field@1, Field@2, Field@3} end,
gleam@dynamic:field(<<"object"/utf8>>, fun object_type_from_json/1),
gleam@dynamic:field(
<<"data"/utf8>>,
gleam@dynamic:list(fun embedding_from_json/1)
),
gleam@dynamic:field(
<<"model"/utf8>>,
fun gopenai@model:model_from_json/1
),
gleam@dynamic:field(
<<"usage"/utf8>>,
gleam@dynamic:decode2(
fun(Field@0, Field@1) -> {usage, Field@0, Field@1} end,
gleam@dynamic:field(
<<"prompt_tokens"/utf8>>,
fun gleam@dynamic:int/1
),
gleam@dynamic:field(
<<"total_tokens"/utf8>>,
fun gleam@dynamic:int/1
)
)
)
)
).
-spec embeddings(gopenai@client:client(), embeddings_request()) -> {ok,
embeddings_response()} |
{error, embeddings_error()}.
embeddings(Client, Req) ->
_assert_subject = gleam@http@request:to(
gleam@string:concat(
[erlang:element(2, Client),
<<"/v1/"/utf8>>,
begin
_pipe = embeddings,
gopenai@client:endpoint_to_str(_pipe)
end]
)
),
{ok, Request} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"gopenai/embeddings"/utf8>>,
function => <<"embeddings"/utf8>>,
line => 124})
end,
_pipe@1 = Request,
_pipe@2 = gleam@http@request:set_method(_pipe@1, post),
_pipe@3 = gleam@http@request:prepend_header(
_pipe@2,
<<"Content-Type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@4 = gleam@http@request:prepend_header(
_pipe@3,
<<"Authorization"/utf8>>,
<<"Bearer "/utf8, (erlang:element(3, Client))/binary>>
),
_pipe@6 = gleam@http@request:set_body(
_pipe@4,
begin
_pipe@5 = Req,
request_to_json(_pipe@5)
end
),
_pipe@7 = gleam@httpc:send(_pipe@6),
_pipe@8 = gleam@result:map_error(_pipe@7, fun(E) -> {dynamic_error, E} end),
_pipe@10 = gleam@result:map(
_pipe@8,
fun(Response) ->
_pipe@9 = embeddings_from_json(erlang:element(4, Response)),
gleam@result:map_error(
_pipe@9,
fun(E@1) -> {json_decode_error, E@1} end
)
end
),
gleam@result:flatten(_pipe@10).