AI 智慧平台
「設定生成式AI API」的相關資訊
取得 API 金鑰
請從「 AI API串接 」標籤中進入「AI 設定管理」頁面,點選「開通服務」按鈕進入設定頁,並依指示完成確認流程。

要取得 API 金鑰,請點選「查看」。系統可能會要求您輸入帳戶密碼以進行驗證。
設定 AI API
API 網域: https://sgai.littlepig.cloud
端點(Endpoint): /anthropic/v1/messages
通訊協定:HTTPS
請求方式:POST
請求內容格式:Serialized JSON format
請求結構
必要的標頭 Headers
x-api-key
字串(string)
從後台管理系統取得的驗證金鑰。
Content-Type
字串(string)
一定要是 application/json
.
請求資料定義
model
字串(string)
是
使用的 Claude 模型。
max_tokens
整數(integer)
是
生成的最大 token 數。
messages
物件陣列(object[])
是
對話歷史紀錄(請參見「訊息格式」說明)。
stream
布林值(boolean)
否
以 Server-Sent Events(SSE)方式串流回應。
system
字串(string)
否
系統級提示詞System-level prompt.)
temperature
數值(number)
否
0.0 至 1.0;數值越低越具決定性,數值越高越具創造力。
top_p
數值(number)
否
Nucleus sampling 比例。請根據需求調整 temperature
或 top_p
.
tools
物件陣列(object[])
否
模型可能呼叫的外部工具定義。
單一使用情境 — 基本對話功能
curl https://sgai.littlepig.cloud/anthropic/v1/messages -H "x-api-key: $API_KEY" -H "anthropic-version: 2023-06-01" -H "Content-Type: application/json" -d '{
"model": "claude_37_sonnet",
"max_tokens": 1024,
"messages": [
{ "role": "user", "content": "Hello, world" }
]
}'
訊息格式
純文字格式(簡化版)
{ "role": "user", "content": "Hello, Claude" }
明確內容區塊(Explicit Content Blocks)
每則輸入訊息的內容可以是單一字串,或是一組具有特定類型的內容區塊陣列。
若直接使用字串作為內容,則等同於只含有一個類型為 "text"
的內容區塊。
以下兩種輸入訊息格式是等效的:
{"role": "user", "content": "Hello, Claude"}
{ "role": "user", "content": [ { "type": "text", "text": "Hello, Claude" } ] }
多輪對話(Multiple Conversational Turns)
[
{ "role": "user", "content": "Hello there." },
{ "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" },
{ "role": "user", "content": "Can you explain LLMs in plain English?" }
]
部分回應填充(Partially‑filled Response)
[
{ "role": "user", "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" },
{ "role": "assistant", "content": "The best answer is (" }
]
圖片 + 文字
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZJRg..."
}
},
{ "type": "text", "text": "What is in this image?" }
]
}
支援的圖片格式: image/jpeg
, image/png
, image/gif
, image/webp
.
工具定義結構(Tool Definition Schema)
若您在 API 請求中包含工具(tools),模型可能會回傳 tool_use
類型的內容區塊,表示模型正在使用這些工具。
接著,您可以依照模型所產生的工具輸入(tool input)來執行對應的工具,並可選擇性地透過 tool_result
類型的內容區塊,將執行結果回傳給模型。
name
字串(string)
是
工具名稱。
description
字串(string)
否
工具的用途說明。
input_schema
物件(object)
是
工具輸入的 JSON 結構描述。
[
{
"name": "get_stock_price",
"description": "Get the current stock price for a given ticker symbol.",
"input_schema": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
}
},
"required": [
"ticker"
]
}
}
]
回應資料定義(Response Data Definition)
content
Claude 所產生的內容區塊。
id
訊息 ID。
model
回應內容所使用的模型名稱。
role
角色,一律為 assistant
.
stop_reason
為何停止生成內容(例如: end_turn
)
usage.input_tokens
請求中所使用的 token 數量。
usage.output_tokens
回應中所產生的 token 數量。
範例
{
"id": "msg_bdrk_01FTuLJMy9aYU5TQhK14rMsE",
"type": "message",
"role": "assistant",
"model": "claude-3-7-sonnet-20250219",
"content": [
{
"type": "text",
"text": "Hello! How can I assist you today? Feel free to ask me any questions or let me know what you'd like to talk about."
}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 10,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"output_tokens": 31
}
}
串流事件 (stream=true
)
stream=true
)content_block_delta
部分 token 或工具輸入資料。
error
錯誤細節。
範例:
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ello frien"}}
event: error
data: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}
工具呼叫串流:
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{"location":"San Fra"}"}}
工具呼叫操作說明
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-3-7-sonnet-20250219",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
],
"messages": [
{
"role": "user",
"content": "What is the weather like in San Francisco?"
}
]
}'
延伸閱讀
https://docs.anthropic.com/en/api/messages
價格
claude_3_haiku
$0.00025
$0.00125
claude_3_sonnet
$0.003
$0.015
claude_3_opus
$0.015
$0.075
claude_35_haiku
$0.0008
$0.004
claude_35_sonnet
$0.003
$0.015
claude_37_sonnet
$0.003
$0.015
若要停用 API 存取,請在設定 管理頁面 中點選「關閉服務」按鈕。
Last updated
Was this helpful?