SDK 範例
SDK 範例
完整的 API 參數說明,以及 Python、TypeScript 與 cURL 的詳細整合範例。
Chat Completions 參數說明
| 參數名稱 | 型別 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 使用的模型名稱,例如 lawbot-pro |
messages | array | 是 | 對話訊息陣列,每則含 role 與 content |
temperature | number | 否 | 取值 0.0–2.0,控制回應隨機性,預設 1.0 |
max_tokens | integer | 否 | 最大輸出 token 數,未設定則使用模型上限 |
top_p | number | 否 | Nucleus sampling,取值 0.0–1.0,預設 1.0 |
frequency_penalty | number | 否 | 取值 -2.0–2.0,降低重複用詞,預設 0 |
presence_penalty | number | 否 | 取值 -2.0–2.0,增加話題多樣性,預設 0 |
stop | string | array | 否 | 遇到此序列時停止生成,最多 4 個 |
n | integer | 否 | 每次請求生成的回應數量,預設 1 |
stream | boolean | 否 | 啟用 SSE 串流模式,預設 false |
user | string | 否 | 使用者識別碼,用於監控與稽核 |
Python — 完整參數範例
sdk_full.py
from openai import OpenAI
from openai.types.chat import ChatCompletion
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="http://127.0.0.1:8000",
timeout=60.0, # 請求逾時(秒)
max_retries=3, # 自動重試次數(遇 429/5xx 時)
)
response: ChatCompletion = client.chat.completions.create(
# ─── 必填參數 ───────────────────────────────────────────
model="lawbot-pro", # 模型名稱
messages=[ # 對話訊息陣列
{
"role": "system",
"content": "你是一位專精台灣法律的 AI 助理。",
},
{
"role": "user",
"content": "請分析以下合約條款是否有風險...",
},
],
# ─── 選填參數 ───────────────────────────────────────────
temperature=0.7, # 創意度(0.0–2.0,預設 1.0)
max_tokens=2048, # 最大輸出 token 數
top_p=1.0, # Nucleus sampling(0.0–1.0)
frequency_penalty=0.0, # 降低重複詞頻(-2.0–2.0)
presence_penalty=0.0, # 鼓勵話題多樣性(-2.0–2.0)
stop=None, # 停止序列(字串或字串陣列)
n=1, # 每次請求生成的回應數量
stream=False, # 是否啟用串流(True/False)
user="user-123", # 使用者識別碼(可選,用於監控)
)
# 取得回應內容
content = response.choices[0].message.content
print(content)
# 取得用量資訊
print(f"Input tokens: {response.usage.prompt_tokens}")
print(f"Output tokens: {response.usage.completion_tokens}")
print(f"Total tokens: {response.usage.total_tokens}")Python — 串流模式(stream)
sdk_stream.py
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="http://127.0.0.1:8000",
)
# 串流模式
with client.chat.completions.stream(
model="lawbot-pro",
messages=[
{"role": "user", "content": "請逐條分析勞動契約的常見陷阱"},
],
temperature=0.5,
max_tokens=4096,
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
# 串流結束後取得完整資訊
final_completion = stream.get_final_completion()
print(f"\n\nTotal tokens: {final_completion.usage.total_tokens}")TypeScript — 完整參數範例(含型別)
sdk_full.ts
import OpenAI from "openai";
import type { ChatCompletion, ChatCompletionCreateParams } from "openai/resources";
const client = new OpenAI({
apiKey: process.env.LAWBOT_API_KEY!,
baseURL: "http://127.0.0.1:8000",
timeout: 60_000, // ms
maxRetries: 3,
});
// ─── 完整參數型別 ────────────────────────────────────────
const params: ChatCompletionCreateParams = {
// 必填
model: "lawbot-pro",
messages: [
{
role: "system",
content: "你是一位專精台灣法律的 AI 助理。",
},
{
role: "user",
content: "公司是否可以在沒有事先通知的情況下調整員工薪資?",
},
],
// 選填
temperature: 0.7,
max_tokens: 2048,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: null,
n: 1,
stream: false,
user: "user-abc123",
};
const response: ChatCompletion = await client.chat.completions.create(params);
console.log(response.choices[0].message.content);
console.log("Usage:", response.usage);TypeScript — 串流模式(stream)
sdk_stream.ts
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.LAWBOT_API_KEY!,
baseURL: "http://127.0.0.1:8000",
});
// 串流模式(完整型別支援)
const stream = client.chat.completions.stream({
model: "lawbot-pro",
messages: [
{ role: "user", content: "請說明著作權法保護的範圍與限制" },
],
temperature: 0.7,
max_tokens: 2048,
});
// 邊接收邊輸出
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}
// 取得完整回應(含 usage)
const finalResponse = await stream.finalChatCompletion();
console.log("\nTotal tokens:", finalResponse.usage?.total_tokens);cURL — 完整參數範例
terminal
curl http://127.0.0.1:8000/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "lawbot-pro",
"messages": [
{
"role": "system",
"content": "你是一位專精台灣法律的 AI 助理。"
},
{
"role": "user",
"content": "什麼情況下可以主張消費者保護法的無條件退貨權?"
}
],
"temperature": 0.7,
"max_tokens": 2048,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"stream": false
}'