Silly metaprogramming experiment: OpenAI functions

Wanted to share a little Livebook-oriented metaprogramming experiment: Autofunctions

OpenAI released an update to their ChatGPT API a couple days ago called functions, a structured way to teach the model about additional procedures that your code provides that can be called by the model. The “catch” is that the functions have to be specified as a JSON Schema, and I thought it would be kinda neat if you could basically ignore all that and just write Elixir functions instead :slightly_smiling_face:

defmodule ExampleModule do
  use Autofunctions

  @doc """
  Duplicates a string.
  """
  @spec dup_string(string :: binary(), num_times :: pos_integer()) :: binary()
  def dup_string(string, n \\ 1) do
    String.duplicate(string, n)
  end
end

So Autofunctions uses the typespec and doc to construct a JSON Schema and provides that to the model during a chat session. Right now it literally only handles the types above, but it would be a somewhat easy task to extend it.

If you want to try it out, clone the repo, open the example in Livebook, add a OPENAI_API_KEY to your hub secrets, and give it a go!

I’m not currently planning to release/expand much on this, but it was a fun enough experiment that I thought it would be worth sharing!

6 Likes