संरचित आउटपुट जनरेट करना

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"
)
  • Response का format JSON के रूप में निर्दिष्ट है
  • Llama संरचित response जनरेट करता है, free-flowing टेक्स्ट नहीं
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 के साथ काम करना

Schema परिभाषित करना

response_format = {
"type": "json_object",

"schema": {
"type": "object",
"properties": { "Product Name": {"type": "string"}, "Category": {"type": "string"}, "Sales Growth": {"type": "float"}}
}
}
  • आप एक schema निर्दिष्ट कर सकते हैं: डेटा कैसे फ़ॉर्मेट होना चाहिए, उसके नियम
Llama 3 के साथ काम करना

Schema परिभाषित करना

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 के साथ काम करना

अभ्यास करते हैं!

Llama 3 के साथ काम करना

Preparing Video For Download...