File size: 7,400 Bytes
6141ba7
 
 
 
 
 
 
 
 
adca48b
 
 
 
bd7f340
adca48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: PIPS Demo
colorFrom: blue
colorTo: green
sdk: gradio
app_file: src/pips/gradio_app.py
pinned: true
---

# PIPS: Python Iterative Problem Solving

**PIPS** (Python Iterative Problem Solving) is a powerful library for iterative code generation and refinement using Large Language Models (LLMs). It provides both programmatic APIs and a web interface for solving complex problems through iterative reasoning and code execution.

Paper: arxiv.org/abs/2510.22849

## Installation

### From PyPI (when available)
```bash
pip install pips-solver
```

### From Source
```bash
git clone <repository-url>
cd pips
pip install -e .
```

### With Optional Dependencies
```bash
# For web interface
pip install pips-solver[web]

# For development
pip install pips-solver[dev]

# All optional dependencies
pip install pips-solver[all]
```

## Quick Start

### 1. Command Line Interface

Start the web interface:
```bash
pips
# or
python -m pips

# Custom host and port
pips --host 127.0.0.1 --port 5000 --debug
```

### 2. Programmatic Usage

```python
from pips import PIPSSolver, get_model
from pips.utils import RawInput

# Initialize a model
model = get_model("gpt-4o", api_key="your-openai-api-key")

# Create solver
solver = PIPSSolver(
    model=model,
    max_iterations=8,
    temperature=0.0
)

# Solve a problem
problem = RawInput(
    text_input="What is the sum of the first 10 prime numbers?",
    image_input=None
)

# Chain of thought solving
answer, logs = solver.solve_chain_of_thought(problem)
print(f"Answer: {answer}")

# Code-based solving
answer, logs = solver.solve_with_code(problem)
print(f"Answer: {answer}")
```

### 3. Streaming Usage

```python
def on_token(token, iteration, model_name):
    print(f"Token: {token}", end="", flush=True)

def on_step(step, message, **kwargs):
    print(f"Step {step}: {message}")

callbacks = {
    "on_llm_streaming_token": on_token,
    "on_step_update": on_step
}

# Solve with streaming
answer, logs = solver.solve_with_code(
    problem, 
    stream=True, 
    callbacks=callbacks
)
```

## Supported Models

### OpenAI Models
- GPT-4o, GPT-4o-mini
- GPT-4, GPT-4-turbo
- GPT-3.5-turbo
- O1-preview, O1-mini
- O3-mini (when available)

### Anthropic Models
- Claude-3.5-sonnet
- Claude-3-opus, Claude-3-sonnet, Claude-3-haiku
- Claude-2.1, Claude-2.0

### Google Models
- Gemini-2.0-flash-exp
- Gemini-1.5-pro, Gemini-1.5-flash
- Gemini-1.0-pro

## API Reference

### PIPSSolver

The main solver class for iterative problem solving.

```python
PIPSSolver(
    model: LLMModel,
    max_iterations: int = 8,
    temperature: float = 0.0,
    max_tokens: int = 4096,
    top_p: float = 1.0
)
```

#### Methods

- `solve_chain_of_thought(sample, stream=False, callbacks=None)`: Solve using chain-of-thought reasoning
- `solve_with_code(sample, stream=False, callbacks=None)`: Solve using iterative code generation

### Model Factory

```python
from pips import get_model

# Get a model instance
model = get_model(model_name, api_key=None)
```

### Utilities

```python
from pips.utils import RawInput, img2base64, base642img

# Create input with text and optional image
input_data = RawInput(
    text_input="Your question here",
    image_input=PIL.Image.open("image.jpg")  # Optional
)
```

## Configuration

### Environment Variables

Set your API keys as environment variables:

```bash
export OPENAI_API_KEY="your-openai-key"
export ANTHROPIC_API_KEY="your-anthropic-key"  
export GOOGLE_API_KEY="your-google-key"
```

### Web Interface Settings

The web interface allows you to configure:
- Model selection
- API keys
- Solving mode (chain-of-thought vs code)
- Temperature, max tokens, iterations
- Code execution timeout

## Examples

### Mathematical Problem
```python
problem = RawInput(
    text_input="Find the derivative of f(x) = x^3 + 2x^2 - 5x + 1",
    image_input=None
)
answer, logs = solver.solve_with_code(problem)
```

### Image-Based Problem
```python
from PIL import Image

image = Image.open("chart.png")
problem = RawInput(
    text_input="What is the trend shown in this chart?",
    image_input=image
)
answer, logs = solver.solve_chain_of_thought(problem)
```

### Multi-Step Reasoning
```python
problem = RawInput(
    text_input="""
    A company has 3 departments with 10, 15, and 20 employees respectively.
    If they want to form a committee with 2 people from each department,
    how many different committees are possible?
    """,
    image_input=None
)
answer, logs = solver.solve_with_code(problem)
```

## Web Interface

The web interface provides:
- **Problem Input**: Text area with optional image upload
- **Model Selection**: Choose from available LLM providers
- **Settings Panel**: Configure solving parameters
- **Real-time Streaming**: Watch the AI solve problems step-by-step
- **Chat History**: Review previous solutions
- **Export Options**: Download chat logs and solutions

## Session Management

PIPS includes comprehensive session management capabilities:

### Automatic Session Loading
- **First Launch**: Automatically loads curated example sessions demonstrating PIPS capabilities
- **Persistent Storage**: All sessions are saved in browser localStorage for persistence across visits
- **Smart Cleanup**: Automatically removes incomplete or invalid sessions

### Import/Export Sessions
- **Bulk Export**: Export all sessions as a JSON file via the "Export" button
- **Individual Export**: Download single sessions using the download icon next to each session
- **Import Sessions**: Import previously exported session files via the "Import" button
- **Duplicate Detection**: Automatically detects and handles duplicate sessions during import

### Session Format
Sessions are exported in a portable JSON format:
```json
{
  "exportDate": "2024-01-15T10:00:00.000Z",
  "sessions": {
    "session_id": {
      "id": "session_id",
      "title": "Session title", 
      "problemText": "Original problem description",
      "image": "base64_image_data_or_null",
      "createdAt": "2024-01-15T09:00:00.000Z",
      "lastUsed": "2024-01-15T09:15:00.000Z", 
      "status": "completed|interrupted|solving|active",
      "chatHistory": [
        {
          "sender": "PIPS|AI Assistant|User",
          "content": "Message content",
          "iteration": "Iteration 1",
          "timestamp": "2024-01-15T09:01:00.000Z"
        }
      ]
    }
  }
}
```

### Session States
- **Active**: New sessions where users can input problems
- **Solving**: Sessions currently being processed by PIPS  
- **Completed**: Successfully finished sessions (read-only)
- **Interrupted**: Sessions stopped by user or error (read-only)

## Development

### Setup Development Environment
```bash
git clone <repository-url>
cd pips
pip install -e .[dev]
```

### Running Tests
```bash
pytest
pytest --cov=pips  # With coverage
```

### Code Formatting
```bash
black pips/
isort pips/
flake8 pips/
mypy pips/
```

## Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License

This project is licensed under the MIT License - see the LICENSE file for details.

## Acknowledgments

- OpenAI for GPT models
- Anthropic for Claude models  
- Google for GenAI models
- Flask and SocketIO communities