File size: 8,611 Bytes
61ad06f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# πŸ€– AI Agent Setup Guide

This guide explains how to set up and configure AI agents for PyCatan.

## πŸ“‹ Table of Contents

1. [Quick Start](#quick-start)
2. [API Key Setup](#api-key-setup)
3. [Configuration System](#configuration-system)
4. [Security Best Practices](#security-best-practices)
5. [Development Workflow](#development-workflow)

---

## πŸš€ Quick Start

### Step 1: Get an API Key

You need an API key from an LLM provider. We recommend **Google Gemini** for development:

1. Go to [Google AI Studio](https://aistudio.google.com/app/apikey)
2. Click "Create API Key"
3. Copy your API key

**Other providers:**
- OpenAI: https://platform.openai.com/api-keys
- Anthropic: https://console.anthropic.com/settings/keys

### Step 2: Setup Environment Variables

```bash
# Copy the template file
cp .env.example .env

# Edit .env and paste your API key
# (Use any text editor)
```

Example `.env` file:
```bash
GEMINI_API_KEY=AIzaSyC_your_actual_api_key_here
```

### Step 3: Install Dependencies

```bash
pip install pyyaml
```

### Step 4: Test the Configuration

```bash
python examples/test_ai_config.py
```

You should see:
```
πŸŽ‰ All tests passed!
```

---

## πŸ”‘ API Key Setup

### Where API Keys Are Stored

**API keys are stored in the `.env` file**, which is:
- βœ… **NOT** committed to Git (protected by `.gitignore`)
- βœ… Stored locally on your machine only
- βœ… Easy to update without changing code

### `.env` File Structure

```bash
# .env - YOUR PRIVATE FILE (never commit this!)
GEMINI_API_KEY=your_actual_key_here
OPENAI_API_KEY=your_openai_key_here      # Optional
ANTHROPIC_API_KEY=your_anthropic_key      # Optional
```

### `.env.example` File

The **`.env.example`** file is a template that:
- βœ… **IS** committed to Git
- βœ… Shows what variables are needed
- βœ… Contains NO actual secrets
- βœ… Helps other developers know what to set up

**Never put real API keys in `.env.example`!**

---

## βš™οΈ Configuration System

### Overview

PyCatan uses a **two-file configuration system**:

```
πŸ“ Project Root
β”œβ”€β”€ .env                          # ❌ NOT in Git - API keys
β”œβ”€β”€ .env.example                  # βœ… IN Git - Template
β”œβ”€β”€ pycatan/ai/
β”‚   β”œβ”€β”€ config_example.yaml       # βœ… IN Git - Documentation
β”‚   β”œβ”€β”€ config_dev.yaml           # βœ… IN Git - Dev defaults
β”‚   └── my_agent_config.yaml      # ❌ NOT in Git - Your custom config
```

### File Types Explained

#### 1. `.env` - Environment Variables (API Keys)
- **Purpose:** Store sensitive API keys
- **Git:** ❌ **NEVER** committed
- **Contains:** `GEMINI_API_KEY=actual_secret_key`
- **Who creates it:** Each developer on their machine

#### 2. `.env.example` - Environment Template
- **Purpose:** Template showing what variables are needed
- **Git:** βœ… Committed to Git
- **Contains:** `GEMINI_API_KEY=` (empty)
- **Who creates it:** Already created in the project

#### 3. `config_dev.yaml` - Development Defaults
- **Purpose:** Default configuration for development
- **Git:** βœ… Committed to Git
- **Contains:** All settings EXCEPT API keys
- **Who uses it:** Everyone, automatically

#### 4. `config_example.yaml` - Configuration Documentation
- **Purpose:** Complete documentation of all options
- **Git:** βœ… Committed to Git
- **Contains:** All possible settings with explanations
- **Who uses it:** Reference when creating custom configs

#### 5. Your Custom Config (e.g., `my_agent_config.yaml`)
- **Purpose:** Your personal agent configuration
- **Git:** ❌ NOT committed (optional)
- **Contains:** Custom personality, strategies, etc.
- **Who uses it:** You, when you want special behavior

---

## πŸ”’ Security Best Practices

### βœ… DO:
- Keep your `.env` file local only
- Use different API keys for development and production
- Rotate your API keys regularly
- Review `.gitignore` to ensure `.env` is excluded
- Use `.env.example` as a template for team members

### ❌ DON'T:
- Never commit `.env` to Git
- Never put API keys in configuration YAML files
- Never share your `.env` file
- Never hardcode API keys in Python code
- Never put API keys in commit messages or PR descriptions

### Checking Security

```bash
# Make sure .env is ignored by Git
git status

# .env should NOT appear in the list
# If it does, remove it:
git rm --cached .env
```

---

## πŸ› οΈ Development Workflow

### Using the Default Development Config

The simplest way - just use `config_dev.yaml`:

```python
from pycatan.ai.config import AIConfig

# Loads config_dev.yaml by default
config = AIConfig.from_file('pycatan/ai/config_dev.yaml')

# API key is automatically loaded from .env
api_key = config.get_api_key()  # Reads GEMINI_API_KEY from .env
```

### Creating a Custom Agent

1. **Copy the example config:**
```bash
cp pycatan/ai/config_example.yaml my_aggressive_agent.yaml
```

2. **Edit your config:**
```yaml
agent_name: "Aggressive Trader"
agent:
  personality: "aggressive"
  risk_tolerance: 0.8
  trade_willingness: 0.9
```

3. **Use it in your code:**
```python
config = AIConfig.from_file('my_aggressive_agent.yaml')
```

4. **The API key is still loaded from `.env`** - you don't need to specify it!

### Multiple Agents with Different Personalities

```python
# Agent 1: Aggressive
config1 = AIConfig.from_file('configs/aggressive.yaml')

# Agent 2: Defensive
config2 = AIConfig.from_file('configs/defensive.yaml')

# Agent 3: Balanced (default)
config3 = AIConfig.from_file('pycatan/ai/config_dev.yaml')

# All three use the same API key from .env
```

---

## πŸ“ Configuration Options

### LLM Settings

```yaml
llm:
  provider: "gemini"              # or "openai", "anthropic"
  model_name: "gemini-2.0-flash-exp"
  temperature: 0.7                # 0.0 = deterministic, 1.0 = creative
  max_tokens: 4096
```

### Agent Personality

```yaml
agent:
  personality: "balanced"         # aggressive, defensive, balanced, trading
  risk_tolerance: 0.5            # 0.0 = safe, 1.0 = risky
  
  # Strategic focus (0.0 to 1.0)
  focus_on_settlements: 0.6
  focus_on_cities: 0.7
  focus_on_roads: 0.5
  focus_on_dev_cards: 0.6
  
  # Trading behavior
  trade_willingness: 0.5         # 0.0 = never, 1.0 = always
  trade_fairness: 0.7            # How fair are trades
```

### Debug Settings

```yaml
debug:
  debug_mode: true               # Enable detailed logging
  log_prompts: true              # Log prompts sent to LLM
  log_responses: true            # Log LLM responses
  save_game_states: true         # Save states for analysis
```

---

## πŸ§ͺ Testing Your Setup

### Test 1: Check API Key

```python
from pycatan.ai.config import AIConfig

config = AIConfig()
try:
    api_key = config.get_api_key()
    print("βœ“ API key loaded successfully")
except ValueError as e:
    print(f"βœ— Error: {e}")
```

### Test 2: Load Configuration

```python
config = AIConfig.from_file('pycatan/ai/config_dev.yaml')
print(f"βœ“ Loaded config for: {config.agent_name}")
print(f"  Provider: {config.llm.provider}")
print(f"  Model: {config.llm.model_name}")
```

### Test 3: Run Full Test Suite

```bash
python examples/test_ai_config.py
```

---

## 🎯 Common Scenarios

### Scenario 1: "I just cloned the repo"

1. Create `.env`: `cp .env.example .env`
2. Add your API key to `.env`
3. Run tests: `python examples/test_ai_config.py`

### Scenario 2: "I want to test different personalities"

Use the existing configs:
- `config_dev.yaml` - Balanced agent
- Or create custom configs based on `config_example.yaml`

### Scenario 3: "I want to switch LLM providers"

Edit your config file:
```yaml
llm:
  provider: "openai"
  model_name: "gpt-4-turbo-preview"
  api_key_env_var: "OPENAI_API_KEY"
```

Add the key to `.env`:
```bash
OPENAI_API_KEY=sk-your-key-here
```

### Scenario 4: "My API key changed"

Just edit `.env` - no code changes needed!

---

## πŸ“š Next Steps

- Read [AI_ARCHITECTURE.md](AI_ARCHITECTURE.md) for system design
- Read [WORK_PLAN.md](WORK_PLAN.md) for development roadmap
- See [config_example.yaml](../pycatan/ai/config_example.yaml) for all options

---

## πŸ†˜ Troubleshooting

### "ModuleNotFoundError: No module named 'yaml'"
```bash
pip install pyyaml
```

### "API key not found"
1. Check `.env` exists in project root
2. Check the API key variable name matches
3. Try: `cat .env` to verify content

### "Configuration file not found"
- Use absolute paths or run from project root
- Check file extension (`.yaml` not `.yml`)

### "Still stuck?"
- Check [GitHub Issues](your-repo-url/issues)
- Review the test output: `python examples/test_ai_config.py`

---

**Last Updated:** January 3, 2026