-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better support for classification tasks with large number of label classes #561
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a070422
for classification tasks with a large number of categories, filter th…
iomap e3c2126
replace Chroma DB with autolabels own VectorStoreWrapper. Remove debu…
iomap 3cfca54
move label selection logic into its own class
iomap f97a82d
allow for LabelSelector.k to be specified in config
iomap 16f769b
clear up comment
iomap 547e811
remove default for embedding_func=OpenAIEmbeddings() , as this requir…
iomap 2a6ec29
if task_selection=true, check that task_type=classification
iomap e5ff12a
remove unnused imports
iomap 921c096
Merge branch 'main' into many_classes_support
iomap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Dict, List | ||
import bisect | ||
|
||
from autolabel.few_shot.vector_store import cos_sim | ||
|
||
from langchain.embeddings.openai import OpenAIEmbeddings | ||
|
||
|
||
class LabelSelector: | ||
"""Returns the most similar labels to a given input. Used for | ||
classification tasks with a large number of possible classes.""" | ||
|
||
labels: List[str] | ||
"""A list of the possible labels to choose from.""" | ||
|
||
k: int = 10 | ||
"""Number of labels to select""" | ||
|
||
embedding_func = OpenAIEmbeddings() | ||
"""Function used to generate embeddings of labels/input""" | ||
|
||
labels_embeddings: Dict = {} | ||
"""Dict used to store embeddings of each label""" | ||
|
||
def __init__( | ||
self, labels: List[str], k: int = 10, embedding_func=OpenAIEmbeddings() | ||
) -> None: | ||
self.labels = labels | ||
self.k = min(k, len(labels)) | ||
self.embedding_func = embedding_func | ||
for l in self.labels: | ||
self.labels_embeddings[l] = self.embedding_func.embed_query(l) | ||
|
||
def select_labels(self, input: str) -> List[str]: | ||
"""Select which labels to use based on the similarity to input""" | ||
input_embedding = self.embedding_func.embed_query(input) | ||
|
||
scores = [] | ||
for label, embedding in self.labels_embeddings.items(): | ||
similarity = cos_sim(embedding, input_embedding) | ||
# insert into scores, while maintaining sorted order | ||
bisect.insort(scores, (similarity, label)) | ||
return [label for (_, label) in scores[-self.k :]] | ||
|
||
@classmethod | ||
def from_examples( | ||
cls, | ||
labels: List[str], | ||
k: int = 10, | ||
embedding_func=OpenAIEmbeddings(), | ||
) -> LabelSelector: | ||
"""Create pass-through label selector using given list of labels | ||
|
||
Returns: | ||
The LabelSelector instantiated | ||
""" | ||
return cls(labels=labels, k=k, embedding_func=embedding_func) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,11 @@ | |
|
||
import json | ||
|
||
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably don't need these imports now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed these imports in e5ff12a |
||
from langchain.embeddings import OpenAIEmbeddings | ||
from langchain.prompts import FewShotPromptTemplate, PromptTemplate | ||
from autolabel.few_shot.vector_store import VectorStoreWrapper | ||
|
||
|
||
class ClassificationTask(BaseTask): | ||
DEFAULT_OUTPUT_GUIDELINES = ( | ||
|
@@ -48,13 +53,18 @@ def __init__(self, config: AutolabelConfig) -> None: | |
if self.config.confidence(): | ||
self.metrics.append(AUROCMetric()) | ||
|
||
def construct_prompt(self, input: Dict, examples: List) -> str: | ||
def construct_prompt( | ||
self, input: Dict, examples: List, selected_labels: List[str] = None | ||
) -> str: | ||
# Copy over the input so that we can modify it | ||
input = input.copy() | ||
|
||
# prepare task guideline | ||
labels_list = self.config.labels_list() | ||
labels_list = ( | ||
self.config.labels_list() if not selected_labels else selected_labels | ||
) | ||
num_labels = len(labels_list) | ||
|
||
fmt_task_guidelines = self.task_guidelines.format( | ||
num_labels=num_labels, labels="\n".join(labels_list) | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this would give an error if label selection was set to true for any task other than classification. This is because the construct_prompt has been changed just for the classification task. Any way to catch this i.e label selection not supported for this task
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. Will check that it is a classification task (if label_selection = true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in 2a6ec29