View Source ExOpenAi.ChatRequest (ex_open_ai v0.1.7)

Represents a chat request to the OpenAI API.

This module allows you to add a message to the list of messages in the request as well as validate any request to the Chat resource.

examples

Examples

iex> params = %{
...>   model: "text-model",
...>   messages: [
...>     %{content: "hello"}
...>   ]
...> }
iex> ExOpenAi.ChatRequest.create(params)
{:ok, %ExOpenAi.Chat{...}}

iex> ExOpenAi.ChatRequest.do_append(params, %{content: "world"})
%{model: "text-model", messages: [%{content: "hello"}, %{content: "world"}]}

Link to this section Summary

Functions

Builds the create chat request and validates the required fields.

Deletes any fields that are not in the list of fields.

Builds the chat messages by appending to the list of messages in the map.

Builds the map by adding to the existing map and updates the map with the attributes passed in.

Ensures that the map has the required fields.

Prepares the map by removing any fields that are not in the list of fields. and ensures required fields are present.

Link to this section Functions

@spec create(params :: map()) :: {:ok, ExOpenAi.Chat.t()} | {:error, map()}

Builds the create chat request and validates the required fields.

examples

Examples

iex> params = %{
...>   model: "text-model",
...>   messages: [
...>     %{role: "system", content: "hello"}
...>   ]
...> }
iex> ExOpenAi.ChatRequest.create(params)
{:ok, %ExOpenAi.Chat{...}}
Link to this function

delete_invalid_fields(map, fields)

View Source
@spec delete_invalid_fields(map(), fields :: list()) :: map()

Deletes any fields that are not in the list of fields.

examples

Examples

  iex> params = %{
  ...>   model: "text-model",
  ...>   messages: [
  ...>     %{content: "hello"}
  ...>   ]
  ...> }
  iex> Request.delete_invalid_fields(params, [:model, :messages])
  %{model: "text-model", messages: [%{content: "hello"}]}
Link to this function

do_append(params, message)

View Source
@spec do_append(params :: map(), message :: map()) :: map()

Builds the chat messages by appending to the list of messages in the map.

examples

Examples

iex> params = %{
...>   model: "text-model",
...>   messages: [
...>     %{role: "system", content: "hello"}
...>   ]
...> }
iex> ExOpenAi.ChatRequest.do_append(params, %{role: "user", content: "world"})
%{model: "text-model", messages: [%{content: "hello"}, %{content: "world"}]}
Link to this function

do_change(map, attributes)

View Source
@spec do_change(map(), attributes :: list()) :: map()

Builds the map by adding to the existing map and updates the map with the attributes passed in.

examples

Examples

iex> params = %{
...>   model: "text-model",
...>   messages: [
...>     %{content: "hello"}
...>   ]
...> }
iex> Request.do_change(params, promp: "this is a prompt")
%{model: "text-model", messages: [%{content: "hello"}], prompt: "this is a prompt"}
Link to this function

has_required_fields?(map, fields)

View Source
@spec has_required_fields?(map(), fields :: list()) :: boolean()

Ensures that the map has the required fields.

examples

Examples

  iex> params = %{model: "text-model", messages: [%{content: "hello"}]}
  iex> Request.has_required_fields?(params, [:model, :messages])
  true

  iex> params = %{model: "text-model"}
  iex> Request.has_required_fields?(params, [:model, :messages])
  false
Link to this function

prepare(map, fields, required_fields)

View Source
@spec prepare(map(), fields :: list(), required_fields :: list()) :: map()

Prepares the map by removing any fields that are not in the list of fields. and ensures required fields are present.

examples

Examples

  iex> params = %{
  ...>   model: "text-model",
  ...>   messages: [
  ...>     %{content: "hello"}
  ...>   ]
  ...> }
  iex> Request.prepare(params, [], [:model, :messages])
  %{model: "text-model", messages: [%{content: "hello"}]}

  iex> params = %{
  ...>   model: "text-model",
  ...>   messages: [
  ...>     %{content: "hello"}
  ...>   ]
  ...> }
  iex> Request.prepare(params, [], [:model, :messages, :prompt])
  ** (RuntimeError) Required fields are missing