ElixirLLM (ElixirLLM v0.3.0)

Copy Markdown View Source

A unified Elixir API for LLMs.

ElixirLLM provides one beautiful interface for OpenAI, Anthropic, Ollama, and more. Inspired by RubyLLM.

Quick Start

# Simple chat
{:ok, response} = ElixirLLM.chat("What is Elixir?")

# With configuration
{:ok, response, chat} =
  ElixirLLM.new()
  |> ElixirLLM.model("claude-sonnet-4-5")
  |> ElixirLLM.temperature(0.7)
  |> ElixirLLM.ask("Explain OTP in simple terms")

# With streaming
ElixirLLM.new()
|> ElixirLLM.model("gpt-4o")
|> ElixirLLM.ask("Write a haiku", stream: fn chunk ->
  IO.write(chunk.content || "")
end)

Tools

defmodule MyApp.Tools.Calculator do
  use ElixirLLM.Tool

  @impl true
  def name, do: "calculator"

  @impl true
  def description, do: "Performs basic math calculations"

  @impl true
  def parameters do
    %{
      expression: [type: :string, description: "Math expression to evaluate"]
    }
  end

  @impl true
  def execute(%{expression: expr}) do
    # Safe evaluation...
    {:ok, 42}
  end
end

{:ok, response, chat} =
  ElixirLLM.new()
  |> ElixirLLM.tool(MyApp.Tools.Calculator)
  |> ElixirLLM.ask("What is 6 * 7?")

Configuration

Configure API keys in your config/config.exs:

config :elixir_llm,
  default_model: "gpt-4o",

  openai: [
    api_key: System.get_env("OPENAI_API_KEY")
  ],

  anthropic: [
    api_key: System.get_env("ANTHROPIC_API_KEY")
  ],

  ollama: [
    base_url: "http://localhost:11434"
  ]

Summary

Functions

Sends a message and returns the assistant's response.

Creates audio content from a file path.

Simple one-shot chat completion.

Generates embeddings for the given text(s).

Creates an image content for multi-modal messages.

Creates image content from base64 data.

Creates image content from a URL.

Creates a new chat instance.

Creates PDF content from a file path.

Returns a stream of chunks for the given message.

Functions

ask(chat, message, opts \\ [])

@spec ask(ElixirLLM.Chat.t(), String.t(), keyword()) ::
  {:ok, ElixirLLM.Response.t(), ElixirLLM.Chat.t()} | {:error, Exception.t()}

Sends a message and returns the assistant's response.

Maintains conversation history in the returned chat.

Options

  • :stream - A callback function (Chunk.t() -> any()) for streaming responses
  • :with - File path(s) to include with the message (not yet implemented)

Examples

# Simple ask
{:ok, response, chat} = ElixirLLM.ask(chat, "Hello!")

# Continue the conversation
{:ok, response, chat} = ElixirLLM.ask(chat, "Tell me more")

# With streaming
{:ok, response, chat} = ElixirLLM.ask(chat, "Write a story", stream: fn chunk ->
  IO.write(chunk.content || "")
end)

audio(path)

Creates audio content from a file path.

chat(message, opts \\ [])

@spec chat(
  String.t(),
  keyword()
) :: {:ok, ElixirLLM.Response.t()} | {:error, Exception.t()}

Simple one-shot chat completion.

Examples

{:ok, response} = ElixirLLM.chat("What is Elixir?")
{:ok, response} = ElixirLLM.chat("Explain OTP", model: "claude-sonnet-4-5")

embed(input, opts \\ [])

Generates embeddings for the given text(s).

Options

  • :model - The embedding model to use (default: "text-embedding-3-small")
  • :dimensions - Number of dimensions (for models that support it)

Examples

{:ok, embedding} = ElixirLLM.embed("Hello, world!")
{:ok, embeddings} = ElixirLLM.embed(["Hello", "World"])

image(path)

Creates an image content for multi-modal messages.

Examples

content = ElixirLLM.image("photo.jpg")
{:ok, response, chat} = ElixirLLM.ask(chat, "What's in this image?", with: content)

image_base64(data, media_type \\ "image/png")

Creates image content from base64 data.

image_url(url)

Creates image content from a URL.

instructions(chat, content, opts \\ [])

See ElixirLLM.Chat.instructions/3.

max_tokens(chat, tokens)

See ElixirLLM.Chat.max_tokens/2.

model(chat, model_id)

See ElixirLLM.Chat.model/2.

new(opts \\ [])

@spec new(keyword()) :: ElixirLLM.Chat.t()

Creates a new chat instance.

Options

  • :model - The model to use (e.g., "gpt-4o", "claude-sonnet-4-5")

Examples

chat = ElixirLLM.new()
chat = ElixirLLM.new(model: "claude-sonnet-4-5")

on_tool_call(chat, callback)

See ElixirLLM.Chat.on_tool_call/2.

on_tool_result(chat, callback)

See ElixirLLM.Chat.on_tool_result/2.

params(chat, params)

See ElixirLLM.Chat.params/2.

pdf(path)

Creates PDF content from a file path.

provider(chat, provider)

See ElixirLLM.Chat.provider/2.

schema(chat, schema_module)

See ElixirLLM.Chat.schema/2.

stream(chat, message)

@spec stream(ElixirLLM.Chat.t(), String.t()) :: Enumerable.t()

Returns a stream of chunks for the given message.

Examples

ElixirLLM.new()
|> ElixirLLM.model("gpt-4o")
|> ElixirLLM.stream("Write a poem")
|> Stream.each(fn chunk -> IO.write(chunk.content || "") end)
|> Stream.run()

temperature(chat, temp)

See ElixirLLM.Chat.temperature/2.

tool(chat, tool)

See ElixirLLM.Chat.tool/2.

tools(chat, tools, opts \\ [])

See ElixirLLM.Chat.tools/3.

with_cache(chat)

See ElixirLLM.Chat.with_cache/1.

with_circuit_breaker(chat)

See ElixirLLM.Chat.with_circuit_breaker/1.

with_rate_limiting(chat)

See ElixirLLM.Chat.with_rate_limiting/1.

with_retry(chat, opts \\ [])

See ElixirLLM.Chat.with_retry/2.