Hi @michael.simpson555
I am trying to get the structured response and currently using GPT 3.5.
Here is my code:
import OpenAI from "openai";
import dotenv from "dotenv";
dotenv.config();
const openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY});
const setTalentDataJson = {
"name": "setTalentInfo",
"description": "Get the tagline and languages of a talent.",
"parameters": {
"type": "object",
"properties": {
"tagline": {
"type": "string",
"description": "The tagline of the talent."
},
"languages": {
"type": "string",
"description": "The language of the talent."
},
"bio": {
"type": "string",
"description": "The bio of the talent."
}
},
// "required": ["tagline", "languages"]
}
};
const getTalentDataJson = {
"name": "getTalentInfo",
"description": "Get the tagline and languages of a talent.",
"parameters": {
"type": "object",
"properties": {
"tagline": {
"type": "string",
"description": "The tagline of the talent."
},
"languages": {
"type": "string",
"description": "The language of the talent."
}
},
// "required": ["tagline", "languages"]
}
};
const getTalentInfo = async ( tagline, languages) => {
const talent_data = {
"first_name": "John",
"last_name": "Doe",
"tagline": "Instructional Designer",
"bio": "Hi, I'm an Instructional Designer with over 7 years of experience developing end-to-end training solutions. I can help you in all stages of the process: assessing the needs of your learners, creating a strategy to train them more effectively, developing custom e-learning modules and selecting the right LMS for your organization.",
"location": "Canada",
"languages": [
"English",
"French",
"Spanish"
]
};
return talent_data
};
const setTalentInfo = async (tagline, languages, bio) => {
const talent_data = {
"tagline": tagline,
"languages": languages,
"bio": bio
};
return talent_data;
};
async function callGpt(model, systemPrompt, userPrompt) {
let messages = [
{role: "system", content: systemPrompt},
{role: "user", content: userPrompt}
];
console.log('---------- Request -------------');
console.log(messages);
const response = await openai.chat.completions.create({
model: model,
messages: messages,
functions: [getTalentDataJson]
});
let responseMessage = response.choices[0].message;
console.log("Got Response: ", responseMessage);
messages.push(responseMessage);
if(responseMessage.function_call?.name === 'getTalentInfo') {
const args = JSON.parse(responseMessage.function_call.arguments);
const talent = await getTalentInfo(args.tagline, args.languages);
const talent_data = {"first_name": talent.first_name, "last_name": talent.last_name, "tagline": talent.tagline, "bio": talent.bio, "languages": talent.languages};
const talent_return_data = await setTalentInfo(talent.tagline, talent.languages, talent.bio);
messages.push({role: "function", name: "getTalentInfo", content: JSON.stringify(talent_return_data)});
const response2 = await openai.chat.completions.create({
model: "gpt-3.5-turbo-1106",
messages: messages,
functions: [setTalentDataJson],
function_call: {
name: "setTalentInfo",
arguments: JSON.stringify(talent_return_data)
}
});
console.log( response2.choices[0].message)
if (response2.function_call && response2.function_call.name === 'setTalentInfo') {
console.log("Response 2", response2.choices[0].message);
return JSON.parse(response2.function_call.arguments);
} else {
console.error("Error in processing the response from OpenAI GPT-3.5-turbo.");
// Handle the error or provide a default value
return null;
}
}
}
const result = await callGpt("gpt-3.5-turbo-1106",
"You are a highly experienced talent recruiter. You help people find the right human resource for their needs.",
"I am looking for Instructional designer who speaks french");
console.log("Result", result);
Whenever function triggers it always return the structured data which i have defined in setTalentData.
Like whatever data search by function call it always return the predefined data like in setTalentData
In this case i want to return tagline, languages, bio. But it did not return the bio, its only return the tagline and languages.
Looking forward