Introduction
In this tutorial, we'll explore how to implement a hybrid compute system similar to Perplexity's new Mac release. Hybrid compute combines cloud-based agentic assistants with local models to process sensitive data securely. You'll learn to build a basic framework that orchestrates tasks between cloud agents and local models, with a focus on privacy-preserving data handling.
Prerequisites
- Python 3.8+
- Basic understanding of machine learning concepts
- Experience with REST APIs and HTTP requests
- Access to a local LLM (like Llama 3 or Mistral)
- Basic knowledge of Docker for containerization
Step-by-Step Instructions
1. Set Up Your Development Environment
First, create a virtual environment and install the necessary packages:
python -m venv hybrid_compute_env
source hybrid_compute_env/bin/activate # On Windows: hybrid_compute_env\Scripts\activate
pip install fastapi uvicorn openai transformers torch requests
This creates an isolated environment for our hybrid compute system, ensuring dependencies don't conflict with other projects.
2. Create the Local Model Interface
Build a local model wrapper that handles sensitive data processing:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
class LocalModel:
def __init__(self, model_path="mistralai/Mistral-7B-v0.1"):
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
self.model = AutoModelForCausalLM.from_pretrained(model_path)
def process_sensitive_data(self, prompt):
inputs = self.tokenizer(prompt, return_tensors="pt")
outputs = self.model.generate(**inputs, max_new_tokens=100)
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
This class wraps a local LLM to process sensitive data that shouldn't leave the device, forming the foundation of your hybrid system.
3. Implement Cloud Agent Communication
Create a cloud agent handler that manages external API calls:
import requests
class CloudAgent:
def __init__(self, api_key, base_url="https://api.perplexity.ai"):
self.api_key = api_key
self.base_url = base_url
def query_cloud(self, prompt):
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"query": prompt,
"model": "llama-3-70b-instruct"
}
response = requests.post(f"{self.base_url}/chat/completions",
headers=headers,
json=data)
return response.json()["choices"][0]["message"]["content"]
This component handles communication with cloud-based models, which are better suited for general knowledge tasks.
4. Build the Hybrid Task Orchestrator
Develop the core logic that decides where to send tasks:
class HybridOrchestrator:
def __init__(self, local_model, cloud_agent):
self.local_model = local_model
self.cloud_agent = cloud_agent
def process_task(self, task):
# Determine if task requires local processing
if self.requires_local_processing(task):
return self.local_model.process_sensitive_data(task)
else:
return self.cloud_agent.query_cloud(task)
def requires_local_processing(self, task):
# Simple rule-based logic for demonstration
sensitive_keywords = ["document", "record", "client", "confidential"]
return any(keyword in task.lower() for keyword in sensitive_keywords)
This orchestrator makes decisions about where to route tasks based on content sensitivity, mimicking Perplexity's gated-on-device approach.
5. Create a REST API Endpoint
Build a FastAPI application to expose your hybrid system:
from fastapi import FastAPI
app = FastAPI()
local_model = LocalModel()
cloud_agent = CloudAgent("your_api_key")
orchestrator = HybridOrchestrator(local_model, cloud_agent)
@app.post("/hybrid-query")
async def hybrid_query(query: str):
result = orchestrator.process_task(query)
return {"response": result}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
This API allows external applications to interact with your hybrid compute system, demonstrating how it could be integrated into real applications.
6. Test Your Hybrid System
Run your application and test with different types of queries:
uvicorn main:app --reload
# Test sensitive query
curl -X POST "http://localhost:8000/hybrid-query" \
-H "Content-Type: application/json" \
-d '{"query": "Analyze this confidential client document"}'
# Test general query
curl -X POST "http://localhost:8000/hybrid-query" \
-H "Content-Type: application/json" \
-d '{"query": "Explain quantum computing in simple terms"}'
The system should route sensitive queries to your local model while sending general queries to the cloud agent.
Summary
This tutorial demonstrated how to build a hybrid compute system that combines local and cloud-based AI models. By implementing a task orchestrator that routes queries based on content sensitivity, you've created a privacy-preserving system similar to Perplexity's new Mac release. The architecture ensures sensitive data remains local while leveraging cloud capabilities for general knowledge tasks. This approach addresses the core challenge of agentic assistants: balancing utility with data privacy. You can extend this system by adding more sophisticated routing logic, caching mechanisms, or additional local model types.



