How to build an AI chatbot with Openfire and OpenAI Chat Completion
Tags: openfire, chatbots, chatgpt, openai
Published: 2023-03-24 13:06:48

---
title: How to build an AI chatbot with Openfire and OpenAI Chat Completion
published: true
date: 2023-03-24 11:02:53 UTC
tags: openfire,chatbots,chatgpt,openai
canonical_url: https://hmh.engineering/how-to-build-an-ai-chatbot-with-openfire-and-openai-chat-completion-33b94afe41d0
---

![](https://cdn-images-1.medium.com/max/1024/0*3cg6LMG-ClblqIq2)
_Photo by Eric Krull on Unsplash_

> Responsible use of artificial intelligence (AI) and ML technologies is key to fostering continued innovation.

AI chatbots are here, there, and everywhere! Ever since the introduction of ChatGPT in December 2022, tech companies of all sizes have been racing to build AI-powered tools and solutions.

This article describes some high-level building blocks to help you build a chatbot powered by Openfire that could be wired up to OpenAI APIs to provide chat completions!

![An architecture diagram showing a chatbot and users using conversejs to connect to openfire, integrated with the Botz library (capable of managing logins and presence for the chatbot as well as intercepts messages) and is able to use openAI through the java sdk to gather responses.](https://cdn-images-1.medium.com/max/1024/1*-Gp_vl4IfiSW_iFeBDA6Qg.png)
_High-level Architecture for building a chatbot with Openfire._

### OpenAI API

The recently released OpenAI APIs are a game changer for both small and large enterprises to provide In-App chatGPT-like applications powered by the same APIs and models that power chatGPT. Check out the [API reference](https://platform.openai.com/docs/api-reference/chat) and the API [playground](https://platform.openai.com/playground?mode=chat) for more details on the Chat Completions API and more.

The early adopters of the OpenAI [APIs](https://openai.com/blog/introducing-chatgpt-and-whisper-apis) such as [My AI](https://newsroom.snap.com/say-hi-to-my-ai) on Snapchat, [Instacart](https://www.instacart.com/), [shop](https://shop.app/) by Shopify, and [Speak](https://www.speak.com/) are great references for building the next generation of AI tools and solutions. For edtech, virtual tutors and learning assistants offered by [Quizlet](https://quizlet.com/labs/qchat), [DuoLingo](https://openai.com/customer-stories/duolingo), and [Khan Academy](https://openai.com/customer-stories/khan-academy) are creative examples of GTP 3 and 4 capabilities!

To make lives easier the APIs come with a variety of [SDKs](https://platform.openai.com/docs/libraries/community-libraries) which includes community-based projects that are well-documented, and easy to integrate with your applications!

### Openfire

[Openfire](http://www.igniterealtime.org/projects/openfire/index.jsp) is a real-time collaboration (RTC) server licensed under the Open Source Apache License. It uses the only widely adopted open protocol for instant messaging, XMPP (also called Jabber). Openfire is incredibly easy to set up and administer but offers rock-solid security and performance.

The project was originated by Jive Software around 2002, and continues to thrive under a community model, as part of the Ignite Realtime Foundation that does a fantastic job!

**Some of the core features (but not limited to):**

- XMPP server written in Java and licensed under the Apache License 2.0
- User-friendly web-based installation and administration panel
- Shared groups for easy roster deploying
- Plugin interface
- SSL/TLS support
- Offline Messages support
- Server-to-Server connectivity
- Database connectivity for storing messages and user details (including the embedded HSQL database and support for MySQL, PostgreSQL, etc.)
- LDAP integration
- Platform independent (with the installers for different platforms)
- Connection manager for load balancing
- Clustering support (hazelcast)
- Message archiving-logging
- Content filtering, packet rules
- Pluggable Roster Module
- Custom Authentication Provider
- Support for BOSH as well as WebSockets.
- File Sharing

To get started visit [working with Openfire](https://download.igniterealtime.org/openfire/docs/latest/documentation/working-with-openfire.html) to get your chat server up in a few mins!

![A picture showing the openfire admin dashboard and configuration options.](https://cdn-images-1.medium.com/max/1024/1*a9a_queuzFCbl1xCbFPZSw.gif)
_An admin dashboard for managing openfire!_

#### XMPP Clients

Openfire server could be used along with a Javascript XMPP client of your choice. Some popular projects and plugins that can get you started are as follows:

- [ConverseJS](https://github.com/conversejs/converse.js/) is a popular Javascript XMPP client that implements a full range of [XMPP extensions](https://github.com/conversejs/converse.js/#supported-xmpp-extensions). Also available as a plugin for openfire — [inverse-openfire-plugin](https://github.com/igniterealtime/openfire-inverse-plugin) and can be installed on openfire with a few clicks.
- [StanzaJS](https://github.com/legastero/stanza) is a JavaScript/TypeScript library for using modern XMPP, and it does that by exposing everything as JSON. Unless you insist, you have no need to ever see or touch any XML when using StanzaJS.
- [JSXC](https://www.jsxc.org/) is a Javascript XMPP client that is also available as a [jsxc openfire plugin](https://github.com/igniterealtime/openfire-jsxc-plugin) that could be installed with a few clicks.

![shows javascript plugins such as inverse and jsxc installed on openfire.](https://cdn-images-1.medium.com/max/1024/1*KWCE8XFwhQ75k2fl8HsVvA.png)
_The plugin page in openfire with inverse and jsxc installed._

#### Plugins

Plugins are a great way to extend or customize capabilities on your Openfire server.

There are a no of [community-based plugins](https://www.igniterealtime.org/projects/openfire/plugins.jsp) that you could readily install and use on your Openfire server. Adding a new plugin for your custom needs requires setting up a jar/war per the [Openfire plugin specification](https://download.igniterealtime.org/openfire/docs/latest/documentation/plugin-dev-guide.html) and deploying them to your Openfire server.

You are going to need one for a chatBot!

### Botz

The [Botz](https://www.igniterealtime.org/projects/botz/) library adds to the already rich and extensible Openfire with the ability to create internal user bots. With the Botz library, programmers may choose to develop a virtual user or a chatbot as a plugin. Although Openfire does not really distinguish this virtual user from the real users, one could intercept messages to the chatBot from your users, and be able to respond per your needs. An example would be to integrate the Botz library with the relatively new [OpenAI Java SDK](https://github.com/TheoKanning/openai-java) to provide an AI chatBot or a ChatGPT experience for your Openfire users.

#### Setup BOTZ within your Openfire plugin
<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d6aea2fff779da1bf9c68905cf6bb02/href">https://medium.com/media/1d6aea2fff779da1bf9c68905cf6bb02/href</a></iframe>

[Botz version 1.2.0](https://discourse.igniterealtime.org/t/botz-version-1-2-0-release/92649) was released recently and can be used alongside [Openfire 4.7.4](https://github.com/igniterealtime/Openfire/releases/tag/v4.7.4). Thanks to [@guusdk](https://github.com/guusdk) !!!

#### OpenAI API integration

Add the OpenAI java dependency to your project. The below example uses gpt-3.

```
<dependency>
 <groupId>com.theokanning.openai-gpt3-java</groupId><!-- use latest as needed -->
 <artifactId>service</artifactId>
 <version>0.11.0</version><!-- use latest as needed -->
</dependency>
```

Add a helper to wrap any customizations for your service.

<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/730ea9ec8030560d8d1030e6097baf74/href">https://medium.com/media/730ea9ec8030560d8d1030e6097baf74/href</a></iframe>

At this point, packaging your plugin code and deploying it to the Openfire server should enable your BOT user and set up the presence.

### Auto Display Bot for your users

Now that we have a way to add a virtual BOT user, we will need a mechanism to have the BOT user appear under the contact lists for your real chat users. In order to do that:

- Create a group in Openfire’s admin console
- Make the bot a member of that group
- Enable contact list group sharing, to make the group appear on the contact list of every user in the system

![Shows the Openfire group and contact listing configuration that allows us to inject the bot user to show up for our real users.](https://cdn-images-1.medium.com/max/1024/1*JH60om9ySk1L-eUk89EU7A.png)
_Openfire group and contact listing for bot user._

### Wrap up

![inverse plugin showing converse js connected to openfire with the bot user exchanging messages with openAI chat completions.](https://cdn-images-1.medium.com/max/1024/1*f8Ze1_QKOnD9Qc1EDE1CPA.png)
_chat completions with openAI with converse (inverse openfire plugin)_

- What you see above is a real user chatting with the virtual user myaibot.
- Since we made the BOT a member of a new group and enabled contact list group sharing for all users we see the BOT listed under the user's contact list along with the online presence that was set up using Botz.

I hope some of these building blocks help you build the next cool AI BOT for your application powered by Openfire, BOTZ, and openAI!

At [Houghton Mifflin Harcourt](https://medium.com/u/ac63ac3175a7) we’ve only just begun to explore ideas on embedding AI and chatGPT-like tools into our platform and products that could benefit learners and educators as well as help find efficiencies with internal workflows. [Julie Miles](https://medium.com/u/549404f77a2c) (SVP — Learning Sciences), myself, and the rest of my colleagues really think of this as the tip of the literal iceberg. Some of the ideas we have been working on across some broad buckets of research are listed below:

- Reduce teacher effort in lesson planning
- Use assessment data to group students or differentiate assignments to assist teachers
- Provide feedback on areas to improve to both the student and the teacher on the student’s work, recommendations, etc
- Provide intelligent tutoring to students (e.g., encouragement, hints, feedback on essays, etc.) while they’re working through a lesson
- Create efficiencies in functional workflows so that we free up more time for thought leadership which leads to more innovation. Some areas we have already experimented with include:  
- Writing first drafts of content or assessment items  
- Translating content into other languages  
- Inserting explanatory comments into existing code  
- Asking how to fix code that doesn’t work  
- Checking code for bugs and auto-completion  
- Drafting marketing names for new products and many more areas.

With open AI leading the way with GPT 3 and GPT-4, the future looks promising as well as exciting! Looking forward to learning and experimenting with building safe, accountable, and trustworthy AI solutions!

#### Helpful Resources

There is a plethora of open-source/no-code/low-code solutions out there that you could play with to spin up your AI BOT. I want to leave you with some helpful resources:

- [RocketChat](https://github.com/RocketChat) recently came out with an [OpenAI chat completion app](https://github.com/RocketChat/Rocket.Chat.OpenAI.Completions.App), and browser-based no-code app builder
- [flutter.io](https://flutterflow.io/) announced easy additions for [OpenAI chat completions](https://community.flutterflow.io/c/community-tutorials/add-ai-completion-to-your-flutterflow-project) to their projects.
- [sms-chatbot with nodejs and twillio](https://www.twilio.com/blog/sms-chatbot-openai-api-node)
- [Your Personal Michelin Star Chef with OpenAI’s GPT-3 Engine, python and twillio](https://www.twilio.com/blog/python-whatsapp-chef-bot-openai-gpt3)
- [Learn more about Botz and Openfire](https://www.igniterealtime.org/projects/botz/)

That’s a wrap for this article. Good Luck and Stay Safe!

Thanks to [Julie Miles](https://medium.com/u/549404f77a2c) and [Tom Holt](https://medium.com/u/c5193d2b3e93) for their contributions to this post!

* * *