Create chat completion
curl --request POST \
--url https://api-direct.dimilinks.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "claude-sonnet-5",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_call_id": "<string>"
}
],
"stream": false,
"temperature": 123,
"top_p": 123,
"max_tokens": 123,
"stop": "<string>",
"presence_penalty": 123,
"frequency_penalty": 123,
"tools": [
{}
],
"tool_choice": "<string>",
"response_format": {},
"user": "<string>"
}
'import requests
url = "https://api-direct.dimilinks.com/v1/chat/completions"
payload = {
"model": "claude-sonnet-5",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_call_id": "<string>"
}
],
"stream": False,
"temperature": 123,
"top_p": 123,
"max_tokens": 123,
"stop": "<string>",
"presence_penalty": 123,
"frequency_penalty": 123,
"tools": [{}],
"tool_choice": "<string>",
"response_format": {},
"user": "<string>"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'claude-sonnet-5',
messages: [{content: '<string>', name: '<string>', tool_call_id: '<string>'}],
stream: false,
temperature: 123,
top_p: 123,
max_tokens: 123,
stop: '<string>',
presence_penalty: 123,
frequency_penalty: 123,
tools: [{}],
tool_choice: '<string>',
response_format: {},
user: '<string>'
})
};
fetch('https://api-direct.dimilinks.com/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api-direct.dimilinks.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'claude-sonnet-5',
'messages' => [
[
'content' => '<string>',
'name' => '<string>',
'tool_call_id' => '<string>'
]
],
'stream' => false,
'temperature' => 123,
'top_p' => 123,
'max_tokens' => 123,
'stop' => '<string>',
'presence_penalty' => 123,
'frequency_penalty' => 123,
'tools' => [
[
]
],
'tool_choice' => '<string>',
'response_format' => [
],
'user' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api-direct.dimilinks.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"claude-sonnet-5\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"temperature\": 123,\n \"top_p\": 123,\n \"max_tokens\": 123,\n \"stop\": \"<string>\",\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": \"<string>\",\n \"response_format\": {},\n \"user\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api-direct.dimilinks.com/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"claude-sonnet-5\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"temperature\": 123,\n \"top_p\": 123,\n \"max_tokens\": 123,\n \"stop\": \"<string>\",\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": \"<string>\",\n \"response_format\": {},\n \"user\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api-direct.dimilinks.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"claude-sonnet-5\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"temperature\": 123,\n \"top_p\": 123,\n \"max_tokens\": 123,\n \"stop\": \"<string>\",\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": \"<string>\",\n \"response_format\": {},\n \"user\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"id": "chatcmpl_xxx",
"object": "chat.completion",
"created": 1777080000,
"model": "claude-sonnet-5",
"choices": [
{
"index": 123,
"message": {
"role": "system",
"content": "<string>",
"name": "<string>",
"tool_call_id": "<string>"
},
"finish_reason": "<string>"
}
],
"usage": {
"prompt_tokens": 123,
"completion_tokens": 123,
"total_tokens": 123
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}对话 / Chat
Create chat completion
用 OpenAI 兼容协议调用 Claude、GPT 等文本模型。
POST
/
chat
/
completions
Create chat completion
curl --request POST \
--url https://api-direct.dimilinks.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "claude-sonnet-5",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_call_id": "<string>"
}
],
"stream": false,
"temperature": 123,
"top_p": 123,
"max_tokens": 123,
"stop": "<string>",
"presence_penalty": 123,
"frequency_penalty": 123,
"tools": [
{}
],
"tool_choice": "<string>",
"response_format": {},
"user": "<string>"
}
'import requests
url = "https://api-direct.dimilinks.com/v1/chat/completions"
payload = {
"model": "claude-sonnet-5",
"messages": [
{
"content": "<string>",
"name": "<string>",
"tool_call_id": "<string>"
}
],
"stream": False,
"temperature": 123,
"top_p": 123,
"max_tokens": 123,
"stop": "<string>",
"presence_penalty": 123,
"frequency_penalty": 123,
"tools": [{}],
"tool_choice": "<string>",
"response_format": {},
"user": "<string>"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'claude-sonnet-5',
messages: [{content: '<string>', name: '<string>', tool_call_id: '<string>'}],
stream: false,
temperature: 123,
top_p: 123,
max_tokens: 123,
stop: '<string>',
presence_penalty: 123,
frequency_penalty: 123,
tools: [{}],
tool_choice: '<string>',
response_format: {},
user: '<string>'
})
};
fetch('https://api-direct.dimilinks.com/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api-direct.dimilinks.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'claude-sonnet-5',
'messages' => [
[
'content' => '<string>',
'name' => '<string>',
'tool_call_id' => '<string>'
]
],
'stream' => false,
'temperature' => 123,
'top_p' => 123,
'max_tokens' => 123,
'stop' => '<string>',
'presence_penalty' => 123,
'frequency_penalty' => 123,
'tools' => [
[
]
],
'tool_choice' => '<string>',
'response_format' => [
],
'user' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api-direct.dimilinks.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"claude-sonnet-5\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"temperature\": 123,\n \"top_p\": 123,\n \"max_tokens\": 123,\n \"stop\": \"<string>\",\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": \"<string>\",\n \"response_format\": {},\n \"user\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api-direct.dimilinks.com/v1/chat/completions")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"claude-sonnet-5\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"temperature\": 123,\n \"top_p\": 123,\n \"max_tokens\": 123,\n \"stop\": \"<string>\",\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": \"<string>\",\n \"response_format\": {},\n \"user\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api-direct.dimilinks.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"claude-sonnet-5\",\n \"messages\": [\n {\n \"content\": \"<string>\",\n \"name\": \"<string>\",\n \"tool_call_id\": \"<string>\"\n }\n ],\n \"stream\": false,\n \"temperature\": 123,\n \"top_p\": 123,\n \"max_tokens\": 123,\n \"stop\": \"<string>\",\n \"presence_penalty\": 123,\n \"frequency_penalty\": 123,\n \"tools\": [\n {}\n ],\n \"tool_choice\": \"<string>\",\n \"response_format\": {},\n \"user\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"id": "chatcmpl_xxx",
"object": "chat.completion",
"created": 1777080000,
"model": "claude-sonnet-5",
"choices": [
{
"index": 123,
"message": {
"role": "system",
"content": "<string>",
"name": "<string>",
"tool_call_id": "<string>"
},
"finish_reason": "<string>"
}
],
"usage": {
"prompt_tokens": 123,
"completion_tokens": 123,
"total_tokens": 123
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "invalid_request_error",
"code": "<string>"
}
}/v1/chat/completions 是 DimiLinks 推荐的对话入口。请求与响应结构与 OpenAI 官方接口保持一致,可直接使用 OpenAI SDK;只要把 model 换成当前密钥可用的文本模型名称,就可以调用 Claude、GPT 等模型。
请求地址
POST https://api-direct.dimilinks.com/v1/chat/completions
Content-Type: application/json
Authorization: Bearer <DIMILINKS_API_KEY>
简单示例
curl "https://api-direct.dimilinks.com/v1/chat/completions" \
-H "Authorization: Bearer $DIMILINKS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-5",
"messages": [
{"role": "system", "content": "你是一位简洁的中文助手。"},
{"role": "user", "content": "用一句话解释什么是冥王星。"}
]
}'
{
"id": "chatcmpl_xxx",
"object": "chat.completion",
"created": 1777080000,
"model": "claude-sonnet-5",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "..." },
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 32,
"completion_tokens": 24,
"total_tokens": 56
}
}
流式输出
请求加上"stream": true,服务端会以 text/event-stream 推回增量片段,最后以 data: [DONE] 结束。
curl "https://api-direct.dimilinks.com/v1/chat/completions" \
-H "Authorization: Bearer $DIMILINKS_API_KEY" \
-H "Content-Type: application/json" \
-N \
-d '{
"model": "gpt-5.5",
"stream": true,
"messages": [
{"role": "user", "content": "写一首关于深海的四行小诗"}
]
}'
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","model":"gpt-5.5",
"choices":[{"index":0,"delta":{"content":"深"},"finish_reason":null}]}
参数
仅列出常用字段,未列出的字段如果模型支持则原样透传给上游。| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
model | string | 必填 | 模型 ID,例如 claude-sonnet-5、claude-opus-4-8、gpt-5.5、gpt-5.6-terra、qwen3.7-max 等。 |
messages | array | 必填 | 消息数组,至少包含一条 role=user。role 支持 system / user / assistant / tool。 |
stream | boolean | false | true 时返回 SSE 流式片段。 |
temperature | number | 模型默认 | 采样温度,0–2。 |
top_p | number | 模型默认 | 核采样概率,0–1。 |
max_tokens | integer | 模型默认 | 单次输出最大 token;调用 Claude 系模型时建议显式设置。 |
stop | string | string[] | 空 | 命中即停止生成。 |
presence_penalty | number | 0 | 重复主题惩罚,-2 到 2。 |
frequency_penalty | number | 0 | 重复词惩罚,-2 到 2。 |
tools | array | 空 | 工具调用定义,结构兼容 OpenAI Tool Calling。 |
tool_choice | string | object | auto | 控制是否强制调用某个工具。 |
response_format | object | 空 | 请求 JSON 输出,例如 { "type": "json_object" }。 |
user | string | 空 | 终端用户标识,用于审计与限流。 |
推荐模型
| 场景 | 模型 |
|---|---|
| 通用对话 / 长文档 | claude-sonnet-5、claude-opus-4-8、gpt-5.5 |
| 复杂推理 / 编码 | claude-opus-4-8、kimi-k2.7-code、gpt-5.6-terra |
| 中文与通用任务 | qwen3.7-max、glm-5.2、MiniMax-M3 |
| 低成本批量 | claude-haiku-4-5、gpt-5.4-mini、deepseek-v4-flash |
| 多模态视觉 | gpt-image-2(图片生成专用);视觉对话能力随模型而定,请先看 /v1/models 的 input_modalities。 |
用 OpenAI SDK 调用
Python:from openai import OpenAI
client = OpenAI(
api_key="sk-...",
base_url="https://api-direct.dimilinks.com/v1",
)
response = client.chat.completions.create(
model="claude-sonnet-5",
messages=[
{"role": "system", "content": "你是一位严谨的中文助手。"},
{"role": "user", "content": "解释下傅里叶变换。"},
],
max_tokens=1024,
)
print(response.choices[0].message.content)
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.DIMILINKS_API_KEY,
baseURL: 'https://api-direct.dimilinks.com/v1',
})
const response = await client.chat.completions.create({
model: 'gpt-5.5',
messages: [{ role: 'user', content: '你好' }],
})
工具调用 (Tool calling)
curl "https://api-direct.dimilinks.com/v1/chat/completions" \
-H "Authorization: Bearer $DIMILINKS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-5",
"messages": [
{"role": "user", "content": "帮我查 Tokyo 现在的天气"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询某个城市的当前天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
}
]
}'
tool_calls 后,再把工具执行结果以 role=tool 的消息放回 messages 继续调用即可。
错误处理
请求失败时返回 OpenAI 风格的error 包装,详见 Errors。流式请求中如果上游断开,最后一个事件可能不是 [DONE],建议把「连接断开」也当作可重试错误。授权
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
请求体
application/json