SDK 範例
SDK 範例
完整的 API 參數說明,以及 Python、TypeScript 與 cURL 的詳細整合範例。
Chat Completions 參數說明
| 參數名稱 | 型別 | 必填 | 說明 |
|---|---|---|---|
model | string | 是 | 模型名稱,目前支援 lawbot-flash。 |
messages | array | 是 | 對話訊息陣列,順序由舊至新,最後一筆需為 user 訊息。 |
stream | boolean | 否 | 啟用 SSE 串流模式,預設 false。 |
temperature | number | 否 | 生成溫度 0.0–2.0,法律問答建議使用預設值 0.3。 |
reasoning_effort | string | 否 | 推理強度:none/minimal/low → 快速;medium → 中等;high/xhigh → 深度推理。透過 extra_body 傳遞。 |
max_completion_tokens | integer | 否 | 回應最大 token 數(新版欄位,優先於 max_tokens)。 |
max_tokens | integer | 否 | [建議改用 max_completion_tokens] 回應最大 token 數,保留舊版客戶端相容性。 |
top_p | number | 否 | Nucleus sampling 機率門檻,取值 0.0–1.0,預設 1.0(與 temperature 擇一使用)。 |
stream_options | object | 否 | 串流選項,例如 {"include_usage": true}(保留相容性)。 |
start_date | string | 否 | [法律擴充] 範篩篩選起始日期,YYYY-MM-DD 格式。透過 extra_body 傳遞。 |
end_date | string | 否 | [法律擴充] 範篩篩選結束日期,YYYY-MM-DD 格式。透過 extra_body 傳遞。 |
level | string[] | 否 | [法律擴充] 範篩法院層級:最高、高等、地方、智財。透過 extra_body 傳遞。 |
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="https://developer-api.lawbot.tw/api/v1",
timeout=60.0, # 請求逾時(秒)
max_retries=3, # 自動重試次數(遇 5xx 伺服端錯誤時;429 為 lifetime 上限,重試無效)
)
response: ChatCompletion = client.chat.completions.create(
# ─── 必填參數 ───────────────────────────────────────────
model="lawbot-flash", # 模型名稱
messages=[ # 對話訊息陣列
{
"role": "user",
"content": "請問股東會決議瑕疵的法律效果為何?",
},
],
# ─── 選填參數 ───────────────────────────────────────────
temperature=0.3, # 生成溫度(0.0–2.0,法律問答建議 0.3)
max_completion_tokens=2048, # 最大輸出 token 數(新版欄位)
top_p=1.0, # Nucleus sampling(0.0–1.0)
stream=False, # 是否啟用串流(True/False)
user="user-123", # 使用者識別碼(可選,用於監控)
# ─── Lawbot 擴充參數(透過 extra_body 傳遞)──────────────
extra_body={
"reasoning_effort": "medium", # 推理強度:none/minimal/low/medium/high/xhigh
# 可選範篩篩選參數:
# "start_date": "2020-01-01",
# "end_date": "2024-12-31",
# "level": ["最高", "高等"],
},
)
# 取得回應內容
content = response.choices[0].message.content
print(content)
# Lawbot 擴充:引用文件清單
if response.x_lawbot_references:
for i, ref in enumerate(response.x_lawbot_references, 1):
print(f"[{i}] {ref['title']}")Python — 串流模式(stream)
sdk_stream.py
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://developer-api.lawbot.tw/api/v1",
)
# 串流模式 + reasoning_effort
with client.chat.completions.stream(
model="lawbot-flash",
messages=[
{"role": "user", "content": "請問股東會決議瑕疵的法律效果為何?"},
],
temperature=0.3,
max_completion_tokens=4096,
extra_body={"reasoning_effort": "high"},
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
# 串流結束後取得完整資訊
final_completion = stream.get_final_completion()
# x_lawbot_references 在最後一個 chunk 回傳
if hasattr(final_completion, "x_lawbot_references") and final_completion.x_lawbot_references:
for i, ref in enumerate(final_completion.x_lawbot_references, 1):
print(f"[{i}] {ref['title']}")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: "https://developer-api.lawbot.tw/api/v1",
timeout: 60_000, // ms
maxRetries: 3,
});
// ─── 完整參數型別 ────────────────────────────────────────
const params: ChatCompletionCreateParams = {
// 必填
model: "lawbot-flash",
messages: [
{
role: "user",
content: "請問股東會決議瑕疵的法律效果為何?",
},
],
// 選填
temperature: 0.3,
max_completion_tokens: 2048,
top_p: 1,
stream: false,
user: "user-abc123",
};
// Lawbot 擴充參數(透過 extra_body 傳遞)
const response: ChatCompletion = await (client.chat.completions.create as Function)({
...params,
reasoning_effort: "medium", // none/minimal/low/medium/high/xhigh
// start_date: "2020-01-01",
// end_date: "2024-12-31",
// level: ["最高", "高等"],
});
console.log(response.choices[0].message.content);
// x_lawbot_references: 引用文件清單
const refs = (response as Record<string, unknown>).x_lawbot_references as Array<{ title: string }> | undefined;
refs?.forEach((ref, i) => console.log(`[${i + 1}] ${ref.title}`));TypeScript — 串流模式(stream)
sdk_stream.ts
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.LAWBOT_API_KEY!,
baseURL: "https://developer-api.lawbot.tw/api/v1",
});
// 串流模式(完整型別支援)
const stream = client.chat.completions.stream({
model: "lawbot-flash",
messages: [
{ role: "user", content: "請問股東會決議瑕疵的法律效果為何?" },
],
temperature: 0.3,
max_completion_tokens: 2048,
// @ts-expect-error Lawbot 擴充欄位
reasoning_effort: "medium",
});
// 邊接收邊輸出
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}
// 取得完整回應(x_lawbot_references 在最後 chunk)
const finalResponse = await stream.finalChatCompletion();
const refs = (finalResponse as Record<string, unknown>).x_lawbot_references as Array<{ title: string }> | undefined;
refs?.forEach((ref, i) => console.log(`[${i + 1}] ${ref.title}`));cURL — 完整參數範例
terminal
curl https://developer-api.lawbot.tw/api/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "lawbot-flash",
"messages": [
{
"role": "user",
"content": "請問股東會決議瑕疵的法律效果為何?"
}
],
"temperature": 0.3,
"max_completion_tokens": 2048,
"top_p": 1,
"reasoning_effort": "medium",
"stream": false
}'