BamlElixir.Client (baml_elixir v1.0.0-pre.6)

A client for interacting with BAML functions. Data structures and functions are generated from BAML source files.

use BamlElixir.Client, path: "priv/baml_src"

When you use BamlElixir.Client, it will define:

  • A module for each function in the BAML source files with call/2 and stream/3 functions along with the types.
  • A module with defstruct/1 and @type t/0 for each class in the BAML source file.
  • A module with @type t/0 for each enum in the BAML source file.

The path option is optional and defaults to "baml_src", you may want to set it to "priv/baml_src".

This module also provides functionality to call BAML functions either sync/async.

Summary

Functions

call(function_name, args, opts \\ %{})

@spec call(String.t(), map(), map()) :: {:ok, term()} | {:error, String.t()}

Calls a BAML function synchronously.

Parameters

  • function_name: The name of the BAML function to call
  • args: A map of arguments to pass to the function
  • opts: A map of options
    • path: The path to the BAML source file
    • collectors: A list of collectors to use
    • llm_client: The name of the LLM client to use

Returns

  • {:ok, term()} on success, where the term is the function's return value
  • {:error, String.t()} on failure, with an error message

Examples

{:ok, result} = BamlElixir.Client.call(client, "MyFunction", %{arg1: "value"})

generate_class_types(class_types)

(macro)

generate_enum_types(enum_types)

(macro)

generate_function_modules(functions, path)

(macro)

stream(function_name, args, callback, opts \\ %{})

Streams a BAML function asynchronously.

Parameters

  • function_name: The name of the BAML function to stream
  • args: A map of arguments to pass to the function
  • callback: A function that will be called with the result of the function
  • opts: A map of options
    • path: The path to the BAML source file
    • collectors: A list of collectors to use
    • llm_client: The name of the LLM client to use