Suc­cess­fully guid­ing new, dynamic mod­els such as Chat­GPT towards the desired beha­viour requires the imple­ment­a­tion of spe­cific strategies. As AI applic­a­tions con­tinue to grow, guar­an­tee­ing the accur­acy of these mod­els becomes abso­lutely neces­sary. In this art­icle, we will explore three fun­da­mental approaches: prompt engin­eer­ing, func­tion call­ing, and fine-tun­ing. These meth­ods offer dis­tinct path­ways towards refin­ing ChatGPT’s beha­viour across GPT 3.5, GPT 4, and GPT 4o. Join us today to get a clear under­stand­ing of these approaches and their sig­ni­fic­ance in shap­ing ChatGPT’s functionalities.

Text Gen­er­a­tion & Prompt Engineering

OpenAI has trained text gen­er­a­tion mod­els to learn from nat­ural lan­guage, images, and code. These mod­els are guided through ‘prompts,’ which are text-based inputs that help shape their responses. When you ‘pro­gram’ a large lan­guage model, you’re essen­tially design­ing a prompt by giv­ing expli­cit instruc­tions or provid­ing examples of how to achieve the desired outcome.

Prompt engin­eer­ing involves reph­ras­ing and struc­tur­ing a prompt to make it clearer or more spe­cific. This pro­cess requires the care­ful selec­tion of words and phrases, as well as the applic­a­tion of strategies to ensure unam­bigu­ous instruc­tions. The goal is to effect­ively com­mu­nic­ate our intent, redu­cing the chances of mis­in­ter­pret­a­tion and improv­ing the qual­ity of gen­er­ated out­put. What’s more, you can manip­u­late some para­met­ers to fur­ther refine the res­ults and to achieve greater accuracy.

Para­met­ers

You can adjust the res­ults by chan­ging the val­ues of some LLM properties:

  • Tem­per­at­ure: Con­trols the sampling tem­per­at­ure, ran­ging from 0 to 2. Higher val­ues, such as 0.8, pro­duce more ran­dom out­puts, while lower val­ues, like 0.2, res­ult in more focused and determ­in­istic outputs.
  • Top_p: Also known as nuc­leus sampling, this para­meter lim­its the model to con­sider only tokens within the top p prob­ab­il­ity mass. For instance, set­ting it to 0.1 means only the top 10% of prob­able tokens are considered.
  • Frecuency_penalty: A value between ‑2.0 and 2.0. Pos­it­ive val­ues pen­al­ise tokens that have already appeared fre­quently in the gen­er­ated text, redu­cing the like­li­hood of repetition.
  • Presence_penalty: A value between ‑2.0 and 2.0. Pos­it­ive val­ues pen­al­ise tokens that have already been used, encour­aging the model to explore new topics.

Com­bin­a­tions

Adjust­ing the tem­per­at­ure and top_p para­met­ers will make the response more tailored to your requirements:

Use CaseTem­per­at­ureTop_PDescrip­tion
Code Gen­er­a­tion0.20.1Pro­duces code that fol­lows estab­lished pat­terns and con­ven­tions, with pre­dict­able and focused out­put, mak­ing it ideal for gen­er­at­ing syn­tactic­ally cor­rect code.
Cre­at­ive Writing0.70.8Cre­ates ima­gin­at­ive and var­ied text for storytelling pur­poses. The out­put is more explor­at­ory and less restric­ted by patterns.
Chat­bot Responses0.50.5Pro­duces con­ver­sa­tional responses that bal­ance coher­ence with diversity, res­ult­ing in more nat­ural and enga­ging dialogue.
Code Com­ment Generation0.30.2Gen­er­ates con­cise and rel­ev­ant code com­ments, with more pre­dict­able out­put that fol­lows con­ven­tional patterns.
Data Ana­lysis Scripting0.20.1Pro­duces data ana­lysis scripts that are more likely to be accur­ate and effi­cient, res­ult­ing in pre­dict­able and focused output.
Explor­at­ory Code Writing0.60.7Cre­ates code that explores altern­at­ive solu­tions and innov­at­ive approaches, with less adher­ence to estab­lished patterns.

For example, if you need a struc­tured response in JSON, there are two dif­fer­ent approaches:

  1. Adjust para­met­ers like tem­per­at­ure and top_p and indic­ate the desired response format in the prompt.
  2. Use func­tion call­ing to provide a struc­tured response.

Note that neither of these approaches can guar­an­tee a valid response.

Func­tion Calling

Cus­tom func­tions can be cre­ated and sup­plied to a gen­er­at­ive AI model through func­tion call­ing. Rather than dir­ectly execut­ing these func­tions, the model pro­duces struc­tured data out­put that iden­ti­fies the func­tion name and recom­mends the argu­ments to use.

Imple­ment­a­tion

To imple­ment func­tion call­ing, you need a mes­sage and a set of func­tions (which could be a single func­tion). They must fol­low a JSON schema as shown below:

1. Call the model with the user query and the set of functions:

let messages = [
      {
        role: 'system',
        content: 'Perform function requests for the user',
      },
      {
        role: 'user',
        content: userMessage,
      },
    ];

let functions = [
      {
        name: 'request',
        description:
          ‘Function description',
        parameters: {
          type: 'object',
          properties: {
            keyword: {
              type: 'string',
              description: "Property description",
            },
          },
          require: ['keyword'],
        },
      },
];

2. The model can choose to call one or more func­tions. In this case, the con­tent will be a string­i­fied JSON object adher­ing to your cus­tom schema (and remem­ber that the model may occa­sion­ally hal­lu­cin­ate parameters).

3. The func­tions and the mes­sage must be included in the body of the request:

const firstResponse = await makeRequestToOpenAI(res, messages, functions);

4. After receiv­ing the response, parse the string into JSON in your code. If valid argu­ments are provided, you can call your func­tion using those argu­ments. Here’s an example:

if (firstResponse.function_call.name == ‘request’) {
    let argumentObj = JSON.parse(firstMeeraResponse.function_call.arguments);
    content = helloWorld(argumentObj.appendString);
    messages.push(firstMeeraResponse);
    messages.push({
      role: 'function',
      name: 'request',
      content,
    });
  }

Once the func­tion response is appen­ded as a new mes­sage, you can call the model again to sum­mar­ise the res­ults and return them to the user:

const secondResponse = await makeRequestToOpenAI(res,secondMessages);
    // Final response
    const successResponse = {
      success: true,
      message: secondResponse.choices[0].message.content,
    };

Fine-Tun­ing

Fine-tun­ing involves train­ing an exist­ing machine learn­ing model on a smal­ler, spe­cial­ised data­set, with the aim of pre­serving the ori­ginal cap­ab­il­it­ies of the pre­trained model while cus­tom­ising it to bet­ter per­form spe­cific tasks or applic­a­tions. This tech­nique is par­tic­u­larly bene­fi­cial when you have a suf­fi­ciently large data­set, typ­ic­ally com­pris­ing 10,000 or more data points.

Fine-tun­ing allows you to max­im­ise the poten­tial of the mod­els access­ible through APIs by offering:

  1. Improved Model Hand­ling: Fine-tun­ing allows the model to fol­low instruc­tions more accur­ately, such as deliv­er­ing more con­cise answers or adapt­ing its out­put to spe­cific lin­guistic or styl­istic requirements.
  2. Increased Con­fid­ence in Out­put Format­ting: With fine-tun­ing, the model can format responses more con­sist­ently, which is cru­cial in applic­a­tions where spe­cific formats like JSON are necessary.
  3. Per­son­al­isa­tion of Out­put Tone: This is an excel­lent way to adapt the model to reflect the spe­cific voice, tone, and style of a brand or com­pany, provid­ing a more per­son­al­ised and cohes­ive user experience.
  4. Token Sav­ings: By using shorter, more effect­ive prompts, you save tokens, which means sav­ing money.

Imple­ment­a­tion

The steps to fine-tune a model are out­lined below:

Pre­pare the Dataset

To train the model effect­ively, the first step is to organ­ise the data­set. It’s essen­tial to cre­ate a diverse range of example con­ver­sa­tions that resemble those the model will encounter in pro­duc­tion when mak­ing predictions.

Each row in the data­set should rep­res­ent an example con­ver­sa­tion, format­ted accord­ing to the Chat­GPT struc­ture. A min­imum of 10 examples is required for train­ing, but to achieve optimal res­ults, it’s advis­able to provide a data­set with at least 50 to 100 examples.

Start with 50 high-qual­ity demon­stra­tions to assess whether fine-tun­ing enhances the model’s per­form­ance. Even if the model is not pro­duc­tion-ready, notice­able enhance­ments indic­ate that provid­ing more data could lead to fur­ther gains, whereas a lack of pro­gress may indic­ate the need to recon­sider the task setup or to restruc­ture the data before expand­ing bey­ond this small example set.

{"messages": [{"role": "system", "content": }, {"role":    "user", "content":}]}

Token Limit

Token lim­its vary depend­ing on the chosen model. For instance, with gpt‑3.5‑turbo-1106, the max­imum con­text length is 16,385 tokens per example. How­ever, with gpt‑3.5‑turbo-0613, each train­ing example is capped at 4,096 tokens.

Check Data Formatting

Before using the data­set, it’s imper­at­ive to ensure that the format is cor­rect. OpenAI provides a Python script to check for errors, token lim­its, and to estim­ate the cost of fine-tuning.

You can find more inform­a­tion here.

Upload Data for Train­ing and Validation

Two data sets are required: one for train­ing and one for val­id­a­tion, and both must be JSONL files. Train­ing the model will take some time, depend­ing on the amount of data provided, the num­ber of epochs, the base model, and other task-spe­cific para­met­ers. Addi­tion­ally, as train­ing jobs are pro­cessed in a queue, the server may exper­i­ence delays due to con­cur­rent tasks from other users.

Deploy the Model

Now the model can be deployed. If the model has been cus­tom­ised mul­tiple times, you can switch between dif­fer­ent versions.

Use the Fine-Tuned Model

Once all these steps have been car­ried out, the model will be ready for use.

Con­clu­sion

Prompt engin­eer­ing, para­met­ers adjust­ment, func­tion call­ing, and fine-tun­ing are vital meth­ods to refine Chat­GPT‘s beha­viour. Each approach offers its own advant­ages, allow­ing users to cus­tom­ise ChatGPT’s out­puts to their spe­cific needs. By under­stand­ing and har­ness­ing these meth­ods effect­ively, you can fully lever­age its cap­ab­il­it­ies to pro­duce accur­ate and con­tex­tu­ally appro­pri­ate responses. As we pro­gress in nat­ural lan­guage pro­cessing, these meth­ods will con­tinue to be highly sig­ni­fic­ant in shap­ing the future of AI com­mu­nic­a­tion and interaction.

Here at Clearpeaks we use these AI cap­ab­il­it­ies in Observation Deck, our own frame­work that provides top decision-makers valu­able insights tailored to their exact needs and makes for an ideal uni­fied solu­tion offer­ing a single point of access to all your BI con­tent, power­ful cus­tom visu­al­isa­tion options, and embed­ded AI-powered chat­bots to stream­line data retrieval.

If you’re inter­ested in learn­ing more about fine-tun­ing AI mod­els or how Observation Deck can trans­form your data and ana­lyt­ics strategy, our team is ready to assist. Con­tact us today and we’ll take your AI and BI solu­tions to the next level!