thmsmlr

thmsmlr

Ecto field descriptions

Hey y’all so i’m working on a project that takes in Ecto schema and generates JSONSchemas for them. Part of the project requires that the user be able to document descriptions on the ecto fields so I can put them in the JSONSchema.

I’ve done some splunking and I’m stuck. I was wondering if y’all could point me in the right direction.

I was thinking of leveraging something like module docs, or somehow retrieving the extra field opts at runtime. I’ve looked at the Ecto.Schema.field/4 source code and can’t find any leads there. Similarly it seems like the @doc attribute can only be defined at the module level.

Ideally I’d like to provide a code experience something like this.

defmodule SpamPredicition do
  use Ecto.Schema

  @primary_key false
  schema "predictions" do

    @doc "I think ideally i'd put field descriptions as @doc attributes"
    field(:class, Ecto.Enum, values: [:spam, :not_spam])

    field(:reason, :string, description: "But I could tolerate field descriptions here")
    field(:confidence_score, :float)
  end
end

Any ideas? Maybe there’s a better approach that’s easier to implement?

Most Liked

mayel

mayel

There was a similar question recently: Support for comments (or other metadata) in Ecto.Schema fields? This could help with reflection

All I know of are comments you can add in migrations, but not sure that meets your need: Ecto.Migration — Ecto SQL v3.14.0

thmsmlr

thmsmlr

These are great suggestions. In my particularly usecase I found an easier way to go about it.

I’m working on the GitHub - thmsmlr/instructor_ex: Structured outputs for LLMs in Elixir · GitHub library and the JSONSchema is getting sent into an LLM. What I realized is that the LLM should be resilient to whether the description is at the schema level or the field level. So I decided to just do something like,

defmodule SpamPredicition do
  use Ecto.Schema
  use Instructor.Validator

  @doc """
  ## Field Descriptions:
  - class: Whether or not the email is spam
  - reason: A short, less than 10 word rationalization for the classification
  - score: A confidence score between 0.0 and 1.0 for the classification
  """
  @primary_key false
  embedded_schema do
    field(:class, Ecto.Enum, values: [:spam, :not_spam])
    field(:reason, :string)
    field(:score, :float)
  end

In some sense, for my usecase, this is even more flexible because you can write whatever you want about the semantics of this schema in the schema level @doc and the AI can make the appropriate associations, since you know.. it’s AI or whatnot.

If I come back to this under other circumstances i’ll definitely explore these solutions. Thanks y’all for your help!

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement