Skip to content

Commit 728bbe3

Browse files
zerob13claude
andcommitted
feat: Add comprehensive AI API support with enhanced logging
- Added full Anthropic Claude API support (/anthropic/v1/models, /anthropic/v1/messages) - Added complete Google Gemini API support (/v1beta/models, :generateContent, :streamGenerateContent) - Enhanced request logging with detailed debugging information - Updated documentation with all three API endpoints and examples - Added proper error handling and 404 responses with available endpoints - Implemented streaming support for all APIs - Added comprehensive test examples for OpenAI, Anthropic, and Gemini APIs - Updated CLI and startup messages to show all supported APIs - Upgraded version to 1.0.5 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent f8365e6 commit 728bbe3

File tree

13 files changed

+707
-43
lines changed

13 files changed

+707
-43
lines changed

README.md

Lines changed: 164 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
*[中文说明](README.zh.md) | English*
1414

15-
A complete OpenAI API compatible mock server that returns predefined test data without calling real LLMs. Perfect for developing, testing, and debugging applications that use the OpenAI API.
15+
A complete **OpenAI, Anthropic, and Gemini API** compatible mock server that returns predefined test data without calling real LLMs. Perfect for developing, testing, and debugging applications that use these AI APIs.
1616

1717
## 🚀 Quick Start
1818

@@ -205,6 +205,8 @@ curl -X POST http://localhost:3000/v1/images/generations \
205205
## 🎯 Features
206206

207207
-**Full OpenAI API Compatibility**
208+
-**Anthropic Claude API Support**
209+
-**Google Gemini API Support**
208210
-**Support for streaming and non-streaming chat completions**
209211
-**Function calling support**
210212
-**Image generation support**
@@ -217,24 +219,31 @@ curl -X POST http://localhost:3000/v1/images/generations \
217219

218220
## 📋 Supported API Endpoints
219221

220-
### Model Management
222+
### OpenAI Compatible Endpoints
221223
- `GET /v1/models` - Get available model list
222224
- `GET /models` - Compatible endpoint
223-
224-
### Chat Completions
225225
- `POST /v1/chat/completions` - Create chat completion
226226
- `POST /chat/completions` - Compatible endpoint
227-
228-
### Image Generation
229227
- `POST /v1/images/generations` - Generate images
230228
- `POST /images/generations` - Compatible endpoint
231229

230+
### Anthropic Compatible Endpoints
231+
- `GET /anthropic/v1/models` - Get Anthropic model list
232+
- `POST /anthropic/v1/messages` - Create message (Claude API)
233+
234+
### Gemini Compatible Endpoints
235+
- `GET /v1beta/models` - Get Gemini model list
236+
- `POST /v1beta/models/{model}:generateContent` - Generate content
237+
- `POST /v1beta/models/{model}:streamGenerateContent` - Generate content (streaming)
238+
232239
### Health Check
233240
- `GET /health` - Server health status
234241

235242
## 🤖 Available Models
236243

237-
### 1. mock-gpt-thinking
244+
### OpenAI Compatible Models
245+
246+
#### 1. mock-gpt-thinking
238247
**Thinking Model** - Shows reasoning process, perfect for debugging logic
239248

240249
```json
@@ -246,7 +255,7 @@ curl -X POST http://localhost:3000/v1/images/generations \
246255

247256
Response will include `<thinking>` tags showing the reasoning process.
248257

249-
### 2. gpt-4-mock
258+
#### 2. gpt-4-mock
250259
**Function Calling Model** - Supports tools and function calling
251260

252261
```json
@@ -269,7 +278,7 @@ Response will include `<thinking>` tags showing the reasoning process.
269278
}
270279
```
271280

272-
### 3. mock-gpt-markdown
281+
#### 3. mock-gpt-markdown
273282
**Markdown Sample Model** - Outputs standard Markdown format plain text
274283

275284
```json
@@ -282,7 +291,7 @@ Response will include `<thinking>` tags showing the reasoning process.
282291
Response will be a complete Markdown document with various formatting elements, perfect for frontend UI debugging.
283292
**Note:** This model focuses on content display and doesn't support function calling to maintain output purity.
284293

285-
### 4. gpt-4o-image
294+
#### 4. gpt-4o-image
286295
**Image Generation Model** - Specialized for image generation
287296

288297
```json
@@ -297,6 +306,45 @@ Response will be a complete Markdown document with various formatting elements,
297306

298307
Supports various sizes and quality settings, returns high-quality simulated images.
299308

309+
### Anthropic Compatible Models
310+
311+
#### 1. mock-claude-markdown
312+
**Claude Markdown Model** - Anthropic-compatible markdown generation
313+
314+
```json
315+
{
316+
"model": "mock-claude-markdown",
317+
"messages": [{"role": "user", "content": "Hello"}],
318+
"max_tokens": 1000
319+
}
320+
```
321+
322+
### Gemini Compatible Models
323+
324+
#### 1. gemini-1.5-pro
325+
**Advanced Multimodal Model** - Google's most advanced model
326+
327+
```json
328+
{
329+
"contents": [
330+
{
331+
"parts": [
332+
{"text": "Explain quantum computing"}
333+
]
334+
}
335+
]
336+
}
337+
```
338+
339+
#### 2. gemini-1.5-flash
340+
**Fast Response Model** - Optimized for quick responses
341+
342+
#### 3. gemini-pro
343+
**Versatile Model** - For various tasks including function calling
344+
345+
#### 4. gemini-pro-vision
346+
**Multimodal Model** - Understands both text and images
347+
300348
## 🛠️ Development
301349

302350
### Local Development
@@ -441,41 +489,40 @@ docker run -p 3000:3000 my-mock-openai-api
441489

442490
### Testing with curl
443491

444-
```bash
445-
# Test public service
446-
curl https://mockllm.anya2a.com/health
447-
curl https://mockllm.anya2a.com/v1/models \
448-
-H "Authorization: Bearer DeepChat"
492+
#### OpenAI API Testing
449493

450-
# Test local service
494+
```bash
495+
# Test OpenAI health check
451496
curl http://localhost:3000/health
497+
498+
# Test OpenAI models
452499
curl http://localhost:3000/v1/models
453500

454-
# Test thinking model
501+
# Test OpenAI thinking model
455502
curl -X POST http://localhost:3000/v1/chat/completions \
456503
-H "Content-Type: application/json" \
457504
-d '{
458505
"model": "mock-gpt-thinking",
459506
"messages": [{"role": "user", "content": "Explain recursion"}]
460507
}'
461508

462-
# Test function calling
509+
# Test OpenAI function calling
463510
curl -X POST http://localhost:3000/v1/chat/completions \
464511
-H "Content-Type: application/json" \
465512
-d '{
466513
"model": "gpt-4-mock",
467514
"messages": [{"role": "user", "content": "What time is it now?"}]
468515
}'
469516

470-
# Test Markdown output
517+
# Test OpenAI markdown output
471518
curl -X POST http://localhost:3000/v1/chat/completions \
472519
-H "Content-Type: application/json" \
473520
-d '{
474521
"model": "mock-gpt-markdown",
475522
"messages": [{"role": "user", "content": "Any content"}]
476523
}'
477524

478-
# Test streaming output
525+
# Test OpenAI streaming
479526
curl -X POST http://localhost:3000/v1/chat/completions \
480527
-H "Content-Type: application/json" \
481528
-d '{
@@ -484,7 +531,7 @@ curl -X POST http://localhost:3000/v1/chat/completions \
484531
"stream": true
485532
}'
486533

487-
# Test image generation
534+
# Test OpenAI image generation
488535
curl -X POST http://localhost:3000/v1/images/generations \
489536
-H "Content-Type: application/json" \
490537
-d '{
@@ -495,6 +542,102 @@ curl -X POST http://localhost:3000/v1/images/generations \
495542
}'
496543
```
497544

545+
#### Anthropic API Testing
546+
547+
```bash
548+
# Test Anthropic models
549+
curl http://localhost:3000/anthropic/v1/models
550+
551+
# Test Anthropic messages
552+
curl -X POST http://localhost:3000/anthropic/v1/messages \
553+
-H "Content-Type: application/json" \
554+
-d '{
555+
"model": "mock-claude-markdown",
556+
"messages": [{"role": "user", "content": "Hello Claude"}],
557+
"max_tokens": 1000
558+
}'
559+
560+
# Test Anthropic streaming
561+
curl -X POST http://localhost:3000/anthropic/v1/messages \
562+
-H "Content-Type: application/json" \
563+
-d '{
564+
"model": "mock-claude-markdown",
565+
"messages": [{"role": "user", "content": "Tell me about AI"}],
566+
"max_tokens": 1000,
567+
"stream": true
568+
}'
569+
```
570+
571+
#### Gemini API Testing
572+
573+
```bash
574+
# Test Gemini models
575+
curl http://localhost:3000/v1beta/models
576+
577+
# Test Gemini content generation (matching official API format)
578+
curl -X POST http://localhost:3000/v1beta/models/gemini-1.5-pro:generateContent \
579+
-H "Content-Type: application/json" \
580+
-d '{
581+
"contents": [
582+
{
583+
"parts": [
584+
{
585+
"text": "Explain how AI works in a few words"
586+
}
587+
]
588+
}
589+
],
590+
"generationConfig": {
591+
"thinkingConfig": {
592+
"thinkingBudget": 0
593+
}
594+
}
595+
}'
596+
597+
# Test Gemini streaming
598+
curl -X POST http://localhost:3000/v1beta/models/gemini-1.5-flash:streamGenerateContent \
599+
-H "Content-Type: application/json" \
600+
-d '{
601+
"contents": [
602+
{
603+
"parts": [
604+
{
605+
"text": "Write a short story about a robot"
606+
}
607+
]
608+
}
609+
]
610+
}'
611+
612+
# Test different Gemini model
613+
curl -X POST http://localhost:3000/v1beta/models/gemini-pro:generateContent \
614+
-H "Content-Type: application/json" \
615+
-d '{
616+
"contents": [
617+
{
618+
"parts": [
619+
{
620+
"text": "What are the benefits of renewable energy?"
621+
}
622+
]
623+
}
624+
],
625+
"generationConfig": {
626+
"temperature": 0.7,
627+
"maxOutputTokens": 1000
628+
}
629+
}'
630+
```
631+
632+
#### Public Service Testing
633+
634+
```bash
635+
# Test public service
636+
curl https://mockllm.anya2a.com/health
637+
curl https://mockllm.anya2a.com/v1/models \
638+
-H "Authorization: Bearer DeepChat"
639+
```
640+
498641
### Testing with OpenAI SDK
499642

500643
```javascript

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mock-openai-api",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "A mock OpenAI Compatible Provider API server",
55
"keywords": [
66
"openai",

src/app.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ app.use(express.urlencoded({ extended: true }));
1717
// Request logging middleware (conditional)
1818
app.use((req, res, next) => {
1919
if (global.verboseLogging) {
20-
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
21-
console.log(`Original URL: ${req.originalUrl}`);
22-
console.log(`Base URL: ${req.baseUrl}`);
20+
console.log(`🌐 ${new Date().toISOString()} - ${req.method} ${req.path}`);
21+
console.log(`🔗 Original URL: ${req.originalUrl}`);
22+
console.log(`🔗 Base URL: ${req.baseUrl}`);
23+
console.log(`🔗 Headers:`, JSON.stringify(req.headers, null, 2));
2324
if (req.body && Object.keys(req.body).length > 0) {
24-
console.log("Request body:", JSON.stringify(req.body, null, 2));
25+
console.log("📄 Request body:", JSON.stringify(req.body, null, 2));
2526
}
2627
}
2728
next();
@@ -38,6 +39,11 @@ app.get("/", (req, res) => {
3839
"GET /v1/models",
3940
"POST /v1/chat/completions",
4041
"POST /v1/images/generations",
42+
"GET /anthropic/v1/models",
43+
"POST /anthropic/v1/messages",
44+
"GET /v1beta/models",
45+
"POST /v1beta/models/{model}:generateContent",
46+
"POST /v1beta/models/{model}:streamGenerateContent",
4147
],
4248
});
4349
});
@@ -47,11 +53,20 @@ app.use("/", routes);
4753

4854
// 404 handler
4955
app.use((req, res) => {
50-
if (global.verboseLogging) {
51-
console.log(`404 - Path not found: ${req.originalUrl}`);
52-
console.log(`Method: ${req.method}`);
53-
console.log(`Headers:`, req.headers);
54-
}
56+
console.log(`❌ 404 - Path not found: ${req.originalUrl}`);
57+
console.log(`❌ Method: ${req.method}`);
58+
console.log(`❌ Headers:`, JSON.stringify(req.headers, null, 2));
59+
console.log(`❌ Available routes:`);
60+
console.log(` - GET /health`);
61+
console.log(` - GET /v1/models`);
62+
console.log(` - POST /v1/chat/completions`);
63+
console.log(` - POST /v1/images/generations`);
64+
console.log(` - GET /anthropic/v1/models`);
65+
console.log(` - POST /anthropic/v1/messages`);
66+
console.log(` - GET /v1beta/models`);
67+
console.log(` - POST /v1beta/models/{model}:generateContent`);
68+
console.log(` - POST /v1beta/models/{model}:streamGenerateContent`);
69+
5570
res.status(404).json({
5671
error: {
5772
message: `Path not found: ${req.originalUrl}`,
@@ -60,6 +75,17 @@ app.use((req, res) => {
6075
method: req.method,
6176
path: req.path,
6277
originalUrl: req.originalUrl,
78+
availableEndpoints: [
79+
"GET /health",
80+
"GET /v1/models",
81+
"POST /v1/chat/completions",
82+
"POST /v1/images/generations",
83+
"GET /anthropic/v1/models",
84+
"POST /anthropic/v1/messages",
85+
"GET /v1beta/models",
86+
"POST /v1beta/models/{model}:generateContent",
87+
"POST /v1beta/models/{model}:streamGenerateContent",
88+
],
6389
},
6490
});
6591
});

0 commit comments

Comments
 (0)