Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

The Python Decorator That Supercharges Developer Experience 🚀
Latest   Machine Learning

The Python Decorator That Supercharges Developer Experience 🚀

Last Updated on August 9, 2023 by Editorial Team

Author(s): Matan Kleyman

Originally published on Towards AI.

The Python Decorator That Supercharges Developer Experience U+1F680

Python’s @overload decorator, found in the typing builtin Python module, allows developers to specify multiple type-specific signatures for a function or method. This provides a foundation for clearer type checking and enhanced IDE tooling.

Why is it important? U+26A0️

The @overload decorator is widely used, often without us explicitly recognizing its presence.

Have you ever wondered how built-in Python functions like max() can accept numerous parameter permutations, and yet your IDE knows how to handle them?

Consider the versatility of max() function as illustrated in the following example. We can use the function in different ways.

A screenshot of max() usages U+007C Image by author

But once you provide an incorrect parameter, your IDE immediately warns you about an unexpected argument.

A screenshot of IDE warning when passing illegal parameter U+007C Image by author

So, how does that work ? U+2699️

The @overload decorator provides cues to static type checkers and IDEs about potential usages. Therefore makes it possible to validate whether calls to an overloaded function or method match one of the defined signatures, highlighting discrepancies during development.

Each overload declaration tackles a specific case of argument and output types.

A screenshot of python source code overload implementation. U+007C Image by author

A Real-World Use-Case in Declarai U+2705

While developing Declarai β€” an open-source framework that translates Python code into LLM tasks β€” I came across the amazing advantages of using overloads.

The Declarai package offers an interface to AI models from various providers. However, initializing these models is not straightforward. Each provider has its unique set of models, configurations, and associated API tokens. This is where overload comes in handy. Rather than depending on runtime errors to guide developers, we've employed overload to guide the developer about valid parameter combinations for each provider.

GitHub β€” vendi-ai/declarai: A simple yet extensible framework for working with LLMs in real world…

A simple yet extensible framework for working with LLMs in real world software. Start small and grow as you need! …

github.com

How we used @overload?

In Declarai’s primary interface, our objective is to guide developers in correctly initializing the Declarai class with the appropriate set of language model configurations.

Therefore we can use the @overload decorator as follows to assist developers in initializing Declarai with models from β€” OpenAI, Huggingface, and AI21.

class Declarai:
@overload
def __init__(
self,
provider: ProviderOpenai,
model: ModelsOpenai,
version: Optional[str] = None,
openai_token: Optional[str] = None,
):
...

@overload
def __init__(
self,
provider: ProviderHuggingface,
model: str,
hub_api_token: Optional[str] = None,
):
...

@overload
def __init__(
self,
provider: ProviderAI21,
model: ModelsAI21,
ai21_api_token: Optional[str] = None,
):
...

def __init__(**kwargs):
# Let's ignore the actual implementation since its
# not relevant to showcase overloads usage
print(**kwargs)

In the example above, you can see different initializers are set for every provider. This ensures that developers receive guidance on the appropriate model names, configuration settings, and token parameters specific to the chosen provider.

Disclaimer β€” Not all of the providers in the example above are implemented in Declarai. Those were just used as an example.

How Does This Impact the Developer Experience? U+1F929

Consider the following PyCharm examples:

PyCharm screenshot. No rights reserved

You can observe how PyCharm IDE alerts about invalid model and parameter combinations, as well as unexpected arguments.

Wow! Isn’t this preferable to encountering runtime errors?

Conclusion

The overload decorator in Python is more than merely a type-checking tool. In complex system interfaces, it acts as a guiding hand, ensuring developers select the right options and provide the appropriate parameters. This β€œsmall” things can create a developer experience that is both intuitive and error-resistant.

Follow Declarai U+1F4E9

I hope you like the article, I would highly recommend you follow my Linkedin Page.

Moreover I encourage you to explore Declarai in real-time and witness how great it is to work with a framework that harnesses overloads to make the developer experience much better.

Check out the docs U+1F4D6

Declarai

Declarai, turning Python code into LLM tasks, easy to use, and production-ready. Declarai turns your Python code into…

vendi-ai.github.io

Star us on github ⭐️

GitHub – vendi-ai/declarai: A simple yet extensible framework for working with LLMs in real world…

A simple yet extensible framework for working with LLMs in real world software. Start small and grow as you need! …

github.com

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓