MODULE 08 | CAPSTONE

Connect Local AI to
Enterprise Databases

Build AI-powered data summarization and analysis using a locally-hosted LLM, DreamFactory's AI Data Gateway, and your enterprise database, all without exposing your data directly to AI.

~8 min read
Intermediate
Hands-on Project
Full Step-by-Step Documentation ~20 min

🎯 What You'll Build

By the end of this module, you'll have a working API endpoint that:

  • Accepts a record identifier as input (customer ID, order number, ticket ID, etc.)
  • Retrieves data from your database via a secure stored procedure
  • Sends the data to a locally-hosted LLM (like Llama 3 via Ollama)
  • Returns an AI-generated summary, analysis, or recommendation

The key insight: your AI never touches the database directly. DreamFactory's AI Data Gateway acts as the secure intermediary, enforcing authentication, rate limiting, and audit logging on every request.

💡 Example Use Cases

This pattern applies to countless business scenarios:

👤

Customer Insights

Summarize customer history, purchase patterns, or support interactions

📦

Order Analysis

Analyze order trends, detect anomalies, or generate fulfillment recommendations

🎫

Support Tickets

Auto-categorize tickets, suggest solutions, or prioritize based on sentiment

📊

Financial Reports

Generate narrative summaries of financial data or variance explanations

🏥

Patient Records

Summarize medical histories or flag potential concerns (HIPAA-compliant)

📝

Document Review

Analyze contracts, extract key terms, or compare document versions

🏗️ Architecture Overview

This integration uses three DreamFactory services working together:

🤖
Local LLM (Ollama / LM Studio)
Runs inference locally. No data leaves your network
🔌
HTTP/RWS Service
Proxies requests to LLM with proper timeouts
⚙️
Python Scripted Service
Orchestrates the workflow: fetch data → build prompt → call AI
🗄️
Database Service (SQL Server, MySQL, PostgreSQL...)
Exposes stored procedures as secure REST endpoints

📋 Prerequisites

  • DreamFactory 7.x instance (self-hosted or cloud)
  • Local LLM running via Ollama, LM Studio, or similar
  • Database with data to analyze (SQL Server, MySQL, PostgreSQL, Oracle, etc.)
  • API Key configured in DreamFactory with appropriate role
  • Basic familiarity with Python and REST APIs

🔧 Implementation Steps

1

Create a Database Stored Procedure

Create a stored procedure that accepts an identifier and returns the relevant data. This encapsulates your business logic and prevents AI from running arbitrary queries. Example: GetCustomerSummaryData, GetOrderDetails, or GetTicketHistory.

2

Configure Database Service in DreamFactory

Add your database as a service. DreamFactory will auto-generate REST endpoints for your tables and stored procedures. No code required.

3

Set Up HTTP Service for LLM

Create an HTTP/RWS service pointing to your local LLM's endpoint (e.g., http://localhost:11434/api/chat for Ollama). Set the timeout to 300 seconds because LLMs can take minutes to respond.

4

Build the Python Orchestration Script

Create a scripted service that ties everything together: fetch data from the database, construct an AI prompt, call the LLM, and return the result.

5

Test the Integration

Call your new endpoint with a record identifier and watch the magic happen. The API returns a clean JSON response with the AI-generated analysis.

💻 Key Code: Python Orchestration

Here's the core logic of the scripted service that orchestrates the entire flow:

Python
import json, urllib.request

def get_ai_summary(record_id, api_key, base_url):
    # Step 1: Fetch data from database via stored procedure
    url = f"{base_url}/api/v2/db/_proc/GetRecordData"
    url += f"?RecordId={record_id}"

    req = urllib.request.Request(url, method='GET')
    req.add_header('X-DreamFactory-Api-Key', api_key)

    with urllib.request.urlopen(req, timeout=120) as response:
        record_data = json.loads(response.read().decode())

    # Step 2: Build the AI prompt (customize for your use case)
    prompt = f"""Analyze this data and provide a concise summary
with key insights and recommendations:

{json.dumps(record_data, indent=2)}"""

    # Step 3: Call the local LLM
    llm_url = f"{base_url}/api/v2/ollama/api/chat"
    payload = {
        "model": "llama3:70b",
        "messages": [{"role": "user", "content": prompt}]
    }

    req = urllib.request.Request(llm_url,
        data=json.dumps(payload).encode(),
        headers={'Content-Type': 'application/json',
                 'X-DreamFactory-Api-Key': api_key})

    with urllib.request.urlopen(req, timeout=300) as response:
        result = json.loads(response.read().decode())

    return result['message']['content']

⚠️ Common Pitfalls

⏱️

Timeout Configuration

LLM inference can take 2-3 minutes for larger models. Set your HTTP service timeout to 300 seconds (5 minutes) to avoid premature failures. The default 30-second timeout will cause most requests to fail.

🔄

Worker Pool Deadlock

If your Python script calls another DreamFactory endpoint internally, you may hit a deadlock where all PHP-FPM workers are waiting on each other. Solution: use Python's urllib directly with explicit timeouts instead of the internal API mechanism.

💡

Why Stored Procedures?

Never let AI generate raw SQL. Stored procedures create a security contract: the AI can only access pre-defined, audited queries. This is essential for compliance (HIPAA, GDPR, FedRAMP) and prevents prompt injection attacks.

📊 Real-World Performance

In production environments, enterprise teams are seeing these response times:

  • 4-16 seconds: Cloud model (GPT-4o mini) end-to-end response
  • 25-32 seconds: Local 14B parameter model on NVIDIA DGX
  • Under 3 minutes: Hot-swapping between completely different models
💡

Smaller Can Be Better

One enterprise team discovered their 14 billion parameter model actually outperformed their 120 billion parameter model for summarization tasks. The smaller model focused on language and context without overthinking. Always benchmark your specific use case, as bigger isn't always better, and smaller models mean faster responses.

🔀

Hybrid Cloud Strategy

Many enterprises run sensitive workloads on local hardware (like NVIDIA DGX) while routing less sensitive queries to cloud APIs. DreamFactory's scripted services make switching between backends a single config change, with no application code rewrites required.

🚀 Next Steps

  • Read the full blog post for deeper technical details and troubleshooting
  • Explore Module 04 to learn why stored procedures are essential for AI data access
  • Try connecting your own database and LLM to build a custom AI-powered API
📖

See Also

The Hidden Cost of Building Your Own LLM Data Layer. Building your own LLM data layer costs $1.5M+ and takes 12-18 months. One Fortune 500 integrated 50 data sources in 2 weeks with DreamFactory.

Ready to implement? Get the complete step-by-step guide:

Full Step-by-Step Documentation ~20 min

🎓 Course Complete!

Congratulations! You've completed the DreamFactory AI Academy! You now have the knowledge to connect any AI agent to your enterprise data with full security, RBAC, identity passthrough, and audit logging.

Book an Architecture Session