Current section
Files
Jump to
Current section
Files
src/glopenai@chat.erl
-module(glopenai@chat).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glopenai/chat.gleam").
-export([user_message/1, system_message/1, developer_message/1, assistant_message/1, tool_message/2, new_create_request/2, with_temperature/2, with_top_p/2, with_n/2, with_stream/2, with_max_completion_tokens/2, with_tools/2, with_tool_choice/2, with_response_format/2, with_reasoning_effort/2, with_stop/2, with_store/2, with_service_tier/2, with_web_search_options/2, with_metadata/2, role_to_json/1, finish_reason_to_json/1, service_tier_to_json/1, input_audio_format_to_json/1, verbosity_to_json/1, web_search_context_size_to_json/1, response_modality_to_json/1, tool_choice_mode_to_json/1, input_audio_to_json/1, file_object_to_json/1, user_content_part_to_json/1, assistant_content_part_to_json/1, user_message_content_to_json/1, developer_message_content_to_json/1, system_message_content_to_json/1, assistant_message_content_to_json/1, tool_message_content_to_json/1, tool_call_to_json/1, chat_completion_tool_to_json/1, tool_choice_to_json/1, stop_configuration_to_json/1, web_search_options_to_json/1, stream_options_to_json/1, chat_message_to_json/1, create_chat_completion_request_to_json/1, role_decoder/0, finish_reason_decoder/0, service_tier_decoder/0, tool_call_decoder/0, url_citation_decoder/0, response_message_annotation_decoder/0, chat_completion_response_message_decoder/0, chat_choice_decoder/0, create_chat_completion_response_decoder/0, function_call_stream_decoder/0, tool_call_chunk_decoder/0, chat_completion_stream_delta_decoder/0, chat_choice_stream_decoder/0, create_chat_completion_stream_response_decoder/0, parse_stream_chunk/1, create_request/2, create_response/1, list_request/1, list_response/1, retrieve_request/2, retrieve_response/1, delete_request/2, delete_response/1]).
-export_type([role/0, finish_reason/0, service_tier/0, input_audio_format/0, verbosity/0, web_search_context_size/0, response_modality/0, tool_choice_mode/0, input_audio/0, file_object/0, user_content_part/0, assistant_content_part/0, developer_message_content/0, system_message_content/0, user_message_content/0, assistant_message_content/0, tool_message_content/0, chat_completion_tool/0, tool_call/0, tool_choice/0, chat_message/0, web_search_location/0, web_search_user_location/0, web_search_options/0, chat_completion_stream_options/0, stop_configuration/0, create_chat_completion_request/0, url_citation/0, response_message_annotation/0, chat_completion_response_message/0, chat_choice/0, create_chat_completion_response/0, chat_completion_list/0, chat_completion_deleted/0, function_call_stream/0, tool_call_chunk/0, chat_completion_stream_delta/0, chat_choice_stream/0, create_chat_completion_stream_response/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 role() :: role_system |
role_user |
role_assistant |
role_tool |
role_developer.
-type finish_reason() :: stop |
length |
tool_calls |
content_filter |
function_call_finish.
-type service_tier() :: service_tier_auto |
service_tier_default |
service_tier_flex |
service_tier_scale |
service_tier_priority.
-type input_audio_format() :: wav | mp3.
-type verbosity() :: verbosity_low | verbosity_medium | verbosity_high.
-type web_search_context_size() :: web_search_low |
web_search_medium |
web_search_high.
-type response_modality() :: modality_text | modality_audio.
-type tool_choice_mode() :: tool_choice_none |
tool_choice_auto |
tool_choice_required.
-type input_audio() :: {input_audio, binary(), input_audio_format()}.
-type file_object() :: {file_object,
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type user_content_part() :: {user_text_part, binary()} |
{user_image_url_part, glopenai@shared:image_url()} |
{user_input_audio_part, input_audio()} |
{user_file_part, file_object()}.
-type assistant_content_part() :: {assistant_text_part, binary()} |
{assistant_refusal_part, binary()}.
-type developer_message_content() :: {developer_text_content, binary()} |
{developer_parts_content, list(binary())}.
-type system_message_content() :: {system_text_content, binary()} |
{system_parts_content, list(binary())}.
-type user_message_content() :: {user_text_content, binary()} |
{user_parts_content, list(user_content_part())}.
-type assistant_message_content() :: {assistant_text_content, binary()} |
{assistant_parts_content, list(assistant_content_part())}.
-type tool_message_content() :: {tool_text_content, binary()} |
{tool_parts_content, list(binary())}.
-type chat_completion_tool() :: {function_tool,
glopenai@shared:function_object()}.
-type tool_call() :: {function_tool_call,
binary(),
glopenai@shared:function_call()}.
-type tool_choice() :: {tool_choice_mode_choice, tool_choice_mode()} |
{tool_choice_function_choice, glopenai@shared:function_name()}.
-type chat_message() :: {developer_message,
developer_message_content(),
gleam@option:option(binary())} |
{system_message, system_message_content(), gleam@option:option(binary())} |
{user_message, user_message_content(), gleam@option:option(binary())} |
{assistant_message,
gleam@option:option(assistant_message_content()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(list(tool_call()))} |
{tool_message, tool_message_content(), binary()}.
-type web_search_location() :: {web_search_location,
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type web_search_user_location() :: {web_search_user_location,
web_search_location()}.
-type web_search_options() :: {web_search_options,
gleam@option:option(web_search_context_size()),
gleam@option:option(web_search_user_location())}.
-type chat_completion_stream_options() :: {chat_completion_stream_options,
gleam@option:option(boolean()),
gleam@option:option(boolean())}.
-type stop_configuration() :: {stop_string, binary()} |
{stop_string_array, list(binary())}.
-type create_chat_completion_request() :: {create_chat_completion_request,
binary(),
list(chat_message()),
gleam@option:option(float()),
gleam@option:option(float()),
gleam@option:option(integer()),
gleam@option:option(boolean()),
gleam@option:option(chat_completion_stream_options()),
gleam@option:option(stop_configuration()),
gleam@option:option(integer()),
gleam@option:option(float()),
gleam@option:option(float()),
gleam@option:option(boolean()),
gleam@option:option(integer()),
gleam@option:option(glopenai@shared:response_format()),
gleam@option:option(list(chat_completion_tool())),
gleam@option:option(tool_choice()),
gleam@option:option(boolean()),
gleam@option:option(glopenai@shared:reasoning_effort()),
gleam@option:option(list(response_modality())),
gleam@option:option(verbosity()),
gleam@option:option(web_search_options()),
gleam@option:option(boolean()),
gleam@option:option(gleam@dict:dict(binary(), binary())),
gleam@option:option(service_tier()),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type url_citation() :: {url_citation, integer(), integer(), binary(), binary()}.
-type response_message_annotation() :: {url_citation_annotation, url_citation()}.
-type chat_completion_response_message() :: {chat_completion_response_message,
role(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(list(tool_call())),
gleam@option:option(list(response_message_annotation()))}.
-type chat_choice() :: {chat_choice,
integer(),
chat_completion_response_message(),
gleam@option:option(finish_reason())}.
-type create_chat_completion_response() :: {create_chat_completion_response,
binary(),
binary(),
integer(),
binary(),
list(chat_choice()),
gleam@option:option(glopenai@shared:completion_usage()),
gleam@option:option(service_tier()),
gleam@option:option(binary())}.
-type chat_completion_list() :: {chat_completion_list,
binary(),
list(create_chat_completion_response()),
gleam@option:option(binary()),
gleam@option:option(binary()),
boolean()}.
-type chat_completion_deleted() :: {chat_completion_deleted,
binary(),
binary(),
boolean()}.
-type function_call_stream() :: {function_call_stream,
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type tool_call_chunk() :: {tool_call_chunk,
integer(),
gleam@option:option(binary()),
gleam@option:option(function_call_stream())}.
-type chat_completion_stream_delta() :: {chat_completion_stream_delta,
gleam@option:option(role()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(list(tool_call_chunk()))}.
-type chat_choice_stream() :: {chat_choice_stream,
integer(),
chat_completion_stream_delta(),
gleam@option:option(finish_reason())}.
-type create_chat_completion_stream_response() :: {create_chat_completion_stream_response,
binary(),
binary(),
integer(),
binary(),
list(chat_choice_stream()),
gleam@option:option(glopenai@shared:completion_usage()),
gleam@option:option(service_tier()),
gleam@option:option(binary())}.
-file("src/glopenai/chat.gleam", 174).
?DOC(" Create a simple user text message.\n").
-spec user_message(binary()) -> chat_message().
user_message(Text) ->
{user_message, {user_text_content, Text}, none}.
-file("src/glopenai/chat.gleam", 179).
?DOC(" Create a simple system text message.\n").
-spec system_message(binary()) -> chat_message().
system_message(Text) ->
{system_message, {system_text_content, Text}, none}.
-file("src/glopenai/chat.gleam", 184).
?DOC(" Create a simple developer text message.\n").
-spec developer_message(binary()) -> chat_message().
developer_message(Text) ->
{developer_message, {developer_text_content, Text}, none}.
-file("src/glopenai/chat.gleam", 189).
?DOC(" Create a simple assistant text message.\n").
-spec assistant_message(binary()) -> chat_message().
assistant_message(Text) ->
{assistant_message,
{some, {assistant_text_content, Text}},
none,
none,
none}.
-file("src/glopenai/chat.gleam", 199).
?DOC(" Create a tool result message.\n").
-spec tool_message(binary(), binary()) -> chat_message().
tool_message(Content, Tool_call_id) ->
{tool_message, {tool_text_content, Content}, Tool_call_id}.
-file("src/glopenai/chat.gleam", 287).
?DOC(" Create a new chat completion request with required fields.\n").
-spec new_create_request(binary(), list(chat_message())) -> create_chat_completion_request().
new_create_request(Model, Messages) ->
{create_chat_completion_request,
Model,
Messages,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none,
none}.
-file("src/glopenai/chat.gleam", 321).
-spec with_temperature(create_chat_completion_request(), float()) -> create_chat_completion_request().
with_temperature(Request, Temperature) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
{some, Temperature},
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 328).
-spec with_top_p(create_chat_completion_request(), float()) -> create_chat_completion_request().
with_top_p(Request, Top_p) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
{some, Top_p},
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 335).
-spec with_n(create_chat_completion_request(), integer()) -> create_chat_completion_request().
with_n(Request, N) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
{some, N},
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 342).
-spec with_stream(create_chat_completion_request(), boolean()) -> create_chat_completion_request().
with_stream(Request, Stream) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
{some, Stream},
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 349).
-spec with_max_completion_tokens(create_chat_completion_request(), integer()) -> create_chat_completion_request().
with_max_completion_tokens(Request, Max_tokens) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
{some, Max_tokens},
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 359).
-spec with_tools(create_chat_completion_request(), list(chat_completion_tool())) -> create_chat_completion_request().
with_tools(Request, Tools) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
{some, Tools},
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 366).
-spec with_tool_choice(create_chat_completion_request(), tool_choice()) -> create_chat_completion_request().
with_tool_choice(Request, Choice) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
{some, Choice},
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 373).
-spec with_response_format(
create_chat_completion_request(),
glopenai@shared:response_format()
) -> create_chat_completion_request().
with_response_format(Request, Format) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
{some, Format},
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 380).
-spec with_reasoning_effort(
create_chat_completion_request(),
glopenai@shared:reasoning_effort()
) -> create_chat_completion_request().
with_reasoning_effort(Request, Effort) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
{some, Effort},
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 387).
-spec with_stop(create_chat_completion_request(), stop_configuration()) -> create_chat_completion_request().
with_stop(Request, Stop) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
{some, Stop},
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 394).
-spec with_store(create_chat_completion_request(), boolean()) -> create_chat_completion_request().
with_store(Request, Store) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
{some, Store},
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 401).
-spec with_service_tier(create_chat_completion_request(), service_tier()) -> create_chat_completion_request().
with_service_tier(Request, Tier) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
erlang:element(24, Request),
{some, Tier},
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 408).
-spec with_web_search_options(
create_chat_completion_request(),
web_search_options()
) -> create_chat_completion_request().
with_web_search_options(Request, Options) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
{some, Options},
erlang:element(23, Request),
erlang:element(24, Request),
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 415).
-spec with_metadata(
create_chat_completion_request(),
gleam@dict:dict(binary(), binary())
) -> create_chat_completion_request().
with_metadata(Request, Metadata) ->
{create_chat_completion_request,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request),
erlang:element(6, Request),
erlang:element(7, Request),
erlang:element(8, Request),
erlang:element(9, Request),
erlang:element(10, Request),
erlang:element(11, Request),
erlang:element(12, Request),
erlang:element(13, Request),
erlang:element(14, Request),
erlang:element(15, Request),
erlang:element(16, Request),
erlang:element(17, Request),
erlang:element(18, Request),
erlang:element(19, Request),
erlang:element(20, Request),
erlang:element(21, Request),
erlang:element(22, Request),
erlang:element(23, Request),
{some, Metadata},
erlang:element(25, Request),
erlang:element(26, Request),
erlang:element(27, Request)}.
-file("src/glopenai/chat.gleam", 529).
-spec role_to_json(role()) -> gleam@json:json().
role_to_json(Role) ->
gleam@json:string(case Role of
role_system ->
<<"system"/utf8>>;
role_user ->
<<"user"/utf8>>;
role_assistant ->
<<"assistant"/utf8>>;
role_tool ->
<<"tool"/utf8>>;
role_developer ->
<<"developer"/utf8>>
end).
-file("src/glopenai/chat.gleam", 539).
-spec finish_reason_to_json(finish_reason()) -> gleam@json:json().
finish_reason_to_json(Reason) ->
gleam@json:string(case Reason of
stop ->
<<"stop"/utf8>>;
length ->
<<"length"/utf8>>;
tool_calls ->
<<"tool_calls"/utf8>>;
content_filter ->
<<"content_filter"/utf8>>;
function_call_finish ->
<<"function_call"/utf8>>
end).
-file("src/glopenai/chat.gleam", 549).
-spec service_tier_to_json(service_tier()) -> gleam@json:json().
service_tier_to_json(Tier) ->
gleam@json:string(case Tier of
service_tier_auto ->
<<"auto"/utf8>>;
service_tier_default ->
<<"default"/utf8>>;
service_tier_flex ->
<<"flex"/utf8>>;
service_tier_scale ->
<<"scale"/utf8>>;
service_tier_priority ->
<<"priority"/utf8>>
end).
-file("src/glopenai/chat.gleam", 559).
-spec input_audio_format_to_json(input_audio_format()) -> gleam@json:json().
input_audio_format_to_json(Format) ->
gleam@json:string(case Format of
wav ->
<<"wav"/utf8>>;
mp3 ->
<<"mp3"/utf8>>
end).
-file("src/glopenai/chat.gleam", 566).
-spec verbosity_to_json(verbosity()) -> gleam@json:json().
verbosity_to_json(V) ->
gleam@json:string(case V of
verbosity_low ->
<<"low"/utf8>>;
verbosity_medium ->
<<"medium"/utf8>>;
verbosity_high ->
<<"high"/utf8>>
end).
-file("src/glopenai/chat.gleam", 574).
-spec web_search_context_size_to_json(web_search_context_size()) -> gleam@json:json().
web_search_context_size_to_json(Size) ->
gleam@json:string(case Size of
web_search_low ->
<<"low"/utf8>>;
web_search_medium ->
<<"medium"/utf8>>;
web_search_high ->
<<"high"/utf8>>
end).
-file("src/glopenai/chat.gleam", 582).
-spec response_modality_to_json(response_modality()) -> gleam@json:json().
response_modality_to_json(M) ->
gleam@json:string(case M of
modality_text ->
<<"text"/utf8>>;
modality_audio ->
<<"audio"/utf8>>
end).
-file("src/glopenai/chat.gleam", 589).
-spec tool_choice_mode_to_json(tool_choice_mode()) -> gleam@json:json().
tool_choice_mode_to_json(Mode) ->
gleam@json:string(case Mode of
tool_choice_none ->
<<"none"/utf8>>;
tool_choice_auto ->
<<"auto"/utf8>>;
tool_choice_required ->
<<"required"/utf8>>
end).
-file("src/glopenai/chat.gleam", 597).
-spec input_audio_to_json(input_audio()) -> gleam@json:json().
input_audio_to_json(Audio) ->
gleam@json:object(
[{<<"data"/utf8>>, gleam@json:string(erlang:element(2, Audio))},
{<<"format"/utf8>>,
input_audio_format_to_json(erlang:element(3, Audio))}]
).
-file("src/glopenai/chat.gleam", 604).
-spec file_object_to_json(file_object()) -> gleam@json:json().
file_object_to_json(File) ->
glopenai@internal@codec:object_with_optional(
[],
[glopenai@internal@codec:optional_field(
<<"file_data"/utf8>>,
erlang:element(2, File),
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"file_id"/utf8>>,
erlang:element(3, File),
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"filename"/utf8>>,
erlang:element(4, File),
fun gleam@json:string/1
)]
).
-file("src/glopenai/chat.gleam", 612).
-spec user_content_part_to_json(user_content_part()) -> gleam@json:json().
user_content_part_to_json(Part) ->
case Part of
{user_text_part, Text} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"text"/utf8>>)},
{<<"text"/utf8>>, gleam@json:string(Text)}]
);
{user_image_url_part, Image_url} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"image_url"/utf8>>)},
{<<"image_url"/utf8>>,
glopenai@shared:image_url_to_json(Image_url)}]
);
{user_input_audio_part, Input_audio} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"input_audio"/utf8>>)},
{<<"input_audio"/utf8>>, input_audio_to_json(Input_audio)}]
);
{user_file_part, File} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"file"/utf8>>)},
{<<"file"/utf8>>, file_object_to_json(File)}]
)
end.
-file("src/glopenai/chat.gleam", 637).
-spec assistant_content_part_to_json(assistant_content_part()) -> gleam@json:json().
assistant_content_part_to_json(Part) ->
case Part of
{assistant_text_part, Text} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"text"/utf8>>)},
{<<"text"/utf8>>, gleam@json:string(Text)}]
);
{assistant_refusal_part, Refusal} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"refusal"/utf8>>)},
{<<"refusal"/utf8>>, gleam@json:string(Refusal)}]
)
end.
-file("src/glopenai/chat.gleam", 652).
-spec text_part_to_json(binary()) -> gleam@json:json().
text_part_to_json(Text) ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"text"/utf8>>)},
{<<"text"/utf8>>, gleam@json:string(Text)}]
).
-file("src/glopenai/chat.gleam", 659).
-spec user_message_content_to_json(user_message_content()) -> gleam@json:json().
user_message_content_to_json(Content) ->
case Content of
{user_text_content, Text} ->
gleam@json:string(Text);
{user_parts_content, Parts} ->
gleam@json:array(Parts, fun user_content_part_to_json/1)
end.
-file("src/glopenai/chat.gleam", 666).
-spec developer_message_content_to_json(developer_message_content()) -> gleam@json:json().
developer_message_content_to_json(Content) ->
case Content of
{developer_text_content, Text} ->
gleam@json:string(Text);
{developer_parts_content, Parts} ->
gleam@json:array(Parts, fun text_part_to_json/1)
end.
-file("src/glopenai/chat.gleam", 675).
-spec system_message_content_to_json(system_message_content()) -> gleam@json:json().
system_message_content_to_json(Content) ->
case Content of
{system_text_content, Text} ->
gleam@json:string(Text);
{system_parts_content, Parts} ->
gleam@json:array(Parts, fun text_part_to_json/1)
end.
-file("src/glopenai/chat.gleam", 684).
-spec assistant_message_content_to_json(assistant_message_content()) -> gleam@json:json().
assistant_message_content_to_json(Content) ->
case Content of
{assistant_text_content, Text} ->
gleam@json:string(Text);
{assistant_parts_content, Parts} ->
gleam@json:array(Parts, fun assistant_content_part_to_json/1)
end.
-file("src/glopenai/chat.gleam", 694).
-spec tool_message_content_to_json(tool_message_content()) -> gleam@json:json().
tool_message_content_to_json(Content) ->
case Content of
{tool_text_content, Text} ->
gleam@json:string(Text);
{tool_parts_content, Parts} ->
gleam@json:array(Parts, fun text_part_to_json/1)
end.
-file("src/glopenai/chat.gleam", 701).
-spec tool_call_to_json(tool_call()) -> gleam@json:json().
tool_call_to_json(Call) ->
case Call of
{function_tool_call, Id, Function} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"function"/utf8>>)},
{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"function"/utf8>>,
glopenai@shared:function_call_to_json(Function)}]
)
end.
-file("src/glopenai/chat.gleam", 712).
-spec chat_completion_tool_to_json(chat_completion_tool()) -> gleam@json:json().
chat_completion_tool_to_json(Tool) ->
case Tool of
{function_tool, Function} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"function"/utf8>>)},
{<<"function"/utf8>>,
glopenai@shared:function_object_to_json(Function)}]
)
end.
-file("src/glopenai/chat.gleam", 722).
-spec tool_choice_to_json(tool_choice()) -> gleam@json:json().
tool_choice_to_json(Choice) ->
case Choice of
{tool_choice_mode_choice, Mode} ->
tool_choice_mode_to_json(Mode);
{tool_choice_function_choice, Function} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"function"/utf8>>)},
{<<"function"/utf8>>,
glopenai@shared:function_name_to_json(Function)}]
)
end.
-file("src/glopenai/chat.gleam", 733).
-spec stop_configuration_to_json(stop_configuration()) -> gleam@json:json().
stop_configuration_to_json(Stop) ->
case Stop of
{stop_string, S} ->
gleam@json:string(S);
{stop_string_array, Arr} ->
gleam@json:array(Arr, fun gleam@json:string/1)
end.
-file("src/glopenai/chat.gleam", 740).
-spec web_search_options_to_json(web_search_options()) -> gleam@json:json().
web_search_options_to_json(Opts) ->
glopenai@internal@codec:object_with_optional(
[],
[glopenai@internal@codec:optional_field(
<<"search_context_size"/utf8>>,
erlang:element(2, Opts),
fun web_search_context_size_to_json/1
),
glopenai@internal@codec:optional_field(
<<"user_location"/utf8>>,
erlang:element(3, Opts),
fun(Loc) ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"approximate"/utf8>>)},
{<<"approximate"/utf8>>,
glopenai@internal@codec:object_with_optional(
[],
[glopenai@internal@codec:optional_field(
<<"country"/utf8>>,
erlang:element(
2,
erlang:element(2, Loc)
),
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"region"/utf8>>,
erlang:element(
3,
erlang:element(2, Loc)
),
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"city"/utf8>>,
erlang:element(
4,
erlang:element(2, Loc)
),
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"timezone"/utf8>>,
erlang:element(
5,
erlang:element(2, Loc)
),
fun gleam@json:string/1
)]
)}]
)
end
)]
).
-file("src/glopenai/chat.gleam", 772).
-spec stream_options_to_json(chat_completion_stream_options()) -> gleam@json:json().
stream_options_to_json(Opts) ->
glopenai@internal@codec:object_with_optional(
[],
[glopenai@internal@codec:optional_field(
<<"include_usage"/utf8>>,
erlang:element(2, Opts),
fun gleam@json:bool/1
),
glopenai@internal@codec:optional_field(
<<"include_obfuscation"/utf8>>,
erlang:element(3, Opts),
fun gleam@json:bool/1
)]
).
-file("src/glopenai/chat.gleam", 783).
-spec chat_message_to_json(chat_message()) -> gleam@json:json().
chat_message_to_json(Message) ->
case Message of
{developer_message, Content, Name} ->
glopenai@internal@codec:object_with_optional(
[{<<"role"/utf8>>, gleam@json:string(<<"developer"/utf8>>)},
{<<"content"/utf8>>,
developer_message_content_to_json(Content)}],
[glopenai@internal@codec:optional_field(
<<"name"/utf8>>,
Name,
fun gleam@json:string/1
)]
);
{system_message, Content@1, Name@1} ->
glopenai@internal@codec:object_with_optional(
[{<<"role"/utf8>>, gleam@json:string(<<"system"/utf8>>)},
{<<"content"/utf8>>,
system_message_content_to_json(Content@1)}],
[glopenai@internal@codec:optional_field(
<<"name"/utf8>>,
Name@1,
fun gleam@json:string/1
)]
);
{user_message, Content@2, Name@2} ->
glopenai@internal@codec:object_with_optional(
[{<<"role"/utf8>>, gleam@json:string(<<"user"/utf8>>)},
{<<"content"/utf8>>,
user_message_content_to_json(Content@2)}],
[glopenai@internal@codec:optional_field(
<<"name"/utf8>>,
Name@2,
fun gleam@json:string/1
)]
);
{assistant_message, Content@3, Refusal, Name@3, Tool_calls} ->
glopenai@internal@codec:object_with_optional(
[{<<"role"/utf8>>, gleam@json:string(<<"assistant"/utf8>>)}],
[glopenai@internal@codec:optional_field(
<<"content"/utf8>>,
Content@3,
fun assistant_message_content_to_json/1
),
glopenai@internal@codec:optional_field(
<<"refusal"/utf8>>,
Refusal,
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"name"/utf8>>,
Name@3,
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"tool_calls"/utf8>>,
Tool_calls,
fun(Calls) ->
gleam@json:array(Calls, fun tool_call_to_json/1)
end
)]
);
{tool_message, Content@4, Tool_call_id} ->
gleam@json:object(
[{<<"role"/utf8>>, gleam@json:string(<<"tool"/utf8>>)},
{<<"content"/utf8>>,
tool_message_content_to_json(Content@4)},
{<<"tool_call_id"/utf8>>, gleam@json:string(Tool_call_id)}]
)
end.
-file("src/glopenai/chat.gleam", 831).
-spec create_chat_completion_request_to_json(create_chat_completion_request()) -> gleam@json:json().
create_chat_completion_request_to_json(Request) ->
glopenai@internal@codec:object_with_optional(
[{<<"model"/utf8>>, gleam@json:string(erlang:element(2, Request))},
{<<"messages"/utf8>>,
gleam@json:array(
erlang:element(3, Request),
fun chat_message_to_json/1
)}],
[glopenai@internal@codec:optional_field(
<<"temperature"/utf8>>,
erlang:element(4, Request),
fun gleam@json:float/1
),
glopenai@internal@codec:optional_field(
<<"top_p"/utf8>>,
erlang:element(5, Request),
fun gleam@json:float/1
),
glopenai@internal@codec:optional_field(
<<"n"/utf8>>,
erlang:element(6, Request),
fun gleam@json:int/1
),
glopenai@internal@codec:optional_field(
<<"stream"/utf8>>,
erlang:element(7, Request),
fun gleam@json:bool/1
),
glopenai@internal@codec:optional_field(
<<"stream_options"/utf8>>,
erlang:element(8, Request),
fun stream_options_to_json/1
),
glopenai@internal@codec:optional_field(
<<"stop"/utf8>>,
erlang:element(9, Request),
fun stop_configuration_to_json/1
),
glopenai@internal@codec:optional_field(
<<"max_completion_tokens"/utf8>>,
erlang:element(10, Request),
fun gleam@json:int/1
),
glopenai@internal@codec:optional_field(
<<"frequency_penalty"/utf8>>,
erlang:element(11, Request),
fun gleam@json:float/1
),
glopenai@internal@codec:optional_field(
<<"presence_penalty"/utf8>>,
erlang:element(12, Request),
fun gleam@json:float/1
),
glopenai@internal@codec:optional_field(
<<"logprobs"/utf8>>,
erlang:element(13, Request),
fun gleam@json:bool/1
),
glopenai@internal@codec:optional_field(
<<"top_logprobs"/utf8>>,
erlang:element(14, Request),
fun gleam@json:int/1
),
glopenai@internal@codec:optional_field(
<<"response_format"/utf8>>,
erlang:element(15, Request),
fun glopenai@shared:response_format_to_json/1
),
glopenai@internal@codec:optional_field(
<<"tools"/utf8>>,
erlang:element(16, Request),
fun(Tools) ->
gleam@json:array(Tools, fun chat_completion_tool_to_json/1)
end
),
glopenai@internal@codec:optional_field(
<<"tool_choice"/utf8>>,
erlang:element(17, Request),
fun tool_choice_to_json/1
),
glopenai@internal@codec:optional_field(
<<"parallel_tool_calls"/utf8>>,
erlang:element(18, Request),
fun gleam@json:bool/1
),
glopenai@internal@codec:optional_field(
<<"reasoning_effort"/utf8>>,
erlang:element(19, Request),
fun glopenai@shared:reasoning_effort_to_json/1
),
glopenai@internal@codec:optional_field(
<<"modalities"/utf8>>,
erlang:element(20, Request),
fun(Mods) ->
gleam@json:array(Mods, fun response_modality_to_json/1)
end
),
glopenai@internal@codec:optional_field(
<<"verbosity"/utf8>>,
erlang:element(21, Request),
fun verbosity_to_json/1
),
glopenai@internal@codec:optional_field(
<<"web_search_options"/utf8>>,
erlang:element(22, Request),
fun web_search_options_to_json/1
),
glopenai@internal@codec:optional_field(
<<"store"/utf8>>,
erlang:element(23, Request),
fun gleam@json:bool/1
),
glopenai@internal@codec:optional_field(
<<"metadata"/utf8>>,
erlang:element(24, Request),
fun(M) ->
gleam@json:object(
begin
_pipe = maps:to_list(M),
gleam@list:map(
_pipe,
fun(Pair) ->
{erlang:element(1, Pair),
gleam@json:string(
erlang:element(2, Pair)
)}
end
)
end
)
end
),
glopenai@internal@codec:optional_field(
<<"service_tier"/utf8>>,
erlang:element(25, Request),
fun service_tier_to_json/1
),
glopenai@internal@codec:optional_field(
<<"safety_identifier"/utf8>>,
erlang:element(26, Request),
fun gleam@json:string/1
),
glopenai@internal@codec:optional_field(
<<"prompt_cache_key"/utf8>>,
erlang:element(27, Request),
fun gleam@json:string/1
)]
).
-file("src/glopenai/chat.gleam", 929).
-spec role_decoder() -> gleam@dynamic@decode:decoder(role()).
role_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Value) -> case Value of
<<"system"/utf8>> ->
gleam@dynamic@decode:success(role_system);
<<"user"/utf8>> ->
gleam@dynamic@decode:success(role_user);
<<"assistant"/utf8>> ->
gleam@dynamic@decode:success(role_assistant);
<<"tool"/utf8>> ->
gleam@dynamic@decode:success(role_tool);
<<"developer"/utf8>> ->
gleam@dynamic@decode:success(role_developer);
_ ->
gleam@dynamic@decode:failure(role_user, <<"Role"/utf8>>)
end end
).
-file("src/glopenai/chat.gleam", 941).
-spec finish_reason_decoder() -> gleam@dynamic@decode:decoder(finish_reason()).
finish_reason_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Value) -> case Value of
<<"stop"/utf8>> ->
gleam@dynamic@decode:success(stop);
<<"length"/utf8>> ->
gleam@dynamic@decode:success(length);
<<"tool_calls"/utf8>> ->
gleam@dynamic@decode:success(tool_calls);
<<"content_filter"/utf8>> ->
gleam@dynamic@decode:success(content_filter);
<<"function_call"/utf8>> ->
gleam@dynamic@decode:success(function_call_finish);
_ ->
gleam@dynamic@decode:failure(stop, <<"FinishReason"/utf8>>)
end end
).
-file("src/glopenai/chat.gleam", 953).
-spec service_tier_decoder() -> gleam@dynamic@decode:decoder(service_tier()).
service_tier_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Value) -> case Value of
<<"auto"/utf8>> ->
gleam@dynamic@decode:success(service_tier_auto);
<<"default"/utf8>> ->
gleam@dynamic@decode:success(service_tier_default);
<<"flex"/utf8>> ->
gleam@dynamic@decode:success(service_tier_flex);
<<"scale"/utf8>> ->
gleam@dynamic@decode:success(service_tier_scale);
<<"priority"/utf8>> ->
gleam@dynamic@decode:success(service_tier_priority);
_ ->
gleam@dynamic@decode:failure(
service_tier_auto,
<<"ServiceTier"/utf8>>
)
end end
).
-file("src/glopenai/chat.gleam", 965).
-spec tool_call_decoder() -> gleam@dynamic@decode:decoder(tool_call()).
tool_call_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"function"/utf8>>,
glopenai@shared:function_call_decoder(),
fun(Function) ->
gleam@dynamic@decode:success(
{function_tool_call, Id, Function}
)
end
)
end
).
-file("src/glopenai/chat.gleam", 971).
-spec url_citation_decoder() -> gleam@dynamic@decode:decoder(url_citation()).
url_citation_decoder() ->
gleam@dynamic@decode:field(
<<"start_index"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Start_index) ->
gleam@dynamic@decode:field(
<<"end_index"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(End_index) ->
gleam@dynamic@decode:field(
<<"title"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Title) ->
gleam@dynamic@decode:field(
<<"url"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Url) ->
gleam@dynamic@decode:success(
{url_citation,
Start_index,
End_index,
Title,
Url}
)
end
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 984).
-spec response_message_annotation_decoder() -> gleam@dynamic@decode:decoder(response_message_annotation()).
response_message_annotation_decoder() ->
gleam@dynamic@decode:field(
<<"type"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(_) ->
gleam@dynamic@decode:field(
<<"url_citation"/utf8>>,
url_citation_decoder(),
fun(Citation) ->
gleam@dynamic@decode:success(
{url_citation_annotation, Citation}
)
end
)
end
).
-file("src/glopenai/chat.gleam", 992).
-spec chat_completion_response_message_decoder() -> gleam@dynamic@decode:decoder(chat_completion_response_message()).
chat_completion_response_message_decoder() ->
gleam@dynamic@decode:field(
<<"role"/utf8>>,
role_decoder(),
fun(Role) ->
gleam@dynamic@decode:optional_field(
<<"content"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Content) ->
gleam@dynamic@decode:optional_field(
<<"refusal"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Refusal) ->
gleam@dynamic@decode:optional_field(
<<"tool_calls"/utf8>>,
none,
gleam@dynamic@decode:optional(
gleam@dynamic@decode:list(
tool_call_decoder()
)
),
fun(Tool_calls) ->
gleam@dynamic@decode:optional_field(
<<"annotations"/utf8>>,
none,
gleam@dynamic@decode:optional(
gleam@dynamic@decode:list(
response_message_annotation_decoder(
)
)
),
fun(Annotations) ->
gleam@dynamic@decode:success(
{chat_completion_response_message,
Role,
Content,
Refusal,
Tool_calls,
Annotations}
)
end
)
end
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1025).
-spec chat_choice_decoder() -> gleam@dynamic@decode:decoder(chat_choice()).
chat_choice_decoder() ->
gleam@dynamic@decode:field(
<<"index"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Index) ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
chat_completion_response_message_decoder(),
fun(Message) ->
gleam@dynamic@decode:optional_field(
<<"finish_reason"/utf8>>,
none,
gleam@dynamic@decode:optional(finish_reason_decoder()),
fun(Finish_reason) ->
gleam@dynamic@decode:success(
{chat_choice, Index, Message, Finish_reason}
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1043).
-spec create_chat_completion_response_decoder() -> gleam@dynamic@decode:decoder(create_chat_completion_response()).
create_chat_completion_response_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"object"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Object) ->
gleam@dynamic@decode:field(
<<"created"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Created) ->
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(
chat_choice_decoder()
),
fun(Choices) ->
gleam@dynamic@decode:optional_field(
<<"usage"/utf8>>,
none,
gleam@dynamic@decode:optional(
glopenai@shared:completion_usage_decoder(
)
),
fun(Usage) ->
gleam@dynamic@decode:optional_field(
<<"service_tier"/utf8>>,
none,
gleam@dynamic@decode:optional(
service_tier_decoder(
)
),
fun(Service_tier) ->
gleam@dynamic@decode:optional_field(
<<"system_fingerprint"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(
System_fingerprint
) ->
gleam@dynamic@decode:success(
{create_chat_completion_response,
Id,
Object,
Created,
Model,
Choices,
Usage,
Service_tier,
System_fingerprint}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1078).
-spec chat_completion_list_decoder() -> gleam@dynamic@decode:decoder(chat_completion_list()).
chat_completion_list_decoder() ->
gleam@dynamic@decode:field(
<<"object"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Object) ->
gleam@dynamic@decode:field(
<<"data"/utf8>>,
gleam@dynamic@decode:list(
create_chat_completion_response_decoder()
),
fun(Data) ->
gleam@dynamic@decode:optional_field(
<<"first_id"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(First_id) ->
gleam@dynamic@decode:optional_field(
<<"last_id"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Last_id) ->
gleam@dynamic@decode:field(
<<"has_more"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(Has_more) ->
gleam@dynamic@decode:success(
{chat_completion_list,
Object,
Data,
First_id,
Last_id,
Has_more}
)
end
)
end
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1104).
-spec chat_completion_deleted_decoder() -> gleam@dynamic@decode:decoder(chat_completion_deleted()).
chat_completion_deleted_decoder() ->
gleam@dynamic@decode:field(
<<"object"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Object) ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"deleted"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
fun(Deleted) ->
gleam@dynamic@decode:success(
{chat_completion_deleted, Object, Id, Deleted}
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1113).
-spec function_call_stream_decoder() -> gleam@dynamic@decode:decoder(function_call_stream()).
function_call_stream_decoder() ->
gleam@dynamic@decode:optional_field(
<<"name"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Name) ->
gleam@dynamic@decode:optional_field(
<<"arguments"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Arguments) ->
gleam@dynamic@decode:success(
{function_call_stream, Name, Arguments}
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1127).
-spec tool_call_chunk_decoder() -> gleam@dynamic@decode:decoder(tool_call_chunk()).
tool_call_chunk_decoder() ->
gleam@dynamic@decode:field(
<<"index"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Index) ->
gleam@dynamic@decode:optional_field(
<<"id"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Id) ->
gleam@dynamic@decode:optional_field(
<<"function"/utf8>>,
none,
gleam@dynamic@decode:optional(
function_call_stream_decoder()
),
fun(Function) ->
gleam@dynamic@decode:success(
{tool_call_chunk, Index, Id, Function}
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1138).
-spec chat_completion_stream_delta_decoder() -> gleam@dynamic@decode:decoder(chat_completion_stream_delta()).
chat_completion_stream_delta_decoder() ->
gleam@dynamic@decode:optional_field(
<<"role"/utf8>>,
none,
gleam@dynamic@decode:optional(role_decoder()),
fun(Role) ->
gleam@dynamic@decode:optional_field(
<<"content"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Content) ->
gleam@dynamic@decode:optional_field(
<<"refusal"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Refusal) ->
gleam@dynamic@decode:optional_field(
<<"tool_calls"/utf8>>,
none,
gleam@dynamic@decode:optional(
gleam@dynamic@decode:list(
tool_call_chunk_decoder()
)
),
fun(Tool_calls) ->
gleam@dynamic@decode:success(
{chat_completion_stream_delta,
Role,
Content,
Refusal,
Tool_calls}
)
end
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1169).
-spec chat_choice_stream_decoder() -> gleam@dynamic@decode:decoder(chat_choice_stream()).
chat_choice_stream_decoder() ->
gleam@dynamic@decode:field(
<<"index"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Index) ->
gleam@dynamic@decode:field(
<<"delta"/utf8>>,
chat_completion_stream_delta_decoder(),
fun(Delta) ->
gleam@dynamic@decode:optional_field(
<<"finish_reason"/utf8>>,
none,
gleam@dynamic@decode:optional(finish_reason_decoder()),
fun(Finish_reason) ->
gleam@dynamic@decode:success(
{chat_choice_stream,
Index,
Delta,
Finish_reason}
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1184).
-spec create_chat_completion_stream_response_decoder() -> gleam@dynamic@decode:decoder(create_chat_completion_stream_response()).
create_chat_completion_stream_response_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"object"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Object) ->
gleam@dynamic@decode:field(
<<"created"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Created) ->
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(
chat_choice_stream_decoder()
),
fun(Choices) ->
gleam@dynamic@decode:optional_field(
<<"usage"/utf8>>,
none,
gleam@dynamic@decode:optional(
glopenai@shared:completion_usage_decoder(
)
),
fun(Usage) ->
gleam@dynamic@decode:optional_field(
<<"service_tier"/utf8>>,
none,
gleam@dynamic@decode:optional(
service_tier_decoder(
)
),
fun(Service_tier) ->
gleam@dynamic@decode:optional_field(
<<"system_fingerprint"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(
System_fingerprint
) ->
gleam@dynamic@decode:success(
{create_chat_completion_stream_response,
Id,
Object,
Created,
Model,
Choices,
Usage,
Service_tier,
System_fingerprint}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glopenai/chat.gleam", 1224).
?DOC(
" Parse a single SSE data line into a stream chunk.\n"
" Returns `Ok(Some(chunk))` for data, `Ok(None)` for the [DONE] sentinel.\n"
).
-spec parse_stream_chunk(binary()) -> {ok,
gleam@option:option(create_chat_completion_stream_response())} |
{error, glopenai@error:glopenai_error()}.
parse_stream_chunk(Data) ->
case Data of
<<"[DONE]"/utf8>> ->
{ok, none};
_ ->
case gleam@json:parse(
Data,
create_chat_completion_stream_response_decoder()
) of
{ok, Chunk} ->
{ok, {some, Chunk}};
{error, Decode_error} ->
{error, {json_decode_error, Data, Decode_error}}
end
end.
-file("src/glopenai/chat.gleam", 1242).
?DOC(" Build a request to create a chat completion.\n").
-spec create_request(glopenai@config:config(), create_chat_completion_request()) -> gleam@http@request:request(binary()).
create_request(Config, Params) ->
glopenai@internal:post_request(
Config,
<<"/chat/completions"/utf8>>,
create_chat_completion_request_to_json(Params)
).
-file("src/glopenai/chat.gleam", 1254).
?DOC(" Parse the response from creating a chat completion.\n").
-spec create_response(gleam@http@response:response(binary())) -> {ok,
create_chat_completion_response()} |
{error, glopenai@error:glopenai_error()}.
create_response(Response) ->
glopenai@internal:parse_response(
Response,
create_chat_completion_response_decoder()
).
-file("src/glopenai/chat.gleam", 1261).
?DOC(" Build a request to list stored chat completions.\n").
-spec list_request(glopenai@config:config()) -> gleam@http@request:request(binary()).
list_request(Config) ->
glopenai@internal:get_request(Config, <<"/chat/completions"/utf8>>).
-file("src/glopenai/chat.gleam", 1266).
?DOC(" Parse the response from listing chat completions.\n").
-spec list_response(gleam@http@response:response(binary())) -> {ok,
chat_completion_list()} |
{error, glopenai@error:glopenai_error()}.
list_response(Response) ->
glopenai@internal:parse_response(Response, chat_completion_list_decoder()).
-file("src/glopenai/chat.gleam", 1273).
?DOC(" Build a request to retrieve a stored chat completion.\n").
-spec retrieve_request(glopenai@config:config(), binary()) -> gleam@http@request:request(binary()).
retrieve_request(Config, Completion_id) ->
glopenai@internal:get_request(
Config,
<<"/chat/completions/"/utf8, Completion_id/binary>>
).
-file("src/glopenai/chat.gleam", 1281).
?DOC(" Parse the response from retrieving a chat completion.\n").
-spec retrieve_response(gleam@http@response:response(binary())) -> {ok,
create_chat_completion_response()} |
{error, glopenai@error:glopenai_error()}.
retrieve_response(Response) ->
glopenai@internal:parse_response(
Response,
create_chat_completion_response_decoder()
).
-file("src/glopenai/chat.gleam", 1288).
?DOC(" Build a request to delete a stored chat completion.\n").
-spec delete_request(glopenai@config:config(), binary()) -> gleam@http@request:request(binary()).
delete_request(Config, Completion_id) ->
glopenai@internal:delete_request(
Config,
<<"/chat/completions/"/utf8, Completion_id/binary>>
).
-file("src/glopenai/chat.gleam", 1293).
?DOC(" Parse the response from deleting a chat completion.\n").
-spec delete_response(gleam@http@response:response(binary())) -> {ok,
chat_completion_deleted()} |
{error, glopenai@error:glopenai_error()}.
delete_response(Response) ->
glopenai@internal:parse_response(
Response,
chat_completion_deleted_decoder()
).