Group prompts into templates under concurrent ingestion

0
Sign in

An API receives prompts from many threads. Most come from a few templates filled with different values: Summarize ticket 48213 for alice@acme.com and Summarize ticket 1007 for bo@x.io share the template Summarize ticket <NUM> for <EMAIL>.

  • template_of(prompt) replaces quoted strings, URLs, emails, UUIDs and whole-token numbers with <STR>, <URL>, <EMAIL>, <UUID> and <NUM>, and collapses whitespace. gpt4 and v2 are not numbers.
  • TemplateIndex counts prompts per template while many threads call add at once, keeps the first 3 example prompts per template, and answers count(template), examples(template) and top(k) (most frequent first, ties alphabetical).
  • Counts must be exact, and threads adding different templates should rarely wait for each other.
def template_of(prompt): ...


class TemplateIndex:
    def __init__(self, shards=64, examples_per_template=3): ...
    def add(self, prompt): ...
    def count(self, template): ...
    def examples(self, template): ...
    def top(self, k): ...
    def total(self): ...
AnthropicCodingMedium

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.