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中文网其他相关文章!