It is often very hard to prompt models into consistent and reliable quality. With Opper it is easy to take the approach of showing models how to complete the task by providing examples. This is called in-context learning (or few-shot prompting).

How it works

In-context learning (ICL) is a technique where large language models (LLMs) learn to perform a task by observing a few examples provided within the prompt, without requiring any further training or parameter updates. Essentially, you show the model what you want it to do through examples rather than telling it directly. This approach leverages the pre-trained knowledge of the LLM and allows it to adapt to new tasks quickly. With Opper you can easily build task specific datasets and have them act as examples for completions. Datasets for tasks can be populated through SDKs, API and in the Dashboard.

Creating a server side task

Task completions are normally defined at call-time, but they can also be managed on the server side. Managing task configuration on the server can simplify reuse, but also help manage configuration, datasets and analytics centrally for each task.

In the following snippet we create a hosted task definition for generating a room description from a database entry:

from opperai import Opper
from pydantic import BaseModel, Field
from typing import List
import os

# Initialize Opper client
opper = Opper(http_bearer=os.getenv("OPPER_API_KEY", ""))

# --------- Schemas --------- #

class RoomDatabaseEntry(BaseModel):
    hotel_name: str = Field(description="Name of the hotel offering the room")
    room_count: int = Field(description="Number of rooms in the suite")
    view: str = Field(description="Type of view from the room, e.g., ocean, city")
    beds: int = Field(description="Number of beds in the room")
    price_per_night: float = Field(description="Cost of the room per night in USD")
    amenities: List[str] = Field(description="List of amenities available in the room, e.g., wifi, breakfast, parking")

class RoomDescription(BaseModel):
    description: str = Field(description="A natural language description of the room suitable for customers")

def setup_function():
    """Setup and return the room description function."""
    try:
        # Create the function
        function = opper.functions.create(
            name="generate_room_description",
            instructions="Given a room database entry, describe the room in a way that is easy to understand and use for a customer.",
            input_schema=RoomDatabaseEntry.model_json_schema(),
            output_schema=RoomDescription.model_json_schema(),
            configuration={
                "invocation.few_shot.count": 3
            }
        )
        
    except Exception as e:
        print(f"Error creating function: {e}")
        # If function already exists, we can get it by name
        function = opper.functions.get(function_id="26ac858f-56a8-436a-9221-f100493dffda")
    
    return function

def main():
    function = setup_function()

if __name__ == "__main__":
    main() 
Notice that we are setting ‘invocation.few_shot.count=3’ on the function. This will mean that invocations of this task will pull 3 semantically relevant examples in the dataset of the function in the prompt when completing new tasks. We will see later how we populate these examples for making sure that the output always follows the same style.

Populating examples

Once we have a server side task definition, we can curate example entries for the task with the correct input and output schemas and populate the task dataset with these. A dataset is a collection of input/output pairs that represent ideal completion of a task.

Here we create three examples of input and output room descriptions where we want them to follow the pattern of: This room at “hotel” features “room description”. Mention “amenities”. Perfect for “recommendation”

def add_examples(dataset_id: str):
    """Create example datasets for few-shot learning."""
    examples = [
        {
            "input": RoomDatabaseEntry(
                hotel_name="Seaside Resort",
                room_count=2,
                view="ocean",
                beds=1,
                price_per_night=250,
                amenities=["wifi", "room service", "minibar", "balcony"]
            ).model_dump(),
            "output": RoomDescription(
                description="This room at Seaside Resort features an elegant 2-room oceanfront suite with a comfortable king bed and private balcony offering stunning ocean views. Premium amenities include complimentary WiFi, personalized room service, and a well-stocked minibar. Perfect for couples seeking a romantic getaway or honeymooners looking for luxury and privacy."
            ).model_dump(),
            "comment": "Example of a luxury oceanview suite with emphasis on romantic atmosphere"
        },
        {
            "input": RoomDatabaseEntry(
                hotel_name="Mountain Lodge",
                room_count=3,
                view="mountain",
                beds=4,
                price_per_night=350,
                amenities=["wifi", "fireplace", "kitchen", "ski storage", "parking"]
            ).model_dump(),
            "output": RoomDescription(
                description="This room at Mountain Lodge features a spacious 3-room suite with 4 comfortable beds, a fully equipped kitchen, and a cozy fireplace. The breathtaking mountain views complement modern amenities like WiFi and convenient ski storage, with complimentary parking included. Ideal for families or groups of friends planning an active mountain getaway."
            ).model_dump(),
            "comment": "Example of a family-friendly mountain suite with practical amenities"
        },
        {
            "input": RoomDatabaseEntry(
                hotel_name="Urban Boutique Hotel",
                room_count=1,
                view="city",
                beds=1,
                price_per_night=150,
                amenities=["wifi", "workspace", "coffee maker", "gym access"]
            ).model_dump(),
            "output": RoomDescription(
                description="This room at Urban Boutique Hotel features a modern space with a comfortable queen bed, dedicated workspace, and spectacular city views. Amenities include complimentary high-speed WiFi, an in-room coffee maker, and access to our fitness center. Perfect for business travelers who need a productive and convenient city-center base."
            ).model_dump(),
            "comment": "Example of a business-oriented city room with focus on productivity"
        }
    ]

    # We populate the dataset of the function with these examples
    for example in examples:
        try:
            opper.datasets.create_entry(
                dataset_id=dataset_id,
                input=str(example["input"]),
                output=str(example["output"]),
                comment=str(example["comment"])
            )
        except Exception as e:
            print(f"Error adding example: {e}")
There are multiple ways of curating and adding these examples. You can do it in code like this, or in the trace view while inspecting a completion. If you have experts that you want to have manage the quality of the completions we recommend building a dedicated frontend for allowing experts to curate perfect outputs and save them using the above patterns

Completing a task

We can now try to complete a task and see that it follows the style of the examples. As we now have all the configuration and datasets for the task stores centrally, we can just call the task and have relevant examples be added automatically:

# Call the function with sample data
response = opper.functions.call(
    function_id=function.id,
    input=RoomDatabaseEntry(
        hotel_name="The Grand Hotel",
        room_count=3,
        view="ocean",
        beds=2,
        price_per_night=100,
        amenities=["wifi", "breakfast", "parking"]
    ).model_dump()
)

print(response)

We can see how this new task completes to the same style:

{
    'description': "This room at The Grand Hotel offers a spacious 3-room suite with 2 comfortable beds and breathtaking ocean views. Guests can enjoy modern amenities such as complimentary WiFi, a delicious breakfast to start the day, and convenient on-site parking. Perfect for families or couples seeking a relaxing seaside retreat at an affordable rate."
}

Inspecting examples and prompts

You can observe the usage of examples in the low level prompt to the model. Click on traces -> expand a trace -> expand a call span: