Function client SDKs contain a number of undocumented features, usually targeted towards maximizing performance
or improving the developer experience.
Monkey Patching in Python
Instead of calling the fxn.predictions.create
method, the Python SDK provides an @fxn.beta.predict
decorator
which monkey-patches an existing function to instead make a prediction:
Creating a Prediction
Apply the @fxn.beta.predict
decorator to a function. When the function is called, it will instead create a prediction
with the provided tag and return the results:
from fxn import Function
# π₯ Create a Function client
fxn = Function(...)
# π₯ Monkey-patch a function
@fxn.beta.predict(tag="@yusuf/add")
def add_numbers (a, b):
...
# π Call the function
print(add_numbers(9, 10))
# Output:
# 21
Streaming a Prediction
To instead stream the prediction, add an Iterator
or Generator
return type annotation to the function:
from fxn import Function
# π₯ Create a Function client
fxn = Function(...)
# π₯ Monkey-patch a function
@fxn.beta.predict(tag="@yusuf/split-sentence")
def split_sentence (sentence) -> Iterator[str]:
...
# π Call the function
for split in split_sentence("This is a long speech"):
print(split)
# Output:
# "This"
# "is"
# "a"
# "long"
# "speech"
Parsing Pydantic Models
The decorator also supports parsing output objects into Pydantic models by specifying a return type annotation:
from fxn import Function
from pydantic import BaseModel
from typing import Literal
class Pet (BaseModel):
sound: Literal["bark", "meow"]
legs: int
# π₯ Create a Function client
fxn = Function(...)
# π₯ Monkey-patch a function
@fxn.beta.predict(tag="@yusuf/pet-image-detector")
def detect_pet (image) -> Pet:
...
# π Call the function
print(repr(detect_pet(cat_image)))
# Output:
# Pet(sound="bark", legs=8)