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
@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)
Creates audio content from a file path.
@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")
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"])
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)
Creates image content from base64 data.
Creates image content from a URL.
@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")
Creates PDF content from a file path.
@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()