|
| 1 | +from skllm.models._base.classifier import ( |
| 2 | + BaseFewShotClassifier, |
| 3 | + BaseDynamicFewShotClassifier, |
| 4 | + SingleLabelMixin, |
| 5 | + MultiLabelMixin, |
| 6 | +) |
| 7 | +from skllm.llm.anthropic.mixin import ClaudeClassifierMixin |
| 8 | +from skllm.models.gpt.vectorization import GPTVectorizer |
| 9 | +from skllm.models._base.vectorizer import BaseVectorizer |
| 10 | +from skllm.memory.base import IndexConstructor |
| 11 | +from typing import Optional |
| 12 | + |
| 13 | + |
| 14 | +class FewShotClaudeClassifier(BaseFewShotClassifier, ClaudeClassifierMixin, SingleLabelMixin): |
| 15 | + """Few-shot text classifier using Anthropic's Claude API for single-label classification tasks.""" |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + model: str = "claude-3-haiku-20240307", |
| 20 | + default_label: str = "Random", |
| 21 | + prompt_template: Optional[str] = None, |
| 22 | + key: Optional[str] = None, |
| 23 | + **kwargs, |
| 24 | + ): |
| 25 | + """ |
| 26 | + Few-shot text classifier using Anthropic's Claude API. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + model : str, optional |
| 31 | + model to use, by default "claude-3-haiku-20240307" |
| 32 | + default_label : str, optional |
| 33 | + default label for failed prediction; if "Random" -> selects randomly based on class frequencies |
| 34 | + prompt_template : Optional[str], optional |
| 35 | + custom prompt template to use, by default None |
| 36 | + key : Optional[str], optional |
| 37 | + estimator-specific API key; if None, retrieved from the global config |
| 38 | + """ |
| 39 | + super().__init__( |
| 40 | + model=model, |
| 41 | + default_label=default_label, |
| 42 | + prompt_template=prompt_template, |
| 43 | + **kwargs, |
| 44 | + ) |
| 45 | + self._set_keys(key) |
| 46 | + |
| 47 | + |
| 48 | +class MultiLabelFewShotClaudeClassifier( |
| 49 | + BaseFewShotClassifier, ClaudeClassifierMixin, MultiLabelMixin |
| 50 | +): |
| 51 | + """Few-shot text classifier using Anthropic's Claude API for multi-label classification tasks.""" |
| 52 | + |
| 53 | + def __init__( |
| 54 | + self, |
| 55 | + model: str = "claude-3-haiku-20240307", |
| 56 | + default_label: str = "Random", |
| 57 | + max_labels: Optional[int] = 5, |
| 58 | + prompt_template: Optional[str] = None, |
| 59 | + key: Optional[str] = None, |
| 60 | + **kwargs, |
| 61 | + ): |
| 62 | + """ |
| 63 | + Multi-label few-shot text classifier using Anthropic's Claude API. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + model : str, optional |
| 68 | + model to use, by default "claude-3-haiku-20240307" |
| 69 | + default_label : str, optional |
| 70 | + default label for failed prediction; if "Random" -> selects randomly based on class frequencies |
| 71 | + max_labels : Optional[int], optional |
| 72 | + maximum labels per sample, by default 5 |
| 73 | + prompt_template : Optional[str], optional |
| 74 | + custom prompt template to use, by default None |
| 75 | + key : Optional[str], optional |
| 76 | + estimator-specific API key; if None, retrieved from the global config |
| 77 | + """ |
| 78 | + super().__init__( |
| 79 | + model=model, |
| 80 | + default_label=default_label, |
| 81 | + max_labels=max_labels, |
| 82 | + prompt_template=prompt_template, |
| 83 | + **kwargs, |
| 84 | + ) |
| 85 | + self._set_keys(key) |
| 86 | + |
| 87 | + |
| 88 | +class DynamicFewShotClaudeClassifier( |
| 89 | + BaseDynamicFewShotClassifier, ClaudeClassifierMixin, SingleLabelMixin |
| 90 | +): |
| 91 | + """ |
| 92 | + Dynamic few-shot text classifier using Anthropic's Claude API for |
| 93 | + single-label classification tasks with dynamic example selection using GPT embeddings. |
| 94 | + """ |
| 95 | + |
| 96 | + def __init__( |
| 97 | + self, |
| 98 | + model: str = "claude-3-haiku-20240307", |
| 99 | + default_label: str = "Random", |
| 100 | + prompt_template: Optional[str] = None, |
| 101 | + key: Optional[str] = None, |
| 102 | + n_examples: int = 3, |
| 103 | + memory_index: Optional[IndexConstructor] = None, |
| 104 | + vectorizer: Optional[BaseVectorizer] = None, |
| 105 | + metric: Optional[str] = "euclidean", |
| 106 | + **kwargs, |
| 107 | + ): |
| 108 | + """ |
| 109 | + Dynamic few-shot text classifier using Anthropic's Claude API. |
| 110 | + For each sample, N closest examples are retrieved from the memory. |
| 111 | +
|
| 112 | + Parameters |
| 113 | + ---------- |
| 114 | + model : str, optional |
| 115 | + model to use, by default "claude-3-haiku-20240307" |
| 116 | + default_label : str, optional |
| 117 | + default label for failed prediction; if "Random" -> selects randomly based on class frequencies |
| 118 | + prompt_template : Optional[str], optional |
| 119 | + custom prompt template to use, by default None |
| 120 | + key : Optional[str], optional |
| 121 | + estimator-specific API key; if None, retrieved from the global config |
| 122 | + n_examples : int, optional |
| 123 | + number of closest examples per class to be retrieved, by default 3 |
| 124 | + memory_index : Optional[IndexConstructor], optional |
| 125 | + custom memory index, for details check `skllm.memory` submodule |
| 126 | + vectorizer : Optional[BaseVectorizer], optional |
| 127 | + scikit-llm vectorizer; if None, `GPTVectorizer` is used |
| 128 | + metric : Optional[str], optional |
| 129 | + metric used for similarity search, by default "euclidean" |
| 130 | + """ |
| 131 | + if vectorizer is None: |
| 132 | + vectorizer = GPTVectorizer(model="text-embedding-ada-002", key=key) |
| 133 | + super().__init__( |
| 134 | + model=model, |
| 135 | + default_label=default_label, |
| 136 | + prompt_template=prompt_template, |
| 137 | + n_examples=n_examples, |
| 138 | + memory_index=memory_index, |
| 139 | + vectorizer=vectorizer, |
| 140 | + metric=metric, |
| 141 | + ) |
| 142 | + self._set_keys(key) |
0 commit comments