-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvercel-integration.ts
More file actions
59 lines (51 loc) · 1.65 KB
/
vercel-integration.ts
File metadata and controls
59 lines (51 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Example: Vercel AI SDK Integration with new API
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
import { prompt, tool } from "@marrakesh/core";
import { z } from "zod";
// Define your prompt with tools using new minimal API
const getUserDetails = tool({
description: "Get user account information",
parameters: z.object({
userId: z.string().describe("User ID to lookup"),
}),
});
const p = prompt(
"You are a helpful assistant that can look up user information.",
)
.system("Always be helpful and accurate")
.tool(getUserDetails);
// API Route handler (app/api/chat/route.ts)
export async function POST(req: Request) {
try {
const { messages } = await req.json();
// Convert to Vercel AI SDK format
const { messages: messagesWithSystem, tools } = p.toVercelAI(messages);
// Use with Vercel AI SDK streaming
return streamText({
model: openai("gpt-4"),
messages: messagesWithSystem,
tools: tools,
toolChoice: "auto",
});
} catch (error) {
console.error("Chat API error:", error);
return new Response("Internal Server Error", { status: 500 });
}
}
// Example: Using with different providers
export async function POSTAnthropic(req: Request) {
try {
const { messages } = await req.json();
// Convert to Anthropic format
const { system, tools } = p.toAnthropic();
return streamText({
model: anthropic("claude-3-sonnet"),
messages: [{ role: "system", content: system }, ...messages],
tools: tools,
});
} catch (error) {
console.error("Chat API error:", error);
return new Response("Internal Server Error", { status: 500 });
}
}