Skip to content

Commit 7cb88f9

Browse files
committed
📦 NEW: Nextjs Example
1 parent d39f27a commit 7cb88f9

25 files changed

+6563
-0
lines changed

examples/nextjs/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

examples/nextjs/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/install-state.gz
8+
9+
# testing
10+
/coverage
11+
12+
# next.js
13+
/.next/
14+
/out/
15+
16+
# production
17+
/build
18+
19+
# misc
20+
.DS_Store
21+
*.pem
22+
23+
# debug
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts

examples/nextjs/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
# or
12+
pnpm dev
13+
# or
14+
bun dev
15+
```
16+
17+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18+
19+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20+
21+
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
22+
23+
## Learn More
24+
25+
To learn more about Next.js, take a look at the following resources:
26+
27+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29+
30+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
31+
32+
## Deploy on Vercel
33+
34+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35+
36+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Pipe} from 'langbase';
2+
3+
export async function POST(req: Request) {
4+
// 0. Take out user prompt.
5+
const {prompt} = await req.json();
6+
7+
// 1. Initiate the Pipe.
8+
const pipe = new Pipe({apiKey: process.env.LANGBASE_PIPE_LESS_WORDY!});
9+
10+
// 3. Generate the text by asking a question.
11+
const result = await pipe.generateText({
12+
// Add user question prompt here to generate completion.
13+
messages: [{role: 'user', content: prompt}],
14+
});
15+
16+
// 4. Done: You got the generated completion on result.completion.
17+
return Response.json(result);
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {Pipe} from 'langbase';
2+
import {NextRequest} from 'next/server';
3+
4+
export const runtime = 'edge';
5+
6+
export async function POST(req: NextRequest) {
7+
const {prompt} = await req.json();
8+
9+
// 1. Initiate the Pipe.
10+
const pipe = new Pipe({
11+
apiKey: process.env.LANGBASE_PIPE_LESS_WORDY_STREAM!,
12+
});
13+
14+
// 2. Generate a stream by asking a question
15+
const stream = await pipe.streamText({
16+
messages: [{role: 'user', content: prompt}],
17+
});
18+
19+
// 3. Create a ReadableStream from the Langbase stream
20+
const readableStream = new ReadableStream({
21+
async start(controller) {
22+
for await (const chunk of stream) {
23+
controller.enqueue(JSON.stringify(chunk) + '\n');
24+
}
25+
controller.close();
26+
},
27+
});
28+
29+
// 4. Return the stream
30+
return new Response(readableStream, {
31+
headers: {
32+
'Content-Type': 'application/x-ndjson',
33+
},
34+
});
35+
}

examples/nextjs/app/favicon.ico

25.3 KB
Binary file not shown.

examples/nextjs/app/globals.css

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
4+
5+
@layer base {
6+
:root {
7+
--background: 0 0% 100%;
8+
--foreground: 0 0% 3.9%;
9+
--card: 0 0% 100%;
10+
--card-foreground: 0 0% 3.9%;
11+
--popover: 0 0% 100%;
12+
--popover-foreground: 0 0% 3.9%;
13+
--primary: 0 0% 9%;
14+
--primary-foreground: 0 0% 98%;
15+
--secondary: 0 0% 96.1%;
16+
--secondary-foreground: 0 0% 9%;
17+
--muted: 0 0% 96.1%;
18+
--muted-foreground: 0 0% 45.1%;
19+
--accent: 0 0% 96.1%;
20+
--accent-foreground: 0 0% 9%;
21+
--destructive: 0 84.2% 60.2%;
22+
--destructive-foreground: 0 0% 98%;
23+
--border: 0 0% 89.8%;
24+
--input: 0 0% 89.8%;
25+
--ring: 0 0% 3.9%;
26+
--radius: 0.5rem;
27+
--chart-1: 12 76% 61%;
28+
--chart-2: 173 58% 39%;
29+
--chart-3: 197 37% 24%;
30+
--chart-4: 43 74% 66%;
31+
--chart-5: 27 87% 67%;
32+
}
33+
34+
.dark {
35+
--background: 0 0% 3.9%;
36+
--foreground: 0 0% 98%;
37+
--card: 0 0% 3.9%;
38+
--card-foreground: 0 0% 98%;
39+
--popover: 0 0% 3.9%;
40+
--popover-foreground: 0 0% 98%;
41+
--primary: 0 0% 98%;
42+
--primary-foreground: 0 0% 9%;
43+
--secondary: 0 0% 14.9%;
44+
--secondary-foreground: 0 0% 98%;
45+
--muted: 0 0% 14.9%;
46+
--muted-foreground: 0 0% 63.9%;
47+
--accent: 0 0% 14.9%;
48+
--accent-foreground: 0 0% 98%;
49+
--destructive: 0 62.8% 30.6%;
50+
--destructive-foreground: 0 0% 98%;
51+
--border: 0 0% 14.9%;
52+
--input: 0 0% 14.9%;
53+
--ring: 0 0% 83.1%;
54+
--chart-1: 220 70% 50%;
55+
--chart-2: 160 60% 45%;
56+
--chart-3: 30 80% 55%;
57+
--chart-4: 280 65% 60%;
58+
--chart-5: 340 75% 55%;
59+
}
60+
}
61+
62+
@layer base {
63+
* {
64+
@apply border-border;
65+
}
66+
body {
67+
@apply bg-background text-foreground;
68+
}
69+
}

examples/nextjs/app/layout.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {cn} from 'mxcn';
2+
import type {Metadata} from 'next';
3+
import {Inter as FontSans} from 'next/font/google';
4+
import './globals.css';
5+
6+
const fontSans = FontSans({
7+
subsets: ['latin'],
8+
variable: '--font-sans',
9+
});
10+
11+
export const metadata: Metadata = {
12+
title: '⌘ Langbase with a Next App',
13+
description: 'Generated by create next app',
14+
};
15+
16+
export default function RootLayout({
17+
children,
18+
}: Readonly<{
19+
children: React.ReactNode;
20+
}>) {
21+
return (
22+
<html lang="en">
23+
<body
24+
className={cn(
25+
'min-h-screen bg-background font-sans antialiased',
26+
fontSans.variable,
27+
)}
28+
>
29+
{children}
30+
</body>
31+
</html>
32+
);
33+
}

examples/nextjs/app/page.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import StreamTextRouteHandler from '@/components/langbase/stream-text-route-handler';
2+
3+
export default function Home() {
4+
return (
5+
<div className="flex min-h-screen flex-col items-center justify-between p-24">
6+
<div className="flex flex-col items-center justify-between gap-10 max-w-lg w-full">
7+
<div className="flex flex-col gap-1 w-full">
8+
<h2 className="text-2xl font-bold tracking-tight">
9+
⌘ Langbase AI Pipe
10+
</h2>
11+
<p className="text-muted-foreground">
12+
An AI agent that responds to your prompts.
13+
</p>
14+
</div>
15+
{/* <GenerateTextRouteHandler /> */}
16+
<StreamTextRouteHandler />
17+
{/* <GenerateTextServerAction /> */}
18+
</div>
19+
</div>
20+
);
21+
}

examples/nextjs/components.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "https://ui.shadcn.com/schema.json",
3+
"style": "default",
4+
"rsc": true,
5+
"tsx": true,
6+
"tailwind": {
7+
"config": "tailwind.config.ts",
8+
"css": "app/globals.css",
9+
"baseColor": "neutral",
10+
"cssVariables": true,
11+
"prefix": ""
12+
},
13+
"aliases": {
14+
"components": "@/components",
15+
"utils": "@/lib/utils"
16+
}
17+
}

0 commit comments

Comments
 (0)