Star

linkObject Initialization

As MTLLM is really great at handling typed outputs, we have added the ability to initialize a new object with only providing few of the required fields. MTLLM will automatically fill the rest of the fields based on the given context.

This behavior is very hard to achieve in other languages, but with MTLLM, it is as simple as providing the required fields and letting the MTLLM do the rest.

In the following example, we are initializing a new object of type Task with only providing the description field. The time_in_min and priority_out_of_10 fields are automatically filled by the MTLLM based on the given context after a step of reasoning.

1linkimport:py from mtllm.llms, OpenAI, Ollama;

2link

3linkglob llm = OpenAI(model_name="gpt-4o");

4link

5linkobj Task {

6link has description: str;

7link has time_in_min: int,

8link priority_out_of_10: int;

9link}

10link

11linkwith entry {

12link task_contents = [

13link "Have some sleep",

14link "Enjoy a better weekend with my girlfriend",

15link "Work on Jaseci Project",

16link "Teach EECS 281 Students",

17link "Enjoy family time with my parents"

18link ];

19link tasks = [];

20link for task_content in task_contents {

21link task_info = Task(description = task_content by llm(method="Reason"));

22link tasks.append(task_info);

23link }

24link print(tasks);

25link}

1link# Output

2link[

3link Task(description='Have some sleep', time_in_min=30, priority_out_of_10=5),

4link Task(description='Enjoy a better weekend with my girlfriend', time_in_min=60, priority_out_of_10=7),

5link Task(description='Work on Jaseci Project', time_in_min=120, priority_out_of_10=8),

6link Task(description='Teach EECS 281 Students', time_in_min=90, priority_out_of_10=9),

7link Task(description='Enjoy family time with my parents', time_in_min=60, priority_out_of_10=7)

8link]

Here is another example with nested custom types,

1linkimport:py from jaclang.core.llms, OpenAI;

2link

3linkglob llm = OpenAI(model_name="gpt-4o");

4link

5linkobj Employer {

6link has name: 'Employer Name': str,

7link location: str;

8link}

9link

10linkobj 'Person'

11linkPerson {

12link has name: str,

13link age: int,

14link employer: Employer,

15link job: str;

16link}

17link

18linkwith entry {

19link info: "Person's Information": str = "Alice is a 21 years old and works as an engineer at LMQL Inc in Zurich, Switzerland.";

20link person = Person(by llm(incl_info=(info)));

21link print(person);

22link}

1link# Output

2linkPerson(name='Alice', age=21, employer=Employer(name='LMQL Inc', location='Zurich, Switzerland'), job='engineer')

In the above example, we have initialized a new object of type Person with only providing info as additional context. The name, age, employer, and job fields are automatically filled by the MTLLM based on the given context.

Object Initialization

Home

Quick Startchevron_right
Building Blockschevron_right
Tutorialschevron_right
API Referencechevron_right
Tips and Trickschevron_right

FAQs