開始使用免費開始

建立 JSON 庫存清單

能夠使用 LLM 產生結構化輸出,代表它們可以在只需最少處理的情況下,可靠地在系統之間交換資訊。

你將把一份以自然語言撰寫的庫存清單轉換為 JSON 格式,藉此練習提示設計技巧。已經為你定義好模型與系統提示,可直接使用。

本練習屬於課程

使用 Llama 3

檢視課程

練習說明

  • 完成 response_format 字典,讓回應以 JSON 格式產生。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

output = llm.create_chat_completion(
        messages=[
            {"role": "system", "content": "You convert inventory lists from text to JSON, extracting item counts and names from the text as keys and values in the form: item: count; for example, 'banana': 32.",},
            {"role": "user", "content": "Fifteen apples, thirty-three oranges, and five thousand fifty-two potatoes."},
        ],
		# Specify output format to JSON
        response_format={
            "type": ____,
        }
)

print(output['choices'][0]['message']['content'])
編輯並執行程式碼