Next.js Discord

Discord Forum

How to store messages generated by AI - Vercel AI SDK and Prisma

Answered
Rhinelander posted this in #help-forum
Open in Discord
Avatar
RhinelanderOP
Hey, I've been losing my mind for the past two days. How do I store messages from AI to db? What do you think is the right approach? I do that by using AI SDK onFinish(), but how should the schema look? I can't get the types to match. Any help is welcome ❤️
Answered by B33fb0n3
I don't know that much about prisma, but this should be a good schema:
model Message {
  id        String  @id @default(cuid())
  threadId  String  
  message   String
}

I added the threadId as well, so you can get all messages by a specific thread id.

You can also extend this even more, to have a title or other attributes as well for the thread:
model Thread {
  id        String  @id @default(cuid())
  title     String
}
View full answer

4 Replies

Avatar
maybe something like:
{
  "id": "someMessageId",
  "message": "some markdown **message**"
}

That should do the job pretty fine or am I missing something?
Avatar
RhinelanderOP
How should prisma schema look like. I always get type error as it is not compatible with message so converstation history can't be made.
Avatar
I don't know that much about prisma, but this should be a good schema:
model Message {
  id        String  @id @default(cuid())
  threadId  String  
  message   String
}

I added the threadId as well, so you can get all messages by a specific thread id.

You can also extend this even more, to have a title or other attributes as well for the thread:
model Thread {
  id        String  @id @default(cuid())
  title     String
}
Answer
Avatar