Current section

Files

Jump to
gopenai src gopenai@client.erl
Raw

src/gopenai@client.erl

-module(gopenai@client).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/0, generic/2, ollama/0, endpoint_to_str/1]).
-export_type([client/0, endpoint/0]).
-type client() :: {client, binary(), binary()}.
-type endpoint() :: completions | embeddings.
-spec new() -> client().
new() ->
Endpoint = <<"https://api.openai.com"/utf8>>,
dot_env:load(),
_assert_subject = dot_env_ffi:get_env(<<"OPENAI_API_KEY"/utf8>>),
{ok, Api_key} = 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/client"/utf8>>,
function => <<"new"/utf8>>,
line => 11})
end,
{client, Endpoint, Api_key}.
-spec generic(binary(), binary()) -> client().
generic(Endpoint, Api_key_env) ->
dot_env:load(),
_assert_subject = dot_env_ffi:get_env(Api_key_env),
{ok, Api_key} = 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/client"/utf8>>,
function => <<"generic"/utf8>>,
line => 17})
end,
{client, Endpoint, Api_key}.
-spec ollama() -> client().
ollama() ->
{client, <<"http://localhost:11434"/utf8>>, <<"ollama"/utf8>>}.
-spec endpoint_to_str(endpoint()) -> binary().
endpoint_to_str(Endpoint) ->
case Endpoint of
completions ->
<<"chat/completions"/utf8>>;
embeddings ->
<<"embeddings"/utf8>>
end.