Skip to content

Commit d18e34a

Browse files
authored
Merge pull request stacklok#124 from stacklok/vllm-provider
Vllm provider
2 parents a795066 + 80d4e6c commit d18e34a

22 files changed

+515
-105
lines changed

config.yaml.example

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
1-
# Example configuration file
2-
# Copy this file to config.yaml and modify as needed
1+
# Codegate Example Configuration
32

4-
# Server configuration
5-
port: 8989
6-
host: "localhost"
3+
# Network settings
4+
port: 8989 # Port to listen on (1-65535)
5+
host: "localhost" # Host to bind to (use localhost for all interfaces)
76

87
# Logging configuration
9-
log_level: "INFO" # ERROR, WARNING, INFO, DEBUG
10-
log_format: "JSON" # JSON, TEXT
8+
log_level: "INFO" # One of: ERROR, WARNING, INFO, DEBUG
119

12-
# Prompts configuration
13-
# Option 1: Define prompts directly in the config file
14-
prompts:
15-
my_system_prompt: "Custom system prompt defined in config"
16-
another_prompt: "Another custom prompt"
10+
# Note: This configuration can be overridden by:
11+
# 1. CLI arguments (--port, --host, --log-level)
12+
# 2. Environment variables (CODEGATE_APP_PORT, CODEGATE_APP_HOST, CODEGATE_APP_LOG_LEVEL)
1713

18-
# Option 2: Reference a separate prompts file
19-
# prompts: "prompts.yaml" # Path to prompts file (relative to config file or absolute)
14+
# Provider URLs
15+
provider_urls:
16+
openai: "https://api.openai.com/v1"
17+
anthropic: "https://api.anthropic.com/v1"
18+
vllm: "http://localhost:8000" # Base URL without /v1 path, it will be added automatically
19+
20+
# Note: Provider URLs can be overridden by environment variables:
21+
# CODEGATE_PROVIDER_OPENAI_URL
22+
# CODEGATE_PROVIDER_ANTHROPIC_URL
23+
# CODEGATE_PROVIDER_VLLM_URL
24+
# Or by CLI flags:
25+
# --vllm-url
26+
# --openai-url
27+
# --anthropic-url
28+
29+
# Embedding model configuration
30+
31+
####
32+
# Inference model configuration
33+
##
34+
35+
# Model to use for chatting
36+
chat_model_path: "./models/qwen2.5-coder-1.5b-instruct-q5_k_m.gguf"
37+
38+
# Context length of the model
39+
chat_model_n_ctx: 32768
40+
41+
# Number of layers to offload to GPU. If -1, all layers are offloaded.
42+
chat_model_n_gpu_layers: -1

docs/cli.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ codegate serve [OPTIONS]
4646
- Must be a valid YAML file
4747
- Overrides default prompts and configuration file prompts
4848

49+
- `--vllm-url TEXT`: vLLM provider URL (default: http://localhost:8000)
50+
- Optional
51+
- Base URL for vLLM provider (/v1 path is added automatically)
52+
- Overrides configuration file and environment variables
53+
54+
- `--openai-url TEXT`: OpenAI provider URL (default: https://api.openai.com/v1)
55+
- Optional
56+
- Base URL for OpenAI provider
57+
- Overrides configuration file and environment variables
58+
59+
- `--anthropic-url TEXT`: Anthropic provider URL (default: https://api.anthropic.com/v1)
60+
- Optional
61+
- Base URL for Anthropic provider
62+
- Overrides configuration file and environment variables
63+
4964
### show-prompts
5065

5166
Display the loaded system prompts:
@@ -100,6 +115,11 @@ Start server with custom prompts:
100115
codegate serve --prompts my-prompts.yaml
101116
```
102117

118+
Start server with custom vLLM endpoint:
119+
```bash
120+
codegate serve --vllm-url https://vllm.example.com
121+
```
122+
103123
Show default system prompts:
104124
```bash
105125
codegate show-prompts

docs/configuration.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ The configuration system in Codegate is managed through the `Config` class in `c
1616
- Log Level: "INFO"
1717
- Log Format: "JSON"
1818
- Prompts: Default prompts from prompts/default.yaml
19+
- Provider URLs:
20+
- vLLM: "http://localhost:8000"
21+
- OpenAI: "https://api.openai.com/v1"
22+
- Anthropic: "https://api.anthropic.com/v1"
1923

2024
## Configuration Methods
2125

@@ -27,6 +31,18 @@ Load configuration from a YAML file:
2731
config = Config.from_file("config.yaml")
2832
```
2933

34+
Example config.yaml:
35+
```yaml
36+
port: 8989
37+
host: localhost
38+
log_level: INFO
39+
log_format: JSON
40+
provider_urls:
41+
vllm: "https://vllm.example.com"
42+
openai: "https://api.openai.com/v1"
43+
anthropic: "https://api.anthropic.com/v1"
44+
```
45+
3046
### From Environment Variables
3147
3248
Environment variables are automatically loaded with these mappings:
@@ -36,13 +52,42 @@ Environment variables are automatically loaded with these mappings:
3652
- `CODEGATE_APP_LOG_LEVEL`: Logging level
3753
- `CODEGATE_LOG_FORMAT`: Log format
3854
- `CODEGATE_PROMPTS_FILE`: Path to prompts YAML file
55+
- `CODEGATE_PROVIDER_VLLM_URL`: vLLM provider URL
56+
- `CODEGATE_PROVIDER_OPENAI_URL`: OpenAI provider URL
57+
- `CODEGATE_PROVIDER_ANTHROPIC_URL`: Anthropic provider URL
3958

4059
```python
4160
config = Config.from_env()
4261
```
4362

4463
## Configuration Options
4564

65+
### Provider URLs
66+
67+
Provider URLs can be configured in several ways:
68+
69+
1. In Configuration File:
70+
```yaml
71+
provider_urls:
72+
vllm: "https://vllm.example.com" # /v1 path is added automatically
73+
openai: "https://api.openai.com/v1"
74+
anthropic: "https://api.anthropic.com/v1"
75+
```
76+
77+
2. Via Environment Variables:
78+
```bash
79+
export CODEGATE_PROVIDER_VLLM_URL=https://vllm.example.com
80+
export CODEGATE_PROVIDER_OPENAI_URL=https://api.openai.com/v1
81+
export CODEGATE_PROVIDER_ANTHROPIC_URL=https://api.anthropic.com/v1
82+
```
83+
84+
3. Via CLI Flags:
85+
```bash
86+
codegate serve --vllm-url https://vllm.example.com
87+
```
88+
89+
Note: For the vLLM provider, the /v1 path is automatically appended to the base URL if not present.
90+
4691
### Log Levels
4792

4893
Available log levels (case-insensitive):

docs/development.md

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Codegate is a configurable Generative AI gateway designed to protect developers
99
- Secure coding recommendations
1010
- Prevention of AI recommending deprecated/malicious libraries
1111
- Modular system prompts configuration
12+
- Multiple AI provider support with configurable endpoints
1213

1314
## Development Setup
1415

@@ -53,7 +54,11 @@ codegate/
5354
│ ├── logging.py # Logging setup
5455
│ ├── prompts.py # Prompts management
5556
│ ├── server.py # Main server implementation
56-
│ └── providers/* # External service providers (anthropic, openai, etc.)
57+
│ └── providers/ # External service providers
58+
│ ├── anthropic/ # Anthropic provider implementation
59+
│ ├── openai/ # OpenAI provider implementation
60+
│ ├── vllm/ # vLLM provider implementation
61+
│ └── base.py # Base provider interface
5762
├── tests/ # Test files
5863
└── docs/ # Documentation
5964
```
@@ -128,9 +133,87 @@ Codegate uses a hierarchical configuration system with the following priority (h
128133
- Log Level: Logging level (ERROR|WARNING|INFO|DEBUG)
129134
- Log Format: Log format (JSON|TEXT)
130135
- Prompts: System prompts configuration
136+
- Provider URLs: AI provider endpoint configuration
131137

132138
See [Configuration Documentation](configuration.md) for detailed information.
133139

140+
## Working with Providers
141+
142+
Codegate supports multiple AI providers through a modular provider system.
143+
144+
### Available Providers
145+
146+
1. **vLLM Provider**
147+
- Default URL: http://localhost:8000
148+
- Supports OpenAI-compatible API
149+
- Automatically adds /v1 path to base URL
150+
- Model names are prefixed with "hosted_vllm/"
151+
152+
2. **OpenAI Provider**
153+
- Default URL: https://api.openai.com/v1
154+
- Standard OpenAI API implementation
155+
156+
3. **Anthropic Provider**
157+
- Default URL: https://api.anthropic.com/v1
158+
- Anthropic Claude API implementation
159+
160+
### Configuring Providers
161+
162+
Provider URLs can be configured through:
163+
164+
1. Config file (config.yaml):
165+
```yaml
166+
provider_urls:
167+
vllm: "https://vllm.example.com"
168+
openai: "https://api.openai.com/v1"
169+
anthropic: "https://api.anthropic.com/v1"
170+
```
171+
172+
2. Environment variables:
173+
```bash
174+
export CODEGATE_PROVIDER_VLLM_URL=https://vllm.example.com
175+
export CODEGATE_PROVIDER_OPENAI_URL=https://api.openai.com/v1
176+
export CODEGATE_PROVIDER_ANTHROPIC_URL=https://api.anthropic.com/v1
177+
```
178+
179+
3. CLI flags:
180+
```bash
181+
codegate serve --vllm-url https://vllm.example.com
182+
```
183+
184+
### Implementing New Providers
185+
186+
To add a new provider:
187+
188+
1. Create a new directory in `src/codegate/providers/`
189+
2. Implement required components:
190+
- `provider.py`: Main provider class extending BaseProvider
191+
- `adapter.py`: Input/output normalizers
192+
- `__init__.py`: Export provider class
193+
194+
Example structure:
195+
```python
196+
from codegate.providers.base import BaseProvider
197+
198+
class NewProvider(BaseProvider):
199+
def __init__(self, ...):
200+
super().__init__(
201+
InputNormalizer(),
202+
OutputNormalizer(),
203+
completion_handler,
204+
pipeline_processor,
205+
fim_pipeline_processor
206+
)
207+
208+
@property
209+
def provider_route_name(self) -> str:
210+
return "provider_name"
211+
212+
def _setup_routes(self):
213+
# Implement route setup
214+
pass
215+
```
216+
134217
## Working with Prompts
135218

136219
### Default Prompts
@@ -188,8 +271,9 @@ codegate serve --port 8989 --host localhost --log-level DEBUG
188271

189272
# Start with custom prompts
190273
codegate serve --prompts my-prompts.yaml
274+
275+
# Start with custom provider URL
276+
codegate serve --vllm-url https://vllm.example.com
191277
```
192278

193279
See [CLI Documentation](cli.md) for detailed command information.
194-
195-
[Rest of development.md content remains unchanged...]

src/codegate/cli.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys
44
from pathlib import Path
5-
from typing import Optional
5+
from typing import Dict, Optional
66

77
import click
88

@@ -88,17 +88,47 @@ def show_prompts(prompts: Optional[Path]) -> None:
8888
default=None,
8989
help="Path to YAML prompts file",
9090
)
91+
@click.option(
92+
"--vllm-url",
93+
type=str,
94+
default=None,
95+
help="vLLM provider URL (default: http://localhost:8000/v1)",
96+
)
97+
@click.option(
98+
"--openai-url",
99+
type=str,
100+
default=None,
101+
help="OpenAI provider URL (default: https://api.openai.com/v1)",
102+
)
103+
@click.option(
104+
"--anthropic-url",
105+
type=str,
106+
default=None,
107+
help="Anthropic provider URL (default: https://api.anthropic.com/v1)",
108+
)
91109
def serve(
92110
port: Optional[int],
93111
host: Optional[str],
94112
log_level: Optional[str],
95113
log_format: Optional[str],
96114
config: Optional[Path],
97115
prompts: Optional[Path],
116+
vllm_url: Optional[str],
117+
openai_url: Optional[str],
118+
anthropic_url: Optional[str],
98119
) -> None:
99120
"""Start the codegate server."""
100121
logger = None
101122
try:
123+
# Create provider URLs dict from CLI options
124+
cli_provider_urls: Dict[str, str] = {}
125+
if vllm_url:
126+
cli_provider_urls["vllm"] = vllm_url
127+
if openai_url:
128+
cli_provider_urls["openai"] = openai_url
129+
if anthropic_url:
130+
cli_provider_urls["anthropic"] = anthropic_url
131+
102132
# Load configuration with priority resolution
103133
cfg = Config.load(
104134
config_path=config,
@@ -107,6 +137,7 @@ def serve(
107137
cli_host=host,
108138
cli_log_level=log_level,
109139
cli_log_format=log_format,
140+
cli_provider_urls=cli_provider_urls,
110141
)
111142

112143
logger = setup_logging(cfg.log_level, cfg.log_format)
@@ -118,6 +149,7 @@ def serve(
118149
"log_level": cfg.log_level.value,
119150
"log_format": cfg.log_format.value,
120151
"prompts_loaded": len(cfg.prompts.prompts),
152+
"provider_urls": cfg.provider_urls,
121153
},
122154
)
123155

0 commit comments

Comments
 (0)