構造化された出力の生成

Llama 3 を使いこなす

Imtihan Ahmed

Machine Learning Engineer

JSON での構造化出力

  • 例:Llama の応答をダッシュボードに入力する場合

  • プレーンテキストは不可 ❌

  • 構造化出力が必要 ✅

食品・飲料アナリティクス企業の社員

レストラン向けレポート自動化の図

Llama 3 を使いこなす

チャット補完での JSON 応答

response_format = {"type": "json_object"}


message_list = [ {"role": "system", # System role defined as market analyst "content": "You are a food industry market analyst. You analyze sales data and generate structured JSON reports of top-selling beverages."},
{"role": "user", # User role to pass the request "content": "Provide a structured report on the top-selling beverages this year."} ]
Llama 3 を使いこなす

チャット補完での JSON 応答

output = llm.create_chat_completion(
         messages = message_list,

response_format = "json_object"
)
  • 応答の形式JSON に指定
  • Llama は構造化応答を生成。自由記述はなし
Llama 3 を使いこなす

JSON 応答の抽出

print(output['choices'][0]['message']['content'])
{
  "report_name": "Top-Selling Beverages 2024",
  "top_beverages": [
    {
      "rank": 1,
      "beverage_name": "Coca-Cola Classic",
      "sales_volume": 2.1,
      "growth_rate": 1.9
    },... ]
}
Llama 3 を使いこなす

スキーマの定義

response_format = {
"type": "json_object",

"schema": {
"type": "object",
"properties": { "Product Name": {"type": "string"}, "Category": {"type": "string"}, "Sales Growth": {"type": "float"}}
}
}
  • スキーマを指定可能:データの書式ルールを定義
Llama 3 を使いこなす

スキーマの定義

output = llm.create_chat_completion(
         messages = message_list,
         response_format = response_format)  

print(output['choices'][0]['message']['content'])
{
  "Product Name": "Coca-Cola",
  "Category": "Soft Drink",
  "Sales Growth": 12.5
}
Llama 3 を使いこなす

Passons à la pratique !

Llama 3 を使いこなす

Preparing Video For Download...