Skip to content

Commit 2ec5d01

Browse files
authored
Update Documentation (#331)
* Update documentation * Add API reference for new readers * Add example to BigQueryReader * Add custom operator tutorial * Add example to DuckDBReader
1 parent 74d62f6 commit 2ec5d01

10 files changed

Lines changed: 129 additions & 5 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: uptrain.operators.BigQueryReader
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: uptrain.operators.DuckDBReader
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: uptrain.operators.ExcelReader

docs/api-reference/operators/code/SQL/ExecuteAndComparseSQL.md renamed to docs/api-reference/operators/code/SQL/ExecuteAndCompareSQL.md

File renamed without changes.

docs/key-components/operator.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ Each Operator implements two methods, which are to be run in order:
1616
Though operators can be used on their own, they are typically used in [Checks](/key-components/check), that make it easy to add [Charts](/key-components/chart) chain multiple operators together. Using a `Check`, you won't have to manually perform the `setup()` and `run()` methods.
1717

1818
To learn more about Operators, see the [Operator documentation](https://uptrain-ai.github.io/uptrain/operators/Accuracy/).
19+
20+
You can also create your own custom operators. To learn more, see the [Custom Operator tutorial](/tutorials/custom-operator).

docs/mint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@
7878
"pages": [
7979
"tutorials/prompt-experiments",
8080
"tutorials/validation",
81-
"tutorials/openai-evals"
81+
"tutorials/openai-evals",
82+
"tutorials/custom-operator"
8283
]
8384
},
8485
{

docs/tutorials/custom-operator.mdx

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
```

uptrain/operators/__init__.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ __all__ = [
5454
"JsonWriter",
5555
"DeltaReader",
5656
"DeltaWriter",
57-
"BigqueryReader",
57+
"BigQueryReader",
58+
"DuckDBReader",
5859
# code
5960
"code",
6061
"ParseCreateStatements",
@@ -104,7 +105,8 @@ from .language.generation import PromptGenerator, TextCompletion, OutputParser
104105
from . import io
105106
from .io.base import CsvReader, JsonReader, DeltaReader, JsonWriter, DeltaWriter
106107
from .io.excel import ExcelReader
107-
from .io.bq import BigqueryReader
108+
from .io.bq import BigQueryReader
109+
from .io.duck import DuckDBReader
108110

109111
from . import code
110112
from .code.sql import (

uptrain/operators/io/bq.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@register_op
18-
class BigqueryReader(TransformOp):
18+
class BigQueryReader(TransformOp):
1919
"""Read data from a bigquery table.
2020
2121
NOTE: To use this operator, you must include the GCP service account credentials in
@@ -24,6 +24,18 @@ class BigqueryReader(TransformOp):
2424
Attributes:
2525
query (str): Query to run against the BigQuery table.
2626
col_timestamp (str): Column name to use as the timestamp column. Only used in the context of monitoring.
27+
28+
Example:
29+
```python
30+
from uptrain.operators.io import BigQueryReader
31+
32+
query = "SELECT * FROM `bigquery-public-data.samples.shakespeare` LIMIT 10"
33+
reader = BigQueryReader(
34+
query=query,
35+
col_timestamp="timestamp"
36+
)
37+
output = reader.setup().run()["output"]
38+
```
2739
"""
2840

2941
query: str

uptrain/operators/io/duck.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,25 @@
1515

1616

1717
@register_op
18-
class DuckdbReader(TransformOp):
18+
class DuckDBReader(TransformOp):
1919
"""Read data from a Duckdb table.
2020
2121
Attributes:
2222
fpath (str): Path to the Duckdb file.
2323
query (str): Query to run against the duckdb database.
2424
col_timestamp (str): Column name to use as the timestamp column. Only used in the context of monitoring.
25+
26+
Example:
27+
```python
28+
from uptrain.operators.io import DuckDBReader
29+
30+
reader = DuckDBReader(
31+
fpath="data/duckdb.db",
32+
query="SELECT * FROM my_table",
33+
col_timestamp="timestamp",
34+
)
35+
output = reader.setup().run()["output"]
36+
```
2537
"""
2638

2739
fpath: str

0 commit comments

Comments
 (0)