# How to Use a Custom LLM with MoneyPrinterTurbo: Complete Integration Guide

> Integrate your custom LLM with MoneyPrinterTurbo using its provider abstraction layer. Learn how to connect any OpenAI-compatible API or build a custom client with this complete guide.

- Repository: [Harry/MoneyPrinterTurbo](https://github.com/harry0703/MoneyPrinterTurbo)
- Tags: how-to-guide
- Published: 2026-03-23

---

**Yes, MoneyPrinterTurbo supports custom LLMs through its provider abstraction layer in [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py), allowing you to integrate any OpenAI-compatible API or build a custom client by extending the configuration and dispatch logic.**

MoneyPrinterTurbo is an open-source automated video generation framework that abstracts Large Language Model access behind a configurable provider system. While it ships with native support for providers like OpenAI, Azure, and Ollama, the modular architecture in the `harry0703/MoneyPrinterTurbo` repository allows you to wire any custom LLM endpoint into the video generation pipeline. This integration requires modifying only two files: the configuration and the service dispatcher.

## How MoneyPrinterTurbo Handles LLM Providers

The LLM abstraction resides in [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py), where the internal helper `_generate_response` routes requests based on the `llm_provider` configuration value. At runtime, the system reads the provider setting from [`config.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.toml):

```python
llm_provider = config.app.get("llm_provider", "openai")  # app/services/llm.py L20-L23

```

Based on this value, the module instantiates the appropriate client. The codebase currently supports the following provider patterns:

- **g4f**: Uses `g4f.ChatCompletion.create` for free-model gateway access (L22-L30)
- **OpenAI-compatible APIs**: Including OpenAI, Azure, Moonshot, DeepSeek, Gemini, and others using standard `OpenAI` client or direct `requests` calls (L31-L84, L92-L132, L138-L171)
- **Custom providers**: Any service you define by adding a new `elif` block following the existing pattern

All credentials and endpoint settings are externalized to [`config.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.toml), with templates available in [`config.example.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.example.toml#L21-L34).

## Step-by-Step Custom LLM Integration

### 1. Configure Your Provider in config.toml

Add your custom provider identifier and credentials to the configuration file. Create entries following the existing naming convention:

```toml
llm_provider = "myprovider"
myprovider_api_key = "your-api-key-here"
myprovider_base_url = "https://api.myprovider.com/v1"
myprovider_model_name = "custom-model-v1"

```

The application loads these values via [`app/config/config.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/config/config.py) and makes them available to the service layer at runtime.

### 2. Extend the LLM Service Dispatcher

Open [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py) and locate the provider dispatch logic. Insert a new `elif` block following the pattern used for existing providers (around L31-L84):

```python
elif llm_provider == "myprovider":
    api_key = config.app.get("myprovider_api_key")
    model_name = config.app.get("myprovider_model_name")
    base_url = config.app.get("myprovider_base_url")
    
    client = OpenAI(api_key=api_key, base_url=base_url)
    
    response = client.chat.completions.create(
        model=model_name,
        messages=messages,
        **params
    )
    return response.choices[0].message.content

```

This pattern aligns with the existing implementation for OpenAI-compatible endpoints, ensuring consistency in error handling and response processing.

### 3. Adapt Response Parsing (Optional)

If your custom LLM returns a non-standard JSON structure, modify the parsing logic where the code extracts the completion text. The default pattern expects `response.choices[0].message.content`. Adjust this extraction in your custom block if your API returns different field names or wrapper objects.

## Code Examples for Custom LLM Integration

### Direct Python Usage

Once configured, invoke the LLM functions directly from your Python scripts:

```python
from app.services import llm

# Ensure config.toml points to your custom provider

script = llm.generate_script(
    video_subject="Quantum Computing Applications",
    language="en",
    paragraph_number=3
)
print("Generated script:", script)

terms = llm.generate_terms(
    video_subject="Quantum Computing Applications",
    video_script=script,
    amount=5
)
print("Search terms:", terms)

```

This works when the project root is in your `PYTHONPATH`, which is automatically handled when running `python main.py`.

### HTTP API Requests

The FastAPI controllers in [`app/controllers/v1/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/controllers/v1/llm.py) expose endpoints that automatically use your configured provider. Generate a script via HTTP:

```bash
curl -X POST http://localhost:8080/v1/llm/scripts \
  -H "Content-Type: application/json" \
  -d '{
        "video_subject": "Quantum Computing Applications",
        "video_language": "en",
        "paragraph_number": 3
      }'

```

Expected response:

```json
{
  "code": 200,
  "msg": "success",
  "data": {
    "video_script": "Quantum computing represents a paradigm shift..."
  }
}

```

To generate search terms, POST to `/v1/llm/terms` with the `video_script` included in the payload (L33-L45 in the controller).

## Key Files and Architecture

| File | Purpose | Relevant Lines |
|------|---------|----------------|
| [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py) | Core LLM provider dispatch and response handling | L20-L23 (config reading), L31-L84 (provider logic) |
| [`app/config/config.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/config/config.py) | Configuration loader for [`config.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.toml) | Entire file |
| [`config.example.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.example.toml) | Template showing supported providers and required keys | L21-L34 |
| [`app/controllers/v1/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/controllers/v1/llm.py) | FastAPI endpoints for script and term generation | L18-L30 (`/scripts`), L33-L45 (`/terms`) |

## Summary

- **MoneyPrinterTurbo uses a provider abstraction** in [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py) that reads the `llm_provider` value from [`config.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.toml) at runtime.
- **Adding a custom LLM requires two changes**: extend [`config.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.toml) with your endpoint credentials and add a dispatch block in [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py).
- **OpenAI-compatible APIs** integrate easily using the existing `OpenAI` client pattern with custom `base_url` parameters.
- **FastAPI endpoints** (`/v1/llm/scripts` and `/v1/llm/terms`) automatically route to your custom provider once configured.
- **Direct Python imports** from `app.services.llm` allow scripting without HTTP overhead.

## Frequently Asked Questions

### Does MoneyPrinterTurbo support local LLMs like Ollama?

Yes, Ollama is supported natively. Set `llm_provider = "ollama"` in [`config.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.toml) and configure `ollama_base_url` pointing to your local instance (typically `http://localhost:11434/v1`). The implementation follows the same OpenAI-compatible client pattern used for other custom providers.

### What format should my custom LLM API endpoint return?

Your endpoint should return a JSON object compatible with the OpenAI chat completions schema. Specifically, the code expects to extract text from `response.choices[0].message.content`. If your API returns different field names, modify the extraction logic in your custom `elif` block within [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py).

### Do I need to modify the FastAPI controllers to use a custom LLM?

No. The controllers in [`app/controllers/v1/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/controllers/v1/llm.py) call `llm.generate_script()` and `llm.generate_terms()` without knowledge of the specific provider. Once you add your custom logic to [`app/services/llm.py`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/app/services/llm.py) and update [`config.toml`](https://github.com/harry0703/MoneyPrinterTurbo/blob/main/config.toml), the REST endpoints automatically use your new provider.

### Can I use the G4F free provider instead of a custom API?

Yes, set `llm_provider = "g4f"` to use the free-model gateway. This requires no API key and uses the `g4f.ChatCompletion.create` interface (L22-L30). However, for production stability or specific model requirements, a custom provider with your own API keys is recommended.