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

Module prompt #81

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Codegate Example Configuration
# Example configuration file
# Copy this file to config.yaml and modify as needed

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

# Logging configuration
log_level: "INFO" # One of: ERROR, WARNING, INFO, DEBUG
log_format: "JSON" # One of: JSON, TEXT
log_level: "INFO" # ERROR, WARNING, INFO, DEBUG
log_format: "JSON" # JSON, TEXT

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

# Option 2: Reference a separate prompts file
# prompts: "prompts.yaml" # Path to prompts file (relative to config file or absolute)
36 changes: 36 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,34 @@ codegate serve [OPTIONS]
- Must be a valid YAML file
- Configuration values can be overridden by environment variables and CLI options

- `--prompts FILE`: Path to YAML prompts file
- Optional
- Must be a valid YAML file
- Overrides default prompts and configuration file prompts

### show-prompts

Display the loaded system prompts:

```bash
codegate show-prompts [OPTIONS]
```

#### Options

- `--prompts FILE`: Path to YAML prompts file
- Optional
- Must be a valid YAML file
- If not provided, shows default prompts from prompts/default.yaml

## Error Handling

The CLI provides user-friendly error messages for:
- Invalid port numbers
- Invalid log levels
- Invalid log formats
- Configuration file errors
- Prompts file errors
- Server startup failures

All errors are output to stderr with appropriate exit codes.
Expand All @@ -72,3 +93,18 @@ codegate serve --log-level DEBUG --log-format TEXT
Start server with configuration file:
```bash
codegate serve --config my-config.yaml
```

Start server with custom prompts:
```bash
codegate serve --prompts my-prompts.yaml
```

Show default system prompts:
```bash
codegate show-prompts
```

Show prompts from a custom file:
```bash
codegate show-prompts --prompts my-prompts.yaml
50 changes: 49 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ The configuration system in Codegate is managed through the `Config` class in `c
1. CLI arguments
2. Environment variables
3. Config file (YAML)
4. Default values
4. Default values (including default prompts from prompts/default.yaml)

## Default Configuration Values

- Port: 8989
- Host: "localhost"
- Log Level: "INFO"
- Log Format: "JSON"
- Prompts: Default prompts from prompts/default.yaml

## Configuration Methods

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

```python
config = Config.from_env()
Expand All @@ -57,6 +59,50 @@ Available log formats (case-insensitive):
- `JSON`
- `TEXT`

### Prompts Configuration

Prompts can be configured in several ways:

1. Default Prompts:
- Located in prompts/default.yaml
- Loaded automatically if no other prompts are specified

2. In Configuration File:
```yaml
# Option 1: Direct prompts definition
prompts:
my_prompt: "Custom prompt text"
another_prompt: "Another prompt text"

# Option 2: Reference to prompts file
prompts: "path/to/prompts.yaml"
```

3. Via Environment Variable:
```bash
export CODEGATE_PROMPTS_FILE=path/to/prompts.yaml
```

4. Via CLI Flag:
```bash
codegate serve --prompts path/to/prompts.yaml
```

### Prompts File Format

Prompts files should be in YAML format with string values:

```yaml
prompt_name: "Prompt text content"
another_prompt: "More prompt text"
```

Access prompts in code:
```python
config = Config.load()
prompt = config.prompts.prompt_name
```

## Error Handling

The configuration system uses a custom `ConfigurationError` exception for handling configuration-related errors, such as:
Expand All @@ -66,3 +112,5 @@ The configuration system uses a custom `ConfigurationError` exception for handli
- Invalid log formats
- YAML parsing errors
- File reading errors
- Invalid prompt values (must be strings)
- Missing or invalid prompts files
163 changes: 49 additions & 114 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Codegate is a configurable Generative AI gateway designed to protect developers
- Secrets exfiltration prevention
- Secure coding recommendations
- Prevention of AI recommending deprecated/malicious libraries
- Modular system prompts configuration

## Development Setup

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

### Configuration Options

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

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

## CLI Interface

The main command-line interface is implemented in `cli.py`. Basic usage:

```bash
# Start server with default settings
codegate serve
## Working with Prompts

# Start with custom configuration
codegate serve --port 8989 --host localhost --log-level DEBUG
```
### Default Prompts

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

## Dependencies Management
### Creating Custom Prompts

### Adding Dependencies
1. Create a new YAML file following the format:
```yaml
prompt_name: "Prompt text content"
another_prompt: "More prompt text"
```

For runtime dependencies:
```bash
poetry add package-name
```
2. Use the prompts file:
```bash
# Via CLI
codegate serve --prompts my-prompts.yaml

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

### Updating Dependencies
# Or via environment
export CODEGATE_PROMPTS_FILE=path/to/prompts.yaml
```

To update all dependencies:
```bash
poetry update
```
### Testing Prompts

To update a specific package:
```bash
poetry update package-name
```
1. View loaded prompts:
```bash
# Show default prompts
codegate show-prompts

## Virtual Environment
# Show custom prompts
codegate show-prompts --prompts my-prompts.yaml
```

Poetry automatically manages virtual environments. To activate:
2. Write tests for prompt functionality:
```python
def test_custom_prompts():
config = Config.load(prompts_path="path/to/test/prompts.yaml")
assert config.prompts.my_prompt == "Expected prompt text"
```

```bash
poetry shell
```
## CLI Interface

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

```bash
poetry run command
```

## Building and Publishing
# Start server with default settings
codegate serve

To build distribution packages:
```bash
poetry build
```
# Start with custom configuration
codegate serve --port 8989 --host localhost --log-level DEBUG

To publish to PyPI:
```bash
poetry publish
# Start with custom prompts
codegate serve --prompts my-prompts.yaml
```

## Debugging Tips

1. Use DEBUG log level for detailed logging:
```bash
codegate serve --log-level DEBUG
```

2. Use TEXT log format for human-readable logs during development:
```bash
codegate serve --log-format TEXT
```
See [CLI Documentation](cli.md) for detailed command information.

3. Check the configuration resolution by examining logs at startup

## Contributing Guidelines

1. Create a feature branch from `main`
2. Write tests for new functionality
3. Ensure all tests pass with `make test`
4. Run `make all` before committing to ensure:
- Code is properly formatted
- All linting checks pass
- Tests pass with good coverage
- Security checks pass
5. Update documentation as needed
6. Submit a pull request

## Best Practices

1. Always commit both `pyproject.toml` and `poetry.lock` files
2. Use `poetry add` instead of manually editing `pyproject.toml`
3. Run `make all` before committing changes
4. Use `poetry run` prefix for Python commands
5. Keep dependencies minimal and well-organized
6. Write descriptive commit messages
7. Add tests for new functionality
8. Update documentation when making significant changes
9. Follow the existing code style and patterns
10. Use type hints and docstrings for better code documentation

## Common Issues and Solutions

1. **Virtual Environment Issues**
- Reset Poetry's virtual environment:
```bash
poetry env remove python
poetry install
```

2. **Dependency Conflicts**
- Update poetry.lock:
```bash
poetry update
```
- Check dependency tree:
```bash
poetry show --tree
```

3. **Test Failures**
- Run specific test:
```bash
poetry run pytest tests/test_specific.py -v
```
- Debug with more output:
```bash
poetry run pytest -vv --pdb
[Rest of development.md content remains unchanged...]
Loading