Skip to content

Go implementation of Duckduckgo-chat api proxy to provide openai compatible API

License

Notifications You must be signed in to change notification settings

nerdneilsfield/go-ddg-chat-api

Repository files navigation

Go DuckDuckGo Chat API

DuckDuckGo Chat API

Artifact Hub Go Report Card Docker Image Size Dockerhub Downloads Number Github Downloads Number GitHub Release GitHub Workflow Status


A Go implementation that provides an OpenAI-compatible API interface for DuckDuckGo's chat service.

Features

  • OpenAI-compatible API endpoints
  • Support for streaming responses
  • Multiple model mappings
  • Token-based authentication
  • Proxy support
  • Health check endpoints
  • CORS enabled

Supported Models

"ddg/gpt-4o-mini" = "gpt-4o-mini"
"ddg/claude-3-haiku" = "claude-3-haiku-20240307"
"ddg/mixtral-8x7b" = "mistralai/Mixtral-8x7B-Instruct-v0.1"
"ddg/meta-Llama-3-1-70B-Instruct-Turbo" = "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"

Installation

  • Install the binary:
go install github.com/nerdneilsfield/go-ddg-chat-api@latest
  • Install from Releases

  • Install from docker:

# docker hub
docker pull nerdneils/go-ddg-chat-api
# ghcr
docker pull ghcr.io/nerdneilsfield/go-ddg-chat-api

Configuration

Create a config.toml file:

port = 8085
host = "0.0.0.0"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
tokens = ["duckduckgo-chat-api-token"]
ddg_chat_api_url = "https://duckduckgo.com/"

[model_mapping]
"ddg/gpt-4o-mini" = "gpt-4o-mini"
"ddg/claude-3-haiku" = "claude-3-haiku-20240307"
"ddg/mixtral-8x7b" = "mistralai/Mixtral-8x7B-Instruct-v0.1"
"ddg/meta-Llama-3-1-70B-Instruct-Turbo" = "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"

Usage

Run the server:

go-ddg-chat-api run config.toml

Check version:

go-ddg-chat-api version

Debug output:

go-ddg-chat-api run config.toml -v

Run with docker:

# normal output
docker run -d --name go-ddg-chat-api -p 8085:8085  -v $(pwd)/config.toml:/app/config.toml nerdneils/go-ddg-chat-api
# with proxy
docker run -d --name go-ddg-chat-api -p 8085:8085 -e HTTPS_PROXY=http://your-proxy-url:8080 -v $(pwd)/config.toml:/app/config.toml nerdneils/go-ddg-chat-api
# debug output
docker run -d --name go-ddg-chat-api -p 8085:8085  -v $(pwd)/config.toml:/app/config.toml nerdneils/go-ddg-chat-api /app/go-ddg-chat-api run /app/config.toml -v

API Endpoints

  • GET /v1/models - List available models
  • POST /v1/chat/completions - Create chat completion
  • DELETE /v1/chat/completions/{id} - Delete chat completion
  • GET /live - Liveness probe
  • GET /ready - Readiness probe

Chat Completion Example

curl -X POST http://localhost:8085/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-token" \
  -d '{
    "model": "ddg/claude-3-haiku",
    "messages": [
      {"role": "user", "content": "Hello, how are you?"}
    ],
    "stream": true
  }'

Environment Variables

  • HTTPS_PROXY or https_proxy - Proxy server URL (optional)

Development

Requirements:

  • Go 1.21 or higher

Build from source:

git clone https://github.com/nerdneilsfield/go-ddg-chat-api
cd go-ddg-chat-api
go build

The Chat Completion Process

                                     +-----------------+
                                     |                 |
                                     |  Client Request |
                                     |                 |
                                     +--------+--------+
                                              |
                                              v
+----------------+    +-----------------+    +----------------------+
|                |    |                 |    |                      |
| Auth Middleware+--->+ Chat Completion +--->+ Generate UUID        |
|                |    | Handler         |    | for Conversation     |
+----------------+    +-----------------+    +----------------------+
                                              |
                                              v
                     +-----------------+    +----------------------+
                     |                 |    |                      |
                     | Stream Response |<---+ Create Response      |
                     | Channel         |    | Channel             |
                     +--------+--------+    +----------------------+
                              |
                              v
+-----------------+    +---------------------+    +------------------+
|                 |    |                     |    |                  |
| Get VQD Token   +<---+ Chat with DDG API   +--->+ Process Messages |
|                 |    |                     |    |                  |
+-----------------+    +---------------------+    +------------------+
         |                      |                          |
         v                      v                          v
+-----------------+    +---------------------+    +------------------+
|                 |    |                     |    |                  |
| Random UserAgent|    | Stream DDG Response |    | Update History   |
|                 |    |                     |    |                  |
+-----------------+    +---------------------+    +------------------+
                              |
                              v
                     +-----------------+
                     |                 |
                     | Client Response |
                     |                 |
                     +-----------------+

License

MIT License

Acknowledgments

References:

This project provides an OpenAI-compatible interface for DuckDuckGo's chat service. It is not affiliated with or endorsed by DuckDuckGo or OpenAI.

Star History

Star History Chart