Package: not-again-ai
not-again-ainot-again-aiis a collection of various functionalities that come up over and over again when developing AI projects as a Data/Applied/Research Scientist. It is designed to be simple and minimize dependencies first and foremost. This is not meant to reinvent the wheel, but instead be a home for functions that don’t belong well elsewhere. Additionally, feel free toa)use this as a template for your own Python package.b)instead of installing the package, copy and paste functions into your own projects (this is made easier with the limited amount of dependencies and the MIT license).Documentationavailable within thereadmesor auto-generated atDaveCoDev.github.io/not-again-ai/.InstallationRequires: Python 3.11, or 3.12Install the entire package fromPyPIwith:$pipinstallnot_again_ai[llm,statistics,viz]The package is split into subpackages, so you can install only the parts you need.Base only:pip install not_again_aiLLM only:pip install not_again_ai[llm]Statistics:pip install not_again_ai[statistics]Visualization:pip install not_again_ai[viz]Quick TourBaseREADMEThe base package includes only functions that have minimal external dependencies and are useful in a variety of situations such as parallelization and filesystem operations.LLM (Large Language Model)READMESupports OpenAI chat completions and text embeddings. Includes functions for creating chat completion prompts, token management, and context management.One example:client=openai_client()messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hello!"}]response=chat_completion(messages=messages,model="gpt-3.5-turbo",max_tokens=100,client=client)["message"]>>>"Hello! How can I help you today?"StatisticsREADMEWe provide a few helpers for data analysis such as:fromnot_again_ai.statistics.dependenceimportpearson_correlation# quadratic dependence>>>x=(rs.rand(500)*4)-2>>>y=x**2+(rs.randn(500)*0.2)>>>pearson_correlation(x,y)0.05VisualizationREADMEWe offer opinionated wrappers around seaborn to make common visualizations easier to create and customize.>>>importnumpyasnp>>>importpandasaspd>>>fromnot_again_ai.viz.time_seriesimportts_lineplot>>>fromnot_again_ai.viz.distributionsimportunivariate_distplot# get some time series data>>>rs=np.random.RandomState(365)>>>values=rs.randn(365,4).cumsum(axis=0)>>>dates=pd.date_range('1 1 2021',periods=365,freq='D')# plot the time series and save it to a file>>>ts_lineplot(ts_data=values,save_pathname='myplot.png',ts_x=dates,ts_names=['A','B','C','D'])# get a random distribution>>>distrib=np.random.beta(a=0.5,b=0.5,size=1000)# plot the distribution and save it to a file>>>univariate_distplot(...data=distrib,...save_pathname='mydistribution.svg',...print_summary=False,bins=100,...title=r'Beta Distribution $\alpha=0.5, \beta=0.5$'...)Development InformationThe following information is relevant if you would like to contribute or use this package as a template for yourself.This package usesPoetryto manage dependencies and
isolatedPython virtual environments. To proceed, be sure to first installpipxand theninstall Poetry.Install Poetry Plugin: Export$pipxinjectpoetrypoetry-plugin-export(Optional) configure Poetry to use an in-project virtual environment.$poetryconfigvirtualenvs.in-projecttrueDependenciesDependencies are defined inpyproject.tomland specific versions are locked
intopoetry.lock. This allows for exact reproducible environments across
all machines that use the project, both during development and in production.To upgrade all dependencies to the versions defined inpyproject.toml:$poetryupdateTo install all dependencies (with all extra dependencies) into an isolated virtual environment:Append--syncto uninstall dependencies that are no longer in use from the virtual environment.$poetryinstall--all-extrasToactivatethe
virtual environment that is automatically created by Poetry:$poetryshellTo deactivate the environment:(.venv)$exitPackagingThis project is designed as a Python package, meaning that it can be bundled up and redistributed
as a single compressed file.Packaging is configured by:pyproject.tomlTo package the project as both asource distributionand
awheel:$poetrybuildThis will generatedist/not-again-ai-<version>.tar.gzanddist/not_again_ai-<version>-py3-none-any.whl.Read more about theadvantages of wheelsto understand why generating
wheel distributions are important.Publish Distributions to PyPISource and wheel redistributable packages can
bepublished to PyPIor installed
directly from the filesystem usingpip.$poetrypublishEnforcing Code QualityAutomated code quality checks are performed usingNoxandnox-poetry. Nox will automatically create virtual
environments and run commands based onnoxfile.pyfor unit testing, PEP 8 style
guide checking, type checking and documentation generation.Note:noxis installed into the virtual environment automatically by thepoetry installcommand above. Runpoetry shellto activate the virtual environment.To run all default sessions:(.venv)$noxUnit TestingUnit testing is performed withpytest. pytest has become the de facto Python
unit testing framework. Some key advantages over the built-inunittestmodule are:Significantly less boilerplate needed for tests.PEP 8 compliant names (e.g.pytest.raises()instead ofself.assertRaises()).Vibrant ecosystem of plugins.pytest will automatically discover and run tests by recursively searching for folders and.pyfiles prefixed withtestfor any functions prefixed bytest.Thetestsfolder is created as a Python package (i.e. there is an__init__.pyfile within it)
because this helpspytestuniquely namespace the test files. Without this, two test files cannot
be named the same, even if they are in different subdirectories.Code coverage is provided by thepytest-covplugin.When running a unit test Nox session (e.g.nox -s test), an HTML report is generated in
thehtmlcovfolder showing each source file and which lines were executed during unit testing.
Openhtmlcov/index.htmlin a web browser to view the report. Code coverage reports help identify
areas of the project that are currently not tested.pytest and code coverage are configured inpyproject.toml.To pass arguments topytestthroughnox:(.venv)$nox-stest---kinvalid_factorialCode Style CheckingPEP 8is the universally accepted style guide for Python
code. PEP 8 code compliance is verified usingRuff. Ruff is configured in the[tool.ruff]section ofpyproject.toml.To lint code, run:(.venv)$nox-slintTo automatically fix fixable lint errors, run:(.venv)$nox-slint_fixAutomated Code FormattingRuffis used to automatically format code and group and sort imports.To automatically format code, run:(.venv)$nox-sfmtTo verify code has been formatted, such as in a CI job:(.venv)$nox-sfmt_checkType CheckingType annotationsallows developers to include
optional static typing information to Python source code. This allows static analyzers such
asmypy,PyCharm,
orPyrightto check that functions are used with the
correct types before runtime.deffactorial(n:int)->int:...mypy is configured inpyproject.toml. To type check code, run:(.venv)$nox-stype_checkSee alsoawesome-python-typing.Distributing Type AnnotationsPEP 561defines how a Python package should
communicate the presence of inline type annotations to static type
checkers.mypy's documentationprovides further examples on how to do this.Mypy looks for the existence of a file namedpy.typedin the root of the
installed package to indicate that inline type annotations should be checked.Continuous IntegrationContinuous integration is provided byGitHub Actions. This
runs all tests, lints, and type checking for every commit and pull request to the repository.GitHub Actions is configured in.github/workflows/python.yml.Visual Studio CodeInstall thePython extensionfor VSCode.Install theRuff extensionfor VSCode.Default settings are configured in.vscode/settings.json. This will enable Ruff and black with consistent settings.DocumentationGenerating a User GuideMaterial for MkDocsis a powerful static site
generator that combines easy-to-write Markdown, with a number of Markdown extensions that increase
the power of Markdown. This makes it a great fit for user guides and other technical documentation.The example MkDocs project included in this project is configured to allow the built documentation
to be hosted at any URL or viewed offline from the file system.To build the user guide, run,(.venv)$nox-sdocsand opendocs/user_guide/site/index.htmlusing a web browser.To build the user guide, additionally validating external URLs, run:(.venv)$nox-sdocs_check_urlsTo build the user guide in a format suitable for viewing directly from the file system, run:(.venv)$nox-sdocs_offlineTo build and serve the user guide with automatic rebuilding as you change the contents,
run:(.venv)$nox-sdocs_serveand openhttp://127.0.0.1:8000in a browser.Each time themainGit branch is updated, the.github/workflows/pages.ymlGitHub Action will
automatically build the user guide and publish it toGitHub Pages.
This is configured in thedocs_github_pagesNox session.Generating API DocumentationThis project usesmkdocstringsplugin for
MkDocs, which rendersGoogle-style docstringsinto an MkDocs project. Google-style docstrings provide a good mix of easy-to-read docstrings in
code as well as nicely-rendered output."""Computes the factorial through a recursive algorithm.Args:n: A positive input value.Raises:InvalidFactorialError: If n is less than 0.Returns:Computed factorial."""MiscIf you get aFailed to create the collection: Prompt dismissed..error when runningpoetry updateon Ubuntu, try setting the following environment variable:```bash
export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
```Attributionspython-blueprintfor the Python package skeleton.