텍스트 생성 (Chat Completions)
GLM-4 시리즈 대규모 언어 모델 채팅 완성 인터페이스. OpenAI Chat Completions API 형식과 완벽하게 호환되며, 스트리밍 출력 및 함수 호출을 지원합니다.
API 엔드포인트
POST
/chat/completions채팅 완성 생성
요청 파라미터
| 파라미터 | 유형 | 필수 | 설명 |
|---|---|---|---|
model | string | 필수 | 모델명: GLM-4-Plus, GLM-4-Air, GLM-4-AirX, GLM-4-Long, GLM-4-FlashX, GLM-4-Flash |
messages | array | 필수 | 대화 메시지 배열, 각 메시지에는 role과 content가 포함됩니다 |
temperature | number | 선택 | 샘플링 온도, 범위 0-1, 기본값 0.7 |
max_tokens | integer | 선택 | 생성할 최대 토큰 수 |
stream | boolean | 선택 | 스트리밍 출력 사용 여부, 기본값 false |
top_p | number | 선택 | 핵 샘플링 파라미터, 기본값 0.9 |
tools | array | 선택 | 사용 가능한 도구 목록 (함수 호출) |
요청 예시
요청 예시
{
"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
}'