<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gulshan Sharma</title>
    <description>The latest articles on DEV Community by Gulshan Sharma (@gulshan_sharma_7d2daa9d67).</description>
    <link>https://dev.to/gulshan_sharma_7d2daa9d67</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2608818%2F929cb8a8-0260-44ed-8668-bb311b0e8590.gif</url>
      <title>DEV Community: Gulshan Sharma</title>
      <link>https://dev.to/gulshan_sharma_7d2daa9d67</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gulshan_sharma_7d2daa9d67"/>
    <language>en</language>
    <item>
      <title>How to integrate GitHub CopilotKit with Prisma Integration into your nextJs project Using OpenAI</title>
      <dc:creator>Gulshan Sharma</dc:creator>
      <pubDate>Tue, 24 Dec 2024 07:46:01 +0000</pubDate>
      <link>https://dev.to/gulshan_sharma_7d2daa9d67/how-to-integrate-copilotkit-with-prisma-integration-into-your-nextjs-project-hg3</link>
      <guid>https://dev.to/gulshan_sharma_7d2daa9d67/how-to-integrate-copilotkit-with-prisma-integration-into-your-nextjs-project-hg3</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is GitHub CopilotKit:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Build deeply integrated AI assistants &amp;amp; agents designed to work alongside your users inside applications.End-to-end systems integrations with deep customizability and support for the leading LLM ecosystem solutions.&lt;/p&gt;

&lt;p&gt;GitHub CopilotKit can seamlessly integrate with multiple AI providers, including OpenAI, Azure OpenAI, and Google Gemini, offering flexibility and scalability to meet diverse business needs. This multi-provider compatibility allows organizations to leverage advanced AI models from different platforms, ensuring access to cutting-edge technologies and features. By supporting multiple providers, Copilot enables users to select the most suitable AI service based on performance, cost, compliance, or specific use-case requirements. This adaptability also reduces dependency on a single provider, enhancing reliability and enabling hybrid AI strategies for improved efficiency and innovation. &lt;a href="https://docs.copilotkit.ai/quickstart" rel="noopener noreferrer"&gt;Document&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a NextJs Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create an app using nextJs: &lt;a href="https://nextjs.org/docs/app/getting-started/installation" rel="noopener noreferrer"&gt;documention&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-next-app@latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Intgreate prisma&lt;/strong&gt;&lt;br&gt;
Install Prisma ORM: &lt;a href="https://www.prisma.io/docs" rel="noopener noreferrer"&gt;documention&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install prisma --save-dev
npm install @prisma/client  
npx prisma init     
prisma generate   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Schema structure in Prisma:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
model Blogs{
  id        Int    @id @default(autoincrement())
  title     String
  content   String
  createdAt DateTime @default(now())
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Schema deploy on database server in Prisma:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm prisma migrate dev     
npm prisma migrate deploy    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The resources to create a free tire PostgreSQL Database:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://console.neon.tech/" rel="noopener noreferrer"&gt;neon&lt;/a&gt;&lt;br&gt;
&lt;a href="https://supabase.com/" rel="noopener noreferrer"&gt;supabase&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to integration of GitHub CopilotKit:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Add GitHub CopilotKit into the &lt;code&gt;app/layout.js&lt;/code&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "@copilotkit/react-ui/styles.css";
import { CopilotKit } from "@copilotkit/react-core"

 &amp;lt;CopilotKit runtimeUrl="/api/copilotkit"&amp;gt; 
         {children}
  &amp;lt;/CopilotKit&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;code&gt;.env&lt;/code&gt; configure environment variables&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_URL="postgresql://xxxxxxx"
OPENAI_API_KEY=xxxxxxx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Customize the &lt;code&gt;app/page.js&lt;/code&gt; for CopilotPopup&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client";
import { useEffect, useState } from "react";
import { CopilotPopup } from "@copilotkit/react-ui";

export default function Home() {
  const [blogs, setBlogs] =useState([])

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      const response = await fetch('/api/blogs');
      const blogData = await response.json();
      setBlogs(blogData);
    };
    fetchData();
  }, []);
  return (
    &amp;lt;div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]"&amp;gt;
    &amp;lt;main className="flex flex-col gap-8 row-start-2 items-center sm:items-start"&amp;gt;
    &amp;lt;table className="table-auto border-collapse border border-gray-300 w-full"&amp;gt;
          &amp;lt;thead&amp;gt;
            &amp;lt;tr className="bg-gray-200"&amp;gt;
              &amp;lt;th className="border border-gray-300 px-4 py-2"&amp;gt;ID&amp;lt;/th&amp;gt;
              &amp;lt;th className="border border-gray-300 px-4 py-2"&amp;gt;Title&amp;lt;/th&amp;gt;
              &amp;lt;th className="border border-gray-300 px-4 py-2"&amp;gt;Content&amp;lt;/th&amp;gt;
            &amp;lt;/tr&amp;gt;
          &amp;lt;/thead&amp;gt;
          &amp;lt;tbody&amp;gt;

            {blogs ? blogs.map((blog, idex) =&amp;gt; (
            &amp;lt;tr key={idex}&amp;gt;
              &amp;lt;td className="border border-gray-300 px-4 py-2"&amp;gt;{blog.id}&amp;lt;/td&amp;gt;
              &amp;lt;td className="border border-gray-300 px-4 py-2"&amp;gt;{blog.title}&amp;lt;/td&amp;gt;
              &amp;lt;td className="border border-gray-300 px-4 py-2"&amp;gt;{blog.content}&amp;lt;/td&amp;gt;
            &amp;lt;/tr&amp;gt;
            )) : &amp;lt;tr&amp;gt;&amp;lt;td colSpan={4}&amp;gt; No records found&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;}
          &amp;lt;/tbody&amp;gt;
        &amp;lt;/table&amp;gt;
        &amp;lt;CopilotPopup
      instructions={"You are assisting the blogs as best as you can. Answer in the best way possible given the data you have."}
      labels={{
        title: "Blog Assistant",
        initial: "Need any help?",
      }}
    /&amp;gt;
    &amp;lt;/main&amp;gt;

  &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Create an API endpoint for &lt;code&gt;api/copilotkit/route.js&lt;/code&gt; and blog data &lt;code&gt;api/blogs/route.js&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;api/copilotkit/route.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
    CopilotRuntime,
    OpenAIAdapter,
    copilotRuntimeNextJSAppRouterEndpoint
  } from '@copilotkit/runtime';
  import { PrismaClient } from '@prisma/client';
  const serviceAdapter = new OpenAIAdapter();
  const prisma = new PrismaClient();
  const runtime = new CopilotRuntime({
    actions: ({properties,url}) =&amp;gt; {
        return [
            {
                name: "updateDb",
                description: "Update blog item to the list",
                parameters: [
                  {
                    name: "id",
                    type: "number",
                    description: "The ID of the post item to data fetch",
                    required: true,
                  },
                  {
                    name: "updateTitle",
                    type: "string",
                    description: "The update title of the blog post to be updated",
                    required: true,
                  },
                  {
                    name: "updateContent",
                    type: "string",
                    description: "The update content of the blog post to be updated",
                    required: true,
                  }
                ],
                handler: async ({ id, updateTitle, updateContent }) =&amp;gt; {
                  await prisma.blogs.update({
                    where: {
                      id : Number(id),
                    },
                    data: {
                      title: updateTitle,
                      content: updateContent
                    }
                  })
                },
              },
              {
                name: "addDb",
                description: "Add blog item to the list",
                parameters: [
                  {
                    name: "createTitle",
                    type: "string",
                    description: "The add title of the blog post to be updated",
                    required: true,
                  },
                  {
                    name: "createContent",
                    type: "string",
                    description: "The add content of the blog post to be updated",
                    required: true,
                  }
                ],
                handler: async ({ createTitle, createContent }) =&amp;gt; {
                  await prisma.blogs.create({
                    data: {
                      title: createTitle,
                      content: createContent
                    }
                  })
                },
              }
        ]
    }
  });

  export const POST = async (req) =&amp;gt; {
    const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
      runtime,
      serviceAdapter,
      endpoint: '/api/copilotkit',
    });
    return handleRequest(req);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;api/blogs/route.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export async function GET() {
  try {
    const tableData = await prisma.blogs.findMany();
    return NextResponse.json(tableData);
  } catch (error) {
    console.error('Error fetching data:', error);
    return NextResponse.json({ error: 'Error fetching data' }, { status: 500 });
  } finally {
    await prisma.$disconnect();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;To run Copiltkit make a build and run the project&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can directly run it on locally because of the strict mode in nextJs. so first we have to make a build and then run the project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Finally your website is ready to use the &lt;em&gt;Copilotkit&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fadu4kzm8pf1wa0wp87rf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fadu4kzm8pf1wa0wp87rf.png" alt="Project Screenshot" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey, My name is Gulshan, If you have enjoyed and learned something useful, like this post, add a comment.&lt;/p&gt;

&lt;p&gt;Don't forget to give it a start 😅.&lt;/p&gt;

&lt;p&gt;Happy Coding 🧑‍💻&lt;/p&gt;

</description>
      <category>ai</category>
      <category>nextjs</category>
      <category>prisma</category>
      <category>githubcopilot</category>
    </item>
  </channel>
</rss>
