View Source LangChain.Utils (LangChain v0.3.0-rc.0)

Collection of helpful utilities mostly for internal use.

Summary

Functions

Return changeset errors as text with comma separated description.

Only add the key to the map if the value is present. When the value is a list, the key will not be added when the list is empty. If the value is nil, it will not be added.

Fire a streaming callback if present.

Creates and returns an anonymous function to handle the streaming response from an API.

Return an {:ok, module} when the string successfully converts to an existing module.

Put the value in the list at the desired index. If the index does not exist, return an updated list where it now exists with the value in that index.

Convert map atom keys to strings

Given a struct, create a map with the selected keys converted to strings. Additionally includes a version number for the data.

Translates an error message using gettext.

Translates the errors for a field from a keyword list of errors.

Validation helper. Validates a struct changeset that the LLM is a struct.

Types

Functions

Link to this function

changeset_error_to_string(changeset)

View Source
@spec changeset_error_to_string(Ecto.Changeset.t()) :: nil | String.t()

Return changeset errors as text with comma separated description.

Link to this function

conditionally_add_to_map(map, key, value)

View Source
@spec conditionally_add_to_map(
  %{required(any()) => any()},
  key :: any(),
  value :: nil | list()
) :: %{
  required(any()) => any()
}

Only add the key to the map if the value is present. When the value is a list, the key will not be added when the list is empty. If the value is nil, it will not be added.

Link to this function

fire_streamed_callback(model, data)

View Source
@spec fire_streamed_callback(
  %{optional(:stream) => boolean(), callbacks: [map()]},
  data :: callback_data() | [callback_data()]
) :: :ok | no_return()

Fire a streaming callback if present.

Link to this function

handle_stream_fn(model, decode_stream_fn, transform_data_fn)

View Source
@spec handle_stream_fn(
  %{optional(:stream) => boolean()},
  decode_stream_fn :: function(),
  transform_data_fn :: function()
) :: function()

Creates and returns an anonymous function to handle the streaming response from an API.

Accepts the following functions that handle the API-specific requirements:

  • decode_stream_fn - a function that parses the raw results from an API. It deals with the specifics or oddities of a data source. The results come back as {[list_of_parsed_json_maps], "incomplete text to buffer"}. In some cases, a API may span the JSON data response across messages. This function assembles what is complete and returns any incomplete portion that is passed in on the next iteration of the function.

  • transform_data_fn - a function that is executed to process the parsed JSON data in the form of an Elixir map into a LangChain struct of the appropriate type.

  • callback_fn - a function that receives a successful result of from the transform_data_fn.

Link to this function

module_from_name(module_name)

View Source

Return an {:ok, module} when the string successfully converts to an existing module.

Link to this function

put_in_list(list, index, value)

View Source
@spec put_in_list([any()], integer(), any()) :: [any()]

Put the value in the list at the desired index. If the index does not exist, return an updated list where it now exists with the value in that index.

Convert map atom keys to strings

Original source: https://gist.github.com/kipcole9/0bd4c6fb6109bfec9955f785087f53fb

Link to this function

to_serializable_map(struct, keys, version \\ 1)

View Source
@spec to_serializable_map(struct(), keys :: [atom()], version :: integer()) :: %{
  required(String.t()) => any()
}

Given a struct, create a map with the selected keys converted to strings. Additionally includes a version number for the data.

Translates an error message using gettext.

Link to this function

translate_errors(errors, field)

View Source

Translates the errors for a field from a keyword list of errors.

Link to this function

validate_llm_is_struct(changeset)

View Source
@spec validate_llm_is_struct(Ecto.Changeset.t()) :: Ecto.Changeset.t()

Validation helper. Validates a struct changeset that the LLM is a struct.