Hello, I would like to learn from you how you woul...
# koog-agentic-framework
d
Hello, I would like to learn from you how you would make the best DTO/json builder using AI Agents. Let's say you have a data grid, with columns and rows, similar to an Excel file. This grid can take a FilterSortModel: continue in thread..
So let's say you want your agent to be able to sort and filter a data grid (powered by AG Grid on frontend) You want to generate a SortDTO and a FilterDTO where: SortDTO { String colId; SortDirection sort } // colId mapped to it's sort direction FilterDTO { Map<String, ColumnFilter> } // colId mapped to it's filter The ColumnFilter is a quite complex object:
Copy code
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "filterType")
@JsonSubTypes( {
    @JsonSubTypes.Type(value = NumberColumnFilter.class, name = "number"),
    @JsonSubTypes.Type(value = StringColumnFilter.class, name = "text"),
    @JsonSubTypes.Type(value = DateTimeColumnFilter.class, name = "datetime"),
    @JsonSubTypes.Type(value = DateColumnFilter.class, name = "date"),
    @JsonSubTypes.Type(value = BooleanColumnFilter.class, name = "bool"),
    @JsonSubTypes.Type(value = EnumColumnFilter.class, name = "enum"),
    @JsonSubTypes.Type(value = MultiEnumColumnFilter.class, name = "multienum")
})
@Data
@NoArgsConstructor(force = true, access = AccessLevel.PROTECTED)
@AllArgsConstructor
public abstract class ColumnFilter implements Serializable {
    @Nonnull
    protected GridOperandType type;
    private ColumnDataType dataType;

    public abstract Bson toMongoFilter(String field);
}
How to make the best strategy so the LLM/Agent understands what you want I assume you have to break your workflow into steps, first you deal with the sortDTO then with the filterDTO
Spring AI has Structured Output feature to deal with it for example: https://www.baeldung.com/spring-artificial-intelligence-structure-output
I found some examples here: https://docs.koog.ai/structured-output/ with the same feature from koog
Turned out I just needed a more powerful model, with local models it will not work as expected
j
Hey Dumitru, thanks for reaching out! I'm glad that you were able to progress on your issue :) Let me answer parts of it anyway - perhaps you will find it valuable. First of all, I highly recommend you take a look at examples in our docs - there's a ton of resources that present various ways of composing agent-based logic with Koog. Now more about your use case > you want your agent to be able to sort and filter a data grid I would start off with modeling the request against your data grid - for example simple data class
DataGridQuery
that would be composed from
Filter
and
Sort
types that you already provided. See https://docs.koog.ai/structured-output/#defining-data-structures from our docs you mentioned before (you might like to use https://docs.koog.ai/structured-output/#nested-classes for your case). Once you define the model for your request, you want to be able to create it from plain String. I assume your flow starts off with user prompt like "Give me entries where column_a contains 'foo' and sort by column_b descending". We want to convert this String into
DataGridQuery
- this is where you would actually delegate some work to LLM. There are couple of ways to do that in Koog - see https://docs.koog.ai/structured-output/#requesting-structured-responses - which one you choose depends on your application's logic and needs. I personally like graph based scenarios so let me suggest that for you to experiment with. To do that, define your
String -> DataGridQuery
conversion logic with node layer (you might want to use
nodeLLMRequestStructured
- it's made exactly for such cases). See this https://docs.koog.ai/structured-output/#layer-3-node-layer. You will end up creating a node that converts
String
to
DataGridQuery
with help of LLM. I imagine that you want to use your
DataGridQuery
now - to do that just define another node that accepts
DataGridQuery
and does something with it. Then you connect your nodes with an edge, effectively forming a graph. You can now expand your application logic by just growing your graph :) See this example of banking application that utilizes this approach: https://docs.koog.ai/examples/Banking/#building-an-agent-with-graph In your specific case, I would also consider creating a dedicated
Tool
for querying a grid. Calling a tool might be a better idea for this than just executing a prompt on LLM. See Money Tools from the example above and check out our docs here: https://docs.koog.ai/tools-overview/
❤️ 1
d
Yes exactly, I would need something like this:
Copy code
val agentStrategy = strategy("weather-forecast") {
    val setup by node<Unit, String> { _ ->
        "Please provide a weather forecast for Amsterdam"
    }

    // Create a structured output node using delegate syntax
    val getWeatherForecast by nodeLLMRequestStructured<WeatherForecast>(
        name = "forecast-node",
        examples = exampleForecasts,
        fixingParser = StructureFixingParser(
            model = OpenAIModels.Chat.GPT4o,
            retries = 3
        )
    )

    val processResult by node<Result<StructuredResponse<WeatherForecast>>, String> { result ->
        when {
            result.isSuccess -> {
                val forecast = result.getOrNull()?.data
                "Weather forecast: $forecast"
            }
            result.isFailure -> {
                "Failed to get structured forecast: ${result.exceptionOrNull()?.message}"
            }
            else -> "Unknown result state"
        }
    }

    edge(nodeStart forwardTo setup)
    edge(setup forwardTo getWeatherForecast)
    edge(getWeatherForecast forwardTo processResult)
    edge(processResult forwardTo nodeFinish)
}
👍 1
Also I noticed Spring Ai does not have any LLMDescription as koog has:
Copy code
@property:LLMDescription("Type of request: Transfer or Analytics")
    val requestType: RequestType
Works at class level as well, not only fields However SpringAI is able to understand the jackson annotations at least, for example JsonPropertyDescription:
Copy code
@JsonPropertyDescription("For reference only (e.g., 'NUMBER', 'INTEGER', 'DECIMAL', etc.)")
@JsonProperty("dataType")
String dataType