Return only selected fields
How do i return only the selected fields instead of the whole struct? Cant seem to find a good example for the action based version
16 Replies
"return" in what context?
over an API?
Elixir structs always have all fields present, so there is no way to literally remove those fields from the structs
My brain is still working in sql, yes over an API. how do I get reduce the amount of unneeded data getting received when I make an api call over even use the code interface method.
You'd do that in the api layer
With ash_json_api that's
default_fields
if you use
ash_json_api
, you can do this
this is an example if your json_api
is defined on your resource
i suppose :id
is your primary key? if so, then you possibly don't need it inside data[*].attributes
of the response. it's already present in data[*].id
Perfect. Then what happens on ash_ai when the tool is called?
You don't need to do selects in the resource if you do it in the default select, and then the tool action will select everything by default
nice, now can the default select only apply to certain actions or does it have to apply to all? just trying to minimize the amount of unneeded data being returned
You can put
default_select
on specific routes in ash_json_api
man still not getting it. this is in the domain?
or
🤔
You don't need to use a different action or anything like that
The default fields is automatically applied to the read action as a select or a load etc
Thanks Zach, if i have multiple read actions and want one to return the full query but the other one to only return the default fields is that possible? Currently i have only found im am able to apply default fields to all actions. Using this with tools in ash ai and have specific actions that i want to not return unneeded data to consume the context window. So just looking for a good way to do specific selects
I'm confused TBH. What does AshAi have to do with ash_json_api? There is no current selecting feature for ash ai
Oh interesting...is ash_json_api's default fields somehow impacting ash_ai? If so that's a bug 😅
Yeah think Im misunderstanding as well.
If i do the following on the resource it applies to all read actions and returns only those fields for the different reads using the tools. I have tried a bunch of different things, adding separate routes in the resource, in the domain, and adding default fields to those routes and they dont seem to apply. when using a tool I can check the json endpoint and get the correct default fields at say artists/default_fields
That's a bug 🙂 AshAi should not be using that config
It's a byproduct of us reusing the ash json api serializer
The reason changing other things doesn't work is because AshAi isn't using routes etc, it just fakes it for serialization.
Please open an issue on ash_ai. The first step will be temoving that behavior
And then we can talk about how to limit fields
Probably what we'd do first is actually just allow the agent to specify a select, or maybe rework the
load
logic to work such that we default to selecting nothing and make the default load statement all public attributesOh interesting idea to have the agent specify., Initial thought was to have a return format on the tool call
I think we'd probably do both
in the long run