DEV Community

Kris Iyer
Kris Iyer

Posted on • Originally published at hmh.engineering on

How to build an AI chatbot with Openfire and OpenAI Chat Completion


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.
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 and the API playground for more details on the Chat Completions API and more.

The early adopters of the OpenAI APIs such as My AI on Snapchat, Instacart, shop by Shopify, and Speak are great references for building the next generation of AI tools and solutions. For edtech, virtual tutors and learning assistants offered by Quizlet, DuoLingo, and Khan Academy are creative examples of GTP 3 and 4 capabilities!

To make lives easier the APIs come with a variety of SDKs which includes community-based projects that are well-documented, and easy to integrate with your applications!

Openfire

Openfire 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 to get your chat server up in a few mins!

A picture showing the openfire admin dashboard and configuration options.
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 is a popular Javascript XMPP client that implements a full range of XMPP extensions. Also available as a plugin for openfire — inverse-openfire-plugin and can be installed on openfire with a few clicks.
  • StanzaJS 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 is a Javascript XMPP client that is also available as a jsxc openfire plugin that could be installed with a few clicks.

shows javascript plugins such as inverse and jsxc installed on openfire.
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 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 and deploying them to your Openfire server.

You are going to need one for a chatBot!

Botz

The 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 to provide an AI chatBot or a ChatGPT experience for your Openfire users.

Setup BOTZ within your Openfire plugin

https://medium.com/media/1d6aea2fff779da1bf9c68905cf6bb02/href

Botz version 1.2.0 was released recently and can be used alongside Openfire 4.7.4. Thanks to @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>
Enter fullscreen mode Exit fullscreen mode

Add a helper to wrap any customizations for your service.

https://medium.com/media/730ea9ec8030560d8d1030e6097baf74/href

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.
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.
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 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 (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:

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

Thanks to Julie Miles and Tom Holt for their contributions to this post!


Top comments (0)