Developer ‘Tiqs & Tricks: Generative AI Functions

Developer ‘Tiqs & Tricks

February 2024

Generative AI Functions

 

If you’ve worked much with Large Language Models, you’ve discovered that they are simultaneously very clever and terribly uninformed. Let’s use a use case by way of example.

Use Case: This very basic Client-based application is designed to provide information about seven airports in California:

Select one of the airports in the list to see the arrivals and departures happening at the airport for the day:

Amongst the many uses for AI in such an application, there is a handy way for the Client user to ask questions. And here’s where LLMs can be clever and ignorant at
the same time:

Q (human): What is the largest airport within 50 miles of La Joya, CA?
A (AI): The major airports near La Jolla, CA are San Diego InternaBonal Airport
(SAN) and McClellan-Palomar Airport (CLD). San Diego InternaBonal Airport is the
larger of the two.

Q: What flights are leaving for Eugene, OR today?
A: I don’t know.

LLMs simply can’t answer real-time questions. Presumably, you could take the day’s schedule and embed it into a Semantic Index to read for question response, but that still isn’t real-time. The application updates constantly with new information as conditions change, and the LLM won’t be able to keep up.

Enter AI Functions.

AI Functions help LLMs find the answers to real-time questions. Developers write the functions to retrieve the desired information, then let the LLM know under what circumstances those functions should be called. The LLM then does what it does best: parses the meaning of the queries, calls functions as it deems it appropriate to do so, and interpolates the results to provide a response.

Note: At the time of this writing, OpenAI is the only company offering AI functions, for its ChatGPT models. Once more AI companies offer functions, Vantiq intends to
support them!

Steps to Creating and Using AI Functions:

Step 1: Write a VAIL Procedure
In our case, we’ll want to be able to query our list of departing flights to get that answer about Eugene. Whether the list is in appliction state or a database type,
it can be scanned to retrieve the information we want.  Example code:

var routes = select * from air.mgr.DataTableDepartureType where ToAirport == destination && OnDate == toDate(truncateTo(toDate(now()),”DAYS”))

var flights = “”
var times = “”
var origins = “”

for ( r in routes ) {
    var tTime = r.Time*1000
    var qTime = toDate(tTime)
    flights += r.FlightNo + “, “
    times += toString(qTime) + “, “
    origins += r.FromWhere + “, “
}

return “Flights to ” + destination + ” are leaving at ” + times + “from” + origins + “flight numbers: ” + flights “respectively.”

This is simple VAIL code to accept an airport code and return flights listed as departing to that airport, today.

Step 2: Explain What the Procedure is For
Write those explanations in three places:

• Parameter description
• First comment before actual code
• Procedure description

Here’s what our procedure declaration and comment look like, at the top of the code you already saw:

package air.mgr
stateless PROCEDURE AnswerBot.findDepartures(destination String DESCRIPTION “The airport code of the destination, like PDX”)
// This procedure accepts a destination point and finds flights going to that destination by selecting them from a database. Example question: “Is there anything leaving to go to PDX?”

…and since this is a Service Procedure, we also put the same procedure description in the Interface tab for the procedure signature:

Step 3: Tell the LLM Where to Look for the Function(s)

We do this at the same time we call LLM.submitPrompt to invoke the AI to respond to the question, by adding our functions as a parameter to the LLM Service procedure call submitPrompt:

package air.mgr
stateless PROCEDURE AnswerBot.SubmitPromptFunctions(event)
var Functions = []
Functions[0] = “procedures/air.mgr.AnswerBot.findArrivals”
Functions[1] = “procedures/air.mgr.AnswerBot.findDepartures”
var ms = io.vantiq.ai.ConversationMemory.getConversation(toString(event.ConvoID))
event.llmResponse = io.vantiq.ai.LLM.submitPrompt(“air.mgr.AnswerBotGen”, event.Content, toString(event.ConvoID), null, Functions)
return event

That’s it. Now our procedure is used in place of a regular SubmitPrompt ActivityPattern in an application, and the AI calls our procedure air.mgr.AnswerBot.findDepartures when circumstances make that action appropriate:

Q (human): Are there any flights leaving for Dallas today?
A (AI): There are flights to Dallas (DFW) today. These are the departure times and flight numbers: 1. From SBP at 20:54 (Flight AA2392) 2. From SJC at 20:36 (Flight AA2046) 3. From SFO at 19:59 (Flight AA1588) 4. From SFO at 21:16 (Flight AA304)

Notice a couple of things:
First, the findDepartures procedure only takes in the airport IATA code, not the city name, as a parameter. The AI automaBcally converted Dallas to the code of its major airport: DFW, even as it recognized the appropriateness of calling the getDepartures procedure.

Also, from the function, the real output would have been: Flights to DFW are leaving at 2024-02-27T20:54:00Z, 2024-02-27T20:36:00Z, 2024-02-27T19:59:00Z,
2024-02-27T21:16:00Z, from SBP, SJC, SFO, SFO, flight numbers: AA2392, AA2046, AA1588, AA304, but the AI again intercedes and reorganizes the output, making it more human-readable for the chat answer.

There are some real advantages to pairing functions with AI!

The Takeaways:
AI Functions in Vantiq look an awful lot like VAIL procedures, probably because they are. It was good programming pracBce to document procedures well, and now even more so, as the AI needs to read it too. Unless you specify otherwise, the large language model you choose will put its own spin on how to interpolate function returns, but you can give instructions about that, too. For now, ChatGPT LLMs are the only ones able to recognize and call funcBons, but the market will catch up with more soon. The advantages of AI Functions are obvious, especially to real-time applications!

 

Attachments:
You must be logged in to view attached files.
Posted: February 28, 2024 at 2:12 am
Replies: 0
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
© Vantiq 2024 All rights reserved  •  Vantiq Corporation’s Privacy Policy and Cookie Policy