# Configuring Your AI Matrix

> Setting up the multi-provider environment, from environment variables to LLM-specific JSON configurations for generation and repository analysis.

- Repository: AsyncFuncAI/deepwiki-open
- GitHub: https://github.com/AsyncFuncAI/deepwiki-open
- Human wiki: https://grok-wiki.com/public/wiki/asyncfuncai-deepwiki-open-4d1f22320747
- Complete Markdown: https://grok-wiki.com/public/wiki/asyncfuncai-deepwiki-open-4d1f22320747/llms-full.txt

## Source Files

- `api/config.py`
- `api/config/generator.json`
- `api/config/repo.json`
- `next.config.ts`
- `package.json`
- `Dockerfile`

---

<details>
<summary>Relevant source files</summary>
The following files were used as context for generating this wiki page:
- [api/config.py](api/config.py)
- [api/config/generator.json](api/config/generator.json)
- [api/config/repo.json](api/config/repo.json)
- [api/config/embedder.json](api/config/embedder.json)
- [api/main.py](api/main.py)
- [Dockerfile](Dockerfile)
</details>

# Configuring Your AI Matrix

The DeepWiki AI Matrix is a modular configuration framework designed to support a multi-provider LLM environment. It enables the system to switch seamlessly between different AI providers for text generation, code analysis, and vector embeddings, while maintaining a consistent internal interface.

This configuration is primarily managed through a combination of environment variables and structured JSON files located in the `api/config/` directory. The system is built to be provider-neutral, allowing developers to bring their own keys (BYOK) and customize the behavior of each AI agent.

## Environment Variables

DeepWiki requires several environment variables to authenticate with AI providers and manage its runtime behavior. These variables are loaded at startup and can be provided via a `.env` file or directly in the shell environment.

| Variable | Description | Default |
|----------|-------------|---------|
| `GOOGLE_API_KEY` | API key for Google Gemini models (Required) | None |
| `OPENAI_API_KEY` | API key for OpenAI GPT models (Required) | None |
| `OPENROUTER_API_KEY` | API key for OpenRouter models | None |
| `DEEPWIKI_EMBEDDER_TYPE` | Type of embedder to use (`openai`, `google`, `ollama`, `bedrock`) | `openai` |
| `DEEPWIKI_CONFIG_DIR` | Custom directory for JSON configuration files | `api/config/` |
| `PORT` | The port for the backend API server | `8001` |

Sources: [api/config.py:19-26](api/config.py#L19-L26), [api/main.py:47-51](api/main.py#L47-L51), [Dockerfile:120-130](Dockerfile#L120-L130)

## JSON Configuration Matrix

The core logic of the AI Matrix resides in three primary JSON files. These files define the providers, models, and hyperparameters used across the application.

### 1. Text Generation (`generator.json`)
Defines the LLM providers and their specific models used for wiki generation and chat. It supports a wide range of providers including Dashscope, Google, OpenAI, OpenRouter, Ollama, Bedrock, and Azure.

- **Default Provider**: Set via `default_provider`.
- **Model Parameters**: Each model can have custom `temperature`, `top_p`, and `top_k` settings.

Sources: [api/config/generator.json:1-100](api/config/generator.json#L1-L100)

### 2. Embedding & Retrieval (`embedder.json`)
Configures the vectorization process for repository analysis. It supports multiple embedding engines and defines how text is split before indexing.

- **Client Classes**: Maps providers to their respective client implementations (e.g., `OpenAIClient`, `OllamaClient`).
- **Text Splitter**: Controls `chunk_size` and `chunk_overlap` for RAG optimization.

Sources: [api/config/embedder.json:1-40](api/config/embedder.json#L1-L40)

### 3. Repository Analysis (`repo.json`)
Manages the scope of the AI's "vision" by defining file filters and repository limits.

- **Excluded Directories**: Automatically ignores common build artifacts (`node_modules`, `.venv`, `.git`).
- **File Filters**: Excludes lock files and binary blobs to minimize token usage.

Sources: [api/config/repo.json:1-50](api/config/repo.json#L1-L50)

## Dynamic Configuration & Placeholders

The AI Matrix supports dynamic values within the JSON files using the `${ENV_VAR}` syntax. This allows you to keep sensitive keys out of the configuration files while maintaining flexibility.

```json
{
  "api_key": "${MY_CUSTOM_SECRET}"
}
```

The `replace_env_placeholders` function recursively traverses the configuration objects at runtime to inject the appropriate environment values.

Sources: [api/config.py:69-97](api/config.py#L69-L97)

## Configuration Architecture

The following diagram illustrates how configuration flows from environment sources into the DeepWiki runtime:

```mermaid
flowchart TD
    Env[Environment Variables/.env] --> ConfigPy[api/config.py]
    JsonFiles[JSON Config Files: generator, embedder, repo] --> ConfigPy
    ConfigPy --> Placeholders[Placeholder Replacement]
    Placeholders --> ClientInit[Client & Provider Initialization]
    
    subgraph "api/config/"
        JsonFiles
    end
    
    subgraph "Core Logic"
        ConfigPy
        Placeholders
    end
    
    ClientInit --> LLM[LLM Generation]
    ClientInit --> RAG[Vector Search/RAG]
```

## Summary

DeepWiki's AI Matrix provides a robust and extensible foundation for multi-provider AI integration. By separating model metadata into JSON files and sensitive credentials into environment variables, it ensures a secure and portable development environment.

Sources: [api/config.py:331-357](api/config.py#L331-L357)
