テキスト生成 (Chat Completions)

GLM-4シリーズ大規模言語モデルのチャット補完インターフェース。OpenAI Chat Completions API形式と完全互換で、ストリーミング出力およびファンクションコーリングをサポート。

APIエンドポイント

POST/chat/completions

チャット補完を作成

リクエストパラメータ

パラメータ必須説明
modelstring必須モデル名:GLM-4-Plus、GLM-4-Air、GLM-4-AirX、GLM-4-Long、GLM-4-FlashX、GLM-4-Flash
messagesarray必須会話メッセージの配列。各メッセージにはroleとcontentを含む
temperaturenumber任意サンプリング温度、範囲0〜1、デフォルト0.7
max_tokensinteger任意生成するトークンの最大数
streamboolean任意ストリーミング出力を使用するかどうか、デフォルトfalse
top_pnumber任意核サンプリングパラメータ、デフォルト0.9
toolsarray任意利用可能なツールリスト(ファンクションコーリング)

リクエスト例

リクエスト例
{
  "model": "GLM-4-Air",
  "messages": [
    {"role": "system", "content": "你是一个智能助手"},
    {"role": "user", "content": "请介绍一下智谱AI"}
  ],
  "temperature": 0.7,
  "top_p": 0.9,
  "stream": false
}

レスポンス例

レスポンス例
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "GLM-4-Air",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "智谱AI是一家专注于大模型与认知智能技术的公司..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 100,
    "total_tokens": 120
  }
}

コード例

Python

from openai import OpenAI

# 使用代理 API
client = OpenAI(
    api_key="your-api-key",
    base_url="https://your-proxy-domain.com/v1"
)

response = client.chat.completions.create(
    model="GLM-4-Air",
    messages=[
        {"role": "system", "content": "你是一个智能助手"},
        {"role": "user", "content": "请介绍一下智谱AI"}
    ],
    temperature=0.7,
    stream=False
)

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

JavaScript

import OpenAI from 'openai';

// 使用代理 API
const client = new OpenAI({
  apiKey: 'your-api-key',
  baseURL: 'https://your-proxy-domain.com/v1'
});

async function chat() {
  const response = await client.chat.completions.create({
    model: 'GLM-4-Air',
    messages: [
      { role: 'system', content: '你是一个智能助手' },
      { role: 'user', content: '请介绍一下智谱AI' }
    ],
    temperature: 0.7,
    stream: false
  });

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

chat();

cURL

curl https://your-proxy-domain.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-api-key" \
  -d '{
    "model": "GLM-4-Air",
    "messages": [
      {"role": "system", "content": "你是一个智能助手"},
      {"role": "user", "content": "请介绍一下智谱AI"}
    ],
    "temperature": 0.7
  }'