welcom ! Handel home

2025年3月31日 星期一

ollama gemma3:4b API call

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}")


#  LLM 回復JSON  資料結構
========================

這段 JSON 是從一個聊天完成(chat completion)API 回傳的資料,其中包含一個 AI 模型(如 gemma3:4b)所生成的回答。以下是這個 JSON 架構與各個 key 的中文說明:


JSON 結構說明:

json
{ "id": "chatcmpl-687", "object": "chat.completion", "created": 1743484387, "model": "gemma3:4b", "system_fingerprint": "fp_ollama", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "..." // AI 回覆的內容 }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 13, "completion_tokens": 1143, "total_tokens": 1156 } }

各 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 的回答。

沒有留言: