Artificial Intelligence

Building 3 Fun AI Applications with ControlFlow

Building 3 Fun AI Applications with ControlFlow
Image by Author | Canva Pro

The AI industry is rapidly advancing towards creating solutions using large language models (LLMs) and maximizing the potential of AI models. Companies are seeking tools that seamlessly integrate AI into existing codebases without the hefty costs associated with hiring professionals and acquiring resources. This is where Controlflow comes into play. With ControlFlow, you can develop complex AI applications using just a few lines of code.

In this tutorial, we will explore ControlFlow and use it to build three exciting AI applications. The projects range from a simple text classifier to complex AI with multiple agents, tasks, and flow.

What is ControlFlow?

ControlFlow is a Python framework that provides a structured approach for defining LLM workflows. It consists of three main components for creating AI applications:

  • Tasks: These are the fundamental building blocks of AI workflows. They define discrete, well-defined objectives that need to be accomplished by one or more AI agents.
  • Agents: These are the intelligent, autonomous entities powering your AI workflows. You can define a model, provide it with custom instructions, and add various tools to create the agents.
  • Flows: Flows are responsible for running multiple AI workflows in a specified order. They offer a structured way to manage tasks, agents, tools, and shared contexts.

By using ControlFlow, you can seamlessly integrate AI capabilities into your Python applications, gain more control over AI workflows, and generate structured outputs rather than just text. It allows you to build complex workflows with ease and is incredibly user-friendly. The best part of using ControlFlow is that it enables you to observe the AI model’s decision-making process at every task.

In simple terms, ControlFlow has two significant uses: it orchestrates your LLM workflows and helps you generate structured outputs, giving you more control over AI.

Setting up ControlFlow

We can simply install ControlFlow by typing the following command in the terminal. It will automatically install all the dependencies. 

Generate the OpenAI API key and set it as an environment variable. 

Before using ControlFlow, ensure it is properly installed. Type the following command in the terminal to view all Python package versions associated with ControlFlow.

Output:

Creating an agent and running the task is quite simple in ControlFlow. In this example, we have created the Horror storytelling agent by providing it with custom instructions. Then, we will use it to run a simple task by providing it with a prompt. Ultimately, we are generating a short story.

Here is the result.

Building 3 Fun AI Applications with ControlFlowBuilding 3 Fun AI Applications with ControlFlow

If you encounter a RuntimeError or runtime issue while running the following code in Colab, please rerun the cell once again.

1. Tweet Classification

Tweet Classifier is a popular small project for students, and it usually takes them months to build a proper text classifier. By using ControlFlow, we can create a proper tweet classifier using a few lines of code.

  1. Create a list of 4 short tweets.
  2. Setup an agent with custom instructions using the GPT-4-mini model.
  1. Create a task to classify tweets as “hate” or “love”, with a prompt, result type, agents, and context. We will provide a list of tweets as context.
  2. Run the task and display the results. 

ControlFlow tasks have classified tweets and generated a list of labels.

Building 3 Fun AI Applications with ControlFlowBuilding 3 Fun AI Applications with ControlFlow

Let’s display them in a proper way using the color code (red for hate and green for love).

Two tweets are labeled “hate,” and two tweets are labeled “love.” This is quite accurate and gives output in a structured format that we can integrate into our existing applications.

Building 3 Fun AI Applications with ControlFlowBuilding 3 Fun AI Applications with ControlFlow

2. Book Recommender

Two years ago, I built a Goodreads application on Deepnote, which sparked my interest in creating a book recommendation application. 

In this project, we will define a data model for book recommendations using the Pydantic model. This model will ensure that each recommendation includes a title, author, publication year, and genre with the correct field types. 

The recommend_books function will use this Pydantic model to define the result type and generate a list of book recommendations based on a specified genre and number of books.

We will now generate 5 Science fiction books. 

The agent first generated the recommended books in text form, converted them into JSON format, and finally transformed them into the Pydentic model type, which contains all the data with the correct data type. 

Building 3 Fun AI Applications with ControlFlowBuilding 3 Fun AI Applications with ControlFlow

To convert the output into the JSON format, we only have to use the ‘.model_dump_json(indent=2)` function. 

We have an output in JSON format that can be easily integrated into a web application or any code base. 

3. Travel Agent

In this project, we are going to connect two tasks. One task will generate the destination based on user preferences, and then use this destination to create a detailed itinerary. To create this project, we will use multiple agents, multiple tasks, and combine them using a flow.

We will define two model classes for travel preferences and travel itinerary. One will be used for user input, and the other will be used as a structure for output.

Also, we will create a flow function that contains two tasks: one for generating the destination and another for planning the trip based on the user’s preferences.

The process will involve generating the destinations and then using the destination to plan a trip for “n” days.

When you are used to coding ControFlow workflows, you will know that it gets quite easy to create a new application and project with just a few lines of code.

We will create user preferences by providing the preferred activity, budget, travel duration, and preferred region. Then, we will use these user preferences to run the flow.

Building 3 Fun AI Applications with ControlFlowBuilding 3 Fun AI Applications with ControlFlow

We will now print out the destination and the daily itinerary.

Our destination is the Amalfi Coast in Italy, and we have a really good travel plan.

We can easily convert this project into a product and build a website that provides users with travel recommendations. 

If you are facing issues running the coder, please check out the ControlFlow Tutorial Colab notebook. 

Final Thoughts

The ControlFlow is still new, and you might encounter issues when running some of the examples available in the documentation. The good news is that there’s a dedicated team working to resolve these issues in real time, ensuring a smooth onboarding process. All you have to do is create an issue on the GitHub repository at Issues · PrefectHQ/ControlFlow. I faced some issues while running it on Colab, but they were resolved within two hours, thanks to Jeremiah Lowin.

I believe we should be working towards building AI solutions that provide business value, instead of solely focusing on improving model performance by a few percentage points. To realize the full potential of AI, we need tools like ControlFlow, LangChain, and other AI frameworks that can help us build complex AI applications with just a few lines of code.

Get Started on The Beginner’s Guide to Data Science!

The Beginner's Guide to Data ScienceThe Beginner's Guide to Data Science

Learn the mindset to become successful in data science projects

…using only minimal math and statistics, acquire your skill through short examples in Python

Discover how in my new Ebook:
The Beginner’s Guide to Data Science

It provides self-study tutorials with all working code in Python to turn you from a novice to an expert. It shows you how to find outliers, confirm the normality of data, find correlated features, handle skewness, check hypotheses, and much more…all to support you in creating a narrative from a dataset.

Kick-start your data science journey with hands-on exercises

See What’s Inside

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button