DEV Community

Cover image for I made this AI tool in 69 lines of code in single HTML file.
Anas Dew
Anas Dew

Posted on

I made this AI tool in 69 lines of code in single HTML file.

Hi Geeks!

In this blog, I am going to share with you my latest project - a simple AI tool that I built in just 69 lines of code within a single HTML file. With the growing popularity of Artificial Intelligence and its applications, I wanted to demonstrate how easy it is to create a basic AI tool with minimal effort and code. Whether you are a beginner in the field of AI or an experienced developer, I believe this blog will provide some useful insights and inspiration for your future AI projects.


The idea is simple, Find a closest suitable word meaning for a phrase given as input.


You gussed it right, I'm going to use OpenAI's model to find that word. And for the frontend, Bootstrap will be fine.

Let's CODE...

I will breakdown this coding part into 2 parts as javascript, frontend. So let's dive in.

Writing the fetch() API

First of all we're going to code an API that will take our prompt and return us a reponse. See code Below

const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer $your-openai-api-key'
                body: JSON.stringify({
                    "model": "text-davinci-003",
                    "prompt": `find one word for this phrase : ${that_phrase}`,
                    "temperature": 0.7,
                    "max_tokens": 256,
                    "top_p": 1,
                    "frequency_penalty": 0,
                    "presence_penalty": 0
Enter fullscreen mode Exit fullscreen mode

Now in prompt, You see that i've just sent a phrase and said that "find one word for this phrase" and OpenAI' model will do it's work and as a reponse we will get json.

That json will filled with lots of data that we don't need for this project so i've made it simple for you.

.then(response => response.json()).then(
                data => { 
                    let response = data['choices'][0]['text'];
                    document.getElementById('reponse-display').innerText = "That word is : " + response
Enter fullscreen mode Exit fullscreen mode

Now it will extract that text reponse came from OpenAI and store it in reponse variable. And there's an element in the frontend that will show the text as well. that is why i wrote the line.

document.getElementById('reponse-display').innerText = "That word is : " + response
Enter fullscreen mode Exit fullscreen mode

Also at the bottom i put a code to trigger this API as shown below.

    let button = document.getElementById('find-button');
        button.addEventListener('click', (() => {
            let user_input = document.getElementById('phraseInput').value
            generateWord('', user_input)
Enter fullscreen mode Exit fullscreen mode

And that is all. Javascript part is done.


As i said earlier, I will be using Bootstrap. And I'm not going to install it. Because it's a very small project, I will be using cdn links to get job done.

<link href="" rel="stylesheet"
        integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <script defer src=""
Enter fullscreen mode Exit fullscreen mode

By pasting this code into the head tag of the HTML file, I can use the a power of Bootstrap. So let's goto bootstrap and get forms to make UI better.

I will copy form from Bootstap and edit according to the need. That is all. Below

        <div class="mb-3">
            <label for="phraseInput" class="form-label">Write your phrase below</label>
            <textarea type="text" class="form-control" id="phraseInput" aria-describedby="emailHelp" placeholder="For Eg. Walking in sunrise while listening to beautiful music."></textarea>
        <button type="button" id="find-button" class="btn btn-primary">Find</button>
Enter fullscreen mode Exit fullscreen mode

Below this form I will place the h2 tag to display that response word.

<h2 style="margin-top: 2rem;" id="reponse-display">Click "Find" to find a word for your phrase</h2>

That's it. The Tool is ready to deploy and for the i see netlify is the easiest.

Final Thoughts

Altough, this tool is complete but i've got more ideas to implement into this so i will keep updating the app.

Also this project is open to all to contribute. So fork the repo and make changes, and i will push it.

Here's the app

Thank you!

Yo! i keep building exciting and interesting things around so don't forget to follow!.

Top comments (0)

The AI Brief: Top Posts from the AI Ecosystem πŸ€–

1. LLMs will fundamentally change software engineering

Large Language Models (LLMs), such as GPT-family, will revolutionize the way we build and interact with software. This article dives into the possibilities and implications of LLMs influencing the industry.

2. Will AI Replace Developers?

While the rise of AI often leads to concerns of developers being replaced, the reality is likely more nuanced. Explore the intricate relationship between AI and software development.

3. Semantic Search With Xata, OpenAI, TypeScript, and Deno

Upgrade your search capabilities by using OpenAI's embeddings API, TypeScript, and Deno for Semantic Search implementation to go beyond just matching keywords in your search system.