working on providers
Some checks failed
CI/CD Pipeline / build (push) Failing after 3s

This commit is contained in:
2026-03-10 14:04:02 -06:00
parent b3619a145f
commit abe27b82d1
10 changed files with 413 additions and 71 deletions

View File

@@ -13,4 +13,7 @@ video ideas:
- recommends oban instead of GenServer <https://hexdocs.pm/oban/Oban.html> - recommends oban instead of GenServer <https://hexdocs.pm/oban/Oban.html>
- can autoscale with flame <https://hexdocs.pm/flame/FLAME.html> - can autoscale with flame <https://hexdocs.pm/flame/FLAME.html>
- tool calling scaling? - tool calling scaling?
- using elixir langchain for tool definitions - using elixir langchain for tool definitions
real time blog: <https://seanmoriarity.com/2024/02/25/implementing-natural-conversational-agents-with-elixir/>

View File

@@ -7,6 +7,7 @@ defmodule ElixirAi.Application do
children = [ children = [
ElixirAiWeb.Telemetry, ElixirAiWeb.Telemetry,
ElixirAi.Repo, ElixirAi.Repo,
{Task, fn -> ElixirAi.AiProvider.ensure_default_provider() end},
{Cluster.Supervisor, {Cluster.Supervisor,
[Application.get_env(:libcluster, :topologies, []), [name: ElixirAi.ClusterSupervisor]]}, [Application.get_env(:libcluster, :topologies, []), [name: ElixirAi.ClusterSupervisor]]},
{Phoenix.PubSub, name: ElixirAi.PubSub}, {Phoenix.PubSub, name: ElixirAi.PubSub},

View File

@@ -45,7 +45,10 @@ defmodule ElixirAi.ChatRunner do
messages: messages, messages: messages,
streaming_response: nil, streaming_response: nil,
pending_tool_calls: [], pending_tool_calls: [],
tools: tools(self(), name) tools: tools(self(), name),
ai_provider_url: Application.get_env(:elixir_ai, :ai_provider_url),
ai_model: Application.get_env(:elixir_ai, :ai_model),
ai_token: Application.get_env(:elixir_ai, :ai_token),
}} }}
end end

View File

@@ -17,13 +17,13 @@ defmodule ElixirAi.ConversationManager do
end end
def init(_) do def init(_) do
names = Conversation.all_names() conversation_list = Conversation.all_names()
conversations = Map.new(names, fn name -> {name, []} end) conversations = Map.new(conversation_list, fn %{name: name} -> {name, []} end)
{:ok, conversations} {:ok, conversations}
end end
def create_conversation(name) do def create_conversation(name, ai_provider_id) do
GenServer.call(@name, {:create, name}) GenServer.call(@name, {:create, name, ai_provider_id})
end end
def open_conversation(name) do def open_conversation(name) do
@@ -38,11 +38,11 @@ defmodule ElixirAi.ConversationManager do
GenServer.call(@name, {:get_messages, name}) GenServer.call(@name, {:get_messages, name})
end end
def handle_call({:create, name}, _from, conversations) do def handle_call({:create, name, ai_provider_id}, _from, conversations) do
if Map.has_key?(conversations, name) do if Map.has_key?(conversations, name) do
{:reply, {:error, :already_exists}, conversations} {:reply, {:error, :already_exists}, conversations}
else else
case Conversation.create(name) do case Conversation.create(name, ai_provider_id) do
:ok -> :ok ->
case start_and_subscribe(name) do case start_and_subscribe(name) do
{:ok, _pid} = ok -> {:reply, ok, Map.put(conversations, name, [])} {:ok, _pid} = ok -> {:reply, ok, Map.put(conversations, name, [])}
@@ -75,8 +75,6 @@ defmodule ElixirAi.ConversationManager do
end end
def handle_info({:store_message, name, message}, conversations) do def handle_info({:store_message, name, message}, conversations) do
messages = Map.get(conversations, name, [])
case Conversation.find_id(name) do case Conversation.find_id(name) do
{:ok, conv_id} -> Message.insert(conv_id, message) {:ok, conv_id} -> Message.insert(conv_id, message)
_ -> :ok _ -> :ok

View File

@@ -0,0 +1,89 @@
defmodule ElixirAi.AiProvider do
import Ecto.Query
alias ElixirAi.Repo
def all do
Repo.all(
from(p in "ai_providers",
select: %{
id: p.id,
name: p.name,
model_name: p.model_name,
api_token: p.api_token,
completions_url: p.completions_url
}
)
)
end
def create(attrs) do
now = DateTime.truncate(DateTime.utc_now(), :second)
case Repo.insert_all("ai_providers", [
[
name: attrs.name,
model_name: attrs.model_name,
api_token: attrs.api_token,
completions_url: attrs.completions_url,
inserted_at: now,
updated_at: now
]
]) do
{1, _} ->
Phoenix.PubSub.broadcast(
ElixirAi.PubSub,
"ai_providers",
{:provider_added, attrs}
)
:ok
_ ->
{:error, :db_error}
end
rescue
e in Ecto.ConstraintError ->
if e.constraint == "ai_providers_name_key",
do: {:error, :already_exists},
else: {:error, :db_error}
end
def find_by_name(name) do
case Repo.one(
from(p in "ai_providers",
where: p.name == ^name,
select: %{
id: p.id,
name: p.name,
model_name: p.model_name,
api_token: p.api_token,
completions_url: p.completions_url
}
)
) do
nil -> {:error, :not_found}
provider -> {:ok, provider}
end
end
def ensure_default_provider do
case Repo.aggregate(from(p in "ai_providers"), :count) do
0 ->
attrs = %{
name: System.get_env("DEFAULT_PROVIDER_NAME", "default_provider"),
model_name: System.get_env("DEFAULT_MODEL_NAME", "gpt-4"),
api_token: System.get_env("DEFAULT_API_TOKEN", ""),
completions_url:
System.get_env(
"DEFAULT_COMPLETIONS_URL",
"https://api.openai.com/v1/chat/completions"
)
}
create(attrs)
_ ->
:ok
end
end
end

View File

@@ -3,20 +3,39 @@ defmodule ElixirAi.Conversation do
alias ElixirAi.Repo alias ElixirAi.Repo
def all_names do def all_names do
Repo.all(from c in "conversations", select: c.name) Repo.all(
from(c in "conversations",
left_join: p in "ai_providers",
on: c.ai_provider_id == p.id,
select: %{
name: c.name,
provider: %{
name: p.name,
model_name: p.model_name,
api_token: p.api_token,
completions_url: p.completions_url
}
}
)
)
end end
def create(name) do def create(name, ai_provider_id) do
case Repo.insert_all("conversations", [[name: name, inserted_at: now(), updated_at: now()]]) do case Repo.insert_all("conversations", [
[name: name, ai_provider_id: ai_provider_id, inserted_at: now(), updated_at: now()]
]) do
{1, _} -> :ok {1, _} -> :ok
_ -> {:error, :db_error} _ -> {:error, :db_error}
end end
rescue rescue
e in Ecto.ConstraintError -> if e.constraint == "conversations_name_index", do: {:error, :already_exists}, else: {:error, :db_error} e in Ecto.ConstraintError ->
if e.constraint == "conversations_name_index",
do: {:error, :already_exists},
else: {:error, :db_error}
end end
def find_id(name) do def find_id(name) do
case Repo.one(from c in "conversations", where: c.name == ^name, select: c.id) do case Repo.one(from(c in "conversations", where: c.name == ^name, select: c.id)) do
nil -> {:error, :not_found} nil -> {:error, :not_found}
id -> {:ok, id} id -> {:ok, id}
end end

View File

@@ -0,0 +1,35 @@
defmodule ElixirAiWeb.FormComponents do
use Phoenix.Component
@doc """
Renders a styled input field with label.
## Examples
<.input type="text" name="email" value={@email} label="Email" />
<.input type="password" name="password" label="Password" />
"""
attr :type, :string, default: "text"
attr :name, :string, required: true
attr :value, :string, default: ""
attr :label, :string, required: true
attr :autocomplete, :string, default: "off"
attr :rest, :global
def input(assigns) do
~H"""
<div>
<label for={@name} class="block text-sm text-cyan-300 mb-1">{@label}</label>
<input
type={@type}
name={@name}
id={@name}
value={@value}
autocomplete={@autocomplete}
class="w-full rounded px-3 py-2 text-sm bg-cyan-950/20 border border-cyan-900/40 text-cyan-100 focus:outline-none focus:ring-1 focus:ring-cyan-700"
{@rest}
/>
</div>
"""
end
end

View File

@@ -0,0 +1,155 @@
defmodule ElixirAiWeb.AiProvidersLive do
use ElixirAiWeb, :live_component
import ElixirAiWeb.FormComponents
alias ElixirAi.AiProvider
def update(assigns, socket) do
if connected?(socket) do
Phoenix.PubSub.subscribe(ElixirAi.PubSub, "ai_providers")
end
{:ok,
socket
|> assign(assigns)
|> assign_new(:providers, fn -> AiProvider.all() end)
|> assign_new(:show_form, fn -> false end)
|> assign_new(
:form_data,
fn ->
%{
"name" => "",
"model_name" => "",
"api_token" => "",
"completions_url" => "https://api.openai.com/v1/chat/completions"
}
end
)
|> assign_new(:error, fn -> nil end)}
end
def render(assigns) do
~H"""
<div>
<div class="flex items-center justify-between mb-8">
<h1 class="text-lg font-semibold text-cyan-300">AI Providers</h1>
<button
phx-click="toggle_form"
phx-target={@myself}
class="px-3 py-1 rounded text-sm border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors"
>
{if @show_form, do: "Cancel", else: "Add Provider"}
</button>
</div>
<%= if @show_form do %>
<form
phx-submit="create_provider"
phx-target={@myself}
class="mb-8 space-y-2 p-4 rounded-lg border border-cyan-900/40 bg-cyan-950/10"
>
<.input type="text" name="name" value={@form_data["name"]} label="Provider Name" />
<.input type="text" name="model_name" value={@form_data["model_name"]} label="Model Name" />
<.input type="password" name="api_token" value={@form_data["api_token"]} label="API Token" />
<.input
type="text"
name="completions_url"
value={@form_data["completions_url"]}
label="Completions URL"
/>
<button
type="submit"
class="w-full px-4 py-2 rounded text-sm border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors"
>
Create Provider
</button>r
</form>
<% end %>
<%= if @error do %>
<p class="mb-4 text-sm text-red-400">{@error}</p>
<% end %>
<ul class="space-y-2">
<%= if @providers == [] do %>
<li class="text-sm text-cyan-700">No providers configured yet.</li>
<% end %>
<%= for provider <- @providers do %>
<li class="p-4 rounded-lg border border-cyan-900/40 bg-cyan-950/20">
<div class="flex items-start justify-between">
<div class="flex-1">
<h3 class="text-sm font-medium text-cyan-300">{provider.name}</h3>
<p class="text-xs text-cyan-500 mt-1">Model: {provider.model_name}</p>
<p class="text-xs text-cyan-600 mt-1 truncate">
Endpoint: {provider.completions_url}
</p>
<p class="text-xs text-cyan-600 mt-1">
Token: {String.slice(provider.api_token || "", 0..7)}***
</p>
</div>
</div>
</li>
<% end %>
</ul>
</div>
"""
end
def handle_event("toggle_form", _params, socket) do
{:noreply, assign(socket, show_form: !socket.assigns.show_form, error: nil)}
end
def handle_event("create_provider", params, socket) do
name = String.trim(params["name"])
model_name = String.trim(params["model_name"])
api_token = String.trim(params["api_token"])
completions_url = String.trim(params["completions_url"])
cond do
name == "" ->
{:noreply, assign(socket, error: "Provider name can't be blank")}
model_name == "" ->
{:noreply, assign(socket, error: "Model name can't be blank")}
api_token == "" ->
{:noreply, assign(socket, error: "API token can't be blank")}
completions_url == "" ->
{:noreply, assign(socket, error: "Completions URL can't be blank")}
true ->
attrs = %{
name: name,
model_name: model_name,
api_token: api_token,
completions_url: completions_url
}
case AiProvider.create(attrs) do
:ok ->
{:noreply,
socket
|> assign(show_form: false)
|> assign(
form_data: %{
"name" => "",
"model_name" => "",
"api_token" => "",
"completions_url" => "https://api.openai.com/v1/chat/completions"
}
)
|> assign(error: nil)}
{:error, :already_exists} ->
{:noreply, assign(socket, error: "A provider with that name already exists")}
_ ->
{:noreply, assign(socket, error: "Failed to create provider")}
end
end
end
def handle_info({:provider_added, _attrs}, socket) do
{:noreply, assign(socket, providers: AiProvider.all())}
end
end

View File

@@ -1,79 +1,102 @@
defmodule ElixirAiWeb.HomeLive do defmodule ElixirAiWeb.HomeLive do
use ElixirAiWeb, :live_view use ElixirAiWeb, :live_view
alias ElixirAi.ConversationManager import ElixirAiWeb.FormComponents
alias ElixirAi.{ConversationManager, AiProvider}
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
if connected?(socket) do
Phoenix.PubSub.subscribe(ElixirAi.PubSub, "ai_providers")
end
{:ok, {:ok,
socket socket
|> assign(conversations: ConversationManager.list_conversations()) |> assign(conversations: ConversationManager.list_conversations())
|> assign(ai_providers: AiProvider.all())
|> assign(new_name: "") |> assign(new_name: "")
|> assign(error: nil)} |> assign(error: nil)}
end end
def render(assigns) do def render(assigns) do
~H""" ~H"""
<div class="max-w-lg mx-auto mt-16 px-4"> <div class="max-w-lg mx-auto mt-16 px-4 space-y-16">
<h1 class="text-lg font-semibold text-cyan-300 mb-8">Conversations</h1> <div>
<h1 class="text-lg font-semibold text-cyan-300 mb-8">Conversations</h1>
<ul class="mb-8 space-y-2"> <ul class="mb-8 space-y-2">
<%= if @conversations == [] do %> <%= if @conversations == [] do %>
<li class="text-sm text-cyan-700">No conversations yet.</li> <li class="text-sm text-cyan-700">No conversations yet.</li>
<% end %>
<%= for name <- @conversations do %>
<li>
<.link
navigate={~p"/chat/#{name}"}
class="block px-4 py-2 rounded-lg border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors text-sm"
>
{name}
</.link>
</li>
<% end %>
</ul>
<form phx-submit="create" class="space-y-2">
<.input type="text" name="name" value={@new_name} label="Conversation Name" />
<select
name="ai_provider_id"
class="w-full rounded px-3 py-2 text-sm bg-cyan-950/20 border border-cyan-900/40 text-cyan-100 focus:outline-none focus:ring-1 focus:ring-cyan-700"
>
<option value="">Select AI Provider</option>
<%= for provider <- @ai_providers do %>
<option value={provider.id}>{provider.name} - {provider.model_name}</option>
<% end %>
</select>
<button
type="submit"
class="w-full px-4 py-2 rounded text-sm border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors"
>
Create
</button>
</form>
<%= if @error do %>
<p class="mt-2 text-sm text-red-400">{@error}</p>
<% end %> <% end %>
<%= for name <- @conversations do %> </div>
<li>
<.link
navigate={~p"/chat/#{name}"}
class="block px-4 py-2 rounded-lg border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors text-sm"
>
{name}
</.link>
</li>
<% end %>
</ul>
<form phx-submit="create" class="flex gap-2"> <div>
<input <.live_component module={ElixirAiWeb.AiProvidersLive} id="ai-providers" />
type="text" </div>
name="name"
value={@new_name}
placeholder="New conversation name"
class="flex-1 rounded px-3 py-2 text-sm bg-cyan-950/20 border border-cyan-900/40 text-cyan-100 placeholder-cyan-800 focus:outline-none focus:ring-1 focus:ring-cyan-700"
autocomplete="off"
/>
<button
type="submit"
class="px-4 py-2 rounded text-sm border border-cyan-900/40 bg-cyan-950/20 text-cyan-300 hover:border-cyan-700 hover:bg-cyan-950/40 transition-colors"
>
Create
</button>
</form>
<%= if @error do %>
<p class="mt-2 text-sm text-red-400">{@error}</p>
<% end %>
</div> </div>
""" """
end end
def handle_event("create", %{"name" => name}, socket) do def handle_event("create", %{"name" => name, "ai_provider_id" => provider_id}, socket) do
name = String.trim(name) name = String.trim(name)
if name == "" do cond do
{:noreply, assign(socket, error: "Name can't be blank")} name == "" ->
else {:noreply, assign(socket, error: "Name can't be blank")}
case ConversationManager.create_conversation(name) do
{:ok, _pid} ->
{:noreply,
socket
|> push_navigate(to: ~p"/chat/#{name}")
|> assign(error: nil)}
{:error, :already_exists} -> provider_id == "" ->
{:noreply, assign(socket, error: "A conversation with that name already exists")} {:noreply, assign(socket, error: "Please select an AI provider")}
_ -> true ->
{:noreply, assign(socket, error: "Failed to create conversation")} case ConversationManager.create_conversation(name, provider_id) do
end {:ok, _pid} ->
{:noreply,
socket
|> push_navigate(to: ~p"/chat/#{name}")
|> assign(error: nil)}
{:error, :already_exists} ->
{:noreply, assign(socket, error: "A conversation with that name already exists")}
_ ->
{:noreply, assign(socket, error: "Failed to create conversation")}
end
end end
end end
def handle_info({:provider_added, _attrs}, socket) do
{:noreply, assign(socket, ai_providers: AiProvider.all())}
end
end end

View File

@@ -1,8 +1,24 @@
-- drop table if exists messages cascade;
-- drop table if exists conversations cascade;
-- drop table if exists ai_providers cascade;
CREATE TABLE IF NOT EXISTS ai_providers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL UNIQUE,
model_name TEXT NOT NULL,
api_token TEXT NOT NULL,
completions_url TEXT NOT NULL,
inserted_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS conversations ( CREATE TABLE IF NOT EXISTS conversations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE,
inserted_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), ai_provider_id UUID NOT NULL REFERENCES ai_providers(id) ON DELETE RESTRICT,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() inserted_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
); );
CREATE TABLE IF NOT EXISTS messages ( CREATE TABLE IF NOT EXISTS messages (