Developer log #22 :: Getting started with AI

Introduction

We have all heard about AI services like ChatGPT or Google Gemini (previously known as Bard). So how can you make those potentially powerful tools available for you and your wiki users, and why?

First off, I am not talking about using them for Wikipedia or similar websites. As results from these AI services cannot be verified, I wouldn't consider their result as the absolute correct truth you want to publish.

I'm more talking about using some functions of their services they are really good at. They can give you insights to certain subjects or perhaps give you a different view on a matter you are creating content for. Or let them create summaries from large documents or a chat logs.


Some use cases

To give you an idea for potential use cases, let me throw some at you to think about and we I'll go into detail for one of those use cases.

  • Translations - Provide the AI service a text document and let it be translated into another language. Or take it a step further and have you audio recording be transcribed, translated or both.
  • Summarize - Let the AI summarize large document or chat history from a teams meeting.
  • If you have the money and resources (or setup a local server where you run these services), you could create complete language models related to all the content (or only relevant content) you have in your wiki. If you then post a question to the AI, they have your whole wiki content as a context to formulate results. This can be very powerful!
  • Have an assistant, similar to the ChatGPT website, so your users do not have to leave your website to ask supporting questions.
  • Fine tune the above example by using WikiSearch on the user input and feed the assistant with data from your own wiki, so the assistant is able to use that data to formulate an answer.
  • Let the AI create Images to illustrate content on your website (like the one on this page!)
  • You can also feed services like Microsoft Copilot with information from your wiki. If you have a SharePoint business back-end this means that Microsoft can link all information together. I'll explain in more details at the end of the page.
  • These are just a few use cases that popped-up for me



Let's have a look for a way to add ChatGPT for your wiki-users, so they can ask questions like they would if they went to the ChatGPT website. A very simple example, but it is a good base to build upon as your needs might grow.


It's not going to be a step-by-step tutorial where I tell you to get your open AI ChatGPT key from this url; then choose keys from the menu, click on the left button; etc.., but it will cover the things you need to think off and how to get going with ChatGPT.



Illustration illustrating the use of AI and MediaWiki




Google Bard logoChat GPT logo

Getting started

There are various ways to connect to ChatGPT's API. Through Python, JavaScript, PHP and many more.

So what would be a good choice? Well Python and JavaScript will at some point need to talk to MediaWiki's API for getting or writing content/pages to your wiki with results from ChatGPT. So a good approach would be to add the ChatGPT functionality to MediaWiki's API. This way, Python, JavaSript and PHP can all access ChatGPT.

Screenshot showing waiting for an answer from ChatGPT

Screenshot showing an answer from ChatGPT

So, we create an extension that will extend MediaWiki's API functionality to communicate with ChatGPT. These are the steps I did:

  • Google for PHP wrapper or PHP Client for Open AI and use that to have your extension communicate with ChatGPT.
  • Add a composer.json and required the PHP client for Open AI.
  • Add a config setting to the extension, so the needed Open API key can be set in localsettings.
  • Add an api action called wsai
  • Add two parameters : what and data. what servers as an action parameter, telling the extension what to do. It can be either test or ask. Where test will always return some text, without chatting with ChatGPT. While the ask action will communicate with ChatGPT. data is the actual question.
  • Created a template in the Wiki with a FlexForm form. It has a simple input type is text field, a button send the text and a result area to show what is happening. Use the FlexForm form argument loadscript, to have FlexForm load a specific JavaScript file with this form.
  • Created the JavaScript file, that FlexForm will load, that monitors the text input field and the ask button. If someone types text and presses enter, the JavaScript will add the text to the results area and send the text also to the new MW API function wsai. The eventual response, from ChatGPT, will be added to the results area. Of course, I disable the ask button until we have results back. Present the user with a visual while we are waiting for the response.
  • To keep things simple, every result in the results area ( so what the user typed and the results ) are clickable. On a mouse click the specific result will be copied to the clipboard for the user to paste it in the Visual Editor if needed.

Things to think about

Although the above example works fine. When you start using it, you will notice that ChatGPT only reacts to the question you ask, but forgets about all your previous questions.


So in our case, What can you tell me about the MediaWiki API, provides a nice extensive answer. If I would then ask something like: Can you explain each keyword in more detail? I would get an answer like :


Certainly! Here are some explanations for various keywords:

1. Keyword: A word or phrase used in a search query to find relevant information or content. Keywords help search engines understand the intent of the user and provide the most appropriate results.


So our extension should, with every question we ask, provide the chat history as context for ChatGPT to be more helpful. Luckily for us, the Open AI api provides ways of adding the context easily.


We might also store chats in cookies so on a later time or day a user can continue where he left off.


As mentioned in the intro of the article, we could also first do a WikiSearch, on the question from the user, and feed relevant pages found in your wiki to the assistant and tell it to use them to formulate a more correct answer if possible.

Let's add some more functionality

I've talked about use cases like asking for a page summary or relevant keywords for a page. Let's add that to our chat extension.

Using the HTML5 Dialog element, I setup a simple form in a pop-up to ask what the user wants to do with the content off the current page as a whole. They get two checkboxes. One for requesting relevant keywords for the current page and one checkbox requesting a summary.

It is important to add the question and the AI's repsonse inside our chatbox. This way we can continue to ask questions about e.g. the keywords as it will be send to the AI as context again.

Screenshot the HTML5 Dialog

Using the MediaWiki Extension Widgets I add the HTML5 dialog just above our AI Chat area. With JavaScript we then check what choice the user made and use our previous JavaScript to send the request. Our API extension gets another argument where a PageTitle can be added so we can get the source of the page. When sending it to open AI, we also tell the assistant that the format of the page is Wikitext, but it should answer in normal text.


Feeding an AI service like Microsoft Copilot

One of the use cases I mentioned was the use of (Microsoft) Copilot. If you are familier with the MS SharePoint back-end and Copilot, you know that Copilot can be very powerful. It is able to link information together from various sources like Outlook and OneDrive. By linking together I mean something like: asking Copilot what your colleagues are working on. Copilot knows who your direct colleagues are through e.g. recent emails; department and job descriptions; etc.. and formulate an answer that can be related to the people you most interact with. Also don't forget that Microsoft Search is also able to search all these sources of information.


Now imagine giving Copilot (and MS Search) all relevant information from your wiki. Even include things like: who created/edited what page; what pages have the most page views; etc.. That can be a very powerfull tool. Not only can you search from MS Search into the content of your wiki, but you could ask Copilot to give you the 10 most recent created pages from the wiki. Or ask if the wiki has more information about a document that is in your OneDrive? You can see the possibilities here!


That is another interesting way of getting started with AI!

Want to know more?

Lua error: bad argument #1 to 'mw.text.jsonDecode' (string expected, got nil).