
Turn a reply into a label
easyTurn a reply into a label
Cohere LLM Interview Question
Cohere's customers sort text into categories of their own by asking a language model to pick one. The model is handed the list and told to reply with a single category, and it usually picks the right one. The reply still has to be cleaned up before the category can be used. The word arrives with a period after it, or wrapped in asterisks, or in a different case, and now and then inside a whole sentence.
Write a function normalize_label(reply, labels) that works out which category the model picked. The reply is the line of text the model sent back, and the labels are the categories it was asked to choose from. Return the matching label exactly as it appears in the labels list, or None when the reply does not name one of them. Strip any spaces and asterisks from both ends of the reply and a period from the end, then compare against each label ignoring case. The whole reply has to be a label: a sentence that merely mentions one does not count, and nor does an empty reply.
Asked of
- AI Engineer
- ML Engineer
- Data Scientist
Example 1
Input
reply = "Positive", labels = ["Positive", "Negative", "Neutral"]
Output
"Positive"
Example 2
Input
reply = "negative.", labels = ["Positive", "Negative", "Neutral"]
Output
"Negative"
Example 3
Input
reply = " **Neutral.** ", labels = ["Positive", "Negative", "Neutral"]
Output
"Neutral"
Example 4
Input
reply = "I think it is positive", labels = ["Positive", "Negative", "Neutral"]
Output
None
Explanation
In the first example the reply is already a label. In the second it is the right word in the wrong case with a period after it. In the third it arrives wrapped in asterisks with a period inside them, and all of that comes off before the comparison. In the fourth the model wrote a sentence, and a sentence is not a label however clearly it points at one.
Submit also runs 3 hidden test cases that check edge cases.
- Parsing
Similar questions
Company
Cohere
Difficulty
easy
Topic
LLM
Language
Python
Your code
Loading Python in the background. You can start writing.