Generarea de rezultate structurate

Lucrul cu Llama 3

Imtihan Ahmed

Machine Learning Engineer

Rezultate structurate în JSON

  • Exemplu: răspunsurile Llama pot fi utilizate într-un tablou de bord

  • Răspunsurile text simplu nu sunt utilizabile ❌

  • Avem nevoie de rezultate structurate ✅

Angajat al unei companii de analiză din industria alimentară

Diagramă pentru automatizarea rapoartelor pentru restaurante

Lucrul cu Llama 3

Răspunsuri JSON cu completare de chat

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."} ]
Lucrul cu Llama 3

Răspunsuri JSON cu completare de chat

output = llm.create_chat_completion(
         messages = message_list,

response_format = "json_object"
)
  • Formatul răspunsului specificat ca JSON
  • Llama generează un răspuns structurat, fără text liber
Lucrul cu Llama 3

Extragerea răspunsului 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
    },... ]
}
Lucrul cu Llama 3

Definirea unei scheme

response_format = {
"type": "json_object",

"schema": {
"type": "object",
"properties": { "Product Name": {"type": "string"}, "Category": {"type": "string"}, "Sales Growth": {"type": "float"}}
}
}
  • Se poate defini o schemă: reguli pentru formatarea datelor
Lucrul cu Llama 3

Definirea unei scheme

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
}
Lucrul cu Llama 3

Să exersăm!

Lucrul cu Llama 3

Preparing Video For Download...