import OpenAI from 'openai'
const openai = new OpenAI()
const functions = [
{
name: 'ReservationAgent',
description:
'Handles reservation-related queries or requests. Responds to messages like "Какво има днес?", "Резервация за 4-ма, днес в 19:00", etc.',
parameters: {
type: 'object',
properties: { message: { type: 'string' } },
required: ['message'],
},
},
{
name: 'DeliveryAgent',
description:
'Handles delivery-related updates such as missing, ordered, or delivered items. Responds to messages like "Няма салфетки", "Свърши бирата", "Дойдоха чашите", etc.',
parameters: {
type: 'object',
properties: { message: { type: 'string' } },
required: ['message'],
},
},
{
name: 'Unknown',
description:
'Used when the message does not relate to any known activity or is unclear. Provides a short, witty fallback message.',
parameters: {
type: 'object',
properties: {
message: {
type: 'string',
description: 'A witty fallback message in Bulgarian.',
},
},
required: ['message'],
},
},
]
// Extract intent and fallback message in one go
export async function extractIntentAndAgent(messageContent) {
const messages = [
{
role: 'system',
content: `
You are a classification assistant for a bar's chatbot. Classify the user's message into one of the following categories and respond with a function call:
1. **ReservationAgent**: If the user is making, editing, querying, or deleting a reservation. (e.g., "Резервация за 4-ма, днес в 19:00")
2. **DeliveryAgent**: If the user is discussing deliveries, stock updates, items that have run out, or incoming shipments. (e.g., "Свърши бирата", "Дойдоха чашите")
3. **Unknown**: If the user's request is not clearly related to reservations or deliveries. In this case, return a witty, short, helpful fallback message in Bulgarian, encouraging the user to clarify. For example: "Не разбрах напълно. Може би питате за резервация или зареждане на стока? Уточнете, моля!"
Make sure to return a function call with name set to one of the functions above and a 'message' property containing either the extracted user query (for known agents) or the witty fallback (for Unknown).
`,
},
{ role: 'user', content: messageContent },
]
try {
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages,
functions,
function_call: 'auto',
})
const message = response.choices[0].message
if (message.function_call) {
const agent = message.function_call.name
const args = JSON.parse(message.function_call.arguments)
const fallbackMessage = args.message
return { agent, messageContent: fallbackMessage }
}
return { agent: 'Unknown', messageContent: 'Неизвестна грешка.' }
} catch (error) {
console.error('Error in intent detection:', error)
return { agent: 'Unknown', messageContent: 'Неизвестна грешка.' }
}
}
export default functions