{"id":13750766,"url":"https://github.com/sticklight-io/declarai","last_synced_at":"2025-05-09T16:31:58.300Z","repository":{"id":183400356,"uuid":"670062991","full_name":"sticklight-io/declarai","owner":"sticklight-io","description":"A Pythonic integration for LLMs.","archived":false,"fork":false,"pushed_at":"2023-11-28T00:31:59.000Z","size":2186,"stargazers_count":87,"open_issues_count":20,"forks_count":14,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-11-09T18:47:42.863Z","etag":null,"topics":["abstraction","ai","gpt","gpt-3","gpt-4","llm","openai","python"],"latest_commit_sha":null,"homepage":"https://declarai.com","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sticklight-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-24T08:03:07.000Z","updated_at":"2024-11-01T14:41:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"681ee080-8106-4e3e-a267-b813ee2eba12","html_url":"https://github.com/sticklight-io/declarai","commit_stats":null,"previous_names":["vendi-ai/declarai"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sticklight-io%2Fdeclarai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sticklight-io%2Fdeclarai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sticklight-io%2Fdeclarai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sticklight-io%2Fdeclarai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sticklight-io","download_url":"https://codeload.github.com/sticklight-io/declarai/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224869045,"owners_count":17383308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abstraction","ai","gpt","gpt-3","gpt-4","llm","openai","python"],"created_at":"2024-08-03T08:00:49.442Z","updated_at":"2024-11-16T02:31:32.864Z","avatar_url":"https://github.com/sticklight-io.png","language":"Python","funding_links":[],"categories":["LLM Tools"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/vendi-ai/declarai\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/pyversions/declarai.svg\" alt=\"versions\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/vendi-ai/declarai\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/license/vendi-ai/declarai.svg\" alt=\"license\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/vendi-ai/declarai/actions/workflows/test.yaml\"\u003e\n    \u003cimg src=\"https://github.com/vendi-ai/declarai/actions/workflows/test.yaml/badge.svg\" alt=\"Tests\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://pypi.org/project/declarai/\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/v/declarai?color=%2334D058\u0026label=pypi%20package\" alt=\"Pypi version\"\u003e\n  \u003c/a\u003e\n    \u003ca href=\"https://pepy.tech/project/declarai\"\u003e\n    \u003cimg src=\"https://static.pepy.tech/badge/declarai/month\" alt=\"Pypi downloads\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://discord.gg/GrszSXNTDm\"\u003e\n    \u003cimg src=\"https://dcbadge.vercel.app/api/server/GrszSXNTDm?compact=true\u0026style=flat\" alt=\"Discord invite\"\u003e\n  \u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/vendi-ai/declarai/blob/main/examples/declarai_intro.ipynb\"\u003e\n    \u003cimg src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"assets/Logo-declarai.png\" alt=\"Logo - declarai.png\"\u003e\n\u003c/p\u003e\n\n---\n\n**Documentation 📖**: \u003ca href=\"https://vendi-ai.github.io/declarai\" target=\"_blank\"\u003ehttps://declarai.com \u003c/a\u003e\n\n**Source Code 💻** : \u003ca href=\"https://github.com/vendi-ai/declarai\" target=\"_blank\"\u003ehttps://github.com/vendi-ai/declarai \u003c/a\u003e\n\n---\n\n## What is Declarai 🤔?\n\n**Declarai** turns your Python code into LLM tasks, allowing you to easily integrate LLM into your existing codebase.\nIt operates on a simple principle: just define a Python function/class. \nBy annotating this function with docstrings and type hints, you provide a clear instruction set for the AI model without any additional effort.\n\nOnce 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.\n\nAfter 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.\n\n**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.\n\n---\n## Main Components 🧩\n\n### Tasks 💡\n\nAI tasks are used for any business logic or transformation.\n```python\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.task \ndef rank_by_severity(message: str) -\u003e int:\n    \"\"\"\n    Rank the severity of the provided message by it's urgency.\n    Urgency is ranked on a scale of 1-5, with 5 being the most urgent.\n    :param message: The message to rank\n    :return: The urgency of the message\n    \"\"\"\n\n\nrank_by_severity(message=\"The server is down!\")\n\n\u003e\u003e\u003e 5\n\nrank_by_severity(message=\"How was your weekend?\"))\n\n\u003e\u003e\u003e 1\n```\n### Chat 🗣\n\nAI Chats are used for an iterative conversation with the AI model, where the AI model can remember previous messages and context.\n\n```python\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.experimental.chat\nclass SQLBot:\n    \"\"\"\n    You are a sql assistant. You help with SQL related questions \n    \"\"\"\n\n\nsql_bot = SQLBot()\nsql_bot.send(\"When should I use a LEFT JOIN?\")\n\n\u003e \"You should use a LEFT JOIN when you want to return all rows from ....\n```\n\n \n### Features:\n\n- [x] 🖍 **Intelligent Prompts**: Automatically generate prompts using type hints and docstrings.\n- [x] 🚄 **Conversational AI**: Chat interface equipped with memory and context management.\n- [x] ⚡ **Real-time streaming**: Stream LLM responses that take longer to complete.\n- [x] 🔥 **Pydantic Model Parsing**: Seamlessly parse llm responses into[ Pydantic models](https://github.com/vendi-ai/declarai#pydantic-models).\n- [x] 🐍 **Pythonic**: Native understanding and parsing of llm responses into [Python primitives](https://github.com/vendi-ai/declarai#tasks-with-python-native-output-parsing).\n- [x] 💾 **Multiple AI Backends**: Integrated with OpenAI \u0026 Azure AI llm providers.\n- [x] 🛠 **Middleware**: Adapt and extend tasks behavior with a modular middleware system.\n- [ ] 🤗 **Coming Soon**: Integration with HuggingFace hub\n\n## Quickstart 🚀\n\n### Installation\n```bash\npip install declarai\n```\n\n### Setup\n```bash\nexport OPENAI_API_KEY=\u003cyour openai token\u003e\n```\nor pass the token when initializing the declarai object\n```python\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\", openai_token=\"\u003cyour-openai-key\u003e\")\n```\n\n## 💡 Basic Usage\nCraft 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!\n```python\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.task\ndef generate_poem(title: str) -\u003e str:\n    \"\"\"\n    Write a 4 line poem on the provided title\n    \"\"\"\n\n\nres = generate_poem(\n    title=\"Declarai, the declarative AI framework for LLMs\"\n)\nprint(res)\n# Declarai, the AI framework,\n# Empowers LLMs with declarative power,\n# Efficiently transforming data and knowledge,\n# Unlocking insights in every hour.\n```\nNot the best poem out there, but hey! You've written your first declarative AI code!\n\nDeclarai aims to promote clean and readable code by enforcing the use of doc-strings and typing.\nThe resulting code is readable and easily maintainable.\n\n\n### Tasks with python native output parsing\n\nPython primitives\n```python\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.task\ndef rank_by_severity(message: str) -\u003e int:\n    \"\"\"\n    Rank the severity of the provided message by it's urgency.\n    Urgency is ranked on a scale of 1-5, with 5 being the most urgent.\n    :param message: The message to rank\n    :return: The urgency of the message\n    \"\"\"\n\n\nrank_by_severity(message=\"The server is down!\")\n\n\u003e\u003e\u003e 5\nrank_by_severity(message=\"How was your weekend?\"))\n\n\u003e\u003e\u003e 1\n```\nPython Lists/Dicts etc..\n\n```python\nfrom typing import List\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.task\ndef multi_value_extraction(text: str) -\u003e List[str]:\n    \"\"\"\n    Extract the phone numbers from the provided text\n    :param text: content to extract phone number from\n    :return: The phone numbers that where identified in the input text\n    \"\"\"\n\n\nmulti_value_extraction(\n    text=\"Hey jenny,\\nyou can call me at 124-3435-132.\\n\"\n         \"you can also reach me at +43-938-243-223\"\n)\n\u003e\u003e\u003e ['124-3435-132', '+43-938-243-223']\n```\nPython complex objects\n```python\nfrom datetime import datetime\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.task\ndef datetime_parser(raw_date: str) -\u003e datetime:\n    \"\"\"\n    Parse the input into a valid datetime string of the format YYYY-mm-ddThh:mm:ss\n    :param raw_date: The provided raw date\n    :return: The parsed datetime output\n    \"\"\"\n\n\ndatetime_parser(raw_date=\"Janury 1st 2020\"))\n\n\u003e\u003e\u003e 2020-01-01 00:00:00\n```\n\n### Pydantic models\n```python\nfrom pydantic import BaseModel\nfrom typing import List, Dict\nimport declarai\n\n\nclass Animal(BaseModel):\n    name: str\n    family: str\n    leg_count: int\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.task\ndef suggest_animals(location: str) -\u003e Dict[int, List[Animal]]:\n    \"\"\"\n    Create a list of numbers from 0 to 5\n    for each number, suggest a list of animals with that number of legs\n    :param location: The location where the animals can be found\n    :return: A list of animal leg count and for each count, the corresponding animals\n    \"\"\"\n\n\nsuggest_animals(location=\"jungle\")\n\n\u003e\u003e\u003e {\n       0: [\n           Animal(name='snake', family='reptile', leg_count=0)\n       ], \n       2: [\n           Animal(name='monkey', family='mammal', leg_count=2), \n           Animal(name='parrot', family='bird', leg_count=2)\n       ], \n       4: [\n          Animal(name='tiger', family='mammal', leg_count=4), \n          Animal(name='elephant', family='mammal', leg_count=4)\n       ]\n }\n```\n### Jinja templates 🥷\n```python\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.task\ndef sentiment_classification(string: str, examples: List[str, int]) -\u003e int:\n    \"\"\"\n    Classify the sentiment of the provided string, based on the provided examples.\n    The sentiment is ranked on a scale of 1-5, with 5 being the most positive.\n    {% for example in examples %}\n    {{ example[0] }} // {{ example[1] }}\n    {% endfor %}\n    {{ string }} //\n    \"\"\"\n    \nsentiment_classification(string=\"I love this product but there are some annoying bugs\",\n                         examples=[[\"I love this product\", 5], [\"I hate this product\", 1]])\n\n\u003e\u003e\u003e 4\n```\n\n### Simple Chat interface\n\n```python\nimport declarai\n\ngpt_35 = declarai.openai(model=\"gpt-3.5-turbo\")\n\n@gpt_35.experimental.chat\nclass CalculatorBot:\n    \"\"\"\n    You a calculator bot,\n    given a request, you will return the result of the calculation\n    \"\"\"\n\n    def send(self, message: str) -\u003e int: ...\n\n\ncalc_bot = CalculatorBot()\ncalc_bot.send(message=\"1 + 1\")\n\n\u003e\u003e\u003e 2\n```\n\n\n📚 For a thorough introduction, features, and best practices, explore our [official documentation](https://vendi-ai.github.io/declarai/) and [beginner's guide](https://vendi-ai.github.io/declarai/beginners-guide/).\n\n## Contributing 💼\nJoin our mission to make declarative AI even better together! Check out our [contributing guide](https://vendi-ai.github.io/declarai/contribute/) to get started.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsticklight-io%2Fdeclarai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsticklight-io%2Fdeclarai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsticklight-io%2Fdeclarai/lists"}