Async AI Workflows with Graph Theory
Tags: python, llm, ai
Published: 2024-02-13 13:28:47

Intelli is a Python module that simplifies the orchestration of AI models by employing asynchronous programming and principles from graph theory. Designed for developers and AI engineers seeking to optimize their workflows and connecting different LLM, image or speech models to create complex tasks.

**Quick Start with Intelli**
To get started with Intelli, make sure you have Python 3.7 or newer. You can install Intelli using pip:
```bash
pip install intelli
```

**Applying Graph Theory in Intelli Workflows**


![intelli work flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmfwv12359j1fdnlzt9b.jpg)
When I am talk about using graph theory with Intelli, it is referring to the way tasks mapped to their dependencies. In this model, each AI task is a node in a graph, and dependencies between tasks are edges. 


This setup allows you to design workflows where the execution sequence reflects the interrelationships between tasks, facilitating both sequential and parallel processing. This approach is especially beneficial for managing tasks that need to wait multiple I/O operations, mapping between output of previous step to input of current tasks, ensuring that non-dependent tasks can proceed without delay, increasing overall efficiency. Furthermore, intelli promotes a modular approach to task design, enabling the easy reuse of components.

**Creating AI Workflows with Intelli**

Let’s say you're developing a content creation platform and need to automate tasks using AI models. With Intelli, you define agents for each task. The agent is based on text, image, vision or speech model. 

Here's a simplified view of setting up your components:

**Defining Agents**

```python
from intelli.flow.agents import Agent

# Define an agent for text generation
text_agent = Agent(
    agent_type="text",
    provider="openai",
    mission="write social media posts",
    model_params={"key": self.openai_api_key, "model": "gpt-3.5-turbo"}
)

# Define an agent for image processing
image_agent = Agent(
    agent_type="image",
    provider="stability",
    mission="generate description only",
    model_params={"key": self.stability_key}
)
```
**Defining Tasks**

You can define tasks and assign agents to them:


```python
from intelli.flow.tasks import Task
# there are other input types, like image, vision, speech
from intelli.flow.input import TextTaskInput

task1 = Task(
    TextTaskInput("Create a post about AI technologies"),
    text_agent,
    log=True
)

task2 = Task(
    TextTaskInput("Generate image description for AI technologies post"),
    image_agent,
    log=True
)
```


**Defining Flows**

After defining tasks, you can orchestrate them using Intelli’s flow management. The system handles task dependencies to ensure they execute efficiently and in the proper order. The task2 will use the output of task1 when generating the images.

```python
from intelli.flow.flow import Flow
import asyncio

async def main():
    flow = Flow(
        tasks={
            "task1": task1,
            "task2": task2
        },
        map_paths={
            "task1": ["task2"]
        },
        log=True
    )

    output = await flow.start()
    print("Final output:", output)

if __name__ == "__main__":
    asyncio.run(main())
```
you can build more complex flow by utilizing the flexible map feature, example:

```python
map_paths={"task1": ["task2", "task3"], "task3": ["task4"]},
```

Check full practical code example in the [wiki page](https://github.com/intelligentnode/Intelli/wiki/Flows).

### Conclusion

This quick guide introduces the basic steps to automate AI workflows with `intelli`. While this still in beta phase, the potential of automation with the graph design is huge.

### Rerefences
- [github repo](https://github.com/intelligentnode/Intelli).
- [pip project](https://pypi.org/project/intelli/).