Using pretrained AI models in Elixir / Phoenix application

This year the Elixir team has made a huge progress in the AI area, it has started with NX, then came Axon with somewhat higher level API and finally Bumblebee which makes using pretrained AI models and integrating them into Phoenix app really easy (I hate it when people say that, but in this case it’s the truth – it’s easy).

This Christmas I finally got to play with it and was surprised at how well it went. Bumblebee is still very new, the documentation is “fresh”, but despite that, it works quite well. There are also few libraries that would help working with the data (Scholar, Explorer, all came this year), but the important part is that we now have the ability to use available open source models without the need to train them.

I am not sure how commercially viable it is, but it’s definitely fun to play with 🙂

Here’s the result showcasing few models integrated into a sample Phoenix application.

Elixir – Testing Phoenix Flash Messages

At times in my integration tests I want to check if a user sees a flash message. It’s easy when re-rendering a template, just check the response body like that:

assert html_response(conn, 200) =~ "some message"

UPD as pointed out by Dave Lugg there is also a built in helper that can do that:

assert get_flash(conn, :info) == “some Message”
assert get_flash(conn, :info) =~ “part of some Message”

But often we set flash messages and redirect the user, in this case response body would not contain them and we’d have to check against the data that is saved on the conn struct, this is doable using Phoenix controller module:

assert(
    conn
    |> Phoenix.Controller.get_flash()
    |> Enum.any?(fn(item) -> String.contains?(elem(item, 1), "some message") end))

which works, but doesn’t look very nice and clutters the tests. So the cleaner solution would be to pack the whole thing in a test helper function and put it in test/support (anything in this directory will be compiled when running tests). Here is an example of such a helper (file test/support/helper.ex): Continue reading

Elixir – Nested Changesets With Phoenix

Say we have a user registration form where the user enters their name, email etc (User schema) as well as their password (Authorization schema). Both have to be validated, both require extra actions and belong together, so if anything fails the whole thing has to roll back and give meaningful feedback.

This is doable using Ecto.Multi (or nested case with transactions in controllers, but that would be ugly). Here is an alternative to Ecto.Multi that keeps it as minimalistic as possible while still allowing for much flexibility when dealing with complex user inputs: nested forms with custom changesets (note: Ecto 2 is needed). Continue reading

Deploying Elixir Apps

Recently we started playing with Elixir which is an awesome language bringing quite a few perks in terms of performance, reliability and scalability but also enforcing a mentality shift to functional programming (as compared to OOP as in modern PHP).

When it came to deployment we basically had two options: using exrm releases or just starting the app (that uses Phoenix framework) in production mode.

Internet didn’t tell us much about which option is more useful aside from exrm being the “cooler” one and the rolling update – “easier”, so we tried both, starting with the cool one 🙂 Continue reading