Structured Output Parser
Parse structured key-value output from an LLM response.
LLMs often produce structured output with labeled fields. Parse a response into a dictionary where each line has the format "Key: Value".
Input:
- Multi-line text (each line is "Key: Value")
- Last line: comma-separated list of required keys
Output: For each required key (in order given), print the value. Print "MISSING" for keys not found.
Matching is case-insensitive for keys. Extra whitespace should be stripped.
Example:
Name: Alice Age: 30 City: New York Name,Age,Email
Alice 30 MISSING
- The input is parsed into a dictionary with case-insensitive keys, resulting in:
name: Alice,age: 30,city: New York - The last line is split into a list of required keys:
Name,Age,Email - For each required key, the corresponding value is looked up in the dictionary, with case-insensitive matching, and printed:
AliceforName,30forAge - Since
Emailis not found in the dictionary,MISSINGis printed for that key
Constraints:
- Key matching is case-insensitive
- Strip whitespace from keys and values
- Lines without ":" are skipped
- Required keys are comma-separated on the last input line
More from LLM 3: Applications & Evaluation
Background Knowledge
The problem of parsing structured output from a Large Language Model (LLM) response involves understanding the format of the input data and how to extract relevant information from it. In this case, the input consists of multi-line text where each line is in the format "Key: Value", followed by a line containing a comma-separated list of required keys. The task is to parse this input into a dictionary and then print the values for each required key. Key-value parsing is a fundamental concept in computer science, and it's essential to understand how to work with dictionaries and string manipulation in programming.
To approach this problem, one needs to be familiar with string processing techniques, such as splitting strings, stripping whitespace, and performing case-insensitive matching. Additionally, understanding how to work with dictionaries (or hash tables) is crucial, as they provide an efficient way to store and look up key-value pairs. The problem also requires attention to detail, as the matching is case-insensitive, and extra whitespace should be stripped from the input.
The concept of parsing is also relevant here, as it involves analyzing the input data and extracting meaningful information from it. In this case, the parsing involves splitting the input into individual lines, extracting the key-value pairs, and storing them in a dictionary. The problem can be solved using a variety of programming languages, but the underlying concepts and techniques remain the same.
Algorithm/Approach
The general approach to solving this problem involves the following algorithm pattern:
- Read the input data line by line
- Parse each line into a key-value pair
- Store the key-value pairs in a dictionary
- Read the last line containing the required keys
- Iterate over the required keys and print the corresponding values from the dictionary
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.