Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2023 05:42
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save maxim-saplin/83ad6bff10ed09834bebcada047ee39c to your computer and use it in GitHub Desktop.
Open AI function calling to enforce LLM response structure/format
import dotenv from "dotenv";
const API_KEY = process.env.API_KEY;
const API_ENDPOINT = process.env.API_ENDPOINT; // ?api-version=2023-07-01-preview is required as of August 2023
const validate = async (name: string, project: string, role: string, category: string, feedback: string, summary: string): Promise<{ status: string, recommendations: string }> => {
var nominee = generateNomineeString(name, project, role, category, feedback, summary);
// Using function call capability of OpenAI API to ensure response is properly formatted JSON
// Before function calling I asked to reply via JSON, one of side effect was that in 'recomendations'
// field LLM returned JSON (e.g. collection os issues), whhich was not desired.
// Function calling on the contrary suggest there're some kind of guarantees by LLM
// as it was trained for that
const body = JSON.stringify({
"messages": [
"role": "system",
"content": priming
"role": "user",
"content": descriptions
"role": "user",
"content": nominee
"functions": [
"name": functionName,
"description": "Sends validation results of nominee's submission and determines\
if the submission requries rework or can be marked as OK and sent for further processing",
"parameters": {
"type": "object",
"properties": {
"status": {
"type": "string",
"description": "Determines if validation is OK or not",
"enum": ["OK", "NOT-OK"]
"recommendations": {
"type": "string",
"description": "Empty if there're no objections and the submission has passed validation. \
Otherwise explains why validation was not passed. The result is a slightly styled text formatted \
as HTML (using paragraphs, lists, bold fonts where necessary)",
"required": ["status", "recommendations"]
"temperature": 0.0,
"frequency_penalty": 0,
"presence_penalty": 0,
"n": 1,
"max_tokens": 400,
"top_p": 0.95
const response = await fetch(API_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'api-key': `${API_KEY}`,
body: body
const data = await response.json();
let obj;
try {
obj = JSON.parse(data.choices[0].message.function_call.arguments);
} catch (_) {
const status = obj.status ?? 'OK';
const recommendations = obj.recommendations ?? '';
return Promise.resolve({ status: status, recommendations: recommendations});
// Passing through LLM reply
export default async function handle(req, res) {
validate(, req.body.project, req.body.role, req.body.category,, req.body.summary).then(result => {
res.json({ status: result.status, recommendations: result.recommendations });
const functionName = "submit_validation_results";
const generateNomineeString = (name: string, project: string, role: string, category: string, feedback: string, summary: string): string => {
return `Nominee: ${name}\nNominee Project: ${project}\nNominee Role: ${role}\nCategory: ${category}\nFeedback: ${feedback}\nSummary: ${summary}`;
const priming =
`You are an AI assistant acting as an experienced project/program manager with 30 years in enterprise software development. You should call the ${functionName} function in response to user message. Only use the function you have been provided with.
You're taking part in recognition commitee which processes employee nominatioins and decides on awarding people for their work. You are tasked with veryfing submissions, flagging problematic ones, listing nominee submission issues and suggesting improvements.
In the following messages you will be presented with nominations descrioptions an a nominee card. Fields in the nominee card:
• Nominee: name of the nominee
• Nominee Project: code of the project where the person showed good work
• Nominee Role: Role at the project at the time of recognition
• Category: this is the name of nomination
• Feedback: this field is supposed to provide description of the achievement, impact etc. which is being recognized in accordance with nomination
• Summary: short personal message which is supposed to be shared with the nominee
Please review user provided submission and decide if it is OK or not, highlighting deficiencies, recomend changes/improvements. Some of the problems that you might highlight:
• Poor description
• Missing information
• Nomination mismatch
Your output must be a function call with 2 fields:
• status - possible values are 'OK' (no complaints, nominees submission can go to the next step) and 'NOT-OK' (you have identified deficiencies and have recommendations)
• recommendations - should you have concerns regrading the submission (as instructed above) and would like to send it back for rework please provide your review results and list you arguments here.
const descriptions =
`Here’s the description of the nominations:
1) Shining Star
Acknowledging exceptional dedication and positive impact within assignments. This could include things like spearheading initiatives, facilitating workshops, mentoring others, or finding innovative solutions to challenges.
2) Master Crafter
Acknowledging outstanding expertise and skills demonstrated through trainings, certifications, resolving complicated issues, or developing optimizations.
3) Team MVP
Acknowledging contributions to major milestones and delivering significant outcomes. This could be for accomplishments like successful releases, completing sprints, or anything deemed impactful.
4) Community Champion
Recognizing those who bring value by activities outside their core role. This could be things like promoting the brand, leading events, developing skills that improve capabilities, finding growth opportunities, or delivering new solutions.`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment