Skip to main content

versions license Tests Pypi version Pypi downloads Discord invite Open In Colab

Logo - declarai.png


Documentation 📖: https://declarai.com

Source Code 💻 : https://github.com/vendi-ai/declarai


What is Declarai 🤔?

Declarai turns your Python code into LLM tasks, allowing you to easily integrate LLM into your existing codebase. It operates on a simple principle: just define a Python function/class. By annotating this function with docstrings and type hints, you provide a clear instruction set for the AI model without any additional effort.

Once you've declared your function, Declarai intelligently compiles the function's docstrings and type hints into a prompt for the AI model, ensuring the model understands exactly what's required.

After executing the task, Declarai retrieves the AI's response and parses it, translating it back into the declared return type of your Python function. This eliminates any manual parsing or post-processing on your part.

Declarai Keeps It Native: At its core, Declarai is about embracing native Python practices. You don't need to learn a new syntax or adapt to a different coding paradigm. Just write Python functions as you always have, and let Declarai handle the AI integration seamlessly.


Main Components 🧩

Tasks 💡

AI tasks are used for any business logic or transformation.

import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.task 
def rank_by_severity(message: str) -> int:
    """
    Rank the severity of the provided message by it's urgency.
    Urgency is ranked on a scale of 1-5, with 5 being the most urgent.
    :param message: The message to rank
    :return: The urgency of the message
    """


rank_by_severity(message="The server is down!")

>>> 5

rank_by_severity(message="How was your weekend?"))

>>> 1

Chat 🗣

AI Chats are used for an iterative conversation with the AI model, where the AI model can remember previous messages and context.

import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.experimental.chat
class SQLBot:
    """
    You are a sql assistant. You help with SQL related questions 
    """


sql_bot = SQLBot()
sql_bot.send("When should I use a LEFT JOIN?")

> "You should use a LEFT JOIN when you want to return all rows from ....

Features:

  • 🖍 Intelligent Prompts: Automatically generate prompts using type hints and docstrings.
  • 🚄 Conversational AI: Chat interface equipped with memory and context management.
  • ⚡ Real-time streaming: Stream LLM responses that take longer to complete.
  • 🔥 Pydantic Model Parsing: Seamlessly parse llm responses into Pydantic models.
  • 🐍 Pythonic: Native understanding and parsing of llm responses into Python primitives.
  • 💾 Multiple AI Backends: Integrated with OpenAI & Azure AI llm providers.
  • 🛠 Middleware: Adapt and extend tasks behavior with a modular middleware system.
  • 🤗 Coming Soon: Integration with HuggingFace hub

Quickstart 🚀

Installation

pip install declarai

Setup

export OPENAI_API_KEY=<your openai token>

or pass the token when initializing the declarai object

import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo", openai_token="<your-openai-key>")

💡 Basic Usage

Craft AI-powered functionalities with ease using the @task decorator. Just add some type hints and a bit of documentation, and watch Declarai do its magic!

import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.task
def generate_poem(title: str) -> str:
    """
    Write a 4 line poem on the provided title
    """


res = generate_poem(
    title="Declarai, the declarative AI framework for LLMs"
)
print(res)
# Declarai, the AI framework,
# Empowers LLMs with declarative power,
# Efficiently transforming data and knowledge,
# Unlocking insights in every hour.

Not the best poem out there, but hey! You've written your first declarative AI code!

Declarai aims to promote clean and readable code by enforcing the use of doc-strings and typing. The resulting code is readable and easily maintainable.

Tasks with python native output parsing

Python primitives

import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.task
def rank_by_severity(message: str) -> int:
    """
    Rank the severity of the provided message by it's urgency.
    Urgency is ranked on a scale of 1-5, with 5 being the most urgent.
    :param message: The message to rank
    :return: The urgency of the message
    """


rank_by_severity(message="The server is down!")

>>> 5
rank_by_severity(message="How was your weekend?"))

>>> 1

Python Lists/Dicts etc..

from typing import List
import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.task
def multi_value_extraction(text: str) -> List[str]:
    """
    Extract the phone numbers from the provided text
    :param text: content to extract phone number from
    :return: The phone numbers that where identified in the input text
    """


multi_value_extraction(
    text="Hey jenny,\nyou can call me at 124-3435-132.\n"
         "you can also reach me at +43-938-243-223"
)
>>> ['124-3435-132', '+43-938-243-223']

Python complex objects

from datetime import datetime
import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.task
def datetime_parser(raw_date: str) -> datetime:
    """
    Parse the input into a valid datetime string of the format YYYY-mm-ddThh:mm:ss
    :param raw_date: The provided raw date
    :return: The parsed datetime output
    """


datetime_parser(raw_date="Janury 1st 2020"))

>>> 2020-01-01 00:00:00

Pydantic models

from pydantic import BaseModel
from typing import List, Dict
import declarai


class Animal(BaseModel):
    name: str
    family: str
    leg_count: int

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.task
def suggest_animals(location: str) -> Dict[int, List[Animal]]:
    """
    Create a list of numbers from 0 to 5
    for each number, suggest a list of animals with that number of legs
    :param location: The location where the animals can be found
    :return: A list of animal leg count and for each count, the corresponding animals
    """


suggest_animals(location="jungle")

>>> {
       0: [
           Animal(name='snake', family='reptile', leg_count=0)
       ], 
       2: [
           Animal(name='monkey', family='mammal', leg_count=2), 
           Animal(name='parrot', family='bird', leg_count=2)
       ], 
       4: [
          Animal(name='tiger', family='mammal', leg_count=4), 
          Animal(name='elephant', family='mammal', leg_count=4)
       ]
 }

Jinja templates 🥷

import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.task
def sentiment_classification(string: str, examples: List[str, int]) -> int:
    """
    Classify the sentiment of the provided string, based on the provided examples.
    The sentiment is ranked on a scale of 1-5, with 5 being the most positive.
    {% for example in examples %}
    {{ example[0] }} // {{ example[1] }}
    {% endfor %}
    {{ string }} //
    """
    
sentiment_classification(string="I love this product but there are some annoying bugs",
                         examples=[["I love this product", 5], ["I hate this product", 1]])

>>> 4

Simple Chat interface

import declarai

gpt_35 = declarai.openai(model="gpt-3.5-turbo")

@gpt_35.experimental.chat
class CalculatorBot:
    """
    You a calculator bot,
    given a request, you will return the result of the calculation
    """

    def send(self, message: str) -> int: ...


calc_bot = CalculatorBot()
calc_bot.send(message="1 + 1")

>>> 2

📚 For a thorough introduction, features, and best practices, explore our official documentation and beginner's guide.

Contributing 💼

Join our mission to make declarative AI even better together! Check out our contributing guide to get started.

Metadata

Release files for declarai 0.1.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for declarai 0.1.13
File Size Uploaded Provenance
declarai-0.1.13.tar.gz 40.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for declarai 0.1.13
File Interpreter ABI Platform
declarai-0.1.13-py3-none-any.whl Python 3 none any Details

Total release size: 98.7 kB

Release files / declarai-0.1.13.tar.gz

Download URL declarai-0.1.13.tar.gz
Size 40.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e3dec986bcb9dde4a3180d55a08dae0176356894b9c63be62848d746072374b5
BLAKE2b-256 checksum
How to use checksums
44718a6fb8513036633d9d2b97f96d96f1533dd1de139637466b53a9ed82b049
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.0 Linux/6.2.0-1012-azure

Release files / declarai-0.1.13-py3-none-any.whl

Download URL declarai-0.1.13-py3-none-any.whl
Size 58.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b06b3c6db05f3a0a71460bc93adbba2a5a9199f1cd83573413ae035929c124c
BLAKE2b-256 checksum
How to use checksums
e97ffcf729242660ad4fbbe84629a84e9e039ffafabc155a5be2b0a265e19654
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.0 Linux/6.2.0-1012-azure

Security for declarai 0.1.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page