首頁 >後端開發 >Python教學 >如何簡單地為使用 watsonx 的應用程式設定所有「採樣參數」或「生成參數」?

如何簡單地為使用 watsonx 的應用程式設定所有「採樣參數」或「生成參數」?

Barbara Streisand
Barbara Streisand原創
2025-01-04 09:53:35978瀏覽

How to set simply all “sampling parameters” or “generation parameters” for applications using watsonx?

介紹

訪問 watsonx.ai LLM 的使用者經常遇到的一個問題是「我們如何設定採樣參數?」 !

其實很簡單。

採樣參數(或產生參數)

  • 存取您的 watsonx.ai 實例。

How to set simply all “sampling parameters” or “generation parameters” for applications using watsonx?

  • 點選「開啟提示實驗室」。進入提示實驗室後,在任一標籤中,按一下參數圖示(如圖所示最右側的圖示)。

How to set simply all “sampling parameters” or “generation parameters” for applications using watsonx?

您可以更改設定的LLM(之前使用的或預設設定的)。

  • 開啟參數對話框後,可以依需求進行設定。

How to set simply all “sampling parameters” or “generation parameters” for applications using watsonx?

  • 設定參數後,在同一組工具的圖示上選擇「查看程式碼>」。

How to set simply all “sampling parameters” or “generation parameters” for applications using watsonx?

介面將提供3種參數的程式碼嵌入實作; Curl、Node.js 和 Python 如下範例。

curl "https://us-south.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29" \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -H "Authorization: Bearer ${YOUR_ACCESS_TOKEN}" \
  -d '{
  "input": "<|start_of_role|>system<|end_of_role|>You are Granite, an AI language model developed by IBM in 2024. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|>",
  "parameters": {
    "decoding_method": "sample",
    "max_new_tokens": 200,
    "min_new_tokens": 100,
    "random_seed": 42,
    "stop_sequences": [],
    "temperature": 0.7,
    "top_k": 50,
    "top_p": 1,
    "repetition_penalty": 1
  },
  "model_id": "ibm/granite-3-8b-instruct",
  "project_id": "the one you get"
}'
export const generateText = async () => {
 const url = "https://us-south.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29";
 const headers = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "Authorization": "Bearer YOUR_ACCESS_TOKEN"
 };
 const body = {
  input: "<|start_of_role|>system<|end_of_role|>You are Granite, an AI language model developed by IBM in 2024. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|>",
  parameters: {
   decoding_method: "sample",
   max_new_tokens: 200,
   min_new_tokens: 100,
   random_seed: 42,
   stop_sequences: [],
   temperature: 0.7,
   top_k: 50,
   top_p: 1,
   repetition_penalty: 1
  },
  model_id: "ibm/granite-3-8b-instruct",
  project_id: "the-one-you-get"
 };

 const response = await fetch(url, {
  headers,
  method: "POST",
  body: JSON.stringify(body)
 });

 if (!response.ok) {
  throw new Error("Non-200 response");
 }

 return await response.json();
}
import requests

url = "https://us-south.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29"

body = {
 "input": """<|start_of_role|>system<|end_of_role|>You are Granite, an AI language model developed by IBM in 2024. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.<|end_of_text|>
<|start_of_role|>assistant<|end_of_role|>""",
 "parameters": {
  "decoding_method": "sample",
  "max_new_tokens": 200,
  "min_new_tokens": 100,
  "random_seed": 42,
  "temperature": 0.7,
  "top_k": 50,
  "top_p": 1,
  "repetition_penalty": 1
 },
 "model_id": "ibm/granite-3-8b-instruct",
 "project_id": "the-one-you-get"
}

headers = {
 "Accept": "application/json",
 "Content-Type": "application/json",
 "Authorization": "Bearer YOUR_ACCESS_TOKEN"
}

response = requests.post(
 url,
 headers=headers,
 json=body
)

if response.status_code != 200:
 raise Exception("Non-200 response: " + str(response.text))

data = response.json()

開發者唯一應該調整的資訊是存取權杖。

瞧?

結論

watsonx.ai 平台讓應用程式開發人員可以非常輕鬆地調整 LLM 採樣參數集。

以上是如何簡單地為使用 watsonx 的應用程式設定所有「採樣參數」或「生成參數」?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn