Next.jsChatGPTReactGoogle CloudOpenAI

ChaGPT OpenAI Google Cloud NextJS App

By Michael Nelles
Picture of the author
Published on
Features
NextJS, TypeScript, Google Cloud, ChatGPT API
Role
Developer, Tester, Production
image alt text
image alt text
image alt text
image alt text

About this project

Technology Stack (extended):

  • Next.js, TypeScript, Google Cloud, ChatGPT API: As previously mentioned.
  • MongoDB: A NoSQL database used to store and manage the conversation history and AI-generated responses.

Features (with MongoDB integration):

  • Conversation History Storage:
    • Each user's conversation history is stored in MongoDB.
    • Conversations are organized as documents, containing user inputs and AI-generated responses in chronological order.

Workflow (with MongoDB integration):

  • User Interaction:

    • Users interact with the conversational interface as described earlier.
  • Conversational AI Processing:

    • The frontend and backend processes remain the same.
  • Storing Conversations in MongoDB:

    • After receiving the AI-generated response from the ChatGPT API, the backend saves the user input and AI response as a document in MongoDB.
    • The document includes metadata such as timestamp, user ID, and conversation context.
  • Retrieving and Displaying Conversations:

    • Users can request their conversation history.
    • The backend queries MongoDB to retrieve the user's conversation documents.
    • The frontend displays the retrieved conversations, allowing users to review their interactions.

Use Cases (with MongoDB integration):

  • MongoDB allows users to revisit past interactions, making it useful for reference, analysis, or record-keeping purposes.
  • Users can track the progression of a conversation and review AI-generated responses.

Benefits (with MongoDB integration):

  • Conversation history is persisted, enabling users to access past interactions.
  • MongoDB offers flexibility in managing unstructured or semi-structured data like chat conversations.
  • Historical data can be used for analysis or improvement of the AI model.

Future Enhancements (with MongoDB integration):

  • Implementing search and filtering options for users to easily find specific conversations.
  • Adding user preferences to customize conversation storage, retrieval, or privacy settings.
  • Incorporating analytics to gain insights from the stored conversation data.

By integrating MongoDB into the app, I have adding a valuable layer of data storage that enables users to revisit their past conversations with the AI chatbot. This enhances the user experience and provides a historical context for interactions.


Tech Stack


Code-Block

An example of the NextJS API Route: Github Repo Source Code

"use client";

import React, { useState } from "react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";

import Form from "@/components/Form";
import { Session } from "next-auth";

const CreatePrompt = () => {
 const router = useRouter();
 const { data: session }: { data: Session | null } = useSession();

 const [submitting, setIsSubmitting] = useState(false);
 const [post, setPost] = useState({ prompt: "", tag: "" });

 const createPrompt = async (e: React.MouseEvent<HTMLButtonElement>) => {
    e.preventDefault();
    setIsSubmitting(true);

    try {
       const response = await fetch("/api/prompt/new", {
          method: "POST",
          body: JSON.stringify({
             prompt: post.prompt,
             userId:
                session && session.user && session.user.id
                   ? session.user.id
                   : null,
             tag: post.tag,
          }),
       });

       if (response.ok) {
          router.push("/");
       }
    } catch (error) {
       console.log(error);
    } finally {
       setIsSubmitting(false);
    }
 };

 return (
    <Form
       type='Create'
       post={post}
       setPost={setPost}
       submitting={submitting}
       handleSubmit={createPrompt}
    />
 );
};

export default CreatePrompt;