Loading...
;
+ }
+
+ return (
+
+ {messages.map(message => (
+
+
+
+
{message.role === 'user' ? 'YOU ' : 'AI '}
+ {message.parts.map((part, i) => {
+ switch (part.type) {
+ case 'text':
+ return
{part.text}
;
+ }
+ })}
+
+
+
+ ))}
+ //add-end
+```
+
+The message rendering logic handles different message types and applies appropriate styling - user messages appear on the right with a dark background, while AI responses appear on the left with a light background.
+
+### 6.4. Add the input form
+
+Now we need to create the input interface that allows users to type and send messages to the AI:
+
+```tsx file=app/page.tsx
+'use client';
+
+import { useChat } from '@ai-sdk/react';
+import { useState, useEffect } from 'react';
+
+export default function Chat() {
+ const [input, setInput] = useState('');
+ const [isLoading, setIsLoading] = useState(true);
+
+ const { messages, sendMessage, setMessages } = useChat();
+
+ useEffect(() => {
+ fetch('/api/messages')
+ .then(res => res.json())
+ .then(data => {
+ if (data.messages && data.messages.length > 0) {
+ setMessages(data.messages);
+ }
+ setIsLoading(false);
+ })
+ .catch(() => setIsLoading(false));
+ }, [setMessages]);
+
+ if (isLoading) {
+ return
Loading...
;
+ }
+
+ return (
+
+ {messages.map(message => (
+
+
+
+
{message.role === 'user' ? 'YOU ' : 'AI '}
+ {message.parts.map((part, i) => {
+ switch (part.type) {
+ case 'text':
+ return
{part.text}
;
+ }
+ })}
+
+
+
+ ))}
+
+ //add-start
+
+ //add-end
+
+ );
+}
+```
+
+## 7. Test your application
+
+To test your application, run the following command:
+
+```terminal
+npm run dev
+```
+
+Open your browser and navigate to [`http://localhost:3000`](http://localhost:3000) to see your application in action.
+
+Test it by sending a message to the AI and see if it's saved to the database. Check Prisma Studio to see the messages in the database.
+
+```terminal
+npx prisma studio
+```
+
+You're done! You've just created a AI SDK chat application with Next.js and Prisma. Below are some next steps to explore, as well as some more resources to help you get started expanding your project.
+
+## Next Steps
+
+Now that you have a working AI SDK chat application connected to a Prisma Postgres database, you can:
+
+- Extend your Prisma schema with more models and relationships
+- Add create/update/delete routes and forms
+- Explore authentication and validation
+- Enable query caching with [Prisma Postgres](/postgres/database/caching) for better performance
+
+### More Info
+
+- [Prisma Documentation](/orm/overview/introduction)
+- [AI SDK Documentation](https://ai-sdk.dev/)
\ No newline at end of file
diff --git a/sidebars.ts b/sidebars.ts
index e0b6b57ba4..a27a7e4884 100644
--- a/sidebars.ts
+++ b/sidebars.ts
@@ -446,6 +446,7 @@ const sidebars: SidebarsConfig = {
"guides/shopify",
"guides/permit-io-access-control",
"guides/betterauth-nextjs",
+ "guides/ai-sdk-nextjs",
"guides/authjs-nextjs",
].sort(),
},
diff --git a/static/img/guides/prisma-ai-sdk-nextjs-cover.png b/static/img/guides/prisma-ai-sdk-nextjs-cover.png
new file mode 100644
index 0000000000..3cb893face
Binary files /dev/null and b/static/img/guides/prisma-ai-sdk-nextjs-cover.png differ