Building an AI Chatbot with FalconAI, LangChain, and Chainlit

Introduction to Generative AI and Large Language Models

In recent years, generative AI and large language models (LLMs) have taken the world by storm. These powerful AI systems, trained on massive amounts of text data, are capable of understanding and generating human-like text, powering applications from chatbots and virtual assistants to content creation tools.

While early LLMs like GPT-3 were massive in scale with 175 billion parameters, they were also proprietary and only accessible via paid APIs. However, the last year has seen the rise of highly capable open source LLMs like Meta‘s LLaMA, Google‘s PaLM, and Anthropic‘s Claude. These democratize access to cutting-edge language AI and allow developers to build all kinds of generative AI apps without costly API fees.

One of the most exciting open source LLMs to emerge recently is FalconAI, developed by TII in the UAE. Let‘s take a closer look at FalconAI and how you can use it alongwith tools like LangChain and Chainlit to easily build your own AI chatbot.

What is FalconAI?

FalconAI is a family of open source large language models developed by the Technology Innovation Institute in Abu Dhabi. The flagship model is Falcon 40B, a massive autoregressive language model with 40 billion parameters. There is also a smaller 7B parameter version called Falcon 7B.

Some key facts about FalconAI:

  • Falcon 40B is currently the most powerful open source LLM available, outperforming models like GPT-J, OPT, and LLaMA on benchmarks.
  • The Falcon language models were trained on 1.5 trillion tokens of high-quality filtered text data.
  • Falcon uses Transformers and the GPT architecture, similar to GPT-3 and ChatGPT.
  • The models are licensed under Apache 2.0, allowing commercial use without restrictions.
  • Falcon 7B can run on a single high-end GPU while Falcon 40B requires parallelization across multiple GPUs.

In addition to the base models, Falcon also provides instruction-tuned versions (Falcon 40B-Instruct and 7B-Instruct) that are finetuned for following instructions and engaging in dialogue. This makes them ideal for building chatbots and conversational AI apps.

Compared to GPT-3 and other proprietary LLMs, Falcon provides similar capability but is completely open source. This gives developers full control and ownership of their generative AI without dependence on 3rd party APIs. It‘s a major step forward for democratizing language AI technology.

Introduction to Chainlit

While large language models like FalconAI provide the "brains" for generative AI apps, developers still need to build the surrounding software infrastructure to create complete applications. On the backend, this includes prompt engineering, data management, orchestration of multiple models, and integration with other systems. On the frontend, it requires building user interfaces for people to interact with the AI system.

Chainlit is an open source Python framework that makes it incredibly easy to build web UIs for AI apps, especially those powered by LLMs. It provides a simple and intuitive way to create interactive chatbot interfaces, multi-step "agents", and other AI-driven web apps.

Some of the key features and benefits of Chainlit:

  • A declarative API for specifying the UI using Python
  • Built-in components for chat, text in/out, images, files, etc.
  • Direct integration with LangChain for building LLM apps
  • Fast development with instant live reloading
  • Deployable as a web app, embedded in docs/blogs, or via API
  • Supports async streaming responses for better UX
  • Headless mode for ChatOps via Slack/Discord

Chainlit is often compared to Streamlit, another Python framework for building web apps. While Streamlit is great for general data apps, Chainlit is purpose-built for LLM and generative AI use cases. It has out-of-the-box support for chatbots, prompt templates, embedding search, agents, and more.

This focus allows developers to build AI apps much faster than general purpose frameworks. Chainlit also provides a better user experience for chatbots and agents with features like streaming responses, chat history, and dynamic interfaces based on the output of language models.

Building a Chatbot with FalconAI, LangChain, and Chainlit

Now that we‘ve covered the key components, let‘s walk through a tutorial on using them to build an actual AI chatbot application. We‘ll use FalconAI to power the conversational AI, LangChain to compose the model with prompts and other components, and Chainlit to build the chat interface.

Setting up the Environment

First, make sure you have Python 3.8+ and pip installed. Then create a new virtual environment and install the required packages:

python -m venv falcon-chatbot
source falcon-chatbot/bin/activate
pip install chainlit langchain huggingface_hub torch

This will install Chainlit, LangChain, and the Hugging Face libraries for accessing the Falcon models.

Accessing FalconAI

While you can run Falcon models locally if you have a beefy GPU, the easiest way to get started is using the hosted inference API on Hugging Face. This allows you to access the model via an API call without needing to host it yourself.

First, sign up for a free account on huggingface.co if you don‘t already have one. Then navigate to your profile settings and create a new inference API token. Make sure to select the "write" role for the token.

Next, set your HF API token as an environment variable in your shell:

export HUGGINGFACEHUB_API_TOKEN=your_api_token_here

Now you can access the FalconAI model in your Python code:

from langchain import HuggingFaceHub

model_name = "tiiuae/falcon-7b-instruct" 
hf_llm = HuggingFaceHub(
    repo_id=model_name, 
    model_kwargs={"temperature": 0.9}
)

This creates a LangChain HuggingFaceHub instance that connects to the Falcon 7B model using your API token. We set a sampling temperature of 0.9 to make the model outputs more engaging.

Building a Prompt Template

While you could use the LLM directly, it‘s better to provide guidance on how it should behave. We can do this by creating a prompt template that instructs Falcon to act as a friendly AI assistant:

from langchain import PromptTemplate

template = """You are a friendly and knowledgeable AI assistant. Provide a conversational response to the user‘s message.

<h3>Message:</h3>
{input}

<h3>Response:</h3>"""

prompt = PromptTemplate(
    input_variables=["input"], 
    template=template
)

Here we define a prompt template that gives clear instructions and includes a placeholder for the user input. The PromptTemplate is then initialized with this template string.

Chaining Components Together

Now that we have a LLM and prompt template, we can combine them together into a chain:

from langchain.chains import LLMChain

llm_chain = LLMChain(
    llm=hf_llm,
    prompt=prompt
)

The LLMChain takes the Falcon LLM and prompt template as arguments. This chain is what we‘ll use to generate responses from our chatbot.

We can test it out quickly in the Python REPL:

llm_chain.run("What is the largest planet in our solar system?")

You should receive a response like:

The largest planet in our solar system is Jupiter. It‘s a gas giant with a radius almost 11 times that of Earth and a mass over twice that of all the other planets combined. Jupiter has a thick atmosphere made mostly of hydrogen and helium, with colourful bands and a famous "Great Red Spot" storm that‘s larger than Earth. While it doesn‘t have a solid surface, Jupiter is thought to have a rocky core surrounded by layers of metallic and liquid hydrogen. This massive planet helps shape the orbits of other objects in the solar system and may have helped protect the inner planets from bombardment by asteroids and comets. Let me know if you‘d like to learn more cool facts about Jupiter!

Building the Chatbot UI with Chainlit

Finally, let‘s create an interactive chatbot UI using Chainlit. Create a new Python file called chatbot.py with the following code:

import chainlit as cl

@cl.langchain_factory(use_async=False)
def factory():
    return llm_chain

Here we use a Chainlit decorator on a factory function that returns our LLM chain. This allows Chainlit to create the UI around it.

To run the app, simply execute:

chainlit run chatbot.py -w

This will start the Chainlit dev server and open the chatbot interface in your web browser. You can now interact with your AI chatbot powered by FalconAI!

Chainlit provides a fully functional chat UI with features like chat history, regenerating responses, and data persistence out of the box. But you can also customize the UI using Chainlit‘s declarative components.

Why Falcon, LangChain, and Chainlit?

You might be wondering – why use this particular stack for building AI chatbots? There are a few key advantages:

  1. FalconAI provides state-of-the-art open source language models that are competitive with proprietary offerings. This gives you powerful AI capabilities without cloud API costs or data privacy issues.

  2. LangChain makes it easy to combine language models with other components like embeddings, knowledge bases, tools, etc. This allows you to build more capable AI agents beyond just simple chatbots.

  3. Chainlit enables you to create compelling UIs and end-user experiences on top of your language AI. The declarative abstractions make web UIs as easy to build as data science notebooks.

The combination of a powerful open source language model, a flexible framework for combining components, and a user-friendly UI toolkit allows developers to build useful and engaging AI apps faster.

Falcon, LangChain, and Chainlit work great for customer service chatbots, knowledge base question-answering, AI writing assistants, virtual tutors, and more. You have full control over your language AI and can customize it to your use case. And the permissive licensing means you can deploy it without restrictions.

Conclusion and Next Steps

In this post, we covered how to use FalconAI, LangChain and Chainlit to build an AI chatbot. We walked through the key steps:

  1. Accessing the Falcon model via the Hugging Face inference API
  2. Defining a prompt template to instruct the model
  3. Combining the model and prompt in a LangChain chain
  4. Creating a chatbot UI with Chainlit

Along the way, we discussed the capabilities of these tools and their advantages for language AI development compared to proprietary models and general purpose frameworks.

The complete source code for this project is available on GitHub:
https://github.com/yourusername/falcon-chainlit-chatbot

Where can you go from here? Here are some ideas to extend your chatbot:

  • Add support for multiple languages using a language detection model
  • Incorporate knowledge retrieval to give your chatbot memory of FAQs, product info, etc.
  • Finetune the Falcon model on your own dialog data to create a custom assistant
  • Deploy your chatbot standalone or embed it on your website
  • Enable voice interaction using an ASR API like AssemblyAI with your chatbot

The possibilities are endless with these powerful open source AI tools! Go build something amazing and share it with the world.

Here are some additional resources to dive deeper:

If you have any questions or feedback, feel free to reach out on Twitter @yourusername or join our Discord community. Happy hacking!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts