新用戶新用戶即享 DNS 與 CDN 免費方案。建立帳號

Getting Started with the Yeap LLM API Using the OpenAI SDK

The Yeap LLM API is OpenAI-compatible: keep your SDK and point base_url at https://llm.yeap.cloud/v1. Covers creating a key, Python and Node.js examples, streaming, embeddings, and per-key quota, model and IP limits.

The Yeap LLM API is an OpenAI-compatible model gateway. The goal is simple: no new SDK to learn and no code to rewrite. Keep your existing OpenAI SDK, change base_url to https://llm.yeap.cloud/v1, use a Yeap API key, and you are ready to go. Usage is billed per token from your Yeap account balance, the same balance your other Yeap products use.

Available endpoints

EndpointPurpose
POST /v1/chat/completionsChat completions, with streaming
POST /v1/completionsText completions
POST /v1/embeddingsEmbeddings
POST /v1/images/generationsImage generation
POST /v1/audio/transcriptionsSpeech to text
GET /v1/modelsList the models available to your key

Every endpoint authenticates with Authorization: Bearer <key>. Models and prices change over time, and the authoritative list is always GET /v1/models or the model list in the console, so the examples below use placeholders instead of specific model names.

Step 1: Create an API key

Sign in at console.yeap.cloud, go to LLM → Tokens and create a key. You can set:

  • Name: we suggest naming by project or environment, for example blog-summarizer-prod;
  • Quota: a spending limit for this key, or unlimited;
  • Model limits: restrict the key to specific models;
  • IP limits: accept requests only from specific IPs;
  • Expiry: the key stops working automatically after this time.

Keys start with sk-; save yours as soon as it is created. Use a separate key per project and per environment, and give non-production keys a low quota, so that a leaked key has a bounded impact.

Keep the key in an environment variable rather than in your repository:

export YEAP_API_KEY="sk-..."

Step 2: Check connectivity with curl

curl https://llm.yeap.cloud/v1/models \
  -H "Authorization: Bearer $YEAP_API_KEY"

In the JSON response, the id of each item in the data array is a model name you can use in requests.

Step 3: Python

Install the official SDK with pip install openai.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["YEAP_API_KEY"],
    base_url="https://llm.yeap.cloud/v1",
)

resp = client.chat.completions.create(
    model="your-model-id",  # pick one from /v1/models
    messages=[
        {"role": "system", "content": "You are a concise technical assistant."},
        {"role": "user", "content": "Explain what a CDN is in one sentence."},
    ],
)

print(resp.choices[0].message.content)
print(resp.usage)  # prompt_tokens / completion_tokens / total_tokens

Streaming

Chat applications usually want to show output as it is generated. Set stream=True and the response arrives incrementally as server-sent events:

stream = client.chat.completions.create(
    model="your-model-id",
    messages=[{"role": "user", "content": "Write a short poem about cache hits."}],
    stream=True,
)

for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Embeddings

emb = client.embeddings.create(
    model="your-embedding-model-id",
    input=["Yeap CDN supports HTTP/3", "How to purge the cache"],
)
vectors = [item.embedding for item in emb.data]
print(len(vectors), len(vectors[0]))

Image generation and transcription

The image and audio endpoints use the SDK's standard methods too; just use a model of the matching type:

# Image generation
img = client.images.generate(
    model="your-image-model-id",
    prompt="A minimalist illustration of a server room",
    size="1024x1024",
)
print(img.data[0].url or "base64 image returned")

# Speech to text
with open("meeting.mp3", "rb") as f:
    tr = client.audio.transcriptions.create(
        model="your-transcription-model-id",
        file=f,
    )
print(tr.text)

Models may return images either as a URL or as base64, so it is worth handling both in your code.

Habits that keep costs predictable

  • Set max_tokens. Output tokens usually cost more than input tokens. A sensible output cap for each kind of request prevents surprise bills from a model that keeps talking.
  • Trim your context. In multi-turn conversations the whole history is resent on every turn. Summarizing or truncating older turns cuts input tokens substantially.
  • Match the model to the task. Classification, extraction and rewriting often work fine on a smaller model; save the larger ones for tasks that really need reasoning. Compare a few models in the Playground before picking a default.
  • Let key quotas be your backstop. Even if a bug makes your code call the API in a loop, the key's quota stops spending from running away.

Step 4: Node.js

Install with npm install openai.

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.YEAP_API_KEY,
  baseURL: "https://llm.yeap.cloud/v1",
});

const stream = await client.chat.completions.create({
  model: "your-model-id",
  messages: [{ role: "user", content: "List three ways to reduce API latency." }],
  stream: true,
});

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

Switching without code changes

The official OpenAI SDKs read the OPENAI_BASE_URL and OPENAI_API_KEY environment variables. If a tool or script already uses the OpenAI SDK and you would rather not touch its code, change only the environment:

export OPENAI_BASE_URL="https://llm.yeap.cloud/v1"
export OPENAI_API_KEY="$YEAP_API_KEY"

The same works for most third-party tools that support a custom API endpoint: set the endpoint to https://llm.yeap.cloud/v1 and the key to your Yeap key.

Errors and retries

In production, plan for failure:

  • 401: the key is invalid or expired, or the request came from an address outside the key's IP allowlist. Check the key's settings.
  • Insufficient quota: requests are rejected when a key's quota is used up or the account balance is too low, and the error message says why. Raise the key's quota or top up your balance in the console.
  • 429 and 5xx: these are usually transient and should be retried with backoff. The OpenAI SDKs retry automatically; tune the count with max_retries and the per-request timeout with timeout.
client = OpenAI(
    api_key=os.environ["YEAP_API_KEY"],
    base_url="https://llm.yeap.cloud/v1",
    max_retries=3,
    timeout=60.0,
)

Watching usage

Every request is logged with the model, token counts and cost. Under the LLM menu in the console you can browse request logs and statistics and see spending per key, which makes it easy to separate projects. To compare models before writing any code, try them in the console's Playground directly in your browser.

Summary

Getting started with the Yeap LLM API takes three things: a key, a base_url, and a model name from /v1/models. The rest of the code you have already written.

在 Yeap Cloud 上打造

新用戶即享 DNS 與 CDN 免費方案。

免費註冊