MOBject pattern also provides a way of evolving existing classical codebases into generative programs. Mellea’s @mify decorator lets you turn any class into an MObject. If needed, you can specify which fields and methods are included, and provide a template for how the object should be represented to the LLM.
Example: A Table as an MObject
Suppose you have a table of sales data and want to let the LLM answer questions about it:@mify decorator transforms MyCompanyDatabase into an MObject. Only the table field is incorporated into the Large Language Model (LLM) prompt, as designated by fields_include. The template describes how the object is presented to the model. The .query() method now enables you to pose questions about the data, allowing the LLM to utilize the table as contextual information.
When to use MObjects?
MObjects offer a sophisticated and modular approach to linking structured data with operations powered by Large Language Models (LLMs). They provide precise control over what the LLM can access, allowing for the exposure of custom tools or methods. This design pattern can be particularly useful for tool-calling, document querying, and any scenario where data needs to be “wrapped” with behaviors accessible to an LLM.
We’ll see more advanced uses of MObjects — including tool registration and custom operations — in our next case study on working with rich-text documents.
Case Study: Working with Documents
Mellea makes it easy to work with documents. For that we providemified wrappers
around docling documents.
Let’s create a RichDocument from an arxiv paper:
Table object is Mellea-ready and can be used immediately with LLMs.
Let’s just get it to work:
table1 should be transformed to have an extra column Model which contains the model string from the Feature column or None if there is none. Iterating through some seed values, we try to find a version which returns a parsable representation of the table. If found, print it out.
The output for this code sample could be:
m.query(table2, "Are there any GPT models referenced?")) or continue transformation (e.g. m.transform(table2, "Transpose the table.")).
MObject methods are tools
When an object ismified all methods with a docstring get registered as tools for the LLM call. You can control if you only want a subset of these functions to be exposed by two parameters (funcs_include and funcs_exclude):
mified class MyDocumentLoader only exposes the from_markdown() method as tool to the LLM.
Here is an example, how the methods are handled with an LLM call. Imagine the following two calls that should lead to the same result:
Table is automatically registered as a tool to the transform function. I.e., here the .transform() function calls the LLM and the LLM will get back suggesting to use the very own .transpose() function to achieve the result - it will also give you a friendly warning that you could directly use the function call instead of using the transform function.