Skip to content

Commit 83f203f

Browse files
committed
Add Anthropic API support
- Including image and text. - Make it back-compatible.
1 parent fc3ee8b commit 83f203f

4 files changed

Lines changed: 341 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,41 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.2.0] - 2026-02-14
9+
10+
### Added
11+
- **Anthropic API support:** `ask2api` now supports Anthropic's Claude models (Opus 4.6, Sonnet 4.5, Haiku 4.5) in addition to OpenAI
12+
- **Provider selection:** New `ASK2API_PROVIDER` environment variable to explicitly choose between `openai` and `anthropic`
13+
- **Provider-agnostic API key:** New `ASK2API_API_KEY` environment variable that works with any provider
14+
- **Anthropic-specific functions:**
15+
- `convert_schema_to_anthropic_tool()` - Converts JSON Schema to Anthropic tool definition
16+
- `build_anthropic_payload()` - Builds API payload using tool calling format
17+
- `build_anthropic_headers()` - Sets Anthropic-specific headers (x-api-key, anthropic-version)
18+
- `parse_anthropic_response()` - Extracts structured output from tool_use response
19+
- `prepare_anthropic_image_content()` - Handles Anthropic's image format
20+
- `convert_content_for_anthropic()` - Converts multimodal content between formats
21+
- **Provider constants:** `OPENAI_BASE_URL`, `ANTHROPIC_BASE_URL`, `OPENAI_DEFAULT_MODEL`, `ANTHROPIC_DEFAULT_MODEL`, `ANTHROPIC_VERSION`
22+
- **Intelligent API key fallback:** Automatically falls back to `ANTHROPIC_API_KEY` or `OPENAI_API_KEY` based on selected provider
23+
- **Vision support for Anthropic:** Image analysis works with both URL and base64-encoded local files
24+
- **Enhanced help text:** CLI help now shows provider-specific environment variables
25+
26+
### Changed
27+
- **Config class refactored:** Added `provider` field with automatic provider-specific defaults
28+
- **Provider routing:** `generate_api_response()` now routes between OpenAI and Anthropic based on configuration
29+
- **Image handling:** `prepare_image_content()` now accepts provider parameter for format-specific handling
30+
- **Function naming:** Renamed OpenAI-specific functions for clarity:
31+
- `build_payload()``build_openai_payload()`
32+
- `build_headers()``build_openai_headers()`
33+
- **Response parsing:** Extracted OpenAI response parsing into `parse_openai_response()` function
34+
- **Default provider:** Defaults to `openai` when no provider is specified (100% backward compatible)
35+
- **Documentation:** Updated README with comprehensive provider support guide and examples
36+
37+
### Technical Details
38+
- Anthropic support uses tool calling to achieve structured output (no native `json_schema` format)
39+
- Single-file architecture maintained (~330 → 501 lines)
40+
- Zero new dependencies (still only `requests`)
41+
- Clean separation between OpenAI and Anthropic code paths
42+
843
## [1.1.3] - 2025-12-30
944

1045
### Added

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,46 @@ export ASK2API_API_KEY="your_api_key"
3838
# export OPENAI_API_KEY="your_api_key"
3939
```
4040

41+
## Provider Support
42+
43+
`ask2api` supports both OpenAI (and OpenAI-compatible) and Anthropic (Claude) models.
44+
45+
### Using OpenAI (default)
46+
47+
By default, `ask2api` uses OpenAI. No additional configuration is needed:
48+
49+
```bash
50+
export OPENAI_API_KEY="sk-..."
51+
ask2api -p "What is 2+2?" -e '{"result": 1}'
52+
```
53+
54+
### Using Anthropic Claude
55+
56+
To use Anthropic's Claude models, set the provider explicitly:
57+
58+
```bash
59+
export ASK2API_PROVIDER="anthropic"
60+
export ANTHROPIC_API_KEY="sk-ant-..."
61+
ask2api -p "What is the capital of France?" -e '{"country": "string", "city": "string"}'
62+
```
63+
64+
You can also customize the model:
65+
66+
```bash
67+
export ASK2API_PROVIDER="anthropic"
68+
export ASK2API_API_KEY="sk-ant-..."
69+
export ASK2API_MODEL="claude-opus-4-5"
70+
ask2api -p "Analyze carbon" -e '{"symbol": "string", "atomic_number": 1}'
71+
```
72+
73+
Anthropic's Claude models support vision as well:
74+
75+
```bash
76+
export ASK2API_PROVIDER="anthropic"
77+
export ANTHROPIC_API_KEY="sk-ant-..."
78+
ask2api -p "What's in this image?" -e '{"description": "string"}' -i photo.jpg
79+
```
80+
4181
## Usage
4282

4383
### Text-only prompts
@@ -141,12 +181,14 @@ wget -O place.jpg "https://upload.wikimedia.org/wikipedia/commons/6/64/Lesdeuxma
141181
## How it works
142182

143183
1. You define the desired output structure using a JSON Schema.
144-
2. The schema is passed to the model using OpenAI's `json_schema` structured output format.
184+
2. The schema is passed to the model:
185+
- For OpenAI: using the `json_schema` structured output format
186+
- For Anthropic: using tool calling with forced tool use
145187
3. The system prompt enforces strict JSON-only responses.
146188
4. For vision tasks, images are automatically encoded (base64 for local files) or passed as URLs.
147189
5. The CLI prints the API-ready JSON output.
148190

149-
The model is treated as a deterministic API function.
191+
The model is treated as a deterministic API function, regardless of provider.
150192

151193
## Example schema
152194

0 commit comments

Comments
 (0)