diff --git a/.nojekyll b/.nojekyll index 2813ffa..b943e3f 100644 --- a/.nojekyll +++ b/.nojekyll @@ -1 +1 @@ -420142e5 \ No newline at end of file +e1be6e82 \ No newline at end of file diff --git a/index.html b/index.html index 2be8e9e..b7eecfa 100644 --- a/index.html +++ b/index.html @@ -296,7 +296,7 @@
Categories
-
+
-8 min +11 min
diff --git a/index.xml b/index.xml index f2c1310..7d37de4 100644 --- a/index.xml +++ b/index.xml @@ -547,6 +547,254 @@ font-style: inherit;">",
  • I can use that same beautiful Enum across all parts of my application

  • +
    +

    v2.0.1: Using Enum and fuzzywuzzy

    +

    A suggestion from a Twitter user inspired me to enhance our approach by implementing similarity-based matching rather than relying on exact matches. To make it so, I installed the fuzzywuzzy library and made the necessary modifications to increase the likelihood of delivering high-quality results.

    +
    class NamedEntityType(str, Enum):
    +    """Valid types of named entities to extract."""
    +
    +    PERSON = "PERSON"
    +    NORP = "NORP"
    +    FAC = "FAC"
    +    ORG = "ORG"
    +    GPE = "GPE"
    +    LOC = "LOC"
    +    PRODUCT = "PRODUCT"
    +    EVENT = "EVENT"
    +    WORK_OF_ART = "WORK_OF_ART"
    +    LAW = "LAW"
    +    LANGUAGE = "LANGUAGE"
    +    DATE = "DATE"
    +    TIME = "TIME"
    +    PERCENT = "PERCENT"
    +    MONEY = "MONEY"
    +    QUANTITY = "QUANTITY"
    +    ORDINAL = "ORDINAL"
    +    CARDINAL = "CARDINAL"
    +    OTHER = "OTHER"
    +
    +
    +class NamedEntity(BaseModel):
    +    """A named entity result."""
    +
    +    def convert_str_to_named_entity_type(v: str | NamedEntityType) -> NamedEntityType:
    +        """Ensure entity type is a valid enum."""
    +        if isinstance(v, NamedEntityType):
    +            return v
    +        else:
    +            try:
    +                match, score = fuzzy_process.extractOne(v.upper(), [e.value for e in list(NamedEntityType)])
    +                return NamedEntityType(match) if score >= 60 else NamedEntityType.OTHER
    +            except ValueError:
    +                return NamedEntityType.OTHER
    +
    +    entity_type: Annotated[str, BeforeValidator(convert_str_to_named_entity_type)]
    +    entity_mention: str = Field(..., description="The named entity recognized.")
    +
    +
    +class DocumentNERTask(BaseModel):
    +    """Extracts the named entities found in the document.
    +
    +    This tool should be used anytime the user asks for named entity recognition (NER)
    +    or wants to identify named entities.
    +    """
    +
    +    named_entities: list[NamedEntity] = Field(
    +        ...,
    +        description=f"Perform Named Entity Recognition that finds the following entities: {', '.join([x.name for x in NamedEntityType])}",
    +    )
    +

    This improves those cases where, for example, the LLM wants to define the entity type as “ORGANIZATION” but it is defined in the Enum as “ORG”.

    +

    Another option potentially worth exploring is to use the llm_validator function to make a call out to the LLM when exceptions happen and prompt it to coerce the value into something in the Enum. This could hike up your costs a bit but I imagine using a cheap model like GPT-3.5-Turbo could do the job just fine, and would likely you give an addtional robustness in quality results.

    +

    Conclusion

    That’s it.

    diff --git a/posts/2024-07-06-llms-and-enums.html b/posts/2024-07-06-llms-and-enums.html index e849ca8..ab72100 100644 --- a/posts/2024-07-06-llms-and-enums.html +++ b/posts/2024-07-06-llms-and-enums.html @@ -238,6 +238,7 @@

    On this page

  • v0: Using Enum
  • v1: Using Literal
  • v2: Using Enum Revisted
  • +
  • v2.0.1: Using Enum and fuzzywuzzy
  • Conclusion
  • @@ -456,6 +457,65 @@

    v2: Using Enu
  • I can use that same beautiful Enum across all parts of my application

  • +
    +

    v2.0.1: Using Enum and fuzzywuzzy

    +

    A suggestion from a Twitter user inspired me to enhance our approach by implementing similarity-based matching rather than relying on exact matches. To make it so, I installed the fuzzywuzzy library and made the necessary modifications to increase the likelihood of delivering high-quality results.

    +
    class NamedEntityType(str, Enum):
    +    """Valid types of named entities to extract."""
    +
    +    PERSON = "PERSON"
    +    NORP = "NORP"
    +    FAC = "FAC"
    +    ORG = "ORG"
    +    GPE = "GPE"
    +    LOC = "LOC"
    +    PRODUCT = "PRODUCT"
    +    EVENT = "EVENT"
    +    WORK_OF_ART = "WORK_OF_ART"
    +    LAW = "LAW"
    +    LANGUAGE = "LANGUAGE"
    +    DATE = "DATE"
    +    TIME = "TIME"
    +    PERCENT = "PERCENT"
    +    MONEY = "MONEY"
    +    QUANTITY = "QUANTITY"
    +    ORDINAL = "ORDINAL"
    +    CARDINAL = "CARDINAL"
    +    OTHER = "OTHER"
    +
    +
    +class NamedEntity(BaseModel):
    +    """A named entity result."""
    +
    +    def convert_str_to_named_entity_type(v: str | NamedEntityType) -> NamedEntityType:
    +        """Ensure entity type is a valid enum."""
    +        if isinstance(v, NamedEntityType):
    +            return v
    +        else:
    +            try:
    +                match, score = fuzzy_process.extractOne(v.upper(), [e.value for e in list(NamedEntityType)])
    +                return NamedEntityType(match) if score >= 60 else NamedEntityType.OTHER
    +            except ValueError:
    +                return NamedEntityType.OTHER
    +
    +    entity_type: Annotated[str, BeforeValidator(convert_str_to_named_entity_type)]
    +    entity_mention: str = Field(..., description="The named entity recognized.")
    +
    +
    +class DocumentNERTask(BaseModel):
    +    """Extracts the named entities found in the document.
    +
    +    This tool should be used anytime the user asks for named entity recognition (NER)
    +    or wants to identify named entities.
    +    """
    +
    +    named_entities: list[NamedEntity] = Field(
    +        ...,
    +        description=f"Perform Named Entity Recognition that finds the following entities: {', '.join([x.name for x in NamedEntityType])}",
    +    )
    +

    This improves those cases where, for example, the LLM wants to define the entity type as “ORGANIZATION” but it is defined in the Enum as “ORG”.

    +

    Another option potentially worth exploring is to use the llm_validator function to make a call out to the LLM when exceptions happen and prompt it to coerce the value into something in the Enum. This could hike up your costs a bit but I imagine using a cheap model like GPT-3.5-Turbo could do the job just fine, and would likely you give an addtional robustness in quality results.

    +

    Conclusion

    That’s it.

    diff --git a/search.json b/search.json index 56cd2c2..32d6247 100644 --- a/search.json +++ b/search.json @@ -67,7 +67,7 @@ "href": "index.html", "title": "ohmeow", "section": "", - "text": "Order By\n Default\n \n Title\n \n \n Date - Oldest\n \n \n Date - Newest\n \n \n Author\n \n \n Modified - Oldest\n \n \n Modified - Newest\n \n \n \n \n \n \n \n\n\n\n\n\n\n\n\n\n\nStructuring Enums for Flawless LLM results with Instructor\n\n\n\n\n\n\nLLMs\n\n\npydantic\n\n\nInstructor\n\n\n\nEnums enhance code readability and maintainability by replacing hard-coded constants with meaningful names and restricting variables to predefined values that can be used across all tiers of your application. This reduces bugs and improves code clarity, making it easier to refactor and understand. However, it can be frustrating to get LLMs and libraries like Instructor to use them correctly when dealing with structured output.\n\n\n\n\n\nJul 6, 2024\n\n\nWayde Gilliam\n\n\n8 min\n\n\n7/6/24, 5:16:47 PM\n\n\n\n\n\n\n\n\n\n\n\n\nLLM Workshop #1 - How to take a course that never ends\n\n\n\n\n\n\nNLP\n\n\nLLMS\n\n\ndatasets\n\n\nlearning\n\n\nprojects\n\n\n\nWelcome to the inaugural post of my series on the intricacies of my course project from Hamel Husain and Dan Barker’s expansive LLM Workshop/Course. In this opening article, I’ll be diving into a post-mortem analysis of the course, sharing key takeaways, and offering insights on how to effectively navigate a course of this nature.\n\n\n\n\n\nJul 3, 2024\n\n\nWayde Gilliam\n\n\n15 min\n\n\n7/6/24, 1:33:48 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 9: Tabular Modeling\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\ntabular\n\n\nstructured\n\n\ndecision trees\n\n\nrandom forest\n\n\nembeddings\n\n\ncategorical variables\n\n\nboosting\n\n\ngdbt\n\n\nxgboost\n\n\n\nIn chapter of 8 of “Deep Learning for Coders with fastai & PyTorch” we learned that the neural network version of of collaborative model is in fact built on something called TabularModel, and that in fact, an EmbeddingNN is nothing but a TabularModel without any continuous (or real) numbers. “Structured” or “tabular” data describes datasets that look like an Excel spreadsheet or a relational database table, of which, it may be a composed of both categorical and/or real numbers. Working with such data is the subject of chapter 9, so lets go!\n\n\n\n\n\nApr 25, 2022\n\n\nWayde Gilliam\n\n\n22 min\n\n\n6/17/24, 6:58:52 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 8: Collaborative Filtering\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\ncollaborative filtering\n\n\nlatent factors\n\n\nembeddings\n\n\nrecommender systems\n\n\nrecsys\n\n\n\nThis chapter of “Deep Learning for Coders with fastai & PyTorch” moves us away from computer vision to collaborative filtering (think recommendation systems). We’ll explore building these models using the traditional “dot product” approach and also using a neural network, but we’ll begin by covering the idea of “latent factors,” which are both important for colloborative and tabular models. Lets go!\n\n\n\n\n\nMar 31, 2022\n\n\nWayde Gilliam\n\n\n10 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 7: Advanced techniques for training image classification models\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nclassification\n\n\ncomputer vision\n\n\ntechniques\n\n\nbag of tricks\n\n\n\nThis chapter of \"Deep Learning for Coders with fastai & PyTorch\" details several techniques you can apply to getting SOTA results with your image classification models! It’s the last chapter dedicated to computer vision before diving into colloborate filtering, tabular, and NLP models\n\n\n\n\n\nMar 28, 2022\n\n\nWayde Gilliam\n\n\n8 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 6: Regression\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nregression\n\n\ncomputer vision\n\n\nkey point\n\n\n\nIts the more things you can do with computer vision chapter of \"Deep Learning for Coders with fastai & PyTorch\"! Having looked at both multiclass and multilable classification, we now turn our attention to regression tasks. In particular, we’ll look at key point regression models covered in chapter 6. Soooo lets go!\n\n\n\n\n\nFeb 9, 2022\n\n\nWayde Gilliam\n\n\n5 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 6: Multilabel Classification\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nmulti-label classification\n\n\nbinary cross entropy\n\n\nBCE\n\n\ncomputer vision\n\n\n\nIts the more things you can do with computer vision chapter of \"Deep Learning for Coders with fastai & PyTorch\"! We’ll go over everything you need to know to get started with multi-label classification tasks from datablocks to training and everything in between. Next post we’ll look at regression tasks, in particular key point regression models that are also covered in chapter 6. Soooo lets go!\n\n\n\n\n\nJun 10, 2021\n\n\nWayde Gilliam\n\n\n10 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 5: Multiclass classification\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nmulticlass classification\n\n\ncomputer vision\n\n\n\nIts the image classification chapter of \"Deep Learning for Coders with fastai & PyTorch\"! We’ll go over everything you need to know to get started with multiclass classification, from setting up your DataBlock and loss function, to some of the core techniques for evaluating and improving your model’s predictions. So without further adieu, lets go …\n\n\n\n\n\nJun 3, 2021\n\n\nWayde Gilliam\n\n\n11 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nContributing to fastai: Setup your local development environment & submit a PR\n\n\n\n\n\n\nfastai\n\n\ngithub\n\n\nopen source\n\n\npull requests\n\n\n\nA few hours ago I was working on a PR for fastai, and as it has been awhile I realized I couldn’t quite remember all the steps required to do so. Fortunately, I got it figured out pretty quickly and decided I better blog the steps for when I forget next (I am almost 50 after all). So for all you developers looking to contribute to fastai, or really any open source project, here’s everything you need to know to setup your local development environment and submit PRs to fastai. Enjoy!\n\n\n\n\n\nJun 2, 2021\n\n\nWayde Gilliam\n\n\n3 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nMultilingual Sequence Classifaction with the MBart Family\n\n\n\n\n\n\nblurr\n\n\nhuggingface\n\n\nfastai\n\n\nmultilingual\n\n\nsequence classification\n\n\n\nNeed to do some multi-lingual sequence classification? Look no further, at least if you want to use MBart and/or the MBart-50 variety of models. Working against the amazon_reviews_multi dataset I’ll show you how to use the blurr library to configure the huggingface objects, build DataLoaders, and train a model that you can use for classifying German text. I’ll throw in a bit of the inference code so that you can see how easy blurr makes it to use your trained model to boot. Let’s go …\n\n\n\n\n\nMay 25, 2021\n\n\nWayde Gilliam\n\n\n2 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 4: Stochastic Gradient Descent\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe fourth in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nMay 23, 2021\n\n\nWayde Gilliam\n\n\n9 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 3: Data Ethics\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe third in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nNov 22, 2020\n\n\nWayde Gilliam\n\n\n9 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 2: Doing Deep Learning\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe second in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nNov 16, 2020\n\n\nWayde Gilliam\n\n\n16 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 1: The Basics of Deep Learning\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe first in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nNov 6, 2020\n\n\nWayde Gilliam\n\n\n25 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nSummarization with blurr\n\n\n\n\n\n\nfastai\n\n\nhuggingface\n\n\nblurr\n\n\nsummarization\n\n\ntext generation\n\n\n\nblurr is a libray I started that integrates huggingface transformers with the world of fastai v2, giving fastai devs everything they need to train, evaluate, and deploy transformer specific models. In this article, I provide a simple example of how to use blurr’s new summarization capabilities to train, evaluate, and deploy a BART summarization model.\n\n\n\n\n\nMay 23, 2020\n\n\nWayde Gilliam\n\n\n4 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nFinding DataBlock Nirvana with fast.ai v2 - Part 1\n\n\n\n\n\n\nfastai\n\n\ndatablock api\n\n\ndata\n\n\npytorch\n\n\n\nThe path to enlightment begins here!\n\n\n\n\n\nApr 11, 2020\n\n\nWayde Gilliam\n\n\n25 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nLoss Functions: Cross Entropy Loss and You!\n\n\n\n\n\nMeet multi-classification’s favorite loss function\n\n\n\n\n\nApr 4, 2020\n\n\nWayde Gilliam\n\n\n4 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nUnderstanding the F-Beta metric\n\n\n\n\n\nWhat is F-Beta, how should I use it, and what in the hell is ‘average’ and ‘sample_weight’\n\n\n\n\n\nJan 1, 2019\n\n\nWayde Gilliam\n\n\n4 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\nNo matching items" + "text": "Order By\n Default\n \n Title\n \n \n Date - Oldest\n \n \n Date - Newest\n \n \n Author\n \n \n Modified - Oldest\n \n \n Modified - Newest\n \n \n \n \n \n \n \n\n\n\n\n\n\n\n\n\n\nStructuring Enums for Flawless LLM results with Instructor\n\n\n\n\n\n\nLLMs\n\n\npydantic\n\n\nInstructor\n\n\n\nEnums enhance code readability and maintainability by replacing hard-coded constants with meaningful names and restricting variables to predefined values that can be used across all tiers of your application. This reduces bugs and improves code clarity, making it easier to refactor and understand. However, it can be frustrating to get LLMs and libraries like Instructor to use them correctly when dealing with structured output.\n\n\n\n\n\nJul 6, 2024\n\n\nWayde Gilliam\n\n\n11 min\n\n\n7/7/24, 1:11:48 PM\n\n\n\n\n\n\n\n\n\n\n\n\nLLM Workshop #1 - How to take a course that never ends\n\n\n\n\n\n\nNLP\n\n\nLLMS\n\n\ndatasets\n\n\nlearning\n\n\nprojects\n\n\n\nWelcome to the inaugural post of my series on the intricacies of my course project from Hamel Husain and Dan Barker’s expansive LLM Workshop/Course. In this opening article, I’ll be diving into a post-mortem analysis of the course, sharing key takeaways, and offering insights on how to effectively navigate a course of this nature.\n\n\n\n\n\nJul 3, 2024\n\n\nWayde Gilliam\n\n\n15 min\n\n\n7/6/24, 1:33:48 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 9: Tabular Modeling\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\ntabular\n\n\nstructured\n\n\ndecision trees\n\n\nrandom forest\n\n\nembeddings\n\n\ncategorical variables\n\n\nboosting\n\n\ngdbt\n\n\nxgboost\n\n\n\nIn chapter of 8 of “Deep Learning for Coders with fastai & PyTorch” we learned that the neural network version of of collaborative model is in fact built on something called TabularModel, and that in fact, an EmbeddingNN is nothing but a TabularModel without any continuous (or real) numbers. “Structured” or “tabular” data describes datasets that look like an Excel spreadsheet or a relational database table, of which, it may be a composed of both categorical and/or real numbers. Working with such data is the subject of chapter 9, so lets go!\n\n\n\n\n\nApr 25, 2022\n\n\nWayde Gilliam\n\n\n22 min\n\n\n6/17/24, 6:58:52 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 8: Collaborative Filtering\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\ncollaborative filtering\n\n\nlatent factors\n\n\nembeddings\n\n\nrecommender systems\n\n\nrecsys\n\n\n\nThis chapter of “Deep Learning for Coders with fastai & PyTorch” moves us away from computer vision to collaborative filtering (think recommendation systems). We’ll explore building these models using the traditional “dot product” approach and also using a neural network, but we’ll begin by covering the idea of “latent factors,” which are both important for colloborative and tabular models. Lets go!\n\n\n\n\n\nMar 31, 2022\n\n\nWayde Gilliam\n\n\n10 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 7: Advanced techniques for training image classification models\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nclassification\n\n\ncomputer vision\n\n\ntechniques\n\n\nbag of tricks\n\n\n\nThis chapter of \"Deep Learning for Coders with fastai & PyTorch\" details several techniques you can apply to getting SOTA results with your image classification models! It’s the last chapter dedicated to computer vision before diving into colloborate filtering, tabular, and NLP models\n\n\n\n\n\nMar 28, 2022\n\n\nWayde Gilliam\n\n\n8 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 6: Regression\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nregression\n\n\ncomputer vision\n\n\nkey point\n\n\n\nIts the more things you can do with computer vision chapter of \"Deep Learning for Coders with fastai & PyTorch\"! Having looked at both multiclass and multilable classification, we now turn our attention to regression tasks. In particular, we’ll look at key point regression models covered in chapter 6. Soooo lets go!\n\n\n\n\n\nFeb 9, 2022\n\n\nWayde Gilliam\n\n\n5 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 6: Multilabel Classification\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nmulti-label classification\n\n\nbinary cross entropy\n\n\nBCE\n\n\ncomputer vision\n\n\n\nIts the more things you can do with computer vision chapter of \"Deep Learning for Coders with fastai & PyTorch\"! We’ll go over everything you need to know to get started with multi-label classification tasks from datablocks to training and everything in between. Next post we’ll look at regression tasks, in particular key point regression models that are also covered in chapter 6. Soooo lets go!\n\n\n\n\n\nJun 10, 2021\n\n\nWayde Gilliam\n\n\n10 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 5: Multiclass classification\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\nmulticlass classification\n\n\ncomputer vision\n\n\n\nIts the image classification chapter of \"Deep Learning for Coders with fastai & PyTorch\"! We’ll go over everything you need to know to get started with multiclass classification, from setting up your DataBlock and loss function, to some of the core techniques for evaluating and improving your model’s predictions. So without further adieu, lets go …\n\n\n\n\n\nJun 3, 2021\n\n\nWayde Gilliam\n\n\n11 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nContributing to fastai: Setup your local development environment & submit a PR\n\n\n\n\n\n\nfastai\n\n\ngithub\n\n\nopen source\n\n\npull requests\n\n\n\nA few hours ago I was working on a PR for fastai, and as it has been awhile I realized I couldn’t quite remember all the steps required to do so. Fortunately, I got it figured out pretty quickly and decided I better blog the steps for when I forget next (I am almost 50 after all). So for all you developers looking to contribute to fastai, or really any open source project, here’s everything you need to know to setup your local development environment and submit PRs to fastai. Enjoy!\n\n\n\n\n\nJun 2, 2021\n\n\nWayde Gilliam\n\n\n3 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nMultilingual Sequence Classifaction with the MBart Family\n\n\n\n\n\n\nblurr\n\n\nhuggingface\n\n\nfastai\n\n\nmultilingual\n\n\nsequence classification\n\n\n\nNeed to do some multi-lingual sequence classification? Look no further, at least if you want to use MBart and/or the MBart-50 variety of models. Working against the amazon_reviews_multi dataset I’ll show you how to use the blurr library to configure the huggingface objects, build DataLoaders, and train a model that you can use for classifying German text. I’ll throw in a bit of the inference code so that you can see how easy blurr makes it to use your trained model to boot. Let’s go …\n\n\n\n\n\nMay 25, 2021\n\n\nWayde Gilliam\n\n\n2 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 4: Stochastic Gradient Descent\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe fourth in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nMay 23, 2021\n\n\nWayde Gilliam\n\n\n9 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 3: Data Ethics\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe third in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nNov 22, 2020\n\n\nWayde Gilliam\n\n\n9 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 2: Doing Deep Learning\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe second in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nNov 16, 2020\n\n\nWayde Gilliam\n\n\n16 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nA Journey Through Fastbook (AJTFB) - Chapter 1: The Basics of Deep Learning\n\n\n\n\n\n\nfastai\n\n\nfastbook\n\n\n\nThe first in a weekly-ish series where I revisit the fast.ai book, \"Deep Learning for Coders with fastai & PyTorch\", and provide commentary on the bits that jumped out to me chapter by chapter. So without further adieu, let’s go!\n\n\n\n\n\nNov 6, 2020\n\n\nWayde Gilliam\n\n\n25 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nSummarization with blurr\n\n\n\n\n\n\nfastai\n\n\nhuggingface\n\n\nblurr\n\n\nsummarization\n\n\ntext generation\n\n\n\nblurr is a libray I started that integrates huggingface transformers with the world of fastai v2, giving fastai devs everything they need to train, evaluate, and deploy transformer specific models. In this article, I provide a simple example of how to use blurr’s new summarization capabilities to train, evaluate, and deploy a BART summarization model.\n\n\n\n\n\nMay 23, 2020\n\n\nWayde Gilliam\n\n\n4 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nFinding DataBlock Nirvana with fast.ai v2 - Part 1\n\n\n\n\n\n\nfastai\n\n\ndatablock api\n\n\ndata\n\n\npytorch\n\n\n\nThe path to enlightment begins here!\n\n\n\n\n\nApr 11, 2020\n\n\nWayde Gilliam\n\n\n25 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nLoss Functions: Cross Entropy Loss and You!\n\n\n\n\n\nMeet multi-classification’s favorite loss function\n\n\n\n\n\nApr 4, 2020\n\n\nWayde Gilliam\n\n\n4 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\n\n\n\n\n\n\nUnderstanding the F-Beta metric\n\n\n\n\n\nWhat is F-Beta, how should I use it, and what in the hell is ‘average’ and ‘sample_weight’\n\n\n\n\n\nJan 1, 2019\n\n\nWayde Gilliam\n\n\n4 min\n\n\n6/17/24, 6:27:14 PM\n\n\n\n\n\n\nNo matching items" }, { "objectID": "posts/2022-04-25-ajtfb-chapter-9.html", @@ -272,6 +272,13 @@ "section": "v2: Using Enum Revisted", "text": "v2: Using Enum Revisted\nHere’s the TL;DR version of the code. This version is working fabulously across all APIs and I have yet to encounter a single exception involving Instructor being unable to assign a valid value from the Enum.\nclass NamedEntityType(str, Enum):\n \"\"\"Valid types of named entities to extract.\"\"\"\n\n PERSON = \"PERSON\"\n NORP = \"NORP\"\n FAC = \"FAC\"\n ORG = \"ORG\"\n GPE = \"GPE\"\n LOC = \"LOC\"\n PRODUCT = \"PRODUCT\"\n EVENT = \"EVENT\"\n WORK_OF_ART = \"WORK_OF_ART\"\n LAW = \"LAW\"\n LANGUAGE = \"LANGUAGE\"\n DATE = \"DATE\"\n TIME = \"TIME\"\n PERCENT = \"PERCENT\"\n MONEY = \"MONEY\"\n QUANTITY = \"QUANTITY\"\n ORDINAL = \"ORDINAL\"\n CARDINAL = \"CARDINAL\"\n OTHER = \"OTHER\"\n\n\nclass NamedEntity(BaseModel):\n \"\"\"A named entity result.\"\"\"\n\n def convert_str_to_named_entity_type(v: str | NamedEntityType) -> NamedEntityType:\n \"\"\"Ensure entity type is a valid enum.\"\"\"\n if isinstance(v, NamedEntityType):\n return v\n else:\n try:\n return NamedEntityType(v)\n except ValueError:\n return NamedEntityType.OTHER\n\n entity_type: Annotated[str, BeforeValidator(convert_str_to_named_entity_type)]\n entity_mention: str = Field(..., description=\"The named entity recognized.\")\n\n\nclass DocumentNERTask(BaseModel):\n \"\"\"Extracts the named entities found in the document.\n\n This tool should be used anytime the user asks for named entity recognition (NER)\n or wants to identify named entities.\n \"\"\"\n\n named_entities: list[NamedEntity] = Field(\n ...,\n description=f\"Perform Named Entity Recognition that finds the following entities: {', '.join([x.name for x in NamedEntityType])}\",\n )\nBesides the return of the Enum, the most noticeable change involves the inclusion of a BeforeValidator that ensures the value is assigned to a valid enum as defined in NamedEntity. In cases where it wants to add an entity to the list of named_entities that isn’t defined in the NamedEntityType enum or is named differently (e.g., “ORGANIZATION” vs. “ORG”), it will assign them to OTHER.\nWith this in place, I now have a solution that is:\n\nMore resiliant\nCan be used in debugging named entity recogintion (e.g, I can explore what named entities might be missing from the Enum or getting named differently by looking at those that get associated with the OTHER value)\nI can use that same beautiful Enum across all parts of my application" }, + { + "objectID": "posts/2024-07-06-llms-and-enums.html#v2.0.1-using-enum-and-fuzzywuzzy", + "href": "posts/2024-07-06-llms-and-enums.html#v2.0.1-using-enum-and-fuzzywuzzy", + "title": "Structuring Enums for Flawless LLM results with Instructor", + "section": "v2.0.1: Using Enum and fuzzywuzzy", + "text": "v2.0.1: Using Enum and fuzzywuzzy\nA suggestion from a Twitter user inspired me to enhance our approach by implementing similarity-based matching rather than relying on exact matches. To make it so, I installed the fuzzywuzzy library and made the necessary modifications to increase the likelihood of delivering high-quality results.\nclass NamedEntityType(str, Enum):\n \"\"\"Valid types of named entities to extract.\"\"\"\n\n PERSON = \"PERSON\"\n NORP = \"NORP\"\n FAC = \"FAC\"\n ORG = \"ORG\"\n GPE = \"GPE\"\n LOC = \"LOC\"\n PRODUCT = \"PRODUCT\"\n EVENT = \"EVENT\"\n WORK_OF_ART = \"WORK_OF_ART\"\n LAW = \"LAW\"\n LANGUAGE = \"LANGUAGE\"\n DATE = \"DATE\"\n TIME = \"TIME\"\n PERCENT = \"PERCENT\"\n MONEY = \"MONEY\"\n QUANTITY = \"QUANTITY\"\n ORDINAL = \"ORDINAL\"\n CARDINAL = \"CARDINAL\"\n OTHER = \"OTHER\"\n\n\nclass NamedEntity(BaseModel):\n \"\"\"A named entity result.\"\"\"\n\n def convert_str_to_named_entity_type(v: str | NamedEntityType) -> NamedEntityType:\n \"\"\"Ensure entity type is a valid enum.\"\"\"\n if isinstance(v, NamedEntityType):\n return v\n else:\n try:\n match, score = fuzzy_process.extractOne(v.upper(), [e.value for e in list(NamedEntityType)])\n return NamedEntityType(match) if score >= 60 else NamedEntityType.OTHER\n except ValueError:\n return NamedEntityType.OTHER\n\n entity_type: Annotated[str, BeforeValidator(convert_str_to_named_entity_type)]\n entity_mention: str = Field(..., description=\"The named entity recognized.\")\n\n\nclass DocumentNERTask(BaseModel):\n \"\"\"Extracts the named entities found in the document.\n\n This tool should be used anytime the user asks for named entity recognition (NER)\n or wants to identify named entities.\n \"\"\"\n\n named_entities: list[NamedEntity] = Field(\n ...,\n description=f\"Perform Named Entity Recognition that finds the following entities: {', '.join([x.name for x in NamedEntityType])}\",\n )\nThis improves those cases where, for example, the LLM wants to define the entity type as “ORGANIZATION” but it is defined in the Enum as “ORG”.\nAnother option potentially worth exploring is to use the llm_validator function to make a call out to the LLM when exceptions happen and prompt it to coerce the value into something in the Enum. This could hike up your costs a bit but I imagine using a cheap model like GPT-3.5-Turbo could do the job just fine, and would likely you give an addtional robustness in quality results." + }, { "objectID": "posts/2024-07-06-llms-and-enums.html#conclusion", "href": "posts/2024-07-06-llms-and-enums.html#conclusion", diff --git a/sitemap.xml b/sitemap.xml index 72eef17..feb4c57 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -26,7 +26,7 @@ https://ohmeow.com/posts/2024-07-06-llms-and-enums.html - 2024-07-07T00:16:47.204Z + 2024-07-07T20:11:48.838Z https://ohmeow.com/posts/2021-05-23-ajtfb-chapter-4.html