Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 8febe0a

Browse files
author
Luke Hinds
authored
Merge pull request #81 from stacklok/module-prompt
2 parents 1b711ff + 55093d1 commit 8febe0a

15 files changed

+724
-171
lines changed

config.yaml.example

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

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

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

11-
# Note: This configuration can be overridden by:
12-
# 1. CLI arguments (--port, --host, --log-level, --log-format)
13-
# 2. Environment variables:
14-
# - CODEGATE_APP_PORT
15-
# - CODEGATE_APP_HOST
16-
# - CODEGATE_APP_LOG_LEVEL
17-
# - CODEGATE_LOG_FORMAT
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"
17+
18+
# Option 2: Reference a separate prompts file
19+
# prompts: "prompts.yaml" # Path to prompts file (relative to config file or absolute)

docs/cli.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,34 @@ codegate serve [OPTIONS]
4141
- Must be a valid YAML file
4242
- Configuration values can be overridden by environment variables and CLI options
4343

44+
- `--prompts FILE`: Path to YAML prompts file
45+
- Optional
46+
- Must be a valid YAML file
47+
- Overrides default prompts and configuration file prompts
48+
49+
### show-prompts
50+
51+
Display the loaded system prompts:
52+
53+
```bash
54+
codegate show-prompts [OPTIONS]
55+
```
56+
57+
#### Options
58+
59+
- `--prompts FILE`: Path to YAML prompts file
60+
- Optional
61+
- Must be a valid YAML file
62+
- If not provided, shows default prompts from prompts/default.yaml
63+
4464
## Error Handling
4565

4666
The CLI provides user-friendly error messages for:
4767
- Invalid port numbers
4868
- Invalid log levels
4969
- Invalid log formats
5070
- Configuration file errors
71+
- Prompts file errors
5172
- Server startup failures
5273

5374
All errors are output to stderr with appropriate exit codes.
@@ -72,3 +93,18 @@ codegate serve --log-level DEBUG --log-format TEXT
7293
Start server with configuration file:
7394
```bash
7495
codegate serve --config my-config.yaml
96+
```
97+
98+
Start server with custom prompts:
99+
```bash
100+
codegate serve --prompts my-prompts.yaml
101+
```
102+
103+
Show default system prompts:
104+
```bash
105+
codegate show-prompts
106+
```
107+
108+
Show prompts from a custom file:
109+
```bash
110+
codegate show-prompts --prompts my-prompts.yaml

docs/configuration.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ The configuration system in Codegate is managed through the `Config` class in `c
77
1. CLI arguments
88
2. Environment variables
99
3. Config file (YAML)
10-
4. Default values
10+
4. Default values (including default prompts from prompts/default.yaml)
1111

1212
## Default Configuration Values
1313

1414
- Port: 8989
1515
- Host: "localhost"
1616
- Log Level: "INFO"
1717
- Log Format: "JSON"
18+
- Prompts: Default prompts from prompts/default.yaml
1819

1920
## Configuration Methods
2021

@@ -34,6 +35,7 @@ Environment variables are automatically loaded with these mappings:
3435
- `CODEGATE_APP_HOST`: Server host
3536
- `CODEGATE_APP_LOG_LEVEL`: Logging level
3637
- `CODEGATE_LOG_FORMAT`: Log format
38+
- `CODEGATE_PROMPTS_FILE`: Path to prompts YAML file
3739

3840
```python
3941
config = Config.from_env()
@@ -57,6 +59,50 @@ Available log formats (case-insensitive):
5759
- `JSON`
5860
- `TEXT`
5961

62+
### Prompts Configuration
63+
64+
Prompts can be configured in several ways:
65+
66+
1. Default Prompts:
67+
- Located in prompts/default.yaml
68+
- Loaded automatically if no other prompts are specified
69+
70+
2. In Configuration File:
71+
```yaml
72+
# Option 1: Direct prompts definition
73+
prompts:
74+
my_prompt: "Custom prompt text"
75+
another_prompt: "Another prompt text"
76+
77+
# Option 2: Reference to prompts file
78+
prompts: "path/to/prompts.yaml"
79+
```
80+
81+
3. Via Environment Variable:
82+
```bash
83+
export CODEGATE_PROMPTS_FILE=path/to/prompts.yaml
84+
```
85+
86+
4. Via CLI Flag:
87+
```bash
88+
codegate serve --prompts path/to/prompts.yaml
89+
```
90+
91+
### Prompts File Format
92+
93+
Prompts files should be in YAML format with string values:
94+
95+
```yaml
96+
prompt_name: "Prompt text content"
97+
another_prompt: "More prompt text"
98+
```
99+
100+
Access prompts in code:
101+
```python
102+
config = Config.load()
103+
prompt = config.prompts.prompt_name
104+
```
105+
60106
## Error Handling
61107

62108
The configuration system uses a custom `ConfigurationError` exception for handling configuration-related errors, such as:
@@ -66,3 +112,5 @@ The configuration system uses a custom `ConfigurationError` exception for handli
66112
- Invalid log formats
67113
- YAML parsing errors
68114
- File reading errors
115+
- Invalid prompt values (must be strings)
116+
- Missing or invalid prompts files

docs/development.md

Lines changed: 49 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Codegate is a configurable Generative AI gateway designed to protect developers
88
- Secrets exfiltration prevention
99
- Secure coding recommendations
1010
- Prevention of AI recommending deprecated/malicious libraries
11+
- Modular system prompts configuration
1112

1213
## Development Setup
1314

@@ -41,12 +42,16 @@ Codegate is a configurable Generative AI gateway designed to protect developers
4142
codegate/
4243
├── pyproject.toml # Project configuration and dependencies
4344
├── poetry.lock # Lock file (committed to version control)
45+
├── prompts/ # System prompts configuration
46+
│ └── default.yaml # Default system prompts
4447
├── src/
4548
│ └── codegate/ # Source code
4649
│ ├── __init__.py
4750
│ ├── cli.py # Command-line interface
4851
│ ├── config.py # Configuration management
52+
│ ├── exceptions.py # Shared exceptions
4953
│ ├── logging.py # Logging setup
54+
│ ├── prompts.py # Prompts management
5055
│ ├── server.py # Main server implementation
5156
│ └── providers/* # External service providers (anthropic, openai, etc.)
5257
├── tests/ # Test files
@@ -114,147 +119,77 @@ Codegate uses a hierarchical configuration system with the following priority (h
114119
1. CLI arguments
115120
2. Environment variables
116121
3. Config file (YAML)
117-
4. Default values
122+
4. Default values (including default prompts)
118123

119124
### Configuration Options
120125

121126
- Port: Server port (default: 8989)
122127
- Host: Server host (default: "localhost")
123128
- Log Level: Logging level (ERROR|WARNING|INFO|DEBUG)
124129
- Log Format: Log format (JSON|TEXT)
130+
- Prompts: System prompts configuration
125131

126132
See [Configuration Documentation](configuration.md) for detailed information.
127133

128-
## CLI Interface
129-
130-
The main command-line interface is implemented in `cli.py`. Basic usage:
131-
132-
```bash
133-
# Start server with default settings
134-
codegate serve
134+
## Working with Prompts
135135

136-
# Start with custom configuration
137-
codegate serve --port 8989 --host localhost --log-level DEBUG
138-
```
136+
### Default Prompts
139137

140-
See [CLI Documentation](cli.md) for detailed command information.
138+
Default prompts are stored in `prompts/default.yaml`. These prompts are loaded automatically when no other prompts are specified.
141139

142-
## Dependencies Management
140+
### Creating Custom Prompts
143141

144-
### Adding Dependencies
142+
1. Create a new YAML file following the format:
143+
```yaml
144+
prompt_name: "Prompt text content"
145+
another_prompt: "More prompt text"
146+
```
145147
146-
For runtime dependencies:
147-
```bash
148-
poetry add package-name
149-
```
148+
2. Use the prompts file:
149+
```bash
150+
# Via CLI
151+
codegate serve --prompts my-prompts.yaml
150152

151-
For development dependencies:
152-
```bash
153-
poetry add --group dev package-name
154-
```
153+
# Or in config.yaml
154+
prompts: "path/to/prompts.yaml"
155155

156-
### Updating Dependencies
156+
# Or via environment
157+
export CODEGATE_PROMPTS_FILE=path/to/prompts.yaml
158+
```
157159

158-
To update all dependencies:
159-
```bash
160-
poetry update
161-
```
160+
### Testing Prompts
162161

163-
To update a specific package:
164-
```bash
165-
poetry update package-name
166-
```
162+
1. View loaded prompts:
163+
```bash
164+
# Show default prompts
165+
codegate show-prompts
167166

168-
## Virtual Environment
167+
# Show custom prompts
168+
codegate show-prompts --prompts my-prompts.yaml
169+
```
169170

170-
Poetry automatically manages virtual environments. To activate:
171+
2. Write tests for prompt functionality:
172+
```python
173+
def test_custom_prompts():
174+
config = Config.load(prompts_path="path/to/test/prompts.yaml")
175+
assert config.prompts.my_prompt == "Expected prompt text"
176+
```
171177

172-
```bash
173-
poetry shell
174-
```
178+
## CLI Interface
175179

176-
To run a single command:
180+
The main command-line interface is implemented in `cli.py`. Basic usage:
177181

178182
```bash
179-
poetry run command
180-
```
181-
182-
## Building and Publishing
183+
# Start server with default settings
184+
codegate serve
183185

184-
To build distribution packages:
185-
```bash
186-
poetry build
187-
```
186+
# Start with custom configuration
187+
codegate serve --port 8989 --host localhost --log-level DEBUG
188188

189-
To publish to PyPI:
190-
```bash
191-
poetry publish
189+
# Start with custom prompts
190+
codegate serve --prompts my-prompts.yaml
192191
```
193192

194-
## Debugging Tips
195-
196-
1. Use DEBUG log level for detailed logging:
197-
```bash
198-
codegate serve --log-level DEBUG
199-
```
200-
201-
2. Use TEXT log format for human-readable logs during development:
202-
```bash
203-
codegate serve --log-format TEXT
204-
```
193+
See [CLI Documentation](cli.md) for detailed command information.
205194

206-
3. Check the configuration resolution by examining logs at startup
207-
208-
## Contributing Guidelines
209-
210-
1. Create a feature branch from `main`
211-
2. Write tests for new functionality
212-
3. Ensure all tests pass with `make test`
213-
4. Run `make all` before committing to ensure:
214-
- Code is properly formatted
215-
- All linting checks pass
216-
- Tests pass with good coverage
217-
- Security checks pass
218-
5. Update documentation as needed
219-
6. Submit a pull request
220-
221-
## Best Practices
222-
223-
1. Always commit both `pyproject.toml` and `poetry.lock` files
224-
2. Use `poetry add` instead of manually editing `pyproject.toml`
225-
3. Run `make all` before committing changes
226-
4. Use `poetry run` prefix for Python commands
227-
5. Keep dependencies minimal and well-organized
228-
6. Write descriptive commit messages
229-
7. Add tests for new functionality
230-
8. Update documentation when making significant changes
231-
9. Follow the existing code style and patterns
232-
10. Use type hints and docstrings for better code documentation
233-
234-
## Common Issues and Solutions
235-
236-
1. **Virtual Environment Issues**
237-
- Reset Poetry's virtual environment:
238-
```bash
239-
poetry env remove python
240-
poetry install
241-
```
242-
243-
2. **Dependency Conflicts**
244-
- Update poetry.lock:
245-
```bash
246-
poetry update
247-
```
248-
- Check dependency tree:
249-
```bash
250-
poetry show --tree
251-
```
252-
253-
3. **Test Failures**
254-
- Run specific test:
255-
```bash
256-
poetry run pytest tests/test_specific.py -v
257-
```
258-
- Debug with more output:
259-
```bash
260-
poetry run pytest -vv --pdb
195+
[Rest of development.md content remains unchanged...]

0 commit comments

Comments
 (0)