LlmEx (LlmEx v0.1.0)

View Source

LlmEx is a flexible client for interacting with various LLM providers.

This library provides a unified API for working with different LLM services, including local models via Ollama and cloud services like Anthropic.

Getting Started

Add LlmEx to your dependencies in mix.exs:

def deps do
  [
    {:llm_ex, "~> 0.1.0"}
  ]
end

Configure your provider API keys (if needed) in your config.exs:

config :llm_ex,
  anthropic_api_key: System.get_env("ANTHROPIC_API_KEY")

Usage

Create a client module and specify the provider:

defmodule MyApp.LlmClient do
  use LlmEx.Client, provider: LlmEx.Providers.OllamaClient
  
  def get_knowledge do
    with_tool(MyTool)
    |> with_prompt("What do you know about Elixir?")
    |> stream()
  end
end

Process the streaming response:

def handle_info({:llm_response, _id, content}, socket) do
  # Handle the response content
  {:noreply, socket}
end

Summary

Functions

Creates a new client with the specified provider.

Streams the response from the LLM provider.

Adds a message to the conversation history.

Sets the prompt for the client.

Sets the system message for the client.

Adds a tool to the client configuration.

Functions

client(provider, opts \\ [])

Creates a new client with the specified provider.

Parameters

  • provider - The LLM provider module to use
  • opts - Additional configuration options

Examples

iex> client = LlmEx.client(LlmEx.Providers.OllamaClient)
iex> is_map(client)
true
iex> client.provider == LlmEx.Providers.OllamaClient
true

stream(client)

Streams the response from the LLM provider.

Parameters

  • client - The client configuration

Examples

iex> client = LlmEx.client(LlmEx.Providers.OllamaClient) |> LlmEx.with_prompt("Hello")
iex> # Function will call provider.stream_chat which returns :ok
iex> # This is just a mock example for doctest
iex> client.provider != nil
true

with_message(client, message)

Adds a message to the conversation history.

Parameters

  • client - The client configuration
  • message - The message to add

Examples

iex> client = LlmEx.client(LlmEx.Providers.OllamaClient)
iex> message = %LlmEx.Types.Message{role: :user, content: "Hello"}
iex> result = LlmEx.with_message(client, message)
iex> length(result.messages)
1

with_prompt(client, prompt)

Sets the prompt for the client.

Parameters

  • client - The client configuration
  • prompt - The prompt text

Examples

iex> client = LlmEx.client(LlmEx.Providers.OllamaClient)
iex> result = LlmEx.with_prompt(client, "What is Elixir?")
iex> result.prompt
"What is Elixir?"

with_system(client, system)

Sets the system message for the client.

Parameters

  • client - The client configuration
  • system - The system message text

Examples

iex> client = LlmEx.client(LlmEx.Providers.OllamaClient)
iex> result = LlmEx.with_system(client, "You are a helpful assistant.")
iex> result.system
"You are a helpful assistant."

with_tool(client, tool)

Adds a tool to the client configuration.

Parameters

  • client - The client configuration
  • tool - The tool to add

Examples

iex> client = LlmEx.client(LlmEx.Providers.OllamaClient)
iex> result = LlmEx.with_tool(client, :test_tool)
iex> result.tools
[:test_tool]