|
| 1 | +# External Providers |
| 2 | + |
| 3 | +Llama Stack supports external providers that live outside of the main codebase. This allows you to: |
| 4 | +- Create and maintain your own providers independently |
| 5 | +- Share providers with others without contributing to the main codebase |
| 6 | +- Keep provider-specific code separate from the core Llama Stack code |
| 7 | + |
| 8 | +## Configuration |
| 9 | + |
| 10 | +To enable external providers, you need to configure the `external_providers_dir` in your Llama Stack configuration. This directory should contain your external provider specifications: |
| 11 | + |
| 12 | +```yaml |
| 13 | +external_providers_dir: /etc/llama-stack/providers.d/ |
| 14 | +``` |
| 15 | +
|
| 16 | +## Directory Structure |
| 17 | +
|
| 18 | +The external providers directory should follow this structure: |
| 19 | +
|
| 20 | +``` |
| 21 | +providers.d/ |
| 22 | + remote/ |
| 23 | + inference/ |
| 24 | + custom_ollama.yaml |
| 25 | + vllm.yaml |
| 26 | + vector_io/ |
| 27 | + qdrant.yaml |
| 28 | + safety/ |
| 29 | + llama-guard.yaml |
| 30 | + inline/ |
| 31 | + inference/ |
| 32 | + custom_ollama.yaml |
| 33 | + vllm.yaml |
| 34 | + vector_io/ |
| 35 | + qdrant.yaml |
| 36 | + safety/ |
| 37 | + llama-guard.yaml |
| 38 | +``` |
| 39 | + |
| 40 | +Each YAML file in these directories defines a provider specification for that particular API. |
| 41 | + |
| 42 | +## Provider Types |
| 43 | + |
| 44 | +Llama Stack supports two types of external providers: |
| 45 | + |
| 46 | +1. **Remote Providers**: Providers that communicate with external services (e.g., cloud APIs) |
| 47 | +2. **Inline Providers**: Providers that run locally within the Llama Stack process |
| 48 | + |
| 49 | +### Remote Provider Specification |
| 50 | + |
| 51 | +Remote providers are used when you need to communicate with external services. Here's an example for a custom Ollama provider: |
| 52 | + |
| 53 | +```yaml |
| 54 | +adapter: |
| 55 | + adapter_type: custom_ollama |
| 56 | + pip_packages: |
| 57 | + - ollama |
| 58 | + - aiohttp |
| 59 | + config_class: llama_stack_ollama_provider.config.OllamaImplConfig |
| 60 | + module: llama_stack_ollama_provider |
| 61 | +api_dependencies: [] |
| 62 | +optional_api_dependencies: [] |
| 63 | +``` |
| 64 | +
|
| 65 | +#### Adapter Configuration |
| 66 | +
|
| 67 | +The `adapter` section defines how to load and configure the provider: |
| 68 | + |
| 69 | +- `adapter_type`: A unique identifier for this adapter |
| 70 | +- `pip_packages`: List of Python packages required by the provider |
| 71 | +- `config_class`: The full path to the configuration class |
| 72 | +- `module`: The Python module containing the provider implementation |
| 73 | + |
| 74 | +### Inline Provider Specification |
| 75 | + |
| 76 | +Inline providers run locally within the Llama Stack process. Here's an example for a custom vector store provider: |
| 77 | + |
| 78 | +```yaml |
| 79 | +module: llama_stack_vector_provider |
| 80 | +config_class: llama_stack_vector_provider.config.VectorStoreConfig |
| 81 | +pip_packages: |
| 82 | + - faiss-cpu |
| 83 | + - numpy |
| 84 | +api_dependencies: |
| 85 | + - inference |
| 86 | +optional_api_dependencies: |
| 87 | + - vector_io |
| 88 | +provider_data_validator: llama_stack_vector_provider.validator.VectorStoreValidator |
| 89 | +container_image: custom-vector-store:latest # optional |
| 90 | +``` |
| 91 | + |
| 92 | +#### Inline Provider Fields |
| 93 | + |
| 94 | +- `module`: The Python module containing the provider implementation |
| 95 | +- `config_class`: The full path to the configuration class |
| 96 | +- `pip_packages`: List of Python packages required by the provider |
| 97 | +- `api_dependencies`: List of Llama Stack APIs that this provider depends on |
| 98 | +- `optional_api_dependencies`: List of optional Llama Stack APIs that this provider can use |
| 99 | +- `provider_data_validator`: Optional validator for provider data |
| 100 | +- `container_image`: Optional container image to use instead of pip packages |
| 101 | + |
| 102 | +## Required Implementation |
| 103 | + |
| 104 | +### Remote Providers |
| 105 | + |
| 106 | +Remote providers must expose a `get_adapter_impl()` function in their module that takes two arguments: |
| 107 | +1. `config`: An instance of the provider's config class |
| 108 | +2. `deps`: A dictionary of API dependencies |
| 109 | + |
| 110 | +This function must return an instance of the provider's adapter class that implements the required protocol for the API. |
| 111 | + |
| 112 | +Example: |
| 113 | +```python |
| 114 | +async def get_adapter_impl( |
| 115 | + config: OllamaImplConfig, deps: Dict[Api, Any] |
| 116 | +) -> OllamaInferenceAdapter: |
| 117 | + return OllamaInferenceAdapter(config) |
| 118 | +``` |
| 119 | + |
| 120 | +### Inline Providers |
| 121 | + |
| 122 | +Inline providers must expose a `get_provider_impl()` function in their module that takes two arguments: |
| 123 | +1. `config`: An instance of the provider's config class |
| 124 | +2. `deps`: A dictionary of API dependencies |
| 125 | + |
| 126 | +Example: |
| 127 | +```python |
| 128 | +async def get_provider_impl( |
| 129 | + config: VectorStoreConfig, deps: Dict[Api, Any] |
| 130 | +) -> VectorStoreImpl: |
| 131 | + impl = VectorStoreImpl(config, deps[Api.inference]) |
| 132 | + await impl.initialize() |
| 133 | + return impl |
| 134 | +``` |
| 135 | + |
| 136 | +## Dependencies |
| 137 | + |
| 138 | +The provider package must be installed on the system. For example: |
| 139 | + |
| 140 | +```bash |
| 141 | +$ uv pip show llama-stack-ollama-provider |
| 142 | +Name: llama-stack-ollama-provider |
| 143 | +Version: 0.1.0 |
| 144 | +Location: /path/to/venv/lib/python3.10/site-packages |
| 145 | +``` |
| 146 | + |
| 147 | +## Example: Custom Ollama Provider |
| 148 | + |
| 149 | +Here's a complete example of creating and using a custom Ollama provider: |
| 150 | + |
| 151 | +1. First, create the provider package: |
| 152 | + |
| 153 | +```bash |
| 154 | +mkdir -p llama-stack-provider-ollama |
| 155 | +cd llama-stack-provider-ollama |
| 156 | +git init |
| 157 | +uv init |
| 158 | +``` |
| 159 | + |
| 160 | +2. Edit `pyproject.toml`: |
| 161 | + |
| 162 | +```toml |
| 163 | +[project] |
| 164 | +name = "llama-stack-provider-ollama" |
| 165 | +version = "0.1.0" |
| 166 | +description = "Ollama provider for Llama Stack" |
| 167 | +requires-python = ">=3.10" |
| 168 | +dependencies = ["llama-stack", "pydantic", "ollama", "aiohttp"] |
| 169 | +``` |
| 170 | + |
| 171 | +3. Create the provider specification: |
| 172 | + |
| 173 | +```yaml |
| 174 | +# /etc/llama-stack/providers.d/remote/inference/custom_ollama.yaml |
| 175 | +adapter: |
| 176 | + adapter_type: custom_ollama |
| 177 | + pip_packages: ["ollama", "aiohttp"] |
| 178 | + config_class: llama_stack_provider_ollama.config.OllamaImplConfig |
| 179 | + module: llama_stack_provider_ollama |
| 180 | +api_dependencies: [] |
| 181 | +optional_api_dependencies: [] |
| 182 | +``` |
| 183 | + |
| 184 | +4. Install the provider: |
| 185 | + |
| 186 | +```bash |
| 187 | +uv pip install -e . |
| 188 | +``` |
| 189 | + |
| 190 | +5. Configure Llama Stack to use external providers: |
| 191 | + |
| 192 | +```yaml |
| 193 | +external_providers_dir: /etc/llama-stack/providers.d/ |
| 194 | +``` |
| 195 | + |
| 196 | +The provider will now be available in Llama Stack with the type `remote::custom_ollama`. |
| 197 | + |
| 198 | +## Best Practices |
| 199 | + |
| 200 | +1. **Package Naming**: Use the prefix `llama-stack-provider-` for your provider packages to make them easily identifiable. |
| 201 | + |
| 202 | +2. **Version Management**: Keep your provider package versioned and compatible with the Llama Stack version you're using. |
| 203 | + |
| 204 | +3. **Dependencies**: Only include the minimum required dependencies in your provider package. |
| 205 | + |
| 206 | +4. **Documentation**: Include clear documentation in your provider package about: |
| 207 | + - Installation requirements |
| 208 | + - Configuration options |
| 209 | + - Usage examples |
| 210 | + - Any limitations or known issues |
| 211 | + |
| 212 | +5. **Testing**: Include tests in your provider package to ensure it works correctly with Llama Stack. |
| 213 | +You can refer to the [integration tests |
| 214 | +guide](https://github.com/meta-llama/llama-stack/blob/main/tests/integration/README.md) for more |
| 215 | +information. Execute the test for the Provider type you are developing. |
| 216 | + |
| 217 | +## Troubleshooting |
| 218 | + |
| 219 | +If your external provider isn't being loaded: |
| 220 | + |
| 221 | +1. Check that the `external_providers_dir` path is correct and accessible. |
| 222 | +2. Verify that the YAML files are properly formatted. |
| 223 | +3. Ensure all required Python packages are installed. |
| 224 | +4. Check the Llama Stack server logs for any error messages - turn on debug logging to get more |
| 225 | + information using `LLAMA_STACK_LOGGING=all=debug`. |
| 226 | +5. Verify that the provider package is installed in your Python environment. |
0 commit comments