import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-routor-YOUR_PROFILE_KEY",
baseURL: "https://api.routor.io/v1",
});
const SYSTEM_PROMPT = `You are a customer support agent for Acme Store.
Be concise and helpful. For order questions, use the lookup_order tool.
Escalate to a human agent if the customer is very upset or the issue is complex.`;
const tools: OpenAI.Chat.ChatCompletionTool[] = [
{
type: "function",
function: {
name: "lookup_order",
description: "Look up order status and details by order ID",
parameters: {
type: "object",
properties: {
order_id: { type: "string", description: "The order ID (e.g. ORD-12345)" },
},
required: ["order_id"],
},
},
},
];
async function handleTicket(userMessage: string): Promise<string> {
const response = await client.chat.completions.create({
model: "auto",
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: userMessage },
],
tools,
tool_choice: "auto",
max_tokens: 500,
});
const choice = response.choices[0];
// Handle tool call
if (choice.finish_reason === "tool_calls") {
const call = choice.message.tool_calls![0];
const args = JSON.parse(call.function.arguments);
const orderData = await lookupOrderFromDB(args.order_id);
// Continue conversation with tool result
const followUp = await client.chat.completions.create({
model: "auto",
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: userMessage },
choice.message,
{ role: "tool", tool_call_id: call.id, content: JSON.stringify(orderData) },
],
});
return followUp.choices[0].message.content ?? "";
}
return choice.message.content ?? "";
}