EasyXAI 文档
客户端集成

OpenAI & Anthropic SDK

在 Python 与 Node.js 中使用官方 OpenAI SDK 和 Anthropic SDK 接入 EasyXAI,涵盖基础对话、流式输出与提示词缓存示例。

EasyXAI 兼容官方 OpenAI SDK 与 Anthropic SDK,只需替换 base_url 与 API Key 即可直接调用,无需改动业务代码逻辑。

  • OpenAI SDK Base URL:https://api.easyx.ai/v1(中国大陆站:https://api.easyxai.cn/v1)
  • Anthropic SDK Base URL:https://api.easyx.ai(末尾不要加 /v1;中国大陆站:https://api.easyxai.cn)
  • 可用模型 ID:以当前 Key 的模型列表为准

准备环境变量

建议通过环境变量注入 API Key,避免将明文密钥写入源码:

export EASYXAI_API_KEY="easyx_ws_xxx"

OpenAI SDK(Python)

安装依赖:

pip install openai

基础对话(Chat Completions)

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.easyx.ai/v1",
    api_key=os.environ["EASYXAI_API_KEY"],
)

response = client.chat.completions.create(
    model="gpt-4.1",
    messages=[
        {"role": "developer", "content": "You are a concise assistant."},
        {"role": "user", "content": "Reply with OK and a 1-sentence greeting."},
    ],
)

print(response.choices[0].message.content)
print("usage:", response.usage)

流式输出(Streaming)

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.easyx.ai/v1",
    api_key=os.environ["EASYXAI_API_KEY"],
)

stream = client.chat.completions.create(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "Write three bullet points about prompt caching."}],
    stream=True,
    stream_options={"include_usage": True},
)

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

Responses 接口与自动前缀缓存

对于支持 OpenAI Responses 的模型,重复较长前缀会自动触发提示词缓存,可在响应的 usage.input_tokens_details.cached_tokens(Chat Completions 为 prompt_tokens_details.cached_tokens)中观察缓存读取量:

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.easyx.ai/v1",
    api_key=os.environ["EASYXAI_API_KEY"],
)

response = client.responses.create(
    model="gpt-4.1",
    instructions="You are a careful code reviewer.",
    input="Summarize why idempotency matters in payment APIs in two sentences.",
)

print(response.output_text)
print("cached_tokens:", response.usage.input_tokens_details.cached_tokens)

OpenAI SDK(Node.js / TypeScript)

安装依赖:

npm install openai

基础对话与流式输出

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.easyx.ai/v1",
  apiKey: process.env.EASYXAI_API_KEY,
});

// Minimal non-streaming chat completion
const completion = await client.chat.completions.create({
  model: "gpt-4.1",
  messages: [{ role: "user", content: "Reply with OK." }],
});
console.log(completion.choices[0]?.message.content);

// Streaming chat completion
const stream = await client.chat.completions.create({
  model: "gpt-4.1",
  messages: [{ role: "user", content: "List 3 tips for low-latency streaming." }],
  stream: true,
});

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

Responses 接口

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.easyx.ai/v1",
  apiKey: process.env.EASYXAI_API_KEY,
});

const response = await client.responses.create({
  model: "gpt-4.1",
  input: "Explain HTTP 429 retry backoff in one sentence.",
});

console.log(response.output_text);

Anthropic SDK(Python)

安装依赖:

pip install anthropic

基础对话(Messages)

注意 base_url 填写 https://api.easyx.ai(不含 /v1):

import os
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.easyx.ai",
    api_key=os.environ["EASYXAI_API_KEY"],
)

message = client.messages.create(
    model="claude-sonnet-4.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Reply with OK and your model role."}],
)

print(message.content[0].text)

流式输出(Streaming)

import os
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.easyx.ai",
    api_key=os.environ["EASYXAI_API_KEY"],
)

with client.messages.stream(
    model="claude-sonnet-4.5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain SSE streaming in three sentences."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
print()

提示词缓存(Prompt Caching)

在较长的可复用 system 或消息内容块上添加 "cache_control": {"type": "ephemeral"},连续请求命中缓存后可显著减少输入消耗,并可在控制台查看缓存命中率:

import os
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.easyx.ai",
    api_key=os.environ["EASYXAI_API_KEY"],
)

long_reference = "Project guidelines and architecture specification...\n" * 200

message = client.messages.create(
    model="claude-sonnet-4.5",
    max_tokens=512,
    system=[
        {
            "type": "text",
            "text": long_reference,
            "cache_control": {"type": "ephemeral"},
        }
    ],
    messages=[{"role": "user", "content": "Summarize the key constraints."}],
)

print(message.content[0].text)
print(
    "cache_creation:",
    message.usage.cache_creation_input_tokens,
    "cache_read:",
    message.usage.cache_read_input_tokens,
)

Anthropic SDK(Node.js / TypeScript)

安装依赖:

npm install @anthropic-ai/sdk

基础对话、流式输出与提示词缓存

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.easyx.ai",
  apiKey: process.env.EASYXAI_API_KEY,
});

// Streaming with prompt caching on a reusable system block
const longReference = "Project guidelines and architecture specification...\n".repeat(200);

const stream = client.messages.stream({
  model: "claude-sonnet-4.5",
  max_tokens: 512,
  system: [
    {
      type: "text",
      text: longReference,
      cache_control: { type: "ephemeral" },
    },
  ],
  messages: [{ role: "user", content: "Summarize the key constraints." }],
});

for await (const event of stream) {
  if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
    process.stdout.write(event.delta.text);
  }
}
process.stdout.write("\n");

const finalMessage = await stream.finalMessage();
console.log("usage:", finalMessage.usage);

更多协议文档