ling-baseling-base

AI 中继

40+ provider 统一 LLM 中继层

在线 Playground

在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。

AI 中继 (relay)

详细专题:Chat · Streaming · 生产配置

文档导航

relay

Unified LLM relay layer for calling 40+ AI/LLM providers through a single, framework-agnostic API with integrated usage metering, retry, and circuit breaking.

Structure

relay/
├── client.go          # Client: unified entry point (Chat, Embed, Image, ...)
├── go.mod             # Separate Go module
├── channel/           # 40 provider adaptors (see below)
├── common/            # Adaptor interface + RelayInfo + shared types
├── common_handler/    # Shared response handlers (rerank, etc.)
├── constant/          # API type constants
├── helper/            # SSE streaming + response ID helpers
├── meter/             # Usage metering (tokens, images, audio seconds)
├── realtime/          # WebSocket realtime API connectors
├── relaykit/          # DTO types, reason maps, relay converters
├── relaymode/         # Relay mode constants (chat, embed, image, ...)
├── service/           # Adaptor-to-relaykit bridge utilities
├── setting/           # Global relay settings stubs
├── task/              # Async task types
└── types/             # Price data and shared types

Key Types

// Client is the unified entry point for all AI API calls.
type Client struct { ... }

// Provider is the high-level interface for AI API providers.
type Provider interface {
    Name() string
    ApiType() int
    Adaptor() common.Adaptor
}

// ChatRequest is the unified chat completion request.
type ChatRequest struct {
    Model       string    `json:"model"`
    Messages    []Message `json:"messages"`
    Temperature *float64  `json:"temperature,omitempty"`
    MaxTokens   *int      `json:"max_tokens,omitempty"`
    Stream      bool      `json:"stream,omitempty"`
    Tools       []Tool    `json:"tools,omitempty"`
    // ...
}

// ChatResponse is the unified chat completion response.
type ChatResponse struct {
    ID       string       `json:"id"`
    Model    string       `json:"model"`
    Choices  []ChatChoice `json:"choices"`
    Usage    meter.Usage  `json:"usage"`
    Provider string       `json:"provider"`
}

// ChatStreamResult holds the stream channel and final usage.
type ChatStreamResult struct {
    Ch    chan ChatStreamChunk
    Usage meter.Usage
}

Client Methods

MethodDescription
ChatSynchronous chat completion
ChatStreamStreaming chat completion (channel)
EmbedText embeddings
ImageImage generation
AudioAudio transcription
AudioTranslationAudio translation
RerankDocument reranking
ResponsesOpenAI Responses API
CompletionsLegacy completions
ModerationsContent moderation
SubmitTaskAsync task submission (Midjourney)
FetchTaskAsync task polling
MidjourneySubmitMidjourney image generation
SubmitSunoTaskSuno music generation

Supported Channels (40)

advancedcustom  ai360       ali          aws          baidu
baidu_v2        claude      cloudflare   codex        cohere
coze            deepseek    dify         gemini       huggingface
jimeng          jina        lingyiwanwu  minimax      mistral
mokaai          moonshot    newapi       ollama       openai
openrouter      palm        perplexity   replicate    siliconflow
sub2api         submodel    tencent      vertex       volcengine
xai             xinference  xunfei       zhipu        zhipu_4v

Quick Start

import (
    "github.com/LingByte/ling-base/relay"
    "github.com/LingByte/ling-base/relay/meter"
    "github.com/LingByte/ling-base/relay/channel/openai"
)

client := relay.New(
    relay.WithProvider(openai.NewProvider("sk-xxx")),
    relay.WithMeter(meter.NewMemoryMeter()),
)

// Synchronous chat
resp, err := client.Chat(ctx, &relay.ChatRequest{
    Model:    "gpt-4o",
    Messages: []relay.Message{{Role: "user", Content: json.RawMessage(`"Hello"`)}},
})

// Streaming chat
result, err := client.ChatStream(ctx, &relay.ChatRequest{
    Model:    "gpt-4o",
    Messages: []relay.Message{{Role: "user", Content: json.RawMessage(`"Tell me a story"`)}},
    Stream:   true,
})
for chunk := range result.Ch {
    fmt.Print(chunk.Delta)
}

Sub-packages

PackageDescription
channel40 provider adaptors (OpenAI, Claude, Gemini, etc.)
commonAdaptor interface, RelayInfo, shared request types
common_handlerShared response handlers (rerank, etc.)
constantAPI type and mode constants
helperSSE streaming, response ID generation helpers
meterUsage metering (tokens, images, audio/video seconds)
realtimeWebSocket realtime API connectors (OpenAI, etc.)
relaykitDTO types, reason maps, relay format converters
relaymodeRelay mode constants (chat, embed, image, audio, ...)
serviceAdaptor-to-relaykit bridge and response utilities
settingGlobal relay settings stubs (overridable by app)
taskAsync task types
typesPrice data and shared types

On this page