Polyglot-Gate-Server

command module
v1.0.0-beta.20241028 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2024 License: MIT Imports: 6 Imported by: 0

README

Polyglot-Gate-Server

简体中文 | English

A fast and flexible translation gateway that routes requests to various Large Language Model (LLM) endpoints with configurable rate limiting and model-specific prompting capabilities.

Key Features

  • Smart Routing: Dynamic routing of translation requests to different LLM endpoints
  • Rate Limiting: Configurable request frequency control for each LLM service
  • Custom Prompts: Model-specific prompt customization
  • Easy Configuration: Simple TOML-based configuration
  • Caching: In-memory caching to improve translation speed and reduce duplicate requests
  • Multiple Interfaces: Support for word selection translation and DeepL translation endpoints

Quick Start

Installation Options
  1. From Source
git clone https://github.com/nerdneilsfield/Polyglot-Gate-Server.git
cd Polyglot-Gate-Server
go build -o Polyglot-Gate-Server main.go
  1. Via Go Get
go install -u github.com/nerdneilsfield/Polyglot-Gate-Server@latest
  1. Docker
# Using Docker Hub
docker run -d --name polyglot-gate-server \
    -p 8080:8080 \
    -v ./config.toml:/app/config.toml \
    nerdneils/polyglot-gate-server

# Using GitHub Container Registry
docker run -d --name polyglot-gate-server \
    -p 8080:8080 \
    -v ./config.toml:/app/config.toml \
    ghcr.io/nerdneilsfield/polyglot-gate-server

Usage

# Run server
Polyglot-Gate-Server run <config_file_path>

# Generate sample config
Polyglot-Gate-Server gen <output_file_path>

# Validate config
Polyglot-Gate-Server valid <config_file_path>

# Show version
Polyglot-Gate-Server version

Configuration

Example configuration (config_example.toml):

[[models]]
name = "gpt-3.5-turbo"
base_url = "https://api.openai.com/v1"
type = "openai"
api_key = "your_api_key"
model_name = "gpt-3.5-turbo"
max_tokens = 1000
temperature = 0.5
prompt = "Translate the following %s text to %s: '%s'"
rate_limit = 10.0
endpoint = "/gpt-3.5-turbo"
cache_expire_hours = 72

Note: The %s placeholders in the prompt represent source language, target language, and text to translate respectively.

API Usage

API Documentation
GET /api/v1/models Returns a list of all supported models. Uses Bearer Token authentication.

Response:

{
  "models_by_endpoint": [
    "/gpt-3.5-turbo"
  ],
  "models_by_name": [
    "gpt-3.5-turbo"
  ]
}
POST /api/v1/translate Translates content. Uses Bearer Token authentication.

Request:

{
  "text": "Hello, world!",
  "from": "English",
  "to": "中文(简体)",
  "model_name": "gpt-3.5-turbo",
  "force_refresh": false
}

Response:

{
  "translated_text": "你好,世界!",
  "model_name": "gpt-3.5-turbo"
}

When force_refresh is set to true, it will force refresh the cache.

POST /api/v1/models/[endpoint] Translates content. Uses Bearer Token authentication.

Request:

{
  "text": "Hello, world!",
  "from": "English",
  "to": "中文(简体)",
  "model_name": "gpt-3.5-turbo",
  "force_refresh": false
}

Response:

{
  "translated_text": "你好,世界!"
}

When force_refresh is set to true, it will force refresh the cache.

POST /api/hcfy Selection translation. No authentication required.

Request:

{
  "name": "gpt-3.5-turbo",
  "text": "Hello, world!",
  "destination": ["中文(简体)", "English"],
  "source": "auto"
}

Response:

{
  "text": "你好,世界!",
  "from": "English",
  "to": "中文(简体)",
  "result": ["你好,世界!"]
}
POST /api/deeplx/[endpoint] Translates content using DeepL. No authentication required.

Request:

{
  "text": "Hello, world!",
  "source_lang": "auto",
  "target_lang": "ZH"
}

Response:

{
  "code": 200,
  "msg": "success",
  "data": "你好,世界!",
  "source_lang": "auto",
  "target_lang": "ZH",
  "alternatives": []
}

Project Structure

.
├── cmd/            # Command-line interface
├── internal/       # Core logic
│   ├── configs/    # Configuration handling
│   └── server/     # Server implementation
└── docker/         # Docker-related files

Contributing

Contributions are welcome! Please feel free to submit pull requests, create issues, or suggest improvements.

License

MIT License

Copyright (c) 2024 DengQi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Star History

Star History Chart

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL