I am using OpenAI’s assisstant API to make a bot, but the prompt_token_usage in usage is too high.
Run 1 : user: what is your purpose? (it’s in the instructions, instructions are 131 token.)
usage: { prompt_tokens: 7399, completion_tokens: 63, total_tokens: 7462 }
Run 2 : user: what day is it today, based on the internet?
usage: { prompt_tokens: 14631, completion_tokens: 72, total_tokens: 14703 },
async function getSearchResult(query) {
console.log('------- CALLING AN EXTERNAL API ----------')
const json = await getJson({
engine: "google",
api_key: SERPAPI_KEY,
q: query,
location: "Austin, Texas",
});
return json["organic_results"];
}
async function checkingStatus(res, threadId, runId, resolve) {
const runObject = await openai.beta.threads.runs.retrieve(
threadId,
runId
);
const status = runObject.status;
console.log(runObject);
console.log('Current status: ' + status);
if (status === 'completed') {
clearInterval(pollingInterval);
const messagesList = await openai.beta.threads.messages.list(threadId);
let messages = [];
messagesList.body.data.forEach(message => {
messages.push(message.content);
});
// Assuming you want the first message in the messages array
const assistantMessage = messages.length > 0 ? messages[0] : null;
resolve(assistantMessage);
}
// + Addition for function calling
else if(status === 'requires_action') {
console.log('requires_action.. looking for a function')
if(runObject.required_action.type === 'submit_tool_outputs' && status !== 'queued') {
console.log('submit tool outputs ... ')
const tool_calls = runObject.required_action.submit_tool_outputs.tool_calls
const parsedArgs = JSON.parse(tool_calls[0].function.arguments);
console.log('Query to search for: ' + parsedArgs.query)
const apiResponse = await getSearchResult(parsedArgs.query)
const run = await openai.beta.threads.runs.submitToolOutputs(
threadId,
runId,
{
tool_outputs: [
{
tool_call_id: tool_calls[0].id,
output: JSON.stringify(apiResponse)
},
],
}
)
}
}
}
app.post('/message', (req, res) => {
const { message, threadId } = req.body;
addMessage(threadId, message).then(() => {
// res.json({ messageId: message.id });
// Run the assistant
runAssistant(threadId).then(run => {
const runId = run.id;
// Check the status with polling and use a promise to handle completion
new Promise((resolve) => {
pollingInterval = setInterval(() => {
checkingStatus(res, threadId, runId, resolve);
}, 5000);
}).then(async (assistantMessage) => {
res.json({message: assistantMessage});
});
});
});
});
1