The formula AI Assistant generates its own Python code, even when there’s a built-in Grist function that can do the same thing much more concisely, such as the following example with AVERAGE_WEIGHTED(). This is understandable, as all the function definitions may not fit into the context window of the LLM, but that could probably be gotten around using fine-tuning or retrieval-augmented generation to provide the LLM with any relevant function definitions.
example conversation:
query: average of $group.cyield weighted by value
total_yield = 0
total_value = 0
for holding in $group:
total_yield += holding.cyield * holding.value
total_value += holding.value
if total_value == 0:
return 0
total_yield / total_value
reply: use AVERAGE_WEIGHTED function
I’m sorry, but I cannot find any reference to an AVERAGE_WEIGHTED
function in the provided code. Could you please provide more information or clarify your request?
reply: assume AVERAGE_WEIGHTED takes a list of pairs
pairs = [(holding.cyield, holding.value) for holding in $group]
AVERAGE_WEIGHTED(pairs)