Back to Get Started

OpenAI Function Integration

Use GenVR models with OpenAI function calling

What is OpenAI Function Calling?

OpenAI's function calling allows GPT models to call external APIs. GenVR provides pre-built function schemas for all models, making it easy to integrate generative AI into your OpenAI agents.

Quick Start

1

Download Function Schema

Browse the model library, select a model, and click "Download OpenAI" to get the function schema.

2

Set Environment Variables

Configure your GenVR credentials:

export GENVR_TEST_UID="your_user_id"
export GENVR_API_TOKEN="your_access_token"
export OPENAI_API_KEY="your_openai_key"
3

Use with OpenAI

Import and use the function with OpenAI SDK:

from openai import OpenAI
from flux_dev_openai_function import FUNCTION_DEFINITION, execute_generate_imagegen_flux_dev

client = OpenAI(api_key="YOUR_OPENAI_API_KEY")

# Chat with function calling enabled
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "user", "content": "Generate an image of a sunset over mountains"}
    ],
    tools=[FUNCTION_DEFINITION],
    tool_choice="auto"
)

# Handle function calls
if response.choices[0].message.tool_calls:
    for tool_call in response.choices[0].message.tool_calls:
        import json
        arguments = json.loads(tool_call.function.arguments)
        
        # Execute the GenVR function
        result = execute_generate_imagegen_flux_dev(arguments)
        
        if result.get("success"):
            print(f"Generated image: {result['content']}")
        else:
            print(f"Error: {result['error']}")

Features

Auto-Generated Schemas

Function schemas are automatically generated from each model's parameter definitions. Always up-to-date with the latest model specifications.

Complete Implementation

Each download includes both the schema definition and a complete execution function that handles the GenVR API workflow.

Type Safety

Proper parameter types, constraints, and descriptions ensure OpenAI generates valid function calls with correct argument types.

Error Handling

Built-in error handling for network issues, timeouts, and generation failures. Returns clear error messages to your agent.

Best Practices

  • Environment Variables: Store credentials in environment variables, never hardcode them in your code.
  • Timeout Handling: The default timeout is 2 minutes (60 polls). Adjust based on your model's typical generation time.
  • Error Messages: Return descriptive error messages to OpenAI so it can inform users or retry appropriately.
  • Multi-Model Agents: You can include multiple GenVR functions in your tools array for multi-modal agents.

Example Use Cases

Creative Assistant Chatbot

Build a chatbot that can generate images, videos, or other content based on natural language requests.

Automated Content Pipeline

Create agents that automatically generate content for social media, marketing, or product demos.

Multi-Step Workflows

Chain multiple GenVR models together - generate an image, then animate it, then add effects.