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.
  • Let the AI create Images to illustrate content on your website (like the one on this 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.

Lua error: bad argument #1 to 'mw.text.jsonDecode' (string expected, got nil).
We use functional cookies that are necessary for the proper functioning of our website. These cookies are always set. Additionally, we use marketing cookies to personalize your experience and to help us improve the performance of our website. You can choose to decline these marketing cookies.
Decline