Current section
Files
Jump to
Current section
Files
src/glopenai@internal.erl
-module(glopenai@internal).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glopenai/internal.gleam").
-export([build_multipart_body/2, parse_response/2, post_request/3, get_request/2, delete_request/2, multipart_request/5, azure_post_request/3, azure_get_request/2, azure_delete_request/2]).
-export_type([multipart_part/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.
?MODULEDOC(false).
-type multipart_part() :: {field_part, binary(), binary()} |
{file_part, binary(), binary(), binary(), bitstring()}.
-file("src/glopenai/internal.gleam", 116).
?DOC(false).
-spec part_header(multipart_part()) -> binary().
part_header(Part) ->
case Part of
{field_part, Name, _} ->
<<<<"Content-Disposition: form-data; name=\""/utf8, Name/binary>>/binary,
"\"\r\n\r\n"/utf8>>;
{file_part, Name@1, Filename, Content_type, _} ->
<<<<<<<<<<<<"Content-Disposition: form-data; name=\""/utf8,
Name@1/binary>>/binary,
"\"; filename=\""/utf8>>/binary,
Filename/binary>>/binary,
"\"\r\nContent-Type: "/utf8>>/binary,
Content_type/binary>>/binary,
"\r\n\r\n"/utf8>>
end.
-file("src/glopenai/internal.gleam", 131).
?DOC(false).
-spec part_body(multipart_part()) -> bitstring().
part_body(Part) ->
case Part of
{field_part, _, Value} ->
gleam_stdlib:identity(Value);
{file_part, _, _, _, Data} ->
Data
end.
-file("src/glopenai/internal.gleam", 100).
?DOC(false).
-spec build_multipart_body(list(multipart_part()), binary()) -> bitstring().
build_multipart_body(Parts, Boundary) ->
Dash_boundary = gleam_stdlib:identity(
<<<<"--"/utf8, Boundary/binary>>/binary, "\r\n"/utf8>>
),
Crlf = gleam_stdlib:identity(<<"\r\n"/utf8>>),
Parts_bytes = gleam@list:fold(
Parts,
<<>>,
fun(Acc, Part) ->
Header = gleam_stdlib:identity(part_header(Part)),
Payload = part_body(Part),
gleam_stdlib:bit_array_concat(
[Acc, Dash_boundary, Header, Payload, Crlf]
)
end
),
Closing = gleam_stdlib:identity(
<<<<"--"/utf8, Boundary/binary>>/binary, "--\r\n"/utf8>>
),
gleam_stdlib:bit_array_concat([Parts_bytes, Closing]).
-file("src/glopenai/internal.gleam", 140).
?DOC(false).
-spec parse_response(
gleam@http@response:response(binary()),
gleam@dynamic@decode:decoder(ESW)
) -> {ok, ESW} | {error, glopenai@error:glopenai_error()}.
parse_response(Response, Decoder) ->
case (erlang:element(2, Response) >= 200) andalso (erlang:element(
2,
Response
)
< 300) of
true ->
case gleam@json:parse(erlang:element(4, Response), Decoder) of
{ok, Value} ->
{ok, Value};
{error, Decode_error} ->
{error,
{json_decode_error,
erlang:element(4, Response),
Decode_error}}
end;
false ->
case gleam@json:parse(
erlang:element(4, Response),
glopenai@error:wrapped_error_decoder()
) of
{ok, Api_error} ->
{error,
{api_response_error,
erlang:element(2, Response),
Api_error}};
{error, _} ->
{error,
{unexpected_response,
erlang:element(2, Response),
erlang:element(4, Response)}}
end
end.
-file("src/glopenai/internal.gleam", 225).
?DOC(false).
-spec apply_custom_headers(
gleam@http@request:request(binary()),
list({binary(), binary()})
) -> gleam@http@request:request(binary()).
apply_custom_headers(Req, Headers) ->
case Headers of
[] ->
Req;
[{Key, Value} | Rest] ->
apply_custom_headers(
begin
_pipe = Req,
gleam@http@request:prepend_header(_pipe, Key, Value)
end,
Rest
)
end.
-file("src/glopenai/internal.gleam", 209).
?DOC(false).
-spec apply_config(
gleam@http@request:request(binary()),
glopenai@config:config()
) -> gleam@http@request:request(binary()).
apply_config(Req, Config) ->
Req@1 = begin
_pipe = Req,
gleam@http@request:prepend_header(
_pipe,
<<"authorization"/utf8>>,
<<"Bearer "/utf8, (erlang:element(3, Config))/binary>>
)
end,
Req@2 = case erlang:element(4, Config) of
{some, Org_id} ->
_pipe@1 = Req@1,
gleam@http@request:prepend_header(
_pipe@1,
<<"openai-organization"/utf8>>,
Org_id
);
none ->
Req@1
end,
Req@3 = case erlang:element(5, Config) of
{some, Project_id} ->
_pipe@2 = Req@2,
gleam@http@request:prepend_header(
_pipe@2,
<<"openai-project"/utf8>>,
Project_id
);
none ->
Req@2
end,
apply_custom_headers(Req@3, erlang:element(6, Config)).
-file("src/glopenai/internal.gleam", 236).
?DOC(false).
-spec set_scheme(
gleam@http@request:request(binary()),
gleam@option:option(binary())
) -> gleam@http@request:request(binary()).
set_scheme(Req, Scheme) ->
case Scheme of
{some, <<"https"/utf8>>} ->
gleam@http@request:set_scheme(Req, https);
{some, <<"http"/utf8>>} ->
gleam@http@request:set_scheme(Req, http);
_ ->
gleam@http@request:set_scheme(Req, https)
end.
-file("src/glopenai/internal.gleam", 244).
?DOC(false).
-spec set_port(gleam@http@request:request(binary()), gleam@uri:uri()) -> gleam@http@request:request(binary()).
set_port(Req, Parsed) ->
case erlang:element(5, Parsed) of
{some, Port} ->
gleam@http@request:set_port(Req, Port);
none ->
Req
end.
-file("src/glopenai/internal.gleam", 251).
?DOC(false).
-spec option_unwrap(gleam@option:option(ETM), ETM) -> ETM.
option_unwrap(Opt, Default) ->
case Opt of
{some, V} ->
V;
none ->
Default
end.
-file("src/glopenai/internal.gleam", 161).
?DOC(false).
-spec build_request(glopenai@config:config(), gleam@http:method(), binary()) -> gleam@http@request:request(binary()).
build_request(Config, Method, Path) ->
Full_url = <<(erlang:element(2, Config))/binary, Path/binary>>,
Req = case gleam_stdlib:uri_parse(Full_url) of
{ok, Parsed} ->
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, Method),
_pipe@3 = gleam@http@request:set_host(
_pipe@1,
begin
_pipe@2 = erlang:element(4, Parsed),
option_unwrap(_pipe@2, <<"api.openai.com"/utf8>>)
end
),
_pipe@4 = gleam@http@request:set_path(
_pipe@3,
erlang:element(6, Parsed)
),
_pipe@5 = set_scheme(_pipe@4, erlang:element(2, Parsed)),
set_port(_pipe@5, Parsed);
{error, nil} ->
_pipe@6 = gleam@http@request:new(),
_pipe@7 = gleam@http@request:set_method(_pipe@6, Method),
_pipe@8 = gleam@http@request:set_host(
_pipe@7,
<<"api.openai.com"/utf8>>
),
gleam@http@request:set_path(_pipe@8, <<"/v1"/utf8, Path/binary>>)
end,
apply_config(Req, Config).
-file("src/glopenai/internal.gleam", 16).
?DOC(false).
-spec post_request(glopenai@config:config(), binary(), gleam@json:json()) -> gleam@http@request:request(binary()).
post_request(Config, Path, Body) ->
_pipe = build_request(Config, post, Path),
_pipe@1 = gleam@http@request:set_body(_pipe, gleam@json:to_string(Body)),
gleam@http@request:prepend_header(
_pipe@1,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
).
-file("src/glopenai/internal.gleam", 27).
?DOC(false).
-spec get_request(glopenai@config:config(), binary()) -> gleam@http@request:request(binary()).
get_request(Config, Path) ->
_pipe = build_request(Config, get, Path),
gleam@http@request:set_body(_pipe, <<""/utf8>>).
-file("src/glopenai/internal.gleam", 33).
?DOC(false).
-spec delete_request(glopenai@config:config(), binary()) -> gleam@http@request:request(binary()).
delete_request(Config, Path) ->
_pipe = build_request(Config, delete, Path),
gleam@http@request:set_body(_pipe, <<""/utf8>>).
-file("src/glopenai/internal.gleam", 81).
?DOC(false).
-spec multipart_request(
glopenai@config:config(),
gleam@http:method(),
binary(),
list(multipart_part()),
binary()
) -> gleam@http@request:request(bitstring()).
multipart_request(Config, Method, Path, Parts, Boundary) ->
Body = build_multipart_body(Parts, Boundary),
_pipe = build_request(Config, Method, Path),
_pipe@1 = gleam@http@request:set_body(_pipe, Body),
gleam@http@request:prepend_header(
_pipe@1,
<<"content-type"/utf8>>,
<<"multipart/form-data; boundary="/utf8, Boundary/binary>>
).
-file("src/glopenai/internal.gleam", 184).
?DOC(false).
-spec build_azure_request(
glopenai@config:azure_config(),
gleam@http:method(),
binary()
) -> gleam@http@request:request(binary()).
build_azure_request(Config, Method, Path) ->
Full_url = <<<<<<(erlang:element(2, Config))/binary,
"/openai/deployments/"/utf8>>/binary,
(erlang:element(4, Config))/binary>>/binary,
Path/binary>>,
Req = case gleam_stdlib:uri_parse(Full_url) of
{ok, Parsed} ->
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, Method),
_pipe@3 = gleam@http@request:set_host(
_pipe@1,
begin
_pipe@2 = erlang:element(4, Parsed),
option_unwrap(_pipe@2, <<""/utf8>>)
end
),
_pipe@4 = gleam@http@request:set_path(
_pipe@3,
erlang:element(6, Parsed)
),
_pipe@5 = set_scheme(_pipe@4, erlang:element(2, Parsed)),
set_port(_pipe@5, Parsed);
{error, nil} ->
_pipe@6 = gleam@http@request:new(),
_pipe@7 = gleam@http@request:set_method(_pipe@6, Method),
gleam@http@request:set_path(_pipe@7, Path)
end,
_pipe@8 = Req,
_pipe@9 = gleam@http@request:set_query(
_pipe@8,
[{<<"api-version"/utf8>>, erlang:element(5, Config)}]
),
gleam@http@request:prepend_header(
_pipe@9,
<<"api-key"/utf8>>,
erlang:element(3, Config)
).
-file("src/glopenai/internal.gleam", 39).
?DOC(false).
-spec azure_post_request(
glopenai@config:azure_config(),
binary(),
gleam@json:json()
) -> gleam@http@request:request(binary()).
azure_post_request(Config, Path, Body) ->
_pipe = build_azure_request(Config, post, Path),
_pipe@1 = gleam@http@request:set_body(_pipe, gleam@json:to_string(Body)),
gleam@http@request:prepend_header(
_pipe@1,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
).
-file("src/glopenai/internal.gleam", 50).
?DOC(false).
-spec azure_get_request(glopenai@config:azure_config(), binary()) -> gleam@http@request:request(binary()).
azure_get_request(Config, Path) ->
_pipe = build_azure_request(Config, get, Path),
gleam@http@request:set_body(_pipe, <<""/utf8>>).
-file("src/glopenai/internal.gleam", 56).
?DOC(false).
-spec azure_delete_request(glopenai@config:azure_config(), binary()) -> gleam@http@request:request(binary()).
azure_delete_request(Config, Path) ->
_pipe = build_azure_request(Config, delete, Path),
gleam@http@request:set_body(_pipe, <<""/utf8>>).