OneDim 文档中心

代码示例

Python、Node.js、cURL 多语言常用调用示例。

本页提供常用调用示例。统一使用:

  • api_key = YOUR_API_TOKEN
  • base_url = https://api.onedim.ai/v1

Python

安装 SDK

pip install openai

基础聊天

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_TOKEN",
    base_url="https://api.onedim.ai/v1",
)

response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "Hello!"}],
)

print(response.choices[0].message.content)

流式响应

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_TOKEN",
    base_url="https://api.onedim.ai/v1",
)

stream = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "写一首诗"}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

图像生成

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_TOKEN",
    base_url="https://api.onedim.ai/v1",
)

response = client.images.generate(
    model="dall-e-3",
    prompt="一只在太空中漫步的可爱猫咪",
    size="1024x1024",
    quality="standard",
    n=1,
)

print(response.data[0].url)

工具调用

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_TOKEN",
    base_url="https://api.onedim.ai/v1",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取指定城市的天气信息",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "城市名称"}
                },
                "required": ["city"],
            },
        },
    }
]

response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "北京今天天气怎么样?"}],
    tools=tools,
    tool_choice="auto",
)

print(response.choices[0].message)

JavaScript

安装 SDK

npm install openai

基础聊天

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'YOUR_API_TOKEN',
  baseURL: 'https://api.onedim.ai/v1',
});

async function chat() {
  const completion = await openai.chat.completions.create({
    model: 'gpt-5.5',
    messages: [{ role: 'user', content: 'Hello!' }],
  });

  console.log(completion.choices[0].message.content);
}

chat();

流式响应

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'YOUR_API_TOKEN',
  baseURL: 'https://api.onedim.ai/v1',
});

async function streamingChat() {
  const stream = await openai.chat.completions.create({
    model: 'gpt-5.5',
    messages: [{ role: 'user', content: '写一首诗' }],
    stream: true,
  });

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

streamingChat();

不建议在生产前端直接暴露 API Token。建议通过后端代理调用。

cURL

基础聊天

curl https://api.onedim.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -d '{
    "model": "gpt-5.5",
    "messages": [
      {"role": "user", "content": "Hello!"}
    ]
  }'

流式响应

curl https://api.onedim.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -d '{
    "model": "gpt-5.5",
    "messages": [{"role": "user", "content": "写一首诗"}],
    "stream": true
  }'

图像生成

curl https://api.onedim.ai/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -d '{
    "model": "dall-e-3",
    "prompt": "一只在太空中漫步的可爱猫咪",
    "n": 1,
    "size": "1024x1024"
  }'

第三方集成(LangChain)

LangChain(Python)

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-5.5",
    api_key="YOUR_API_TOKEN",
    base_url="https://api.onedim.ai/v1",
)

response = llm.invoke("Hello!")
print(response.content)

LangChain(JavaScript)

import { ChatOpenAI } from '@langchain/openai';

const model = new ChatOpenAI({
  modelName: 'gpt-5.5',
  openAIApiKey: 'YOUR_API_TOKEN',
  configuration: {
    baseURL: 'https://api.onedim.ai/v1',
  },
});

const response = await model.invoke('Hello!');
console.log(response.content);

On this page