Skip to main content

first_llmp_job

Your First LLMP Job

Introduction to LLMP Jobs

Large Language Models (LLMs) can revolutionize programming projects with their generative capabilities. But harnessing their full potential can often require crafting tailored prompts, curating examples, and fine-tuning parameters—a process that's both time-intensive and breaks the rhythm of iterative development.

While LLMs excel at free-form text generation, programming tasks demand a more structured, reliable output format. Enter LLMP: it's designed to seamlessly integrate LLM tasks into your programming projects, ensuring you stay in the development flow.

At its core, an LLMP unit of work, termed a "Job", minimizes prompt engineering. By defining just the input and output model, LLMP takes over: from generating examples and instructions to optimizing the prompt for reliability. Moreover, once crafted, these Jobs can be easily reused within your project.

Initializing Your First Job

The essence of LLMP lies in the simplicity of initializing a job. Let's walk through it:

  1. Job Definition: At the bare minimum, define the input and output model for the task.
  2. Automation by LLMP: The framework then automatically crafts examples, generates instructions, and optimizes the prompt.
  3. Storage & Reusability: Each job is stored, either in the default directory or a custom one, identified by a unique job ID or an optional name. This ensures you can effortlessly reference and reuse them later.

In subsequent sections, we'll delve deeper into different methods to initialize a Job. Stay tuned!

Define Input and Output Model using Pydantic

For our example we will define a Job for simple labeling task. Where we want to define the genre of a book. To define the possible Labels we will use a Enum class from the standard library. To define the input and output model we will use Pydantic.

from typing import Literal
from pydantic import BaseModel
from llmp.services.program import Program



class InputObject(BaseModel):
book_title: str
book_author: str
release_year: int

class OutputObject(BaseModel):
genre: Literal["fiction", "non-fiction", "fantasy", "sci-fi", "romance", "thriller", "horror", "other"]


# Initialize a job
program = Program("Book to Genre", input_model=InputObject, output_model=OutputObject)

# load a job
# program = Program("Book to Genre")
    Job name 'Book to Genre' already exists. Using 'Book to Genre_v2' instead.
input_data={
"book_title": "The Lord of the Rings",
"book_author": "J. R. R. Tolkien",
"release_year": 1954
}
program(input_data=input_data)
    {'genre': 'fantasy'}

Despite defining an Enum class we can also set options via Field or use the Literal type. The following example shows how to define the same OutputModel using Field and Literal.

Let's define a new program

from typing import Optional


class InputObject(BaseModel):
book_title: str
book_author: str
release_year: int

class OutputObject(BaseModel):
genre: Literal["fiction", "non-fiction", "fantasy", "sci-fi", "romance", "thriller", "horror", "other"]
has_sequal: bool
sequal_name: Optional[str] = "None"


# Initialize a job
program = Program("Book to Genre/Sequal", input_model=InputObject, output_model=OutputObject)

# load a job
#program = Program("Book to Genre/Sequal")
    ---------------------------------------------------------------------------

NameError Traceback (most recent call last)

Cell In[1], line 4
1 from typing import Optional
----> 4 class InputObject(BaseModel):
5 book_title: str
6 book_author: str


NameError: name 'BaseModel' is not defined
program.job.generation_log
    [{'event_id': '6eee28be26ef44b68a7085418e8964e7',
'input': {'book_title': 'The Bible',
'book_author': 'Johannes Gutenberg',
'release_year': 1450},
'output': {'genre': 'non-fiction',
'has_sequal': False,
'sequal_name': 'None'}},
{'event_id': '9a1109c8b8fd46a0a2195a8c2dce0d63',
'input': {'book_title': 'Harry Potter',
'book_author': 'J. K. Rowling',
'release_year': 1997},
'output': {'genre': 'fantasy',
'has_sequal': True,
'sequal_name': 'Harry Potter and the Chamber of Secrets'}}]
input_data={
"book_title": "Harry Potter",
"book_author": "J. K. Rowling",
"release_year": 1997
}
result = program(input_data=input_data)


if result.has_sequal:
print(f"The book {result.sequal_name} is a sequal to {input_data['book_title']}")
else:
print(f"The book {input_data['book_title']} has no sequal")
    The book Harry Potter and the Chamber of Secrets is a sequal to Harry Potter