openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Add support for filtering out properties in serialized objects from function calls

Open kvitso opened this issue 3 years ago • 0 comments

When function calls request data with potentially large amounts of data, it would have been great if we could customize the serializer in a way such that a filter can be applied. This would have to be done in the mapper.writer() call. It would potetially save ALOT of tokens if this could be done, from my experience the assistant is only interested in a small portion of the data sent back.

ObjectMapper mapper = new ObjectMapper();
FilterProvider filters = new SimpleFilterProvider()
    .addFilter("myFilter", SimpleBeanPropertyFilter
    .filterOutAllExcept("field1", "field2"));
mapper.setFilterProvider(filters);

String json = mapper.writer(filters).writeValueAsString(myObject);

A "hack" (that could be useful in other scenarios as well) would be to apply a UnaryOperator to the result of a function call, before it is sent back. That way, we could deserialize and then serialize with the given filter. However, it would be better to apply the serialization filter automatically the first time the result is provided.

kvitso avatar Nov 21 '23 14:11 kvitso