artificial intelligence – Sesame Disk https://sesamedisk.com Sun, 10 Sep 2023 12:34:19 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.3 https://sesamedisk.com/wp-content/uploads/2020/05/cropped-favicon-transparent-32x32.png artificial intelligence – Sesame Disk https://sesamedisk.com 32 32 Role of AI in Shaping Our Everyday Lives https://sesamedisk.com/role-of-ai-in-shaping-our-everyday-lives/ https://sesamedisk.com/role-of-ai-in-shaping-our-everyday-lives/#respond Sun, 10 Sep 2023 12:34:18 +0000 https://sesamedisk.com/?p=10241 Imagine a world where your morning alarm automatically adjusts based on your sleep cycle, your car predicts the least congested route to work, and a virtual assistant schedules your day down to the last detail—all before you’ve even had your first cup of coffee. This doesn’t sound like a scene straight out of a science fiction novel anymore, does it? Indeed, thanks to the role of Artificial Intelligence, or AI, a lot of it has already become real. Whether it’s personalized healthcare, smarter communication, or more efficient transportation, AI is integrating itself into the very fabric of our existence.

Role of AI in Daily Lives

Ever wondered why Artificial Intelligence—AI for those in the know—is skyrocketing in popularity? Is it the buzz of breakthroughs and innovations? While the jury may still be out on the ‘why,’ one thing’s crystal clear: AI isn’t some pie-in-the-sky dream of tomorrow—it’s shaping our world today!

In this article, we will uncover the numerous ways AI has already become an integral part of our daily existence, adding onto our many articles on the role of AI chatbots like ChatGPT, Colossal AI, and more, shaping industries, enhancing experiences, and changing the way we interact with technology.

The Pervasive Role of AI in Daily Life

One big reason for AI’s popularity is that it’s now a part of our everyday lives. You might not realize it, but AI is all around us, making things more convenient.

Role of AI in Changing the Way We Communicate

Take your smartphone, for example. AI algorithms work tirelessly to improve your typing speed through predictive text. These algorithms analyze your behavior and patterns, making educated guesses about what you might type next. They offer music and movie recommendations tailored to your taste, aggregating data from your past choices to suggest new content you’re likely to enjoy. Even when you’re behind the wheel, AI intervenes by calculating the quickest route for your journey, adjusting in real-time for traffic conditions and other variables.

Traditional GPS systems would get you from point A to point B, but modern AI-powered mapping services offer far more. These advanced systems analyze real-time traffic conditions, anticipate delays, and suggest alternative routes to optimize your journey. Beyond cars, the role of AI-driven mapping technologies also serve pedestrians, cyclists, and public transport users

Through these myriad applications, AI not only increases efficiency but also enriches our lives in a subtle, yet profound way.

AI in Communication: Chatbots, Virtual Assistants, Smart Home Devices

Yet, perhaps one of the most transformative effects of the role AI is in the realm of communication, particularly with the advent of chatbots like ChatGPT. These AI-driven virtual assistants are embedded in various websites and apps, streamlining your interactions by providing real-time answers to your queries, solving issues, and even assisting you with online purchases. The days of waiting on hold to speak to a human customer service representative are becoming increasingly obsolete. With chatbots, immediate and personalized help is just a click away, allowing you to focus on what matters most.

Virtual assistants like Siri and Alexa are another manifestation of AI’s impact on communication. The integration of AI in these virtual assistants makes them increasingly versatile and personalized tools that enhance everyday communication.

Another reason AI is popular is the rise of smart home devices. Devices like Amazon Echo and Google Home use AI to understand your voice commands. These commands help it set alarms, control lights, or play your favorite songs. These smart speakers have brought AI right into our living rooms. They only serve to make our lives easier and more entertaining.

AI in Communication: Chatbots, Virtual Assistants, Smart Home Devices

AI in Healthcare, Entertainment, Transportation, and Other Industries

Let’s explore the transformative impact of AI across various sectors, including healthcare, entertainment, and transportation, highlighting how it’s revolutionizing industry practices and consumer experiences.

Healthcare and Diagnosis

AI’s popularity isn’t limited to entertainment and convenience; it’s also saving lives. In the healthcare industry, AI helps doctors make faster and more accurate diagnoses. AI algorithms can analyze medical images like X-rays and MRIs. They can then detect abnormalities that might not have been seen by the human eye. This early detection can be crucial in treating diseases like cancer.

AI in Healthcare and Diagnosis

Entertainment

AI has also made a big splash in the world of entertainment. If you’ve ever used Netflix, you’ve experienced AI recommendations. Netflix’s AI sees what you’ve watched and suggests other shows and movies you might enjoy. It’s like having your own personal movie critic!

AI is also transforming the gaming industry. Video games now use AI to create dynamic and challenging opponents. These help in adapting to your skill level. Thus keeping the excitement levels high when you’re playing. AI ensures a fun gaming experience for players of all abilities.

Retail and Business

Businesses love AI because it boosts efficiency and saves money. Chatbots and virtual assistants handle customer inquiries 24/7. They help in freeing up human employees for more complex tasks. AI also helps with data analysis. It can help identify trends and make predictions that can guide business decisions.

On the customer-facing side, AI systems provide personalized shopping experiences by recommending products based on past purchases or browsing history, thus increasing sales and customer satisfaction.

Role of AI in Gaming and Creativity

AI isn’t just about numbers and calculations; it can also be surprisingly creative. For example, AI can generate art, compose music, and even write stories. AI-generated art has become a trendy topic, with artworks created by AI selling for millions of dollars. Musicians use AI to compose music, and writers employ AI to help generate content ideas and suggest improvements in their writing.

Role of AI in Accessibility

One of the best things about AI is how it’s making the world more accessible to people with disabilities. AI-powered apps can help the visually impaired. They can now navigate their surroundings, read text, and even describe objects. Speech recognition and text-to-speech technology is also useful. Allowing those with mobility issues to control their devices and communicate with more ease.

Predictions and Trends for the Future of the Role of AI

Future advancements may include more robust natural language processing capabilities, making interactions with virtual assistants and chatbots virtually indistinguishable from human conversation. In healthcare, we may see AI-powered remote monitoring systems that can detect anomalies in real-time, allowing for prompt medical intervention. In retail and other consumer-facing industries, AI might provide increasingly personalized services, with algorithms sophisticated enough to understand nuanced consumer behavior and preferences.

As the technology matures, we must also remain vigilant about ethical considerations like data privacy and job automation, ensuring that the benefits of AI are equitably distributed across society.

Ethical Considerations in AI Deployment

  • Data Privacy: Extensive data collection for AI personalization poses risks of breaches and misuse, calling for stringent privacy regulations.
  • Job Automation: AI’s capability to perform tasks traditionally done by humans raises concerns about job displacement and necessitates training programs.
  • AI Bias: Algorithms can inherit and perpetuate human biases, affecting decision-making in areas like law enforcement and hiring, requiring efforts for minimization.

Conclusion: The Enduring Impact of the Role of AI on our Daily Lives

Artificial Intelligence is not just a technological advancement; it’s a paradigm shift affecting almost every facet of our lives. From enhancing personal technology and streamlining communications to revolutionizing healthcare, transportation, and retail, the role of AI’s impact is both broad and profound. However, as we embrace this new era, ethical considerations such as data privacy, job automation, and AI bias must be rigorously addressed.

The integration of AI into our daily lives is inevitable and increasingly pervasive. As we look to the future, it offers tantalizing possibilities for convenience and efficiency, but it also poses questions that we, as a society, need to answer. Ensuring that the technology we create serves us—while respecting our ethical boundaries—will be one of the defining challenges of our time.

If you’d like to learn more about the future and role of AI, read our article on ChatGPT AI Productivity Features and LLaMA and ChatGPT: Two Major LLMs. Happy reading!

By: Syed Umar Bukhari.

]]>
https://sesamedisk.com/role-of-ai-in-shaping-our-everyday-lives/feed/ 0
LLaMA and ChatGPT: Exploring Two Major AI LLMs https://sesamedisk.com/llama-vs-chatgpt/ https://sesamedisk.com/llama-vs-chatgpt/#respond Thu, 31 Aug 2023 13:32:13 +0000 https://sesamedisk.com/?p=10254 Artificial Intelligence (AI) has entered a new era with a broad range of applications. One of the most crucial developments of this AI is the Large Language Model (LLM). Also, these models can generate human-like content. In recent weeks, Open AI’s ChatGPT and Meta’s LLaMA are the two most significant LLMs. 

Thus, this article will explore some common differences between LLaMA and ChatGPT (3.5, not 4 as we will compare free models) LLM models by comparing their architecture, prompt-generating capacity, and computational capability.

Llama 2 AI LLM models
Image Source: Meta

Brief Introduction of the LLMs

Meta and Microsoft’s AI model, LLaMA 2, is free for research since it’s an open-source model. Additionally, the pre-trained and fine-tuned LLM collection has a training dataset ranging from 7 billion to 70 billion! Hence, this is huge, increasing the size of the training parameters by 40% from its predecessor, LLaMA.

The major purpose of LLAMA is to:

  • Investigate the applications like natural language comprehension, answering questions, etc.
  • Develop methods and strategies for enhancing the language models.
  • Help in solving complex mathematical theorems and techniques.

ChatGPT is the OpenAI-developed language model, and we already have many posts, on ChatGPT API and ChatGPT Prompts, etc. Also, OpenAI trained it trained on massive data and its main features include:

  1. Conversational AI: ChatGPT is designed to engage in natural language conversations with users, responding to prompts in a human-like manner.
  2. Text Generation: It can generate coherent and contextually relevant text based on the input, making it useful for content creation, explanations, and recommendations.
  3. Versatility: ChatGPT’s versatility allows it to be applied across various applications, from customer support and education to creative writing and problem-solving.
  4. GPT Architecture: Powered by the GPT (Generative Pre-trained Transformer) architecture, more on this in the next section, ChatGPT learns language patterns from extensive text data to produce responses.
  5. Responsibility: While powerful, ChatGPT has limitations such as potential biases, inaccuracies, and the risk of generating inappropriate content, necessitating responsible usage and oversight. 

1. LLaMA and ChatGPT: Architecture of the LLMs

This is what the LLAMA Model architecture looks like, ranging from its model size (parameters) to model architecture and more.

LLaMA 2 architecture

Image Source: Official site of Meta

On the other hand, ChatGPT is based on transformer architecture and this architecture is based on the self-attention mechanism.

ChatGPT architecture

Image Source: Google

Then, let’s compare the computational resources of both.

2. LLaMA vs ChatGPT: Computational Resources

Regarding computational power, LLaMA uses less computational power than ChatGPT. Moreover, the major aim of the LLAMA model is to be less resource intensive and more efficient than ChatGPT. Besides, LLAMA is designed to be available and accessible to all organizations and researchers under a non-commercial license. Also, for free.

Thus, here is the LLAMA 2 Chat comparison with the other open and closed source models, including ChatGPT, by safety human evaluation.

LLAMA and ChatGPT compared

3. LLaMA vs ChatGPT: Application Chatbot

So, now let’s see compare how to access the chatbots for both LLMs in this next section

Using API token from Replicate to build LLaMA 2 Chat

  • Get the Replicate API token

First, you need to get your API token from your Replicate account. Replicate allows you to run models in the cloud at scale. Then, once you create an account, you can get it from the API token option. And you must create the token by clicking on the create token button as shown below:

Replicate API token

These tokens are required to access the LLaMA 2 model and use it for chatbot development. 

  • Set up the Coding Environment

Then set up your coding environment for cloud or local development. You can use Python or Jupyter Notebook, and any IDE of your choosing, if you want.

  • Build your LLaMA 2 Chatbot

Before writing the Python code to build the LLaMA chatbot, you need to install the replicate package. In Python, you can do it as:

pip install Replicate

After that, you can write this Python code:

from getpass import getpass

import os

REPLICATE_API_TOKEN = getpass()

os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN

Then, it will prompt you to enter your API token (which we created above).

Enter Replicate API token for LLM

Okay, once you enter the Replicate API token, you can use its methods to import the model you want to use and the input prompt.

import replicate

data=replicate.run(

 "replicate/llama-2-70b-chat:2c1608e18606fad2812020dc541930f2d0495ce32eee50074220b87300bc16e1",

  input={

"prompt": "Input Prompt"}

)

for item in data:

  print(item,end='')

Here, we have used the LLAMA 2 70b chat model. The model generates text based on the input and produces a series of items as output. Additionally, these items are then printed sequentially, devoid of newline breaks between them. Essentially, the code demonstrates the interaction with the specified model, showcasing its responses to the given input prompt.

In addition to this, you can also use Hugging Face, where you can find all the LLaMA 2 models.

Making of Chatbot using ChatGPT

You can use OpenAI’s API to create your chatbot, for which you need to have your API. To do so, check our blog post for creating a chatbot using OpenAI’s API here.

4. LLaMA vs ChatGPT: Prompt Response Comparison

For simplicity, you can also access LLaMA chat using Perplexity AI’s instance here.

  1. Visit LLaMA Perplexity: Access the website by navigating to llama.perplexity.ai in your web browser.
  2. Input your message: The website’s interface should have a text input field to type your message or query. Then, you can choose 3 models of llama-2, including 7b-chat, 13b-chat, and 70b-chat. 
  3. Generate a response: After that, click on a button or press enter to generate a response from LLaMA 2. Then, the model will process your input and generate a text-based response.
LLAMA chat on Perplexity AI

All set up? Great! So, we can now compare a series of prompts to compare the responses of LLaMA and ChatGPT.

Prompt 1: Generate a list of creative writing prompts for a short story collection

LLaMa Chat (llama-2-13b-chat) Response

AI prompt

Chatgpt-3.5 Response

ChatGPT prompt

Prompt 2: Generate a list of healthy meal ideas for a week-long meal plan

LLAMA 2-70B-chat Response

LLaMA diet plan prompt

Chatgpt-3.5 Response

ChatGPT diet plan prompt

You should know that portion control is key when it comes to diet, and you should aim for balanced meals, including lean proteins, complex carbohydrates, and healthy fats. Also, try to limit processed foods and added sugars. Additionally, his meal plan provides approximately 1500-1700 calories per day. Adjust according to your individual needs and activity level. Moreover, consult a doctor or registered dietitian for specific dietary requirements or concerns.

In summary, below is a table that compares the responses of LLaMA and ChatGPT based on certain aspects. So, let’s explore the differences.

Comparative Analysis of the Prompt Responses of the LLMs

AspectLlama ChatChatGPT
Creative Writing Prompts– Imaginative and emotionally resonant prompts.– Diverse and intriguing scenarios.
 – Covers various themes, from loss to magic.– Concise, yet sparks the imagination.
 – Formal and polished writing style.– Casual and conversational tone.
Healthy Meal Ideas– Well-structured week-long meal plan.– Comprehensive meal plan for the week.
 – Balanced meals and nutrients from different groups.– Emphasis on variety and balanced nutrients.
 – Professional tone and informative.– Friendly and relatable language.
Writing Style– Formal and polished.– Casual and conversational.
 – Precise and refined language.– Approachable and relatable.
Validity and Adaptation– Content generated based on patterns.– Content generated based on patterns.
 – Users should critically evaluate and adapt.– Users should critically evaluate and adapt.
Choice Considerations– Formal, refined style.– Friendly, relatable approach.
– Appropriate for those seeking sophistication.– Ideal for those preferring a relaxed tone.
– Volume of content may influence choice.– Extensive prompts might sway decisions.

From the above analysis, it’s clear.; the choice between LLaMA Chat and ChatGPT depends on your specific preferences and needs. Moreover, both models offer valuable capabilities, but which one is “better” depends on what you’re looking for.

  • Choose LLaMA Chat if:
    • You prefer a more formal and polished writing style.
    • You value emotionally resonant and thought-provoking prompts.
    • You’re seeking a more organized and structured approach to meal planning.
  • Choose ChatGPT if:
    • You enjoy a conversational and relatable writing style.
    • You’re looking for a diverse range of creative writing prompts.
    • You appreciate a comprehensive yet friendly meal plan emphasizing variety.

At this point, it’s a good moment to draw up a table to compare the two models based on their architectures, training dataset, purpose, performance, and much more. So, you can look at the full table below:

5. LLaMA vs ChatGPT: Full Comparison

NamesLLAMA 2ChatGPT
Types of ModelAn auto-regressive large language modelAuto-generative system
ArchitectureOptimized transformer architecture with multiple associative memory units interconnected through lateralized pathwaysTransformer architecture with decoder-only
Training datasetRanging from 7 billion to 70 billionTrained on a large corpus of text data
PurposeOptimized for dialogue use cases. So, it can assist with tasks such as composing emails, essays, and code and solving complex mathematical theorems and techniques.Respond to user input in a human-like manner, allowing for natural conversations with the virtual assistant. Hence, it can do any operation on demand and can be used for tasks such as composing emails, essays, and code, and solving math problems.
PerformanceOutperforms open-source chat models on most benchmarks tested.Enables users to refine and steer a conversation towards a desired length, format, style, level of detail, and language used, but has displayed a tendency to provide inaccurate information confidently.
AccessOpen-source and free for research and research work.Operated on a freemium model, allowing users on its free tier to access the GPT-3.5-based version.

Both models generate content based on patterns and therefore require the user to critically evaluate and adapt the information presented. However, neither can be wholly relied upon for entirely accurate or fact-based content.

Conclusion: LLaMA or ChatGPT?

The comparison between LLaMA and ChatGPT reveals an interesting landscape of Large Language Models and their capabilities. Furthermore, both models exhibit strengths and weaknesses that align with different user needs and preferences. But before we wrap this article, let’s compare the writing styles and content generation of both LLaMA and ChatGPT.

Writing Style

  • LLaMA: If you value a formal, polished writing style, LLaMA is your go-to model. Not only that, but it also produces precise and refined language that could be highly suitable for academic or corporate settings.
  • ChatGPT: For users looking for a more conversational and relatable writing style, ChatGPT could be a better fit. Also, its tone is generally more approachable, making it ideal for casual interactions and content creation.

Content Generation

  • LLaMA: Known for generating emotionally resonant and imaginative prompts, especially in creative writing. However, LLaMA might be more aligned with users seeking depth and emotional complexity in the content.
  • ChatGPT: ChatGPT excels in generating a wide variety of content that sparks the imagination. Hence, it is versatile and can generate content that is as informative as it is engaging.

In conclusion, the choice between LLaMA and ChatGPT boils down to your specific requirements and the context in which you plan to deploy these models. Whether you seek formality or relatability, structured content or a variety of ideas, both models offer robust options, making them leaders in the realm of AI-based language models.

If you liked this post, please let us know and drop a comment if you learned something new or face any problems with the code or instructions. Moreover, you can let us know your experience using either LLM model.

Also, similar articles to read: Colossal AI and ChatGPT Features to 6x Your Productivity.

Edited by: Syed Umar Bukhari.

]]>
https://sesamedisk.com/llama-vs-chatgpt/feed/ 0
Colossal AI: A Deep Dive into the Open-Source Chatbot Framework https://sesamedisk.com/colossal-ai-chatbot/ https://sesamedisk.com/colossal-ai-chatbot/#respond Tue, 27 Jun 2023 15:50:36 +0000 https://sesamedisk.com/?p=9917 In a digital revolution, chatbots have ascended the throne, reshaping the landscape of human-machine communication. Yet, it’s the unprecedented rise of revolutionary AI chatbots like ChatGPT, and the pioneering Colossal AI, that has ignited an insatiable global demand. Thus, they are now utilized everywhere — from customer support to sales and yes, even hotlines. The coolest part? With the advent of advanced natural language processing, building a chatbot has turned from Mission Impossible into a walk in the park. Now, that’s what we call an upgrade!

We have already discussed ChatGPT in detail before, so in this article, we’re going to dive right into the nitty-gritty of how to create a badass chatbot using the Colossal AI framework. No jargon, no fluff, just good ol’ practical know-how. So grab a cup of coffee, sit back, and get ready for a wild ride into the exhilarating world of chatbots.

What is the Colossal AI Framework for Chatbots?

Colossal AI Framework for building chatbots

As an innovative open-source platform, Colossal AI is redefining how engaging and adaptive conversational platforms are created, paving the way for interaction that feels incredibly natural and intuitive. But what does this mean for you, the developer? With Colossal AI at your fingertips, you’re offered a platform that is as flexible as it is scalable, leveraging advanced Natural Language Processing (NLP) techniques. This means more adaptability and less hassle, giving you the freedom to focus on crafting the perfect user experience.

But where Colossal AI truly shines is its status as a premier open-source solution for handling large AI models. It proudly wears the badge of being the first system to introduce a comprehensive end-to-end RLHF pipeline. RLHF is an acronym that stands for “Reinforcement Learning from Human Feedback” and specifically focuses on adding human feedback into the learning process. The RLHF pipeline includes supervised data collection, fine-tuning, and reinforcement learning fine-tuning. These exciting features build upon the LLaMA pre-trained model and signify a breakthrough in AI training and learning!

Colossal AI’s architecture consists of several components that work together to process user inputs, generate contextual responses, and manage the chatbot’s state. These components include Input Processing, Language Model, Context Management, and Response Generation.

Additionally, Colossal AI also presents Colossal Chat, aiming to mirror ChatGPT‘s technical approach.

Why Use Colossal AI for Chatbots

Here are a few advantages of using the Colossal AI for chatbots:

  • Scalability: Colossal AI can handle large-scale deployments. It can scale to allow larger loads without degrading performance or response times.
  • Flexibility: The framework supports various NLP models and allows developers to customize their chatbots according to their needs.
  • Extensibility: Colossal AI offers a modular design. It enables developers to add or replace components as needed to improve the overall functionality of chatbots.
  • Open-Source: As an open-source project, Colossal AI benefits from a global community of developers. They contribute to its continuous improvement and expansion.

This image below from their GitHub repo highlights the importance of GPU RAM & Throughput:

Colossal AI scaling ViT with GPU RAM and Throughput

How to Install and Set Up Colossal AI Locally?

Follow these steps to install and set up Colossal AI locally:

Prerequisite: Ensure you have Python 3.6 or higher installed on your system.

Assuming you already have Python 3.6 or higher, we can begin the local installation of Colossal.

  • Firstly, install the required package using pip using this command:
pip install colossalai
  • Then, create a new Python script and import these necessary modules to build an AI chatbot. Copy the lines below to import these packages to your script:
import colossalai
from colossalai import Chatbot
  • Configure the AI chatbot by specifying the desired NLP model and other settings as follows. Make sure to tune as necessary. For example, you could use another model instead of gpt-2. And the path depends on where you store that model.
config = {
    'model': 'gpt-2',
    'tokenizer': 'gpt-2',
    'model_path': 'path/to/pretrained/model',
    'max_context_length': 100
}
  • After that, instantiate the chatbot with the config using this command:
chatbot = Chatbot(config)
  • Now, use the chatbot to process user inputs and generate responses, like below:
user_input = "What is the weather like today?"
response = chatbot.generate_response(user_input)
print(response)

This is just a simple example. There’s so much more you can do to test. So, get your creative juices flowing. To further customize your chatbot, explore the Colossal AI documentation.

Building a Chatbot with Colossal AI Framework

Let’s focus on the required steps for building a chatbot using the Colossal AI framework. For the purposes of this article, we will build a news chatbot.

Requirements for Colossal AI Chatbot

The necessary requirements for the Colossal AI chatbot setup include:

  • PyTorch version 1.11 or higher (with support for PyTorch 2.x in development)
  • Python version 3.7 or higher
  • CUDA version 11.0 or higher
  • An NVIDIA GPU with a compute capability of 7.0 or higher, such as V100 or RTX20 series
  • Python libraries, including Beautiful Soup, Requests, NLTK

Designing and Deploying an AI Chatbot

Decide on the platform where you want to deploy your chatbot, such as a website, messaging app, or social media platform. Also, consider the target audience and the platform’s requirements when designing the interface.

Gather Training Data For News Chatbot

A crucial step in building an AI chatbot is the collection of data for training purposes using APIs or web scraping tools. For a news chatbot, you may gather data from news websites, RSS feeds, or other relevant sources. This step requires web scraping libraries in Python, like Beautiful Soup and Requests.

To install Beautiful Soup, enter the following command:

pip install beautifulsoup4

After that, to install the Requests library, use the following command:

pip install requests

These commands will download and install the Beautiful Soup and Requests libraries for you to use in your Python projects.

import requests
from bs4 import BeautifulSoup


url = "https://www.bbc.com/news"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')


# BBC News uses <h3> tags with the class 'gs-c-promo-heading__title' for headlines
headlines = soup.find_all('h3', class_='gs-c-promo-heading__title')


for headline in headlines:
    print(headline.text.strip())

The code above will print the headlines from BBC News. Here is the output:

web scraping output for designing chatbot from Colossal AI

Data Preprocessing: Clean, Transform, and Prepare Data for Training of Chatbot

Once we have fetched the data after scraping BBC News, we must clean and preprocess the collected data to prepare it for training. You can do this by tokenizing the text, removing stop words, and performing necessary preprocessing steps like data normalization and data integration. This step uses the NLTK library in Python.

pip install nltk

This command will download and install the NLTK library. NLTK is a popular library for natural language processing (NLP) tasks and provides a wide range of functionalities and resources for text analysis and NLP research. After installation, you can use the code below to perform data preprocessing.

import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize


# Download required NLTK resources
nltk.download("punkt")
nltk.download("stopwords")


# Define stop words
stop_words = set(stopwords.words("english"))


# Define the preprocessing function
def preprocess_text(text):
    tokens = word_tokenize(text)
    tokens = [token.lower() for token in tokens if token.isalnum()]
    tokens = [token for token in tokens if token not in stop_words]
    return " ".join(tokens)


# Scrape headlines from BBC News
url = "https://www.bbc.com/news"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
headlines = soup.find_all('h3', class_='gs-c-promo-heading__title')


# Preprocess the headlines
preprocessed_data = [preprocess_text(headline.text) for headline in headlines]


# Print the preprocessed headlines
for preprocessed_headline in preprocessed_data:
    print(preprocessed_headline)

Here is the output:

data preprocess using NLTK library in Python for AI chatbot

Training and Optimizing the AI Model For News Chatbot

Great! Once the data has been cleaned and preprocessed, it’s time to train and optimize the Colossal AI model for the news chatbot. The code below helps you fine-tune your AI model on the training data.

from colossalai import FineTuner

config = {
    "model": "gpt-2",
    "tokenizer": "gpt-2",
    "model_path": "Insert the model path here",
    "max_context_length": 100,
}

fine_tuner = FineTuner(config)
fine_tuner.train(preprocessed_data, epochs=5, batch_size=32, learning_rate=1e-4)
fine_tuner.save_model("Model path here")

This code uses the Colossal AI library to fine-tune a GPT-2 model on preprocessed data. The configuration dictionary specifies the model, tokenizer, and pre-trained model path. Along with this it also specifies the maximum input sequence length. Then, it’s trained on the preprocessed data for 5 epochs with a batch size of 32. Once the training is complete, the fine-tuned model is saved to a specified path.

Deploying the Colossal AI Chatbot using Flask

Finally, we can integrate the chatbot with the chosen platform using APIs, or other relevant methods. It’s essential to ensure proper authentication, and data privacy measures are in place to protect user information. We must also monitor the chatbot’s performance, and gather user feedback to improve its performance. And this allows us to make necessary updates to the model and interface to improve the chatbot’s accuracy and user experiences.

Use this command to install Flask in Python:

pip install Flask

After that, let’s deploy the AI based news chatbot using Flask to create a web app.

from flask import Flask, request, jsonify
from colossalai import Chatbot


app = Flask(__name__)
chatbot = Chatbot(config)


@app.route("/chatbot", methods=["POST"])
def handle_request():
    user_input = request.json["message"]
    response = chatbot.generate_response(user_input)
    return jsonify({"response": response})


if __name__ == "__main__":
    app.run()

The code creates a web application using the Flask framework. It serves as an interface for a chatbot powered by the Colossal AI library. Basically, it sets up a route named “/chatbot” that accepts POST requests for users to interact with the chatbot.

When the server receives a message, it processes the user’s input and generates a response from the chatbot. As a result, it returns the response as a JSON object. The application listens for incoming requests and handles them. As such, it provides a simple way for users to communicate with the AI chatbot.

And there you have it! We have successfully implemented a news chatbot using the Colossal AI framework.

Note: Please consult the official documentation and relevant platform APIs for more detailed information.

Conclusion: The Scope of Colossal AI

Colossal AI is a powerful open-source chatbot framework. It offers a wide range of features and such platforms will contribute to the growth of AI. Also, it continues to empower users to push the boundaries of what is possible in the realm of AI.

To recap, initially, in this article we carried out the data collection and the preprocessing process. For this, we implemented web scraping, where we used one of the popular Python libraries, Beautiful Soup. Then, we installed the Colossal AI library and imported the chatbot library. After that, we trained and optimized the model using a fine tuner. Finally, we used the Flask framework for deploying the AI chatbot on a web app. 

Similar articles: ChatGPT API: Automate Content Generation with Python and ChatGPT AI: Features to 6X Your Productivity in 2023.

Please share your thoughts in the comments and if you have any relevant queries.

Edited by: Syed Umar Bukhari.

]]>
https://sesamedisk.com/colossal-ai-chatbot/feed/ 0
Mojo Programming for AI: Better than Python? https://sesamedisk.com/mojo-programming-for-ai-better-than-python/ https://sesamedisk.com/mojo-programming-for-ai-better-than-python/#respond Sat, 24 Jun 2023 16:48:42 +0000 https://sesamedisk.com/?p=10077 If you’re passionate about staying at the cutting edge of AI innovation, you’re in the right place! As we lunge into the future, being at the forefront of AI technology is no longer just an advantage—it’s a necessity. This is where Mojo programming comes into the fray.

Now, most of us rightfully associate AI / ML programming with Python. So where does Mojo programming language come in? That’s what this follow-up article on Mojo aims to find out after we explored the fundamentals of the Mojo language in our previous article.

Mojo Programming for AI: Better than Python?

Why Choose Mojo Programming Language?

You wouldn’t be silly to ask this question when Mojo programming language hasn’t even publically released yet. In fact, it’s a smart question. But do you know what it means? Opportunity. And do you know who is behind this new language? Chris Lattner, the creator of Swift programming language. We know how that turned out. And he’s also the co-founder of LLVM, Clang compiler, and MLIR compiler. So, this is the best time to fine-tune your skills to specialize in a language most people will not know about until later.

And choosing Mojo programming language for AI development carries significant benefits. Modular’s powerful AI engine, Mojo’s ability to support existing Python code and infrastructure, and its high performance make it a compelling choice for developers. That’s not all! Mojo language also aims to be a complete superset of Python before release.

But is the hype real, especially when it comes to its performance?

Mojo Programming Language, Analyzed

While Python has been the go-to language for various tasks over the years, Mojo’s features are specifically tailored for AI and systems development. Remember that Mojo programming language aims to deal with the pain points.

For instance, it offers in-built support for concurrent and distributed computing and parametric metaprogramming—fundamental attributes for today’s data-intensive AI applications. This makes Mojo a strong contender compared to Python in AI development.

And yet… how exactly does it compare to Python? Is Mojo is better than Python? Let’s take a deeper look.

Python vs Mojo: A Comprehensive Comparison

What’s really exciting is that the Mojo playground allows you to use the default Python interpreter for the Python code versions of any program. It will make more sense once I show you how.

All you have to do is add this to the top of your Jupyter notebook cell in the Mojo programming playground to indicate that the cell contains Python code.:

%%python

Then, you can write any Python code, and it will run as if on native Python.

For instance, this compiles in the Mojo playground even though it doesn’t natively support lists or list functions. It works because it executes using the Python interpreter.

%%python
data = [1, 2, 3, 4, 5]

# Calculate the mean (average) of the numbers
mean = sum(data) / len(data)

# Print the mean
print(mean)

#output: 3.0

Note that the variables, functions, and imports defined in a Python cell are available for access in future Mojo cells. This lets you write and execute normal Python code within the Mojo programming playground. And you can neatly integrate it with native Mojo language code too. How futuristic is that?

That said, it’s time to explore the key differences between Python and Mojo programming language.

Basic Data Types: How are Integer and String Different in Mojo Programming Language

If you have been paying attention, you must have noticed that Mojo uses Int (with strong type checking), with a capital I, compared to int in Python, with a lowercase i. This is on purpose.

Mojo’s Int type aims to be simple, fast, and perform better. On the other hand, int in Python is dynamically allocated space and doesn’t have a fixed limit on its size. Mojo’s Int is a fixed 32-bit signed integer, while Python’s int is an arbitrary precision integer. Of course, a fixed-size type also presents problems like overflow and underflow, which are not present in Python.

Mojo fixes the slower performance of Python’s data types by utilizing stronger type checking, like in C++ and Swift (Lattner, remember?). Thus, Mojo’s Int will likely have more consistent and faster performance compared to Python’s int. Especially when it comes to larger data sets or more complex calculations.

Here’s an example highlighting the difference in the variable declaration for integer type.

#mojo encourages strong type checking but allows implicit variable data types too
var number: Int = 10
#this also works:
var number = 10

#python does not support explicit or strong type checking
number = 10

In Python, the variable dynamically takes on the type for the value of 10. And the string data type is also different. In Mojo programming, the String type is imported from the String module. Instead of using the str() method for type conversion as in Python, Mojo uses the String() constructor. Another key difference is that Mojo only supports commas to separate or concatenate strings and variables.

To work with strings, you must add this line to your code:

from String import String

#Strings in Mojo langauge, including the import, String constructor, and ',' for concat

from String import String
let name: String = "Umar"
let age: Int = 99
print("Hello, my name is ", name, "and I'm ", String(age), "years old.")

# Strings in Python using built-in str data type, str() method for type conversion, and supports '+' or ',' for concat

name = "UmarB"
age = 92
print("Hello, my name is " + name + " and I'm " + str(age) + " years old.")  # Output: Hello, my name is Alice and I'm 25 years old.

The outputs of both code snippets are similar, as shown below, except for better spacing in the Python version.

data types, integer, string in mojo programmng language

Beyond data types, the differences also extend to function declaration.

Comparing ‘fn’ and ‘def’ Functions in Mojo Programming: Unveiling Control, Mutability, and Scoping Differences

Both fn and def keywords can be used to define functions in Mojo, are interchangeable at the interface level, and have parameters and return values. They can also raise exceptions using the raises function effect.

All values passed in Mojo functions use value semantics by default, unlike Python. Mojo functions, by default, receive a copy of arguments and can change them inside the function scope with no visible change outside.

Here is a list of comparison between fn and def:

  • fn provides a more controlled and restricted environment inside its body compared to def.
  • In a fn declaration, argument values default to being immutable and require explicit type specifications, similar to let declarations, while in def they default to being mutable and argument declaration doesn’t have to be explicit.
  • The inout modifier in fn indicates that the parameter can be mutated, and the changes will be reflected outside the function, like in Python. def functions do not support the inout parameter modifier, while fn functions allow specifying inout parameters.
  • fn functions require explicit type annotations for parameters and the return type. If the return type is not explicitly declared, it is interpreted as returning None. In contrast, def functions can have implicit return types, where if a return statement is missing, it will implicitly return None.
  • fn functions support the owned parameter modifier, which indicates ownership transfer, allowing explicit control over memory management, while def functions do not have an equivalent modifier.
from String import String

def greet(name):
    print("Hello,")
    print(name)

fn farewell(name: String):
    print("Goodbye, ", name)

# Both can be called with the same syntax
greet("Alice")
farewell("Bob")

def add(x, y):
    return x + y

fn subtract(x: Int, y: Int) -> Int:
    return x - y

print(add(1,2))
print("Subtract: ", subtract(5,2))

Here is the output of the code above:

Comparing 'fn' and 'def' Functions: Unveiling Control, Mutability, and Scoping Differences

To recap, `def` is defined by necessity to be very dynamic, flexible and generally compatible with Python: arguments are mutable, local variables are implicitly declared on first use, and scoping isn’t enforced. This is great for high level programming and scripting, but is not always great for systems programming. To complement this, Mojo provides an `fn` declaration which is like a “strict mode” for `def`.

Note:

It’s not possible to use print(“Hello, “, name) inside the def function. This is the error you get: no matching function in call to ‘print’:

Struct vs. Class: Contrasting Data Containers in Mojo and Object-Oriented Classes in Python

The Mojo programming language does not support classes yet. Currently, it only supports structures. Structs in Mojo programming, similar to classes in Python language, can have member variables and methods. They are also static and inline their data within their container. On the contrary, classes in Python have a more dynamic nature and provide extensive support for OOP concepts.

Here is a comprehensive comparison between structs and classes:

  • Structs provide a static and compile-time bound approach, unlike the dynamic nature of classes in Python.
  • Structs in Mojo are inlined into their container instead of being implicitly indirect and reference counted. Structs primarily focus on data representation rather than behavior. Classes, on the other hand, allow for inheritance and polymorphism, facilitating code reuse and extensibility.
  • Structs offer indirection-free field access, allowing direct access to fields without method calls. But classes can have methods that encapsulate behavior and can be called on instances of the class.
  • Structs are often used for performance-critical scenarios and low-level systems programming. However, classes commonly building complex software systems, model real-world entities, and organize code in a modular way.
#creating a Person struct with functions to greet

from String import String
@value
struct Person:
    var name: String
    var age: Int

    fn greet(self):
        print("Hello, my name is ", self.name, "and I'm ", String(self.age), "years old.")

# Creating an instance of the Person struct
person = Person("Umar", 22)
person.greet()


#creating a class and methods in Python for greeting a person

%%python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print("Hello, my name is " + self.name + " and I'm " + str(self.age) + " years old.")

# Creating an instance of the Person class
person = Person("Bukari", 30)
person.greet()

Note that as I pointed out in the last article, a complete guide to Mojo language, using the @value decorator allows you to skip boilerplate code such as the init method. As shown in the code above, the class uses an init method. However, in the struct in Mojo uses the decorator to create the boilerplate methods implicitly.

This is the code output in Mojo programming:

Comparing structs and classes in mojo language

Importing Python Libraries in Mojo Programming: Leveraging Python’s Existing Rich Ecosystem

The cool thing about Mojo programming language is that you can use Python’s existing rich and versatile ecosystem of libraries! Don’t believe me? Give me a moment, and I will show you.

At the top of your Mojo programming code, add this line:

from PythonInterface import Python

After that, you can use the import_module function to add any Python library to your Mojo program.

from PythonInterface import Python
# This is equivalent to Python's `import numpy as np`
let np = Python.import_module("numpy")


# Now use numpy as if writing in Python
array = np.array([1, 2, 3])
print(array)
import python libraries and using numpy to create a vector ndarray

Just like that, you can use a variety of other Python libraries already!

And now, let’s discuss the fabled high performance of Mojo. Is it really so fast?

Performance Comparison: Mojo vs Python language

The CEO of Modular, Chris Lattner claims Mojo is 35 THOUSAND times faster than Python. Yes, you read that right. Can you even imagine that? Note that this metric is is for Mojo-optimized code only.

So, for now, we will test a matrix multiplication Python function and then convert the code to Mojo language. After that, we will compare the benchmark for performance simply by using the same Python implementation in Mojo code.

Here is the Python implementation of matrix multiplication on the Mojo playground:

%%python
import numpy as np
from timeit import timeit

class Matrix:
    def __init__(self, value, rows, cols):
        self.value = value
        self.rows = rows
        self.cols = cols
        
    def __getitem__(self, idxs):
        return self.value[idxs[0]][idxs[1]]
    
    def __setitem__(self, idxs, value):
        self.value[idxs[0]][idxs[1]] = value

def benchmark_matmul_python(M, N, K):
    A = Matrix(list(np.random.rand(M, K)), M, K)
    B = Matrix(list(np.random.rand(K, N)), K, N)
    C = Matrix(list(np.zeros((M, N))), M, N)
    secs = timeit(lambda: matmul_python(C, A, B), number=2)/2
    gflops = ((2*M*N*K)/secs) / 1e9
    print(gflops, "GFLOP/s")
    return gflops

def matmul_python(C, A, B):
    for m in range(C.rows):
        for k in range(A.cols):
            for n in range(C.cols):
                C[m, n] += A[m, k] * B[k, n]

After that, run this command in the next cell:

python_gflops = benchmark_matmul_python(128, 128, 128).to_float64()

This is the output on my machine (MacBook M1):

Python code run in Mojo playground shows that it is run at 0.002 GFLOP/s

Next, let’s modify this Python code for a Mojo program implementation:

#|code-fold: true
#|code-summary: "Import utilities and define `Matrix` (click to show/hide)"

from Benchmark import Benchmark
from DType import DType
from Intrinsics import strided_load
from List import VariadicList
from Math import div_ceil, min
from Memory import memset_zero
from Object import object, Attr
from Pointer import DTypePointer
from Random import rand, random_float64
from TargetInfo import dtype_sizeof, dtype_simd_width

# This exactly the same Python implementation, 
# but is infact Mojo code!
def matmul_untyped(C, A, B):
    for m in range(C.rows):
        for k in range(A.cols):
            for n in range(C.cols):
                C[m, n] += A[m, k] * B[k, n]
fn matrix_getitem(self: object, i: object) raises -> object:
    return self.value[i]


fn matrix_setitem(self: object, i: object, value: object) raises -> object:
    self.value[i] = value
    return None


fn matrix_append(self: object, value: object) raises -> object:
    self.value.append(value)
    return None


fn matrix_init(rows: Int, cols: Int) raises -> object:
    let value = object([])
    return object(
        Attr("value", value), Attr("__getitem__", matrix_getitem), Attr("__setitem__", matrix_setitem), 
        Attr("rows", rows), Attr("cols", cols), Attr("append", matrix_append),
    )

def benchmark_matmul_untyped(M: Int, N: Int, K: Int, python_gflops: Float64):
    C = matrix_init(M, N)
    A = matrix_init(M, K)
    B = matrix_init(K, N)
    for i in range(M):
        c_row = object([])
        b_row = object([])
        a_row = object([])
        for j in range(N):
            c_row.append(0.0)
            b_row.append(random_float64(-5, 5))
            a_row.append(random_float64(-5, 5))
        C.append(c_row)
        B.append(b_row)
        A.append(a_row)

    @parameter
    fn test_fn():
        try:
            _ = matmul_untyped(C, A, B)
        except:
            pass

    let secs = Float64(Benchmark().run[test_fn]()) / 1_000_000_000
    _ = (A, B, C)
    let gflops = ((2*M*N*K)/secs) / 1e9
    let speedup : Float64 = gflops / python_gflops
    print(gflops, "GFLOP/s, a", speedup.value, "x speedup over Python")
    

In the next cell, run this line:

benchmark_matmul_untyped(128, 128, 128, 0.0022926400430998525)

On my machine, this code runs nearly 5x times faster, with minimal optimization. We simply converted the Python code to Mojo’s native code:

Mojo code is 5x times faster than Python language as shown in the image and runs at less than 0.01 GFLOP/s for matrix multiplication

I also want to show you this Mojo code from the playground:

fully optimized Mojo code runs 14000X faster than Python code for matrix multiplication

The optimized Mojo code for matrix multiplication runs 14000x times faster. Even if it’s not the same on your machine, it is still unprecedented!

Aren’t you interested to learn more about Mojo programming’s role in AI development now?

Mojo’s Role in AI Development: The Need for Powerful Tools in a Competitive Future

Modular‘s AI engine natively supports dynamic shapes for AI workloads, based on a new technology called Shapeless, outpacing other statically shaped compilers. Shapeless allows Modular to represent and manipulate variable-length inputs without having to know their exact shapes in advance. The AI engine is also fully compatible with existing frameworks and servers, and it allows developers to write their own custom operators. As a result, Modular’s engine can deploy AI models in a variety of environments, including the cloud, on-premises, and edge devices.

Impressively, the Modular AI Engine exhibits speedups of 3x-9x versus the default TensorFlow on BERT, a highly optimized language model. While XLA necessitates a significantly longer compile time than TensorFlow, it does offer superior execution performance. Despite this, the Modular AI Engine continuously outperforms, delivering a 2x-4x faster and superior performance than XLA.

Comparing Mojo’s AI Engine to Python Engines:

  • Mojo’s AI engine is a good choice if you need to build a model with dynamic shapes. Python engines can also build models with dynamic shapes, but they may require more code and be less efficient.
  • If you need to deploy your model on various platforms, Mojo’s AI engine is a good choice. Python engines can also be deployed on a variety of platforms, but Mojo’s AI engine is designed to be more portable.
  • If you need to write custom operators for your model, Mojo’s AI engine is a good choice. Python engines can also write custom operators, but Mojo’s AI engine makes it easier.

Conclusion: The Future Scope of Mojo Programming for AI

When you choose to embark on the journey of Mojo programming for AI development, you’re not only adopting a programming language—you’re aligning with a philosophy that prioritizes performance, flexibility, and user-friendliness, as explored in our recent article: Mojo Language 101. As Mojo progresses to be a complete superset of Python before its public release, early adoption and specialization in Mojo programming could offer significant advantages in the future.

The emerging Mojo programming language’s tailored features for AI and systems development, along with compatibility with Python code and libraries, make it a potential game-changer. With distinct advantages over Python, including high performance, in-built support for concurrent and distributed computing, and superior handling of data types, Mojo positions itself as a powerful tool for AI development.

From matrix multiplication benchmarks that indicate Mojo to be up to five times faster than Python, to the staggering claim of fully optimized Mojo code running 14,000 times faster, the future of AI and systems development could be revolutionized. But Mojo’s proficiency is not limited to its speed; it also boasts superior compatibility and a more streamlined approach to handling dynamic shapes in AI workloads!

Please comment your thoughts on Mojo programming language and share with your friends if you found it helpful.

Related articles: Mojo Language Complete Guide and Classification of NSFW Images.

Written by: Syed Umar Bukhari.

]]>
https://sesamedisk.com/mojo-programming-for-ai-better-than-python/feed/ 0