call_api.py
==============================
import requests
import json
url = "http://192.168.5.53:11434/v1/chat/completions"
headers = {'Content-Type': 'application/json'}
data = {
"model": "gemma3:4b",
"messages": [
{
"role": "user",
"content": "Explain how AI works"
}
]
}
try:
response = requests.post(url, headers=headers, data=json.dumps(data))
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
print(response.json()) # Print the JSON response
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
print(f"Raw response text: {response.text}")
這段 JSON 是從一個聊天完成(chat completion)API 回傳的資料,其中包含一個 AI 模型(如 gemma3:4b
)所生成的回答。以下是這個 JSON 架構與各個 key 的中文說明:
JSON 結構說明:
各 key 值的中文說明:
Key 名稱 | 類型 | 中文說明 |
---|---|---|
id | 字串 | 本次對話的唯一識別碼。 |
object | 字串 | 回傳的物件類型,這裡固定是 "chat.completion" ,代表是聊天補全結果。 |
created | 整數(timestamp) | UNIX 時間戳,代表這次請求產生的時間。 |
model | 字串 | 使用的 AI 模型名稱,例如 "gemma3:4b" 。 |
system_fingerprint | 字串 | 系統模型的指紋,用來追蹤模型的版本或變動。 |
choices | 陣列 | 包含一或多個 AI 的回覆,每個回覆是一個選項(通常只有一個)。 |
choices
陣列中的物件:
Key 名稱 | 類型 | 中文說明 |
---|---|---|
index | 整數 | 回覆的索引(如果有多個回覆時用來區分)。 |
message | 物件 | 回覆的訊息本體,內含角色與內容。 |
├ role | 字串 | 發話者的角色,如 assistant (AI 回覆)、user (使用者輸入)。 |
└ content | 字串 | AI 實際輸出的文字內容。 |
finish_reason | 字串 | 表示完成回應的原因,常見如 "stop" (正常結束)、"length" (長度限制結束)。 |
usage
區塊:
Key 名稱 | 類型 | 中文說明 |
---|---|---|
prompt_tokens | 整數 | 使用者輸入使用的 token 數量。 |
completion_tokens | 整數 | AI 回覆使用的 token 數量。 |
total_tokens | 整數 | 總共使用的 token 數量(= prompt_tokens + completion_tokens )。 |
(LLM 串接 API 或 顯示回覆),通常會從 choices[0].message.content
取出 AI 的回答。
沒有留言:
張貼留言