跳转到主要内容

Documentation Index

Fetch the complete documentation index at: https://docs.metask.ai/llms.txt

Use this file to discover all available pages before exploring further.

Because 元任务 AI 网关 is fully OpenAI-compatible, you can use the official openai Python and Node.js libraries without any additional packages or adapters. The only change required is setting base_url (Python) or baseURL (Node.js/TypeScript) to the gateway endpoint. Your existing prompts, model parameters, and response-handling code stay the same.

Installation

pip install openai

Basic usage

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://napi.origintask.cn/v1"
)

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

print(response.choices[0].message.content)
Replace YOUR_API_KEY with a key from your dashboard. You can use any model supported by the gateway in the model field — see Available Models for the full list.

Streaming responses

Streaming works the same way as with the standard OpenAI API. Set stream=True (Python) or stream: true (Node.js) and iterate over the response chunks.
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://napi.origintask.cn/v1"
)

stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Tell me a short story."}],
    stream=True
)

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

Setting the API key via environment variable

Rather than hardcoding your key, set it as an environment variable. The openai library reads OPENAI_API_KEY automatically, so you only need to set the base_url.
export OPENAI_API_KEY="YOUR_API_KEY"
python
from openai import OpenAI

# api_key is read from OPENAI_API_KEY environment variable
client = OpenAI(base_url="https://napi.origintask.cn/v1")
The base_url must always be set explicitly because the library defaults to https://api.openai.com/v1. Environment variables cannot override the base URL in all SDK versions, so pass it in the constructor.