Integrating Asteroid SDK with OpenAI

In this guide, we’ll explore how to integrate the Asteroid SDK with the OpenAI SDK to build AI applications that are not only powerful but also supervised and controllable. Integration with Asteroid is possible with a few lines of code.

Introduction

This guide demonstrates how to use the Asteroid SDK with OpenAI, including how to create supervised tools, manage supervision contexts, and build assistant agents that interact with OpenAI’s GPT models.

Creating Supervised Tools

Supervised tools are functions wrapped with the @supervise decorator, allowing you to enforce policies, validations, and involve human supervisors when necessary.

The @supervise Decorator

The @supervise decorator adds supervision capabilities to your functions. Place the supervisor decorator above the tool definition that you provide to OpenAI.

from asteroid_sdk.supervision import supervise

@supervise(supervision_functions=[...])
def your_function(params):
    # Function implementation
    pass

Interacting with OpenAI Models

Use the OpenAI Chat Completion API with the Asteroid-wrapped client to interact with the model. Example code:

from openai import OpenAI
from asteroid_sdk.wrappers.openai import asteroid_openai_client, asteroid_init
from typing import List, Dict, Any

# Initialize OpenAI and Asteroid
client = OpenAI()
run_id = asteroid_init(project_name="AI Assistant")
wrapped_client = asteroid_openai_client(client, run_id)

def chat_with_openai(messages: List[Dict[str, Any]], tools: List[Dict[str, Any]]):
    response = wrapped_client.chat.completions.create(
        model="gpt-4",
        messages=messages,
        tools=tools,
        tool_choice="auto",
    )
    return response

This example shows how to:

  1. Initialize the OpenAI client with Asteroid supervision
  2. Use the wrapped client to make chat completion requests
  3. Pass tools (instead of functions) to the API

Example Application

Let’s put everything together in an example where an assistant searches the internet, creates calendar events, and sends emails, all under supervision. Full example code is implemented in examples/openai/run.ipynb with more details in the Quickstart guide example.

Conclusion

Integrating the Asteroid SDK with OpenAI models allows you to build AI applications with robust supervision capabilities. By defining supervised tools and managing the supervision context, you can ensure that your AI assistant operates within defined policies and involves human oversight when necessary.

Key takeaways:

  • Define supervisors to enforce policies or require human approval.
  • Use the @supervise decorator to add supervision to your functions.
  • Wrap your OpenAI client with asteroid_openai_client to register all supervised functions and enable supervision.