Introduction
In the rapidly evolving AI landscape, one of the most critical decisions startups face is whether to build with open or closed AI systems. At TechCrunch Disrupt 2026, Nvidia's Nader Khalil and Sydney Sykes highlighted this pivotal choice that's shaping the next generation of AI startups. This tutorial will guide you through creating a hybrid AI system that can seamlessly switch between open and closed AI approaches, demonstrating the practical implementation of both methodologies.
Prerequisites
- Python 3.8 or higher installed
- Basic understanding of machine learning concepts
- Installed libraries:
transformers,torch,openai,flask - Access to Hugging Face account for open models
- OpenAI API key (optional for closed AI approach)
Step-by-step instructions
Step 1: Setting up the Project Structure
We'll begin by creating a project structure that separates open and closed AI implementations. This modular approach allows for easy switching between approaches.
Creating the directory structure
mkdir ai_hybrid_system
mkdir ai_hybrid_system/open_ai
mkdir ai_hybrid_system/closed_ai
mkdir ai_hybrid_system/shared
Why: This structure provides clear separation between different AI approaches, making it easier to maintain and scale your system.
Step 2: Installing Required Dependencies
Creating requirements.txt
torch==2.1.0
transformers==4.36.0
openai==1.12.0
flask==2.3.2
python-dotenv==1.0.0
Installing dependencies
pip install -r requirements.txt
Why: These libraries provide the core functionality needed for both open and closed AI implementations. PyTorch is essential for model inference, transformers for Hugging Face models, and Flask for API creation.
Step 3: Implementing Open AI System
Creating open_ai/manager.py
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
from typing import Dict, Any
class OpenAIManager:
def __init__(self, model_name: str = "gpt2"):
self.model_name = model_name
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModelForCausalLM.from_pretrained(model_name)
self.pipeline = pipeline('text-generation', model=self.model, tokenizer=self.tokenizer)
def generate_text(self, prompt: str, max_length: int = 100) -> str:
try:
result = self.pipeline(prompt, max_length=max_length, num_return_sequences=1)
return result[0]['generated_text']
except Exception as e:
return f"Error in open AI generation: {str(e)}"
def get_model_info(self) -> Dict[str, Any]:
return {
'type': 'open',
'model_name': self.model_name,
'approach': 'hugging_face_pretrained'
}
Why: This implementation uses Hugging Face's transformers library to load and run open-source models locally, demonstrating how startups can leverage community models without external dependencies.
Step 4: Implementing Closed AI System
Creating closed_ai/manager.py
import openai
from typing import Dict, Any
class ClosedAIManager:
def __init__(self, api_key: str):
openai.api_key = api_key
def generate_text(self, prompt: str, max_length: int = 100) -> str:
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=max_length,
temperature=0.7
)
return response.choices[0].text.strip()
except Exception as e:
return f"Error in closed AI generation: {str(e)}"
def get_model_info(self) -> Dict[str, Any]:
return {
'type': 'closed',
'model_name': 'openai-davinci-003',
'approach': 'openai_api'
}
Why: This implementation connects to OpenAI's API, representing the closed AI approach that many startups use for production applications requiring high-quality outputs.
Step 5: Creating the Hybrid System Controller
Creating shared/hybrid_controller.py
from open_ai.manager import OpenAIManager
from closed_ai.manager import ClosedAIManager
from typing import Dict, Any
class HybridAIController:
def __init__(self, open_model_name: str = "gpt2", closed_api_key: str = None):
self.open_ai = OpenAIManager(open_model_name)
self.closed_ai = None
if closed_api_key:
self.closed_ai = ClosedAIManager(closed_api_key)
def generate_text(self, prompt: str, approach: str = "open", max_length: int = 100) -> Dict[str, Any]:
if approach == "open" and self.open_ai:
result = self.open_ai.generate_text(prompt, max_length)
info = self.open_ai.get_model_info()
elif approach == "closed" and self.closed_ai:
result = self.closed_ai.generate_text(prompt, max_length)
info = self.closed_ai.get_model_info()
else:
return {
'error': 'Invalid approach or AI manager not initialized',
'result': None
}
return {
'result': result,
'info': info
}
def switch_approach(self, new_approach: str, api_key: str = None) -> bool:
if new_approach == "closed" and api_key:
self.closed_ai = ClosedAIManager(api_key)
return True
elif new_approach == "open":
self.closed_ai = None
return True
return False
Why: This controller acts as the decision-making layer that allows you to switch between approaches dynamically, simulating how startups might choose their AI strategy based on their needs and constraints.
Step 6: Creating a Simple Web Interface
Creating app.py
from flask import Flask, request, jsonify
from shared.hybrid_controller import HybridAIController
import os
app = Flask(__name__)
# Initialize hybrid AI controller
controller = HybridAIController(
open_model_name="gpt2",
closed_api_key=os.getenv('OPENAI_API_KEY')
)
@app.route('/generate', methods=['POST'])
def generate_text():
data = request.get_json()
prompt = data.get('prompt', '')
approach = data.get('approach', 'open')
max_length = data.get('max_length', 100)
result = controller.generate_text(prompt, approach, max_length)
return jsonify(result)
@app.route('/switch', methods=['POST'])
def switch_approach():
data = request.get_json()
new_approach = data.get('approach', 'open')
api_key = data.get('api_key', None)
success = controller.switch_approach(new_approach, api_key)
return jsonify({'success': success})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
Why: This web interface demonstrates how a startup might expose their hybrid AI system to users, allowing them to choose between open and closed approaches through a simple API.
Step 7: Testing the System
Creating test.py
from shared.hybrid_controller import HybridAIController
# Initialize the hybrid system
controller = HybridAIController()
# Test open AI approach
print("Testing Open AI:")
result = controller.generate_text("The future of AI is", "open", 50)
print(f"Result: {result['result']}")
print(f"Info: {result['info']}")
# Test closed AI approach (if API key is available)
print("\nTesting Closed AI:")
result = controller.generate_text("The future of AI is", "closed", 50)
print(f"Result: {result['result']}")
print(f"Info: {result['info']}")
Why: Testing validates that both approaches work correctly and demonstrates the flexibility of the hybrid system.
Summary
This tutorial demonstrated how to build a hybrid AI system that can seamlessly switch between open and closed AI approaches. The implementation shows practical considerations that startups face when choosing their AI strategy, including local model deployment versus API-based services. By following these steps, you've created a system that can leverage the benefits of both approaches - the flexibility and control of open AI with the quality and support of closed AI.
The key takeaway is that the choice between open and closed AI isn't binary but can be strategic, allowing startups to adapt their approach based on specific use cases, cost considerations, and performance requirements. This hybrid approach represents the evolving nature of AI development in the startup ecosystem.