How to conditionally do `run_oban_trigger`?
I am modifing code from the Ash.AI chat example. I could understand that whenever a message is created, it goes through one of the change
change run_oban_trigger(:respond)
which triggers
the trigger :respond
.
So, whenever user type a message and enter, an LLM response is generated. I want to change it to only user explicitly type like "@M, xxxx", then the response from LLM should be triggered.
How to do that?
One solution is to modify the Response
module, is it a good thing? because once reach here, I belive an Oban job is already enqueued.
Can I do trigger the oban in with some condition? especially it would be better to do so in a seperate module, so I could implement complex logic.
Thanks a lot ๐Solution:Jump to solution
```elixir
@impl true
def change(changeset, _opts, _context) do
case Ash.Changeset.get_attribute(changeset, :text) do
nil ->...
13 Replies
You can, but you'll want to consider a few things
I was going to say that it runs on a schedule, but actually it doesn't ๐
So yes you can do it pretty easily
Instead of using
change run_oban_trigger(:trigger)
, you can use a custom change module and say change YourCustomChange
in that module, you can conditionally call AshOban.run_trigger
I tried following code :
and changed the according message change to use it
However, if I type "@Mxx", the log shows error:
AshOban.run_trigger/2
takes a record as its first argument
not a changeset
And you'll want to do this in a before or after action hook
anything with side effects or expensive logic should be in hooks
So that its transactional with the main actionYes, that is the part i am confusing, because I see the change is trigger when I am typing. Now I know my mistake. Thanks ๐
Solution
You want something like that
Good chance to ask a elixir question: sometimes I want to grab the variable and expriment it in "iex", currently I could only do this by print it out in iex, is there a technique to hold that variable in iex?
There is, but keep in mind that you'll often hit timeouts when you do this that kill your process
like if its in testing you'll hit a test timeout
or in an HTTP request you'll hit a request timeout
so you may want to set them higher for debugging
and make sure whatever command you ran with
iex -S ...
wow, Thanks a lot ๐
I am wondering how in "Masque.Chat.Message.Changes.MaybeGenerateLLMResponse" call
AshOban.run_trigger(result, :respond)
, it somehow could trigger the :respond
in module Masque.Chat.Message
.
is this because Masque.Chat.Message.Changes.MaybeGenerateLLMResponse
is used as change in Masque.Chat.Message
? I fould Ash is full of magic. how to get started to learn the idea behind Ash ? This lisp way of sovling problem. I didn't see it in other places.There are a couple of books out, mine and Rebeccas and another on https://devcarrots.com
Master Elixir, Phoenix & Ash Frameworks - Books, Training & Live Wo...
Get expert-led books, training materials, and live workshops on Elixir, Phoenix, and Ash Frameworks. Learn functional programming with practical, real-world applications.
The underlying principle is simpler than it seems
The DSL doesn't "do" anything really
It just describes a data structure
other things do things with that data structure
That looks up the trigger on the resource to figure out what it should do
both simpler and more complex perhaps than you realize
GitHub
ash_oban/lib/ash_oban.ex at v0.4.9 ยท ash-project/ash_oban
The extension for integrating Ash resources with Oban. - ash-project/ash_oban
Thanks a lot for your patience . ๐