Integrate with OpenAI

ChatGPT is a conversational AI language model trained and designed to provide informative and engaging responses to users in natural language, and it supports multiple languages. It may be used in many different areas, such as customer service, education/training, personal assistants (summary, translations, planning), content creation, entertainment and programming.

ChatGPT is created by OpenAI. The application ChatGPT is just the application that is available to the public, and it operates on top of powerful Large Language Models (LLMs).

The great thing with OpenAI is that all of this is available through APIs. You may try the OpenAI API platform for free, but in general, it is pay-as-you-go.

The key to getting the most value out of Chat API is to design a (input/questions) in an optimal way. You may read more on prompt design here.

A few showcases of OpenAI API integrations are found in our Showroom! You may try the functionality, as well as access the setup in Appfarm Create. If you do not have access, you may register here.

Getting Started

First off, you need to sign up (or login) at https://platform.openai.com

Once inside, you have access to a lot of tutorials. But technically, you only need to go to your Account and locate the API Keys menu. Generate a new API key and store it somewhere safely (for example, as a Secret in Appfarm Create).

The API we will be using in this example is the Chat Completion API. It takes a prompt and a few parameters as input, and returns the response from OpenAI. In the below example, we use text only as input, but the API also supports images and files.

Setting up the integration in Appfarm Create

The integration is a simple web request.

In the above illustration:

Body Content (the input to the API)

{
  "model": "gpt-4.1",
  "messages": [
      {"role": "system", "content": "Act as a professional chef"},
      {"role": "user", "content": "Give me your best lasagna recipe"}
      ],
  "temperature": 0.3,
  "max_tokens": 1000,
}

You may read more about these and additional parameters here. The most important parameters are:

  • model: Required. The ID of the model to use. See the overview here.

  • messages: Required. This is a list of messages describing the conversation so far. 3 Roles exist:

    • system: Typically, your conversation (list of messages) starts with a system message. This message helps you set the default behaviour or context of the "AI assistant".

    • user: this is the one instructing the assistant.

    • assistant: this helps you store previous responses. If you have a full conversation between the AI assistant and the user, you may input alternate messages between user and assistant, such as in this example:

      messages=[
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "Who won the world series in 2020?"},
              {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
              {"role": "user", "content": "Where was it played?"}
          ]
  • temperature: 0 means deterministic. You will get the same response for the same prompt each time. 1 is the opposite - OpenAI will take a higher risk and give you different responses each time. You may use something in between like 0.4 as well, depending on the use case

  • max_tokens: The number of "words". OpenAI operates with its own term here, where a token is similar to a word, but a token may be smaller as well.

Response

The response contains more information that you would typically need to store or process. Example response of the above input is:

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

The textual output from the API is found in the choices node, and you may refer to this first entry in the Result Mapping choices.0.message.content

Implementing a use case (example)

Our Appfarm Showroom showcase - "Chef" - is an App for entering a list of ingredients and getting a suggestion of a dinner matching those ingredients. The end user just inputs a comma-separated list of ingredients, such as "chicken, egg, pasta" as well as the number of servings.

What we do in this case, is to save the user input into App Variables Ingredients List and Servings.

We create the prompt by merging a question/prompt with the user inputs, as seen from the function below, where we construct the body (input) to the Completions API.

const prompt =  `Make a ${dinnerRecipe ? 'Dinner' : 'Dessert'} recipe for ${servings} people with the following ingredients as main components:
${ingrediensList}.

Also, use the metric system for all units.

`

return {
  "model": "gpt-4.1",
  "messages": [
      {"role": "system", "content": "Act as a professional chef"},
      {"role": "user", "content": prompt}
      ],
  "temperature": 0.3,
  "max_tokens": 1000,
}

As seen in the setup above, we also define a system as "Act as a professional chef".

The concept above is called Prompt Design. Tuning the wording of the prompt, combined with a good system definition, will allow you to take more control of the quality of the output from the API.

Sending data to OpenAI

In many business use cases, you may want to either

  1. Send files or business data "on-demand" together with the prompt.

    1. Example 1: Give me the business name, amount, and date from this receipt

    2. Example 2: Analyze these data records (represented as CSV). The list represents all action items of a project plan of an implementation project with the following project description and timelines: "....". Give me a list of suggested action items to add, represented as JSON.

  2. Uploads a large file or many files to a file storage, for the AI to use as a basis for its reasoning

    1. This is RAG (Retrieval Augmented Generation), where the files are uploaded (and chunked) into a vector store, and you can enable a file-search tool in the API - enabling the AI to search for relevant information to be used together with the prompt.

    2. Example 3: You are an assistant for our in-house HR department. Use file search to provide the user with an answer to the user query. Always ground your reasoning in these files. If you cannot find an answer in the files, inform the user, and give an answer based on best practices and current legislation.

Analyze business data or files on demand

For this purpose, you may use the Chat Completion API.

  • For sending an image, see the following example

{
    "model": "gpt-4.1",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "Give me the business name, amount, and date from this receipt"
          },
          {
            "type": "image",
            "image_url": {
              "url": "URL to the image here"
            }
          }
        ]
      }
    ],
    "max_tokens": 3000
}
  • For sending files (other than images), you may only send PDF files They need to be converted to base64 before sending, and added as a message such as the below example.

{
    "model": "gpt-4.1",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "Give me the business name, amount, and date from this receipt"
          },
          {
            "type": "file",
            "file": {
              "file_data": "base64 string here",
              "file_name": "MyReceipt.pdf"
            }
          }
        ]
      }
    ],
    "max_tokens": 3000
}
  • For sending CSV or other file formats, they must be converted to a string, and passed along with the prompt as a text message. You may convert a data source (holding all entries you want to analyze) to a CSV string with the following function (example):

// Example for creating a CSV representation of the data source "projects"
// "projects" has been added as a function parameter

if (!Array.isArray(projects) || projects.length === 0) return "";

// Collect all unique keys across all projects
const allKeys = Array.from(
    new Set(projects.flatMap(project => Object.keys(project)))
);

// Create header row
const header = allKeys.join(";");

// Create data rows
const rows = projects.map(project => {
    return allKeys.map(key => {
        const value = project[key];
        if (value === undefined || value === null) return '""';
        if (typeof value === "string") return `"${value.replace(/"/g, '""')}"`;
        return value;
    }).join(";");
});

// Combine header and rows
return [header, ...rows].join("\n");

Analyze a large set of files

For this purpose, you should do the following

  1. Create a Vector Store (manually from the OpenAI developer portal, or via API). This is the database holding the files you want the AI to use for its reasoning. You may want a separate vector store for different use cases (e.g. an "HR assistant" and an "Accountant assistant" operate best if they have their own vector stores)

  2. Add (update or delete) the files you want to analyze using the Vector store files API.

  3. Use the Responses API instead of chat completion. To enable file search, you must add the section tool_choice and tools to the input (read more in the documentation).

Last updated

Was this helpful?