Current section
Files
Jump to
Current section
Files
src/pig@openai.erl
-module(pig@openai).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/pig/openai.gleam").
-export([provider_with_base_url_and_timeout/4, provider_with_base_url/3, provider/2, with_http_timeout/2, build_request_body/3, parse_response/1]).
-export_type([open_a_i_config/0, open_a_i_provider/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 open_a_i_config() :: {open_a_i_config,
binary(),
binary(),
binary(),
integer()}.
-type open_a_i_provider() :: {open_a_i_provider,
open_a_i_config(),
fun((list(pig_protocol@message:message()), list(pig_protocol@tool_definition:tool_definition())) -> {ok,
pig_protocol@inference:inference_result()} |
{error, pig_protocol@error:ai_error()})}.
-file("src/pig/openai.gleam", 109).
-spec do_inference(
open_a_i_config(),
list(pig_protocol@message:message()),
list(pig_protocol@tool_definition:tool_definition())
) -> {ok, pig_protocol@inference:inference_result()} |
{error, pig_protocol@error:ai_error()}.
do_inference(Config, Messages, Tools) ->
Mode = {standard_api, erlang:element(2, Config), erlang:element(4, Config)},
Url = pig_protocol@auth:chat_url(Mode),
gleam@result:'try'(
pig_protocol@auth:headers(Mode, false),
fun(Headers) ->
Body = pig_protocol@codec@chat:build_request_body(
Messages,
Tools,
erlang:element(3, Config)
),
Req = {http_request, Url, Headers, Body, erlang:element(5, Config)},
gleam@result:'try'(
pig_protocol@transport@httpc:transport(Req),
fun(Raw) -> pig_protocol@codec@chat:parse_response(Raw) end
)
end
).
-file("src/pig/openai.gleam", 79).
?DOC(
" Wrap an `OpenAIConfig` in an `OpenAIProvider` whose `call` closure\n"
" invokes `do_inference` with that config.\n"
).
-spec build_provider(open_a_i_config()) -> open_a_i_provider().
build_provider(Config) ->
{open_a_i_provider,
Config,
fun(Messages, Tools) -> do_inference(Config, Messages, Tools) end}.
-file("src/pig/openai.gleam", 52).
?DOC(" Create a provider with a custom base URL and HTTP timeout.\n").
-spec provider_with_base_url_and_timeout(
binary(),
binary(),
binary(),
integer()
) -> open_a_i_provider().
provider_with_base_url_and_timeout(Api_key, Model, Base_url, Http_timeout_ms) ->
build_provider({open_a_i_config, Api_key, Model, Base_url, Http_timeout_ms}).
-file("src/pig/openai.gleam", 38).
?DOC(" Create a provider with a custom base URL (for Ollama, Together, etc).\n").
-spec provider_with_base_url(binary(), binary(), binary()) -> open_a_i_provider().
provider_with_base_url(Api_key, Model, Base_url) ->
provider_with_base_url_and_timeout(Api_key, Model, Base_url, 120000).
-file("src/pig/openai.gleam", 33).
?DOC(" Create a provider with the default OpenAI base URL.\n").
-spec provider(binary(), binary()) -> open_a_i_provider().
provider(Api_key, Model) ->
provider_with_base_url(Api_key, Model, <<"https://api.openai.com/v1"/utf8>>).
-file("src/pig/openai.gleam", 67).
?DOC(" Set the HTTP timeout in milliseconds on an OpenAI provider.\n").
-spec with_http_timeout(open_a_i_provider(), integer()) -> open_a_i_provider().
with_http_timeout(Provider, Timeout_ms) ->
build_provider(
begin
_record = erlang:element(2, Provider),
{open_a_i_config,
erlang:element(2, _record),
erlang:element(3, _record),
erlang:element(4, _record),
Timeout_ms}
end
).
-file("src/pig/openai.gleam", 93).
?DOC(
" Build the JSON request body for the OpenAI Chat Completions API.\n"
" Pure function — no IO.\n"
).
-spec build_request_body(
list(pig_protocol@message:message()),
list(pig_protocol@tool_definition:tool_definition()),
binary()
) -> binary().
build_request_body(Messages, Tools, Model) ->
pig_protocol@codec@chat:build_request_body(Messages, Tools, Model).
-file("src/pig/openai.gleam", 103).
?DOC(
" Parse an OpenAI Chat Completions JSON response into an InferenceResult.\n"
" Pure function — no IO.\n"
).
-spec parse_response(binary()) -> {ok,
pig_protocol@inference:inference_result()} |
{error, pig_protocol@error:ai_error()}.
parse_response(Raw) ->
pig_protocol@codec@chat:parse_response(Raw).