LLM 參數
本頁面提供有關 Koog 代理框架中 LLM 參數的詳細資訊。LLM 參數讓您可以控制並自訂語言模型的行為。
總覽
LLM 參數是用於微調語言模型產生回應方式的配置選項。這些參數控制回應的隨機性、長度、格式和工具使用等面向。透過調整參數,您可以針對不同的使用案例優化模型行為,從創意內容生成到確定性的結構化輸出。
在 Koog 中,LLMParams 類別整合了 LLM 參數,並為配置語言模型行為提供了統一的介面。您可以透過以下方式使用 LLM 參數:
- 建立提示詞時:
val prompt = prompt(
id = "dev-assistant",
params = LLMParams(
temperature = 0.7,
maxTokens = 500
)
) {
// 新增系統訊息以設定上下文
system("You are a helpful assistant.")
// 新增使用者訊息
user("Tell me about Kotlin")
}Prompt prompt = Prompt.builder("dev-assistant")
.withParams(new LLMParams(
0.7, // temperature
500, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
LLMParams.ToolChoice.Auto.INSTANCE, // toolChoice
null, // user
null // additionalProperties
))
.system("You are a helpful assistant.")
.user("Tell me about Kotlin")
.build();如需更多關於提示詞建立的資訊,請參閱 提示詞。
- 建立子圖時:
val processQuery by subgraphWithTask<String, String>(
tools = listOf(searchTool, calculatorTool, weatherTool),
llmModel = OpenAIModels.Chat.GPT4o,
llmParams = LLMParams(
temperature = 0.7,
maxTokens = 500
),
parallelTools = false,
assistantResponseRepeatMax = 3,
) { userQuery ->
"""
You are a helpful assistant that can answer questions about various topics.
Please help with the following query:
$userQuery
"""
}如需更多關於 Koog 中現有子圖型別的資訊,請參閱 預定義子圖。若要了解如何建立並實作您自己的子圖,請參閱 自訂子圖。
- 在 LLM 寫入工作階段中更新提示詞時:
llm.writeSession {
changeLLMParams(
LLMParams(
temperature = 0.7,
maxTokens = 500
)
)
}如需更多關於工作階段的資訊,請參閱 LLM 工作階段與手動歷程記錄管理。
LLM 參數參考
下表提供了 LLMParams 類別中包含的 LLM 參數參考,這些參數受 Koog 開箱即用的所有 LLM 提供者支援。 如需特定提供者專用參數的清單,請參閱 提供者專用參數。
| 參數 | 型別 | 說明 |
|---|---|---|
temperature | Double | 控制輸出中的隨機性。較高的值(如 0.7–1.0)會產生更多樣化且具創意的回應,而較低的值則會產生更具確定性且專注的回應。 |
maxTokens | Integer | 回應中產生的最大 token 數量。用於控制回應長度。 |
numberOfChoices | Integer | 要產生的替代回應數量。必須大於 0。 |
speculation | String | 一個影響模型行為的投機配置字串,旨在提高結果的速度與準確性。僅部分模型支援,但可能大幅提升速度與準確性。 |
schema | Schema | 定義模型回應格式的結構,啟用如 JSON 的結構化輸出。如需更多資訊,請參閱 架構。 |
toolChoice | ToolChoice | 控制語言模型的工具呼叫行為。如需更多資訊,請參閱 工具選擇。 |
user | String | 發出請求的使用者識別碼,可用於追蹤用途。 |
additionalProperties | Map<String, JsonElement> | 可用於儲存特定模型提供者之自訂參數的額外屬性。 |
如需各個參數預設值的清單,請參閱相應的 LLM 提供者文件:
架構 (Schema)
Schema 介面定義了模型回應格式的結構。 Koog 支援 JSON 架構,如下面各節所述。
JSON 架構
JSON 架構讓您可以向語言模型要求結構化的 JSON 資料。Koog 支援以下兩種類型的 JSON 架構:
- 基礎 JSON 架構 (
LLMParams.Schema.JSON.Basic):用於基礎的 JSON 處理能力。此格式主要關注巢狀資料定義,不包含進階的 JSON 架構功能。
// 使用基礎 JSON 架構建立參數
val jsonParams = LLMParams(
temperature = 0.2,
schema = LLMParams.Schema.JSON.Basic(
name = "PersonInfo",
schema = JsonObject(mapOf(
"type" to JsonPrimitive("object"),
"properties" to JsonObject(
mapOf(
"name" to JsonObject(mapOf("type" to JsonPrimitive("string"))),
"age" to JsonObject(mapOf("type" to JsonPrimitive("number"))),
"skills" to JsonObject(
mapOf(
"type" to JsonPrimitive("array"),
"items" to JsonObject(mapOf("type" to JsonPrimitive("string")))
)
)
)
),
"additionalProperties" to JsonPrimitive(false),
"required" to JsonArray(listOf(JsonPrimitive("name"), JsonPrimitive("age"), JsonPrimitive("skills")))
))
)
)// 使用基礎 JSON 架構建立參數
LLMParams jsonParams = new LLMParams(
0.2, // temperature
null, // maxTokens
1, // numberOfChoices
null, // speculation
new LLMParams.Schema.JSON.Basic(
"PersonInfo",
new JsonObject(Map.of(
"type", new JsonPrimitive("object"),
"properties", new JsonObject(Map.of(
"name", new JsonObject(Map.of("type", new JsonPrimitive("string"))),
"age", new JsonObject(Map.of("type", new JsonPrimitive("number"))),
"skills", new JsonObject(Map.of(
"type", new JsonPrimitive("array"),
"items", new JsonObject(Map.of("type", new JsonPrimitive("string")))
))
)),
"additionalProperties", new JsonPrimitive(false),
"required", new JsonArray(List.of(
new JsonPrimitive("name"),
new JsonPrimitive("age"),
new JsonPrimitive("skills")
))
))
),
LLMParams.ToolChoice.Auto.INSTANCE, // toolChoice
null, // user
null // additionalProperties
);- 標準 JSON 架構 (
LLMParams.Schema.JSON.Standard):表示符合 json-schema.org 的標準 JSON 架構。此格式是官方 JSON 架構規範的正當子集。請注意,不同 LLM 提供者的特性可能有所不同,因為並非所有提供者都支援完整的 JSON 架構。
// 使用標準 JSON 架構建立參數
val standardJsonParams = LLMParams(
temperature = 0.2,
schema = LLMParams.Schema.JSON.Standard(
name = "ProductCatalog",
schema = JsonObject(mapOf(
"type" to JsonPrimitive("object"),
"properties" to JsonObject(mapOf(
"products" to JsonObject(mapOf(
"type" to JsonPrimitive("array"),
"items" to JsonObject(mapOf(
"type" to JsonPrimitive("object"),
"properties" to JsonObject(mapOf(
"id" to JsonObject(mapOf("type" to JsonPrimitive("string"))),
"name" to JsonObject(mapOf("type" to JsonPrimitive("string"))),
"price" to JsonObject(mapOf("type" to JsonPrimitive("number"))),
"description" to JsonObject(mapOf("type" to JsonPrimitive("string")))
)),
"additionalProperties" to JsonPrimitive(false),
"required" to JsonArray(listOf(JsonPrimitive("id"), JsonPrimitive("name"), JsonPrimitive("price"), JsonPrimitive("description")))
))
))
)),
"additionalProperties" to JsonPrimitive(false),
"required" to JsonArray(listOf(JsonPrimitive("products")))
))
)
)// 使用標準 JSON 架構建立參數
LLMParams standardJsonParams = new LLMParams(
0.2, // temperature
null, // maxTokens
1, // numberOfChoices
null, // speculation
new LLMParams.Schema.JSON.Standard(
"ProductCatalog",
new JsonObject(Map.of(
"type", new JsonPrimitive("object"),
"properties", new JsonObject(Map.of(
"products", new JsonObject(Map.of(
"type", new JsonPrimitive("array"),
"items", new JsonObject(Map.of(
"type", new JsonPrimitive("object"),
"properties", new JsonObject(Map.of(
"id", new JsonObject(Map.of("type", new JsonPrimitive("string"))),
"name", new JsonObject(Map.of("type", new JsonPrimitive("string"))),
"price", new JsonObject(Map.of("type", new JsonPrimitive("number"))),
"description", new JsonObject(Map.of("type", new JsonPrimitive("string")))
)),
"additionalProperties", new JsonPrimitive(false),
"required", new JsonArray(List.of(
new JsonPrimitive("id"),
new JsonPrimitive("name"),
new JsonPrimitive("price"),
new JsonPrimitive("description")
))
))
))
)),
"additionalProperties", new JsonPrimitive(false),
"required", new JsonArray(List.of(new JsonPrimitive("products")))
))
),
LLMParams.ToolChoice.Auto.INSTANCE, // toolChoice
null, // user
null // additionalProperties
);工具選擇 (Tool choice)
ToolChoice 類別控制語言模型如何使用工具。它提供以下選項:
LLMParams.ToolChoice.Named:語言模型呼叫指定的工具。接受代表要呼叫工具名稱的name字串引數。LLMParams.ToolChoice.All:語言模型呼叫所有工具。LLMParams.ToolChoice.None:語言模型不呼叫工具,僅產生文字。LLMParams.ToolChoice.Auto:語言模型自動決定是否呼叫工具以及呼叫哪個工具。LLMParams.ToolChoice.Required:語言模型至少呼叫一個工具。
以下是使用 LLMParams.ToolChoice.Named 類別呼叫特定工具的範例:
val specificToolParams = LLMParams(
toolChoice = LLMParams.ToolChoice.Named(name = "calculator")
)LLMParams specificToolParams = new LLMParams(
null, // temperature
null, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
new LLMParams.ToolChoice.Named("calculator"), // toolChoice
null, // user
null // additionalProperties
);提供者專用參數
Koog 支援部分 LLM 提供者的專用參數。這些參數擴充了基礎的 LLMParams 類別並加入特定提供者的功能。以下類別包含各個提供者的專用參數:
OpenAIChatParams:OpenAI Chat Completions API 的專用參數。OpenAIResponsesParams:OpenAI Responses API 的專用參數。GoogleParams:Google 模型的專用參數。AnthropicParams:Anthropic 模型的專用參數。MistralAIParams:Mistral 模型的專用參數。DeepSeekParams:DeepSeek 模型的專用參數。OpenRouterParams:OpenRouter 模型的專用參數。DashscopeParams:阿里巴巴模型的專用參數。OllamaParams:Ollama 模型的專用參數。
以下是 Koog 中提供者專用參數的完整參考:
| 參數 | 型別 | 說明 |
|---|---|---|
audio | OpenAIAudioConfig | 使用具備音訊能力模型時的音訊輸出配置。如需詳細資訊,請參閱 OpenAIAudioConfig 的 API 文件。 |
frequencyPenalty | Double | 針對頻繁出現的 token 進行懲罰以減少重複。較高的 frequencyPenalty 值會產生更多的措辭變化並減少重複。取值範圍為 -2.0 到 2.0。 |
logprobs | Boolean | 若為 true,則在輸出 token 中包含對數機率 (log-probabilities)。 |
parallelToolCalls | Boolean | 若為 true,則可以並行執行多個工具呼叫。特別適用於自訂節點或代理策略 (agent strategies) 之外的 LLM 互動。 |
presencePenalty | Double | 防止模型重複使用已經包含在輸出中的 token。較高的值會鼓勵引入新的 token 和主題。取值範圍為 -2.0 到 2.0。 |
promptCacheKey | String | 用於提示快取 (prompt caching) 的穩定快取金鑰。OpenAI 使用它來快取相似請求的回應。 |
reasoningEffort | ReasoningEffort | 指定模型將使用的推理程度 (reasoning effort)。如需詳細資訊與可用值,請參閱 ReasoningEffort 的 API 文件。 |
safetyIdentifier | String | 穩定且唯一的使用者識別碼,可用於偵測違反 OpenAI 政策的使用者。 |
serviceTier | ServiceTier | OpenAI 處理層級選擇,讓您可以優先考慮效能而非成本,反之亦然。如需詳細資訊,請參閱 ServiceTier 的 API 文件。 |
stop | List<String> | 當模型遇到這些字串時,會發出停止產生內容訊號的字串列表。例如,要讓模型在產生兩個換行符號時停止產生內容,請將停止序列指定為 stop = listOf("/n/n")。 |
store | Boolean | 若為 true,提供者可能會存儲輸出內容供日後檢索。 |
topLogprobs | Integer | 每個位置最可能出現的前幾個 token 數量。取值範圍為 0–20。需要將 logprobs 參數設定為 true。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
webSearchOptions | OpenAIWebSearchOptions | 配置網路搜尋工具的使用方式(若支援)。如需詳細資訊,請參閱 OpenAIWebSearchOptions 的 API 文件。 |
| 參數 | 型別 | 說明 |
|---|---|---|
background | Boolean | 在背景執行回應。 |
include | List<OpenAIInclude> | 模型回應中要包含的額外資料,例如網路搜尋工具呼叫的來源或檔案搜尋工具呼叫的搜尋結果。如需詳細參考資訊,請參閱 Koog API 參考中的 OpenAIInclude。若要進一步了解 include 參數,請參閱 OpenAI 的文件。 |
logprobs | Boolean | 若為 true,則在輸出 token 中包含對數機率 (log-probabilities)。 |
maxToolCalls | Integer | 此回應中允許的內建工具呼叫最大總數。取值需大於或等於 0。 |
parallelToolCalls | Boolean | 若為 true,則可以並行執行多個工具呼叫。特別適用於自訂節點或代理策略 (agent strategies) 之外的 LLM 互動。 |
promptCacheKey | String | 用於提示快取 (prompt caching) 的穩定快取金鑰。OpenAI 使用它來快取相似請求的回應。 |
reasoning | ReasoningConfig | 具備推理能力模型的推理配置。如需詳細資訊,請參閱 ReasoningConfig 的 API 文件。 |
safetyIdentifier | String | 穩定且唯一的使用者識別碼,可用於偵測違反 OpenAI 政策的使用者。 |
serviceTier | ServiceTier | OpenAI 處理層級選擇,讓您可以優先考慮效能而非成本,反之亦然。如需詳細資訊,請參閱 ServiceTier 的 API 文件。 |
store | Boolean | 若為 true,提供者可能會存儲輸出內容供日後檢索。 |
topLogprobs | Integer | 每個位置最可能出現的前幾個 token 數量。取值範圍為 0–20。需要將 logprobs 參數設定為 true。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
truncation | Truncation | 接近上下文視窗時的截斷策略。如需詳細資訊,請參閱 Truncation 的 API 文件。 |
| 參數 | 型別 | 說明 |
|---|---|---|
thinkingConfig | GoogleThinkingConfig | 控制模型是否應公開其思維鏈 (chain-of-thought),以及可以在其上消耗多少 token。如需詳細資訊,請參閱 GoogleThinkingConfig 的 API 參考。 |
topK | Integer | 產生輸出時要考慮的前幾個 token 數量。取值需大於或等於 0(可能適用特定提供者的最小值限制)。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
| 參數 | 型別 | 說明 |
|---|---|---|
container | String | 用於跨請求重複使用的容器識別符號。容器由 Anthropic 的程式碼執行工具使用,以提供安全且容器化的程式碼執行環境。透過提供先前回應中的容器識別符號,您可以跨多個請求重複使用容器,進而保留請求之間建立的檔案。如需詳細資訊,請參閱 Anthropic 文件中的 容器 (Containers)。 |
mcpServers | List<AnthropicMCPServerURLDefinition> | 請求中要使用的 MCP 伺服器定義。最多支援 20 個伺服器。如需詳細資訊,請參閱 AnthropicMCPServerURLDefinition 的 API 參考。 |
serviceTier | ServiceTier | OpenAI 處理層級選擇,讓您可以優先考慮效能而非成本,反之亦然。如需詳細資訊,請參閱 ServiceTier 的 API 文件。 |
stopSequences | List<String> | 導致模型停止產生內容的自訂文字序列。如果匹配,回應中的 stop_reason 值將為 stop_sequence。 |
thinking | AnthropicThinking | 啟動 Claude 擴充思考的配置。啟動後,回應還會包含思考內容區塊。如需詳細資訊,請參閱 AnthropicThinking 的 API 參考。 |
topK | Integer | 產生輸出時要考慮的前幾個 token 數量。取值需大於或等於 0(可能適用特定提供者的最小值限制)。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
| 參數 | 型別 | 說明 |
|---|---|---|
frequencyPenalty | Double | 針對頻繁出現的 token 進行懲罰以減少重複。較高的 frequencyPenalty 值會產生更多的措辭變化並減少重複。取值範圍為 -2.0 到 2.0。 |
parallelToolCalls | Boolean | 若為 true,則可以並行執行多個工具呼叫。特別適用於自訂節點或代理策略 (agent strategies) 之外的 LLM 互動。 |
presencePenalty | Double | 防止模型重複使用已經包含在輸出中的 token。較高的值會鼓勵引入新的 token 和主題。取值範圍為 -2.0 到 2.0。 |
promptMode | String | 讓您在推理模式與無系統提示之間切換。當設定為 reasoning 時,將使用推理模型的預設系統提示。如需詳細資訊,請參閱 Mistral 的 推理 (Reasoning) 文件。 |
randomSeed | Integer | 用於隨機取樣的種子。如果設定,具有相同參數 and 相同種子值的不同呼叫將產生確定性的結果。 |
safePrompt | Boolean | 指定是否在所有對話之前注入安全提示。安全提示用於執行護欄 (guardrails) 並防止有害內容。如需詳細資訊,請參閱 Mistral 的 審核與護欄 (Moderation & Guardarailing) 文件。 |
stop | List<String> | 當模型遇到這些字串時,會發出停止產生內容訊號的字串列表。例如,要讓模型在產生兩個換行符號時停止產生內容,請將停止序列指定為 stop = listOf("/n/n")。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
| 參數 | 型別 | 說明 |
|---|---|---|
frequencyPenalty | Double | 針對頻繁出現的 token 進行懲罰以減少重複。較高的 frequencyPenalty 值會產生更多的措辭變化並減少重複。取值範圍為 -2.0 到 2.0。 |
logprobs | Boolean | 若為 true,則在輸出 token 中包含對數機率 (log-probabilities)。 |
presencePenalty | Double | 防止模型重複使用已經包含在輸出中的 token。較高的值會鼓勵引入新的 token 和主題。取值範圍為 -2.0 到 2.0。 |
stop | List<String> | 當模型遇到這些字串時,會發出停止產生內容訊號的字串列表。例如,要讓模型在產生兩個換行符號時停止產生內容,請將停止序列指定為 stop = listOf("/n/n")。 |
topLogprobs | Integer | 每個位置最可能出現的前幾個 token 數量。取值範圍為 0–20。需要將 logprobs 參數設定為 true。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
| 參數 | 型別 | 說明 |
|---|---|---|
frequencyPenalty | Double | 針對頻繁出現的 token 進行懲罰以減少重複。較高的 frequencyPenalty 值會產生更多的措辭變化並減少重複。取值範圍為 -2.0 到 2.0。 |
logprobs | Boolean | 若為 true,則在輸出 token 中包含對數機率 (log-probabilities)。 |
minP | Double | 過濾掉相對機率低於指定 minP 值(相對於最可能出現的 token)的 token。取值範圍為 0.0–0.1。 |
models | List<String> | 此請求允許的模型列表。 |
presencePenalty | Double | 防止模型重複使用已經包含在輸出中的 token。較高的值會鼓勵引入新的 token 和主題。取值範圍為 -2.0 到 2.0。 |
provider | ProviderPreferences | 包含一系列參數,讓您可以明確控制 OpenRouter 如何選擇要使用的 LLM 提供者。如需詳細資訊,請參閱 ProviderPreferences 的 API 文件。 |
repetitionPenalty | Double | 針對 token 重複進行懲罰。對於已經出現在輸出中的 token,其下一個 token 的機率會除以 repetitionPenalty 的值,若 repetitionPenalty > 1,則會使它們再次出現的可能性降低。取值範圍為大於 0.0 且小於或等於 2.0。 |
route | String | 要使用的請求路由策略。 |
stop | List<String> | 當模型遇到這些字串時,會發出停止產生內容訊號的字串列表。例如,要讓模型在產生兩個換行符號時停止產生內容,請將停止序列指定為 stop = listOf("/n/n")。 |
topA | Double | 根據模型置信度動態調整取樣視窗。如果模型很有把握(存在佔主導地位的高機率下一個 token),則取樣視窗會限制在少數幾個頂級 token 內。如果置信度較低(存在許多機率相似的 token),則取樣視窗中會保留更多 token。取值範圍為 0.0–0.1(含)。值越高代表動態適應程度越高。 |
topK | Integer | 產生輸出時要考慮的前幾個 token 數量。取值需大於或等於 0(可能適用特定提供者的最小值限制)。 |
topLogprobs | Integer | 每個位置最可能出現的前幾個 token 數量。取值範圍為 0–20。需要將 logprobs 參數設定為 true。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
transforms | List<String> | 上下文轉換列表。定義當上下文超過模型 token 限制時的轉換方式。預設轉換為 middle-out,即從提示的中間部分進行截斷。使用空列表表示不進行轉換。如需詳細資訊,請參閱 OpenRouter 文件中的 訊息轉換 (Message Transforms)。 |
| 參數 | 型別 | 說明 |
|---|---|---|
enableSearch | Boolean | 指定是否啟用網路搜尋功能。如需詳細資訊,請參閱阿里巴巴的 網路搜尋 (Web search) 文件。 |
enableThinking | Boolean | 指定在使用混合思考模型時是否啟用思考模式。如需詳細資訊,請參閱阿里巴巴關於 深度思考 (Deep thinking) 的文件。 |
frequencyPenalty | Double | 針對頻繁出現的 token 進行懲罰以減少重複。較高的 frequencyPenalty 值會產生更多的措辭變化並減少重複。取值範圍為 -2.0 到 2.0。 |
logprobs | Boolean | 若為 true,則在輸出 token 中包含對數機率 (log-probabilities)。 |
parallelToolCalls | Boolean | 若為 true,則可以並行執行多個工具呼叫。特別適用於自訂節點或代理策略 (agent strategies) 之外的 LLM 互動。 |
presencePenalty | Double | 防止模型重複使用已經包含在輸出中的 token。較高的值會鼓勵引入新的 token 和主題。取值範圍為 -2.0 到 2.0。 |
stop | List<String> | 當模型遇到這些字串時,會發出停止產生內容訊號的字串列表。例如,要讓模型在產生兩個換行符號時停止產生內容,請將停止序列指定為 stop = listOf("/n/n")。 |
topLogprobs | Integer | 每個位置最可能出現的前幾個 token 數量。取值範圍為 0–20。需要將 logprobs 參數設定為 true。 |
topP | Double | 也稱為核取樣 (nucleus sampling)。透過將機率值最高的 token 加入子集,直到其機率總和達到指定的 topP 值,以此建立下一個 token 的子集。取值範圍為大於 0.0 且小於或等於 1.0。 |
| 參數 | 型別 | 說明 |
|---|---|---|
think | Boolean | 啟動 Ollama 擴充思考的配置。啟動後,回應還會包含思考內容區塊。如需詳細資訊,請參閱 Ollama thinking 的 API 參考。 |
以下範例顯示使用提供者專用的 OpenRouterParams 類別定義 OpenRouter LLM 參數:
val openRouterParams = OpenRouterParams(
temperature = 0.7,
maxTokens = 500,
frequencyPenalty = 0.5,
presencePenalty = 0.5,
topP = 0.9,
topK = 40,
repetitionPenalty = 1.1,
models = listOf("anthropic/claude-3-opus", "anthropic/claude-3-sonnet"),
transforms = listOf("middle-out")
)OpenRouterParams openRouterParams = new OpenRouterParams(
0.7, // temperature
500, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
null, // toolChoice
null, // user
null, // additionalProperties
0.5, // frequencyPenalty
null, // logprobs
null, // minP
Arrays.asList("anthropic/claude-3-opus", "anthropic/claude-3-sonnet"), // models
0.5, // presencePenalty
null, // provider
1.1, // repetitionPenalty
null, // route
null, // stop
null, // topA
40, // topK
null, // topLogprobs
0.9, // topP
Arrays.asList("middle-out") // transforms
);使用範例
基礎用法
// 一組具有限制長度的基礎參數
val basicParams = LLMParams(
temperature = 0.7,
maxTokens = 150,
toolChoice = LLMParams.ToolChoice.Auto
)// 一組具有限制長度的基礎參數
LLMParams basicParams = new LLMParams(
0.7, // temperature
150, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
LLMParams.ToolChoice.Auto.INSTANCE, // toolChoice
null, // user
null // additionalProperties
);推理控制 (Reasoning control)
您可以透過控制模型推理的提供者專用參數來實作推理控制。 當使用 OpenAI Chat API 且模型支援推理時,請使用 reasoningEffort 參數來控制模型在提供回應前產生多少推理 token:
val openAIReasoningEffortParams = OpenAIChatParams(
reasoningEffort = ReasoningEffort.MEDIUM
)OpenAIChatParams openAIReasoningEffortParams = new OpenAIChatParams(
null, // temperature
null, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
null, // toolChoice
null, // user
null, // additionalProperties
null, // audio
null, // frequencyPenalty
null, // logprobs
null, // parallelToolCalls
null, // presencePenalty
null, // promptCacheKey
ReasoningEffort.MEDIUM, // reasoningEffort
null, // safetyIdentifier
null, // serviceTier
null, // stop
null, // store
null, // topLogprobs
null, // topP
null // webSearchOptions
);此外,當在無狀態模式下使用 OpenAI Responses API 時,您會保留推理項目的加密歷程記錄,並在每次對話輪次中將其傳送給模型。加密是在 OpenAI 端完成的,您需要透過在請求中將 include 參數設定為 reasoning.encrypted_content 來要求加密的推理 token。 接著,您可以在後續的對話輪次中將加密的推理 token 傳回給模型。
val openAIStatelessReasoningParams = OpenAIResponsesParams(
include = listOf(OpenAIInclude.REASONING_ENCRYPTED_CONTENT)
)OpenAIResponsesParams openAIStatelessReasoningParams = new OpenAIResponsesParams(
null, // temperature
null, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
null, // toolChoice
null, // user
null, // additionalProperties
null, // background
Arrays.asList(OpenAIInclude.REASONING_ENCRYPTED_CONTENT), // include
null, // logprobs
null, // maxToolCalls
null, // parallelToolCalls
null, // promptCacheKey
null, // reasoning
null, // safetyIdentifier
null, // serviceTier
null, // store
null, // topLogprobs
null, // topP
null // truncation
);自訂參數
若要新增特定提供者專用且 Koog 未原生支援的自訂參數,請使用 additionalProperties 屬性,如下例所示。
// 為特定的模型提供者新增自訂參數
val customParams = LLMParams(
additionalProperties = additionalPropertiesOf(
"top_p" to 0.95,
"frequency_penalty" to 0.5,
"presence_penalty" to 0.5
)
)// 為特定的模型提供者新增自訂參數
LLMParams customParams = new LLMParams(
null, // temperature
null, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
null, // toolChoice
null, // user
AdditionalPropertiesKt.additionalPropertiesOf(
"top_p", 0.95,
"frequency_penalty", 0.5,
"presence_penalty", 0.5
)
);設定與覆寫參數
下方的程式碼範例顯示如何定義一組您可能主要使用的 LLM 參數,然後透過部分覆寫原始組合中的值並新增新值來建立另一組參數。 這讓您可以定義大多數請求通用的參數,同時加入更具體的參數組合,而無需重複通用參數。
// 定義預設參數
val defaultParams = LLMParams(
temperature = 0.7,
maxTokens = 150,
toolChoice = LLMParams.ToolChoice.Auto
)
// 建立具有部分覆寫的參數,其餘部分使用預設值
val overrideParams = LLMParams(
temperature = 0.2,
numberOfChoices = 3
).default(defaultParams)// 定義預設參數
LLMParams defaultParams = new LLMParams(
0.7, // temperature
150, // maxTokens
1, // numberOfChoices
null, // speculation
null, // schema
LLMParams.ToolChoice.Auto.INSTANCE, // toolChoice
null, // user
null // additionalProperties
);
// 建立具有部分覆寫的參數,其餘部分使用預設值
LLMParams overrideParams = new LLMParams(
0.2, // temperature
null, // maxTokens
3, // numberOfChoices
null, // speculation
null, // schema
null, // toolChoice
null, // user
null // additionalProperties
).applyDefaults(defaultParams);產生的 overrideParams 組合中的值等同於以下內容:
val overrideParams = LLMParams(
temperature = 0.2,
maxTokens = 150,
toolChoice = LLMParams.ToolChoice.Auto,
numberOfChoices = 3
)LLMParams overrideParams = new LLMParams(
0.2, // temperature
150, // maxTokens
3, // numberOfChoices
null, // speculation
null, // schema
LLMParams.ToolChoice.Auto.INSTANCE, // toolChoice
null, // user
null // additionalProperties
);