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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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

Other popular topics Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement