CopilotKit 是一個開源框架,可以輕鬆地將強大的、可用於生產的 AI Copilot 整合到任何應用程式中。透過 CopilotKit,您可以無縫實施自訂 AI 聊天機器人、代理程式、文字區域等來增強您的產品。
?讓我們建立一個應用程序,在其中我們將學習如何將 CopilotKit 整合到我們的應用程式中:-
該應用程式使用 CopilotKit 自動產生抽認卡和測驗。只需要求人工智慧聊天機器人創建任何主題的抽認卡,它就會立即產生抽認卡和相應的測驗。這是學習任何學科的快速有效的方式。
前端:NextJs、Tailwind CSS、shadcdn、Zustand
後端:下一個Js
資料儲存:本地儲存
npm install @copilotkit/react-core @copilotkit/react-ui @copilotkit/runtime
GROQ_API_KEY=<your_groq_api_key>
?若要取得您的 Groq API 金鑰,請依照下列步驟操作:
前往 GroqCloud 並透過點擊「建立 API 金鑰」按鈕來產生 API 金鑰。
後端:對於後端,我們將設定一個 /api/copilotkit 端點。此端點將處理來自前端的請求,提供資料或根據需要進行回應。這個端點是您使用 CopilotKit 為應用程式提供支援所需的全部內容。
import { CopilotRuntime, GroqAdapter, copilotRuntimeNextJSAppRouterEndpoint, } from "@copilotkit/runtime"; import { NextRequest } from "next/server"; import Groq from "groq-sdk"; const groq:Groq = new Groq({ apiKey: process.env.GROQ_API_KEY }) ; const copilotKit = new CopilotRuntime(); const serviceAdapter = new GroqAdapter({ groq, model: "llama3-groq-8b-8192-tool-use-preview" }); export const POST = async (req: NextRequest) => { const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({ runtime: copilotKit, serviceAdapter, endpoint: "/api/copilotkit", }); return handleRequest(req); };
前端:
現在,讓我們將 CopilotKit 整合到我們的應用程式中。 CopilotKit 提供了幾個有用的鉤子,在本教程中,我們將重點放在兩個重要的鉤子:
npm install @copilotkit/react-core @copilotkit/react-ui @copilotkit/runtime
GROQ_API_KEY=<your_groq_api_key>
import { CopilotRuntime, GroqAdapter, copilotRuntimeNextJSAppRouterEndpoint, } from "@copilotkit/runtime"; import { NextRequest } from "next/server"; import Groq from "groq-sdk"; const groq:Groq = new Groq({ apiKey: process.env.GROQ_API_KEY }) ; const copilotKit = new CopilotRuntime(); const serviceAdapter = new GroqAdapter({ groq, model: "llama3-groq-8b-8192-tool-use-preview" }); export const POST = async (req: NextRequest) => { const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({ runtime: copilotKit, serviceAdapter, endpoint: "/api/copilotkit", }); return handleRequest(req); };
useCopilotReadable({ description: 'A code snippet manager', value: flashcards, });
useCopilotAction({ name: "create-flashcards-and-also-quiz-questions-for-those-flashcards", description: `Create a new flashcard along with corresponding quiz questions. Each flashcard should contain a term, description, topic, and relevant tags. Additionally, for each flashcard, generate quiz questions with multiple answer options. The quiz questions should conform to the 'QuizQuestion' interface, where: - Each question contains a string 'question', an array of four 'options', and the 'correctOption' corresponding to the correct answer. `, parameters: [ { name: "flashcards", description: "The flashcards for the given topic", type: "object[]", // Use "array" as the type }, { name: "quiz", description: "The quiz questions for the given topic, adhering to the QuizQuestion interface", type: "object[]", // Use "array" for QuizQuestion[] }, { name:"topic", description: "The title of the topic", type: "string" } ], handler: (args: { flashcards: Flashcard[], quiz: QuizQuestion[], topic: string }) => { addTopics(args); }, });
最終應用截圖:
這是我引用的項目:
https://github.com/Niharika0104/learn-using-flash-cards
這裡是該專案的現場演示:
https://learn-using-flash-cards.vercel.app/
我希望您喜歡這個關於 CopilotKit 的簡短教學。請繼續關注更多這類有趣且簡潔的教學!
希望在下次見到大家
尼哈里卡。
以上是輕鬆整合 AI:CopilotKit 使用初學者指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!