|
| 1 | +--- |
| 2 | +title: Custom Operator |
| 3 | +description: Learn how to create your own custom operator |
| 4 | +--- |
| 5 | + |
| 6 | +## What are Custom Operators? |
| 7 | +UpTrain offers a wide range of built-in operators to help you get started with your training pipeline. However, you may want to create your own custom operator to perform a specific task. This tutorial will walk you through the process of creating a custom operator. |
| 8 | + |
| 9 | +There are a few things that need to be kept in mind while creating an operator: |
| 10 | +1. Operators have two methods: `setup` and `run` |
| 11 | + 1. `setup` is called once when the operator is initialized. This is where you can pass in any settings that you need to use in the `run` method. |
| 12 | + 2. `run` is called for each batch of data that is passed to the operator. This is where you will perform the actual operation on the data. It returns a dictionary with the key `output` and the value depends on the type of operator. Any extra information can be put in the `extra` key of the dictionary. |
| 13 | +2. There are two types of operators: `TransformOp` and `ColumnOp`. |
| 14 | + 1. `TransformOp` represents an operator that transforms the data into another form. |
| 15 | + - This is used for operations like filtering, cleaning, etc. |
| 16 | + - The value of the `output` key of the dictionary returned by the `run` method should be a polars.DataFrame or None. |
| 17 | + 2. `ColumnOp` represents an operator that adds a new column to the data. |
| 18 | + - This is used for operations like adding a new column, renaming a column, etc. |
| 19 | + - The value of the `output` key of the dictionary returned by the `run` method should set as the computed table. |
| 20 | +3. The operator should be registered using the `register_custom_op` decorator. |
| 21 | + |
| 22 | +## Examples |
| 23 | + |
| 24 | +#### Example 1: Cleanup Operator |
| 25 | + |
| 26 | +An Operator that goes through a list of messages and extracts the question, document title, document link, document text, and response from the messages. |
| 27 | +```python |
| 28 | +from uptrain.operators import TransformOp, register_custom_op |
| 29 | + |
| 30 | +@register_custom_op |
| 31 | +class Cleanup(TransformOp): |
| 32 | + def setup(self, settings): |
| 33 | + return self |
| 34 | + |
| 35 | + def run(self, dataset): |
| 36 | + import json |
| 37 | + import polars as pl |
| 38 | + |
| 39 | + table_cols = [ |
| 40 | + "question", |
| 41 | + "document_title", |
| 42 | + "document_link", |
| 43 | + "document_text", |
| 44 | + "response", |
| 45 | + ] |
| 46 | + out = [] |
| 47 | + for point in dataset.to_dicts(): |
| 48 | + messages = json.loads(point["messages"]) |
| 49 | + question = messages[0]["content"].split("The input is: '")[1].split("?")[0] |
| 50 | + name = ( |
| 51 | + messages[0]["content"] |
| 52 | + .split("technical documentation titled ")[1] |
| 53 | + .split(", found at")[0] |
| 54 | + ) |
| 55 | + link = messages[0]["content"].split("found at ")[1].split(". \n")[0] |
| 56 | + text = ( |
| 57 | + messages[0]["content"] |
| 58 | + .split("--- START: Document ---")[1] |
| 59 | + .split(name + "\n")[1] |
| 60 | + .split("\n\n--- END: Document")[0] |
| 61 | + ) |
| 62 | + response = messages[1]["content"][1:-1] |
| 63 | + |
| 64 | + new_row = dict(zip(table_cols, [question, name, link, text, response])) |
| 65 | + out.append(new_row) |
| 66 | + |
| 67 | + return {"output": pl.from_dicts(out)} |
| 68 | +``` |
| 69 | + |
| 70 | +#### Example 2: AddContext Operator |
| 71 | + |
| 72 | +An Operator that adds the model and pipeline name to the data. |
| 73 | +```python |
| 74 | +from uptrain.operators import TransformOp, register_custom_op |
| 75 | + |
| 76 | +@register_custom_op |
| 77 | +class AddContext(TransformOp): |
| 78 | + def setup(self, settings): |
| 79 | + return self |
| 80 | + |
| 81 | + def run(self, dataset): |
| 82 | + import polars as pl |
| 83 | + |
| 84 | + return { |
| 85 | + "output": dataset.with_columns( |
| 86 | + [ |
| 87 | + pl.lit("gpt-4").alias("model"), |
| 88 | + pl.lit("context_retrieval").alias("pipeline"), |
| 89 | + ] |
| 90 | + ) |
| 91 | + } |
| 92 | +``` |
0 commit comments