Elixir AI SDK

An Elixir port of the Vercel AI SDK, designed to help you build AI-powered applications using Elixir and Phoenix.

Features

Installation

def deps do
  [
    {:ai_sdk, "~> 0.0.1-rc.0"}
  ]
end

Usage Examples

Text Generation

{:ok, result} = AI.generate_text(%{
  model: AI.openai("gpt-4o"),
  system: "You are a friendly assistant!",
  prompt: "Why is the sky blue?"
})

IO.puts(result.text)

Streaming Text Generation

{:ok, result} = AI.stream_text(%{
  model: AI.openai("gpt-4o"),
  system: "You are a friendly assistant!",
  prompt: "Why is the sky blue?"
})

# Process chunks as they arrive (safely with proper termination)
r = Enum.reduce_while(result.stream, [], fn
  # Text chunk received - print it and continue
  {:text_delta, chunk}, acc ->
    IO.write(chunk)
    {:cont, [chunk | acc]}
    
  # Stream finished - print reason and halt collection
  {:finish, reason}, acc ->
    IO.puts("\nFinished: #{reason}")
    {:halt, acc}
    
  # Error received - print it and halt collection
  {:error, error}, acc ->
    IO.puts("\nError: #{inspect(error)}")
    {:halt, acc}
    
  # Other events - just continue
  _, acc ->
    {:cont, acc}
end)

IO.inspect(r, label: "Collected chunks")

Architecture

The Elixir AI SDK follows similar patterns to the original Vercel AI SDK but uses Elixir idioms and patterns:

Development Status

This project is under active development. See the project roadmap for more information.

Documentation