ACF Library

CallChatGPT

Back to List

Description: An experimental ChatGPT integration

FileMaker Prototype:

Set Variable [$res; ACF_Run("CallChatGPT"; string_prompt;  string_apiType;  string_model)]

Category: REST API

NOTE: This function serves as a reference template. To use it effectively, you may need to adjust certain parts of the code, such as field names, database structures, and specific data lists to fit the requirements of your system. This customization allows the function to work seamlessly within your environment.

The CallChatGPT Function

The CallChatGPT function is an experimental implementation of ChatGPT, inspired by the work of LuminFire—FileMaker AI Demo. It showcases an ACF-based approach for calling API in FileMaker.

While this functionality can be implemented without an ACF function by relying solely on scripts, the CallChatGPT function offers a significant advantage in maintainability—especially when dealing with rapidly evolving technologies like OpenAI. As changes occur in their API (e.g., new parameters, models, or ways to interact with the API), updates can be made directly in the ACF source code and deployed across multiple customer sites. This eliminates the need to log in to each site and manually update scripts.

Using ACF, you can also streamline deployment by setting up a small web service (Read the article about it here, and the source is available here. Look in the left menu for additional pages) With this approach, you can encrypt and upload the compiled ACF functions, then remotely install updates with just a click—no additional on-site configuration required.

Use cases

  • Automating FAQ responses within FileMaker.
  • Summarizing or rephrasing data for reports.
  • Translating text directly in FileMaker records.

Implementation details

The implementation includes a helper function, CreateAiJSON, which builds the JSON request to the API. The main function features a case branch to handle various API types, constructing the necessary JSON request and applying type-specific tags.

Then, it posts the request object to the selected endpoint and processes the result object from the API.

Function source:

Function CreateAiJSON(int fpenalty, int maxtokens, string model, int prespenalty, float temperature, float topP)
   JSON Js;
   Js = JSON(
         "frequency_penalty",fpenalty,
         "max_tokens",maxtokens,
         "model",model,
         "presence_penalty",prespenalty,
         "temperature",temperature,
         "top_p",topP);
   return Js;
End

function CallChatGPT (  string prompt, string apiType, string model )
   
   string APIkey = Questions_Prefs::APIKey; 
   string url, err,restxt; 
   JSON quest; 
   case
      :(apiType == "Images")
         url = "https://api.openai.com/v1/images/generations";
         quest = JSON ("prompt", prompt, "n", 1, "size", "1024x1024"); 
      :(apiType == "Completions" )
         url = "https://api.openai.com/v1/completions";   
         quest = CreateAiJSON ( 0,  QUESTIONS::MaxTokens, model, 0, QUESTIONS::Temperature, QUESTIONS::TopP); 
         quest ["prompt"] = prompt; 
      :(apiType == "Embeddings")
         url = "https://api.openai.com/v1/embeddings";
         quest = CreateAiJSON ( 0,  QUESTIONS::MaxTokens, model, 0, QUESTIONS::Temperature, QUESTIONS::TopP); 
         quest["prompt"] = prompt;
      default // chatGPT
         url = "https://api.openai.com/v1/completions";   
         quest = CreateAiJSON ( 0,  QUESTIONS::MaxTokens, model, 0, QUESTIONS::Temperature, QUESTIONS::TopP); 
         quest["messages[]"] = JSON ( "role", "user", "content", prompt); 
         quest["n"] = 1; 
   end case
   
   string hdr = "Content-Type: application/json\nAuthorization: Bearer "+APIkey;
   string respons = HTTP_POST ( url, string (quest), hdr);
   print respons; 
   
   JSON resp; 
   resp = respons; 
   if ( list_keys (resp) == "error") then
      err = resp["error.message"];
      alert (err);
      return "Error"; 
   else
      if ( apiType == "Images") then
         restxt = resp["data[1].url"];
         return trimboth(restxt);
      else
         restxt = resp["choices[1].text"];
         return trimboth(restxt);
      end if
   end if
   
   return "OK"; 
end

Example

Set Field [QUESTIONS::response; ACF_Run("CallChatGPT"; "What is AI?"; "completions"; "gpt-4")]

Output:
"AI, or Artificial Intelligence, refers to the simulation of human intelligence in machines that are programmed to think and learn."

The service response is a JSON object. The function first checks if the object has an "error"-tag. If it has, it alerts the user about its message. Otherwise, it retrieves the response either as an image URL or a text response depending on the type of request.

In case of an error, you will get an alert dialogue like this:

Skjermbilde 2024-11-17 kl. 04.29.24

Stay tuned

We will continue to update this article with more on this, so stay tuned. The first plan is to expand the ACF email demo with the chatGPT support in crafting the e-mail text. This will be available as part of the library.

Back to List