🚧 Documentation is a work in progress, thank you for your patience! 🚧

Create Your First Function

Create a Basic Function

In this guide, we’ll walk you through creating your first function using Func Runner. This example will demonstrate how to define a function that returns a list of purchases. Our examples will be simple, but close your eyes and imagine that these functions can do just about anything you want — including interacting with a database or sending an email!

Step 1: Define Your Function

Start by defining your function using the @app.activity_trigger decorator, which is a binding specific to Azure Durable Functions. When you go to name your function, remember that you will be using this same function name in your OpenAI Assistant function definitions. Consistency will be important when we go forward to step two!

To learn more about activity bindings, visit the Bindings for Durable Functions - Azure documentation. For the purposes of this guide, all you need to know is that the Func Runner system passes your OpenAI Assistant ‘tool call’ parameters through the parameters argument as a Python dictionary.

# function_app.py

import json

import azure.durable_functions as df

from func_runner_blueprint import func_runner_blueprint
from openai_proxy_blueprint import openai_proxy_blueprint

app = df.DFApp()
app.register_blueprint(openai_proxy_blueprint)
app.register_blueprint(func_runner_blueprint)

@app.activity_trigger(input_name="parameters")
def get_purchases(parameters: dict):
  return json.dumps([
    {"product": "Ballcap", "price": 24.59, "customer_id": 1},
    {"product": "Football Helmet", "price": 73.23, "customer_id": 2},
    {"product": "Hockey Pucks (3 pack)", "price": 19.73, "customer_id": 3},
  ])

Important Note - Always return a string.

OpenAI Assistants expect your function to always return a string value. You will see in our example above that we are returning a JSON formatted string of purchases. So make sure you cast your return value to a string.

Step 2: Create the Matching OpenAI Assistant Function Definition

Your OpenAI Assistant needs to know what functions are available to it, what they are for and what types of parameters your function can accept. To do this navigate to the Assistants - OpenAI API page and select the assistant you plan on using this function with.

Click on + Functions inside the OpenAI Assistant blade and paste the following JSON object.

{
  "name": "get_purchases",
  "description": "This function is used to list all the purchases.",
  "strict": false,
  "parameters": {
    "type": "object",
    "properties": {},
    "required": []
  }
}

Press Save to attach the ‘get_purchases’ function definition to your assistant.

Important Note - Function names should match.

It is important that the name of your Python function is identical to the name value of OpenAI Assistant function definition. If for any reason these do not match, then your function will never be called and could result in unhandled exceptions in your workflow.