Making API of AI

Let’s discuss how to make AI response fitting the structure an API needs – Structured Response.

The discussion will start, with a simple 1 parameter API, and in section 5 we will expand the discussion to multiple parameters.

1. AI Query such as:

Seems like a beautiful response, but cannot be chained to further automation processing, unless the response required is a block of text.

2. Trying a different approach:

Well it did answer Apple, but still we will need to strip down all the text to find the answer.

3. The Response: keyword

We will try to show AI what we mean as a good response:

This is not working, but again AI understand specific formats, some works and some not …

Some keywording just works better then others:

Ok, that’s nice! but usually we need more than one value from our response, we need more sturcutre.

4. Converting to JSON

🤭

This will work but you need to strip down some characters of the response.

After couple of more tries:

We finally get it to work:

NOTE: Thinking about prompt optimization? while it will save cost, it will not always give the proper results:

5. Multiple Parameters
[Picking a different problem with more parameters and more interesting business logic]

AI that generate a color pallete for a website by description:

5.1. Getting mock API output structure from AI:

5.2. Always the best to use AI to craft the initial prompt, althought its not guaranteed that it will execute it properly in the first go:


5.3. AI provided the following prompt:

Create a color palette for my blog website, inspired by the theme "colors of the sky and ground."

Response in JSON without code block
`
{
  "primaryColor": "[primaryColor]",
  "secondaryColor": "[secondaryColor]",
  "accentColor": "[accentColor]",
  "textColor": {
    "primary": "[textPrimaryColor]",
    "secondary": "[textSecondaryColor]",
    "contrast": "[textContrastColor]"
  },
  "background": {
    "main": "[backgroundMainColor]",
    "secondary": "[backgroundSecondaryColor]"
  }
}
`

5.4 Let’s try it!, …, After couple of more tries I managed to make it work!!

5.5. Response:

5.6. To make this an API we still need to replace the User Prompt with some key we can replace and voila, got the prompt to generate an API that recommend color pallete based on user prompt, which can be chained further to other automation tools or as a response to the user.

6. Some things to consider:

6.1. Fact check layer

How do you make sure the response is good? acceptable by end-user and doesn’t needs a disclaimer about the output.

By itself this is just a nice example, to make this AI an API, another fact checking layer should be developed over the AI response – because as people call it AI can hullicinate a response, if it doesn’t have enough information, or the prompt was not leading enough to the proper response.

Philosophy: I will argue if that can be called hullicination or simple auto complete bug, the machine is forced to answer the next word, what he desn’t have in how knowledge interpret to the next word with probability on his exiting knowledge.

Fact checking layer, is another must have layer for AI automation, without it the API can only be is considered for learning purpose only.

6.2. Next Steps

In my next article I will show how to wrap this into a real API in local development mode, using Ollama. I will also include code and installation steps to put this API on a real server.

7. Write me in the comments!