Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add-nextjs-llamaindex-agent-example #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nextjs-llamaindex-agent/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": ["next/core-web-vitals", "prettier"],
"rules": {
"max-params": ["error", 4],
"prefer-const": "error",
"no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }]
}
}
38 changes: 38 additions & 0 deletions nextjs-llamaindex-agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

cache/
16 changes: 16 additions & 0 deletions nextjs-llamaindex-agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:20-alpine as build

WORKDIR /app

# Install dependencies
COPY package.json package-lock.* ./
RUN npm install

# Build the application
COPY . .
RUN npm run build

# ====================================
FROM build as release

CMD ["npm", "run", "start"]
37 changes: 37 additions & 0 deletions nextjs-llamaindex-agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
This is a [LlamaIndex](https://www.llamaindex.ai/) project using [Next.js](https://nextjs.org/) bootstrapped with [`create-llama`](https://github.com/run-llama/LlamaIndexTS/tree/main/packages/create-llama).

This project was created to demonstrate the use of React Server Components with LlamaIndex Agents.

## Getting Started

First, you need to specify an `OPENAI_API_KEY` in an .env file in this directory.
```
OPENAI_API_KEY=<openai_api_key>
```

Then, install the dependencies:

```
pnpm install
```

Run the development server:

```
pnpm run dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about LlamaIndex, take a look at the following resources:

- [LlamaIndex Documentation](https://docs.llamaindex.ai) - learn about LlamaIndex (Python features).
- [LlamaIndexTS Documentation](https://ts.llamaindex.ai) - learn about LlamaIndex (Typescript features).

You can check out [the LlamaIndexTS GitHub repository](https://github.com/run-llama/LlamaIndexTS) - your feedback and contributions are welcome!
12 changes: 12 additions & 0 deletions nextjs-llamaindex-agent/app/components/chat-section.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use client";

import { ChatInput, ChatMessages } from "./ui/chat";

export default function ChatSection() {
return (
<div className="space-y-4 max-w-5xl w-full">
<ChatMessages />
<ChatInput />
</div>
);
}
28 changes: 28 additions & 0 deletions nextjs-llamaindex-agent/app/components/header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Image from "next/image";

export default function Header() {
return (
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
<code className="font-mono font-bold">app/page.tsx</code>
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
<a
href="https://www.llamaindex.ai/"
className="flex items-center justify-center font-nunito text-lg font-bold gap-2"
>
<span>Built by LlamaIndex</span>
<Image
className="rounded-xl"
src="/llama.png"
alt="Llama Logo"
width={40}
height={40}
priority
/>
</a>
</div>
</div>
);
}
56 changes: 56 additions & 0 deletions nextjs-llamaindex-agent/app/components/ui/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";

import { cn } from "./lib/utils";

const buttonVariants = cva(
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
},
);

export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button";
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
);
},
);
Button.displayName = "Button";

export { Button, buttonVariants };
35 changes: 35 additions & 0 deletions nextjs-llamaindex-agent/app/components/ui/chat/chat-avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Blend, User2 } from "lucide-react";
import Image from "next/image";

type AvatarType = "user" | "function" | "assistant";

export default function ChatAvatar({ role = "user" }: { role: AvatarType }) {
if (role === "user") {
return (
<div className="flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border bg-background shadow">
<User2 className="h-4 w-4" />
</div>
);
}

if (role === "function") {
return (
<div className="flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border bg-background shadow">
<Blend className="h-4 w-4" />
</div>
);
}

return (
<div className="flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border bg-black text-white shadow">
<Image
className="rounded-md"
src="/llama.png"
alt="Llama Logo"
width={24}
height={24}
priority
/>
</div>
);
}
57 changes: 57 additions & 0 deletions nextjs-llamaindex-agent/app/components/ui/chat/chat-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { AI } from "@/app/rsc";
import { UIStateItem } from "@/app/rsc/type";
import { useAIState, useActions, useUIState } from "ai/rsc";
import { nanoid } from "nanoid";
import { FormEvent, useState } from "react";
import { Button } from "../button";
import { Input } from "../input";
import ChatMessage from "./chat-message";

export default function ChatInput() {
const [inputValue, setInputValue] = useState("");
const [aiState] = useAIState();
const [messages, setMessages] = useUIState<typeof AI>();
const { submitUserMessage } = useActions();
console.log({ aiState, messages });

const onSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!inputValue) return;

// Add user message to UI state
setMessages((currentMessages) => [
...currentMessages,
{
id: nanoid(),
display: <ChatMessage role="user" message={inputValue} />,
},
]);

// Submit and get response message
const responseMessage: UIStateItem = await submitUserMessage(inputValue);
setMessages((currentMessages) => [...currentMessages, responseMessage]);

setInputValue("");
};

return (
<form
onSubmit={onSubmit}
className="rounded-xl bg-white p-4 shadow-xl space-y-4"
>
<div className="flex w-full items-start justify-between gap-4 ">
<Input
autoFocus
name="message"
placeholder="Type a message"
className="flex-1"
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value);
}}
/>
<Button type="submit">Send message</Button>
</div>
</form>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Loader2 } from "lucide-react";

export default function ChatLoading() {
return (
<div className="flex justify-center items-center pt-10">
<Loader2 className="h-4 w-4 animate-spin" />
</div>
);
}
21 changes: 21 additions & 0 deletions nextjs-llamaindex-agent/app/components/ui/chat/chat-message.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ChatAvatar from "./chat-avatar";
import Markdown from "./markdown";

export default function ChatMessage({
message,
role,
}: {
message: string;
role: "user" | "function" | "assistant";
}) {
return (
<div className="flex items-start gap-4 pr-5 pt-5">
<ChatAvatar role={role} />
<div className="group flex flex-1 justify-between gap-2">
<div className="flex-1 space-y-4">
<Markdown content={message} />
</div>
</div>
</div>
);
}
34 changes: 34 additions & 0 deletions nextjs-llamaindex-agent/app/components/ui/chat/chat-messages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { AI } from "@/app/rsc";
import { useUIState } from "ai/rsc";
import { Fragment, useEffect, useRef } from "react";

export default function ChatMessages() {
const [messages] = useUIState<typeof AI>();
const scrollableChatContainerRef = useRef<HTMLDivElement>(null);
const messageLength = messages.length;
const lastMessage = messages[messageLength - 1];

const scrollToBottom = () => {
if (scrollableChatContainerRef.current) {
scrollableChatContainerRef.current.scrollTop =
scrollableChatContainerRef.current.scrollHeight;
}
};

useEffect(() => {
scrollToBottom();
}, [messageLength, lastMessage]);

return (
<div className="w-full rounded-xl bg-white p-4 shadow-xl pb-0">
<div
className="flex h-[50vh] flex-col gap-5 divide-y overflow-y-auto pb-4"
ref={scrollableChatContainerRef}
>
{messages.map((message) => (
<Fragment key={message.id}>{message.display}</Fragment>
))}
</div>
</div>
);
}
Loading