New ChatGPT Platform API 调用示例

首先获取 base url 和 token

打开 https://chat.datapipe.app ,右上角点击头像 -> API 设置,即可看到 base url https://chat-api.datapipe.app 和个人 token (请勿泄露)。

如果 token 泄露可点击重置密钥进行重置。


建议开始先使用 curl 命令调试,简单方便。

curl 命令调用

export OPENAI_API_KEY="sk-9ff4d600d55097d719ea404dc3e748c7952fxxxxx"

curl "https://chat-api.datapipe.app/v1/chat/completions" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d '{
        "model": "gpt-4o-mini",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Write a haiku that explains the concept of recursion."
            }
        ]
    }'

返回结果代表调用成功。

{"id":"chatcmpl-f32610e17c8d240846b9849f37cd7912","object":"chat.completion","created":1734877711,"model":"gpt-4o-mini","choices":[{"index":0,"message":{"role":"assistant","content":"Nested calls arise,  \nEach step mirrors the last,  \nEndless loops unfold."},"finish_reason":"stop"}],"usage":{"prompt_tokens":28,"completion_tokens":20,"total_tokens":48},"quota":0.0012800001}

Python SDK 调用

1. 安装 Python3 环境和 pip

2. 安装 OpenAI SDK

pip install openai

3. 获取到 base url 和 token

4. Python 代码,文件名 datapipe.api.py

from openai import OpenAI
client = OpenAI(base_url="https://chat-api.datapipe.app/v1", api_key="sk-9ff4d600d55097d719ea404dc3e748c7952fc5cc192f1fcdxxxxxxx")

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {
            "role": "user",
            "content": "Write a haiku about recursion in programming."
        }
    ]
)

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

5. 执行代码输出

python3 datapipe_api.py
Code calls itself back,
Infinite loops in silence,
Depths of thought unwind.

调试成功。👍🏻

更详细的 API 兼容 OpenAI 格式,可参考 OpenAI 官方使用文档:https://platform.openai.com/docs/quickstart?context=curl&language-preference=curl


评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注