Skip to content

anthropic

The antrhopic implementation of page level calssification.

AnthropicClassificationProvider

Bases: BaseClassificationProvider

The Anthropic implementation of unscored page classification.

Source code in docprompt/tasks/classification/anthropic.py
class AnthropicClassificationProvider(BaseClassificationProvider):
    """The Anthropic implementation of unscored page classification."""

    name = "anthropic"

    async def _ainvoke(
        self, input: Iterable[bytes], config: ClassificationConfig = None, **kwargs
    ) -> List[ClassificationOutput]:
        messages = _prepare_messages(input, config)

        parser = AnthropicPageClassificationOutputParser.from_task_input(
            config, provider_name=self.name
        )

        completions = await inference.run_batch_inference_anthropic(messages)

        return [parser.parse(res) for res in completions]

AnthropicPageClassificationOutputParser

Bases: BasePageClassificationOutputParser

The output parser for the page classification system.

Source code in docprompt/tasks/classification/anthropic.py
class AnthropicPageClassificationOutputParser(BasePageClassificationOutputParser):
    """The output parser for the page classification system."""

    def parse(self, text: str) -> ClassificationOutput:
        """Parse the results of the classification task."""
        pattern = re.compile(r"Answer: (.+)")
        match = pattern.search(text)

        result = self.resolve_match(match)

        if self.confidence:
            conf_pattern = re.compile(r"Confidence: (.+)")
            conf_match = conf_pattern.search(text)
            conf_result = self.resolve_confidence(conf_match)

            return ClassificationOutput(
                type=self.type,
                labels=result,
                score=conf_result,
                provider_name=self.name,
            )

        return ClassificationOutput(
            type=self.type, labels=result, provider_name=self.name
        )

parse(text)

Parse the results of the classification task.

Source code in docprompt/tasks/classification/anthropic.py
def parse(self, text: str) -> ClassificationOutput:
    """Parse the results of the classification task."""
    pattern = re.compile(r"Answer: (.+)")
    match = pattern.search(text)

    result = self.resolve_match(match)

    if self.confidence:
        conf_pattern = re.compile(r"Confidence: (.+)")
        conf_match = conf_pattern.search(text)
        conf_result = self.resolve_confidence(conf_match)

        return ClassificationOutput(
            type=self.type,
            labels=result,
            score=conf_result,
            provider_name=self.name,
        )

    return ClassificationOutput(
        type=self.type, labels=result, provider_name=self.name
    )