Files
humanlayer/examples/ts_vercel_ai_nextjs/app/api/chat/route.ts
dexhorthy 021149207b docs and examples updates for vercel, chainlit, and more
- Add [quickstart guide for using the humanlayer typescript sdk](https://humanlayer.dev/docs/quickstart-typescript)
- Add [guide for using Function Calls for Classification](https://humanlayer.dev/docs/core/classifications) with human-in-the-loop
- Add [framework guide for using humanlayer with chainlit](https://humanlayer.dev/docs/frameworks/chainlit)
- Add [framework guide for using humanlayer with the vercel ai sdk](https://humanlayer.dev/docs/frameworks/vercel-ai-sdk)
- Update [humanlayer-ts readme](https://github.com/humanlayer/humanlayer-ts) to include quickstart guide

- Add [example of a fullstack chat app with nextjs and the vercel ai sdk](https://github.com/humanlayer/humanlayer/tree/main/examples/vercel_ai_nextjs)
- Simplify the [chainlit example](https://github.com/humanlayer/humanlayer/tree/main/examples/chainlit) by using `AsyncHumanLayer`
- Update [langchain email example](https://github.com/humanlayer/humanlayer/tree/main/examples/langchain/09-email-contact.py) to showcase the use of custom jinja templates for email payloads
2025-02-20 17:08:43 -08:00

78 lines
2.3 KiB
TypeScript

import { openai } from "@ai-sdk/openai";
import { Message as AIMessage, streamText } from "ai";
import { z } from "zod";
import { humanlayer } from "humanlayer-vercel-ai-sdk";
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
const hl = humanlayer({
verbose: true,
});
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai("gpt-4o"),
messages,
maxSteps: 10,
tools: {
// Functions that don't require approval
fetch_active_orders: {
description: "Fetch active orders using the user's email.",
parameters: z.object({
email: z.string().describe("The user's email address"),
}),
execute: async ({ email }: { email: string }) => {
console.log(`[API] Fetching active orders for ${email}`);
return [
{
order_id: "123",
status: "active",
amount: 100,
created_at: "2021-01-01",
updated_at: "2021-01-01",
},
{
order_id: "456",
status: "cancelled",
amount: 200,
created_at: "2021-01-01",
updated_at: "2021-01-01",
},
]; // Simulated active orders
},
},
// Functions requiring approval
reimburse_order: hl.requireApproval({
reimburse_order: {
description: "Process a refund with human approval",
parameters: z.object({
order_id: z.string().describe("The order ID to reimburse"),
reason: z.string().describe("The reason for the reimbursement"),
}),
execute: async ({
order_id,
reason,
}: {
order_id: string;
reason: string;
}) => {
console.log(
`[API] Processing refund for order ${order_id} with reason: ${reason}`,
);
// Reimbursement logic would go here
return "refund processed";
},
},
}),
},
system:
"You are a helpful assistant. If the user asks for anything that requires order information, you should use the fetch_active_orders tool first.",
});
return result.toDataStreamResponse();
}