LoslegenKostenlos loslegen

Specifying structured JSON schema

You are part of a team working on an online education platform designing new interactive exercise where students are able to ask questions and their answer is displayed through a graphical view. This question-answering feature is powered by an LLM, but the graphical view requires a JSON input with the fields Question and Answer to correctly show the question and answer:

{
    "Question": "...",
    "Answer": "..."
}

A system prompt and example user question have been stored in the messages variable.

Diese Übung ist Teil des Kurses

Working with Llama 3

Kurs anzeigen

Anleitung zur Übung

  • Specify the "Question" and "Answer" fields in the schema with the "string" type.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

output = llm.create_chat_completion(
    messages=messages,
    response_format={
        "type": "json_object",
        "schema": {
            "type": "object",
            # Set the properties of the JSON fields and their data types
            "properties": {"____": {"type": "____"}, "____": {"type": "____"}}
        }
    }
)

print(output['choices'][0]['message']['content'])
Code bearbeiten und ausführen