import { humanlayer } from "@humanlayer/sdk";
import OpenAI from "openai";
// Initialize clients
const hl = humanlayer({ runId: "quickstart", verbose: true });
const openai = new OpenAI();
// Define a function that requires approval
const sendEmail = hl.requireApproval(
async (to: string, subject: string, body: string) => {
// Your email sending logic here
return `Email sent to ${to}`;
},
);
// Use in an OpenAI chat completion
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Send a welcome email to new@example.com" },
];
const completion = await openai.chat.completions.create({
messages,
model: "gpt-3.5-turbo",
tools: [
{
type: "function",
function: {
name: "sendEmail",
description: "Send an email to a user",
parameters: {
type: "object",
properties: {
to: { type: "string", description: "Recipient email" },
subject: { type: "string", description: "Subject line" },
body: { type: "string", description: "Email content" },
},
required: ["to", "subject", "body"],
},
},
},
],
});
// Handle tool calls
const message = completion.choices[0].message;
if (message.tool_calls) {
for (const toolCall of message.tool_calls) {
if (toolCall.type === "function") {
const args = JSON.parse(toolCall.function.arguments);
await sendEmail(args.to, args.subject, args.body);
}
}
}