Skip to content

How to feed chat history? #3857

Answered by patelvivekdev
o1Suleyman asked this question in Help
Discussion options

You must be logged in to vote

If you use useChat, you can fetch the messages from the db and pass them on as arguments.

import { useChat } from 'ai/react';

// fetch the initial messages from db.

const { messages } = useChat({
    id,
    initialMessages,
  });

In API, you can get messages from req

export async function POST(req: Request) {
  const { messages } = await req.json();
  const result = streamText({
     model: openai('gpt-4-turbo'),
     messages,
   })
}

Read more at https://sdk.vercel.ai/examples/next/state-management/restore-messages

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by o1Suleyman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants