An open API service indexing awesome lists of open source software.

https://github.com/honwhy/dpnp


https://github.com/honwhy/dpnp

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

          

# DeepSeek Proxy Service

A Hono + TypeScript proxy service that forwards OpenAI-compatible API requests to the DeepSeek API, supporting both streaming and non-streaming modes.

## Features

- ✅ Full OpenAI API compatibility
- ✅ Streaming support (Server-Sent Events)
- ✅ Non-streaming request support
- ✅ Request/response transformation
- ✅ Authentication middleware
- ✅ CORS support
- ✅ Comprehensive error handling
- ✅ TypeScript strict mode
- ✅ Request validation with Zod

## Quick Start

### 1. Install Dependencies

```bash
npm install
```

### 2. Configure Environment

Copy `.env.example` to `.env`:

```bash
cp .env.example .env
```

Edit `.env`:
```env
PORT=3000
NODE_ENV=development
DEEPSEEK_BASE_URL=https://api.deepseek.com/v1
```

**Note:** The API key is now provided by clients through the Authorization header (see Usage section below).

### 3. Run in Development Mode

```bash
npm run dev
```

The server will start on `http://localhost:3000`

### 4. Build for Production

```bash
npm run build
npm start
```

## API Endpoints

### Chat Completions

**POST** `/v1/chat/completions`

Send chat completion requests in OpenAI format.

#### Non-Streaming Request

```bash
curl -X POST http://localhost:3000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-deepseek-api-key" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "user", "content": "Hello!"}
],
"stream": false,
"temperature": 0.7,
"max_tokens": 100
}'
```

#### Streaming Request

```bash
curl -N -X POST http://localhost:3000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-deepseek-api-key" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "user", "content": "Hello!"}
],
"stream": true
}'
```

**Important:** Replace `sk-your-deepseek-api-key` with your actual DeepSeek API key. The proxy will forward this key to authenticate with the DeepSeek API.

#### Response Format

```json
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "deepseek-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 15,
"total_tokens": 25
}
}
```

## Usage with OpenAI SDK

```
import OpenAI from 'openai';

const openai = new OpenAI({
baseURL: 'http://localhost:3000/v1',
apiKey: 'sk-your-deepseek-api-key', // Provide your DeepSeek API key
});

// Non-streaming
const completion = await openai.chat.completions.create({
model: 'deepseek-chat',
messages: [{ role: 'user', content: 'Hello!' }],
});

console.log(completion.choices[0].message.content);

// Streaming
const stream = await openai.chat.completions.create({
model: 'deepseek-chat',
messages: [{ role: 'user', content: 'Hello!' }],
stream: true,
});

for await (const chunk of stream) {
process.stdout.write(chunk.choices[0].delta.content || '');
}
```

**Note:** The `apiKey` you provide here will be used to authenticate with the DeepSeek API. Make sure to use a valid DeepSeek API key.

## Project Structure

```
src/
├── index.ts # Entry point and server setup
├── config/
│ └── index.ts # Configuration manager
├── routes/
│ └── chat.ts # Chat completion routes
├── services/
│ ├── proxy.ts # Core proxy logic
│ └── transformer.ts # Request/response transformers
├── middleware/
│ ├── auth.ts # Authentication middleware
│ └── logger.ts # Request logging
└── types/
├── openai.ts # TypeScript type definitions
└── validation.ts # Zod validation schemas
```

## Environment Variables

| Variable | Description | Default | Required |
|----------|-------------|---------|----------|
| `DEEPSEEK_API_KEY` | Your DeepSeek API key | - | ✅ |
| `DEEPSEEK_BASE_URL` | DeepSeek API base URL | `https://api.deepseek.com/v1` | ❌ |
| `PORT` | Server port | `3000` | ❌ |
| `NODE_ENV` | Environment | `development` | ❌ |

## Development

### Available Scripts

- `npm run dev` - Start development server with hot reload
- `npm run build` - Build for production
- `npm start` - Start production server

### Code Quality

The project uses:
- TypeScript strict mode
- ESLint (recommended)
- Prettier (recommended)
- Zod for runtime validation

## Error Handling

The proxy returns errors in OpenAI format:

```json
{
"error": {
"message": "Error description",
"type": "invalid_request_error",
"param": null,
"code": "validation_error"
}
}
```

## Security Notes

- Never expose your DeepSeek API key to clients
- The proxy handles authentication internally
- Consider adding rate limiting in production
- Use HTTPS in production environments

## License

MIT