Yapılandırılmış çıktı üretme

Llama 3 ile Çalışmak

Imtihan Ahmed

Machine Learning Engineer

JSON ile yapılandırılmış çıktı

  • Örnek: Llama yanıtları bir gösterge paneline girdi olabilir

  • Düz metin yanıtlar uygun değil ❌

  • Yapılandırılmış çıktılar gerekir ✅

Yiyecek-içecek analitiği şirketi çalışanı

Restoran raporlarını otomatikleştirme diyagramı

Llama 3 ile Çalışmak

Sohbet tamamlama ile JSON yanıtları

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 ile Çalışmak

Sohbet tamamlama ile JSON yanıtları

output = llm.create_chat_completion(
         messages = message_list,

response_format = "json_object"
)
  • Yanıt biçimi JSON olarak belirtilir
  • Llama serbest metin olmadan yapılandırılmış yanıt üretir
Llama 3 ile Çalışmak

JSON yanıtını çıkarma

print(output['choices'][0]['message']['content'])
{
  "report_name": "En Çok Satan İçecekler 2024",
  "top_beverages": [
    {
      "rank": 1,
      "beverage_name": "Coca-Cola Classic",
      "sales_volume": 2.1,
      "growth_rate": 1.9
    },... ]
}
Llama 3 ile Çalışmak

Şema tanımlama

response_format = {
"type": "json_object",

"schema": {
"type": "object",
"properties": { "Product Name": {"type": "string"}, "Category": {"type": "string"}, "Sales Growth": {"type": "float"}}
}
}
  • Bir şema belirtebilirsiniz: verinin nasıl biçimleneceğine dair kurallar
Llama 3 ile Çalışmak

Şema tanımlama

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 ile Çalışmak

Hadi pratik yapalım!

Llama 3 ile Çalışmak

Preparing Video For Download...